Results 1 to 20 of 24
Thread: order of sequence
- 04-06-2011, 12:14 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
order of sequence
Hello
I am working on a project on school with netbeans (java).
I'm making tic-tac-toe as my project.
Unfortunately I am stuck with the order of sequences in my code.
Here is my code:
Java Code:package machten; import java.awt.*; import java.applet.*; import java.awt.event.*; public class Main extends Applet implements ActionListener { Button knop1; Button knop2; Button knop3; Button knop4; Button knop5; Button knop6; Button knop7; Button knop8; Button knop9; Button resetknop; String k1 = ""; String k2 = ""; String k3 = ""; String k4 = ""; String k5 = ""; String k6 = ""; String k7 = ""; String k8 = ""; String k9 = ""; int stap=1; int teller=1; public void init() { setLayout(null); setSize (1300,1250); knop1 = new Button ( "" ); knop1.setBounds(400,300,99,99); knop1.addActionListener ( this ); knop1.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop2 = new Button ( "" ); knop2.setBounds (500,300,99,99); knop2.addActionListener ( this ); knop2.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop3 = new Button ( "" ); knop3.setBounds(600,300,99,99); knop3.addActionListener ( this ); knop3.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop4 = new Button ( "" ); knop4.setBounds (400,400,99,99); knop4.addActionListener ( this ); knop4.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop5 = new Button ( "" ); knop5.setBounds(500,400,99,99); knop5.addActionListener ( this ); knop5.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop6 = new Button ( "" ); knop6.setBounds (600,400,99,99); knop6.addActionListener ( this ); knop6.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop7 = new Button ( "" ); knop7.setBounds(400,500,99,99); knop7.addActionListener ( this ); knop7.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop8 = new Button ( "" ); knop8.setBounds (500,500,99,99); knop8.addActionListener ( this ); knop8.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); knop9 = new Button ( "" ); knop9.setBounds(600,500,99,99); knop9.addActionListener ( this ); knop9.setFont( new Font ( " sansserif ",Font.PLAIN,120 )); resetknop = new Button ("Reset"); resetknop.setBounds (800,300,200,200); resetknop.addActionListener (this); add ( knop1 ); add ( knop2 ); add ( knop3 ); add ( knop4 ); add ( knop5 ); add ( knop6 ); add ( knop7 ); add ( knop8 ); add ( knop9 ); add ( resetknop); } public void actionPerformed(ActionEvent e ) { // speler if (e.getSource()== resetknop) { knop1.setLabel(""); knop2.setLabel(""); knop3.setLabel(""); knop4.setLabel(""); knop5.setLabel(""); knop6.setLabel(""); knop7.setLabel(""); knop8.setLabel(""); knop9.setLabel(""); k1 = (""); k2 = (""); k3 = (""); k4 = (""); k5 = (""); k6 = (""); k7 = (""); k8 = (""); k9 = (""); stap=0; } if ((e.getSource() == knop1) && (k1.compareTo("")==0)){ knop1.setLabel ("x"); k1="x"; } if ((e.getSource() == knop2) && (k2.compareTo("")==0)){ knop2.setLabel ("x"); k2="x"; } if ((e.getSource() == knop3) && (k3.compareTo("")==0)){ knop3.setLabel ("x"); k3="x"; } if ((e.getSource() == knop4) && (k4.compareTo("")==0)){ knop4.setLabel ("x"); k4="x"; } if ((e.getSource() == knop5) && (k5.compareTo("")==0)){ knop5.setLabel ("x"); k5="x"; } if ((e.getSource() == knop6) && (k6.compareTo("")==0)){ knop6.setLabel ("x"); k6="x"; } if ((e.getSource() == knop7) && (k7.compareTo("")==0)){ knop7.setLabel ("x"); k7="x"; } if ((e.getSource() == knop8) && (k8.compareTo("")==0)){ knop8.setLabel ("x"); k8="x"; } if ((e.getSource() == knop9) && (k9.compareTo("")==0)){ knop9.setLabel ("x"); k9="x"; } stap++; if (stap==2){stap2();} if (stap==4){stap4();} if (stap==6){stap6();} if (stap==8){stap8();} } public void stap2(){ // computer knop5.setLabel("o"); k5="o"; stap += 1; } public void stap4() { // computer if (k1.compareTo("x")==0 && k2.compareTo("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0){ k2="o"; knop2.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0){ k4="o"; knop4.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0){ k6="o"; knop6.setLabel("o"); } stap += 1; } public void stap6() { // computer if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k7.compareTo ("x")==0){ k4="o"; knop4.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k8.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0 && k3.compareTo ("x")==0){ k2="o"; knop2.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0 && k7.compareTo ("x")==0){ k4="o"; knop4.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k6.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0 && k3.compareTo ("x")==0){ k2="o"; knop2.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0 && k4.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k4.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k6.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k8.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k9.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k2.compareTo ("x")==0){ k8="o"; knop8.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k4.compareTo ("x")==0){ k8="o"; knop8.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k6.compareTo ("x")==0){ k8="o"; knop8.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k9.compareTo ("x")==0){ k8="o"; knop8.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k9.compareTo ("x")==0){ k8="o"; knop8.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0 && k2.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0 && k6.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0 && k8.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0 && k9.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0 && k2.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0 && k4.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0 && k8.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0 && k9.compareTo ("x")==0){ k7="o"; knop7.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k2.compareTo ("x")==0){ k6="o"; knop6.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k3.compareTo ("x")==0){ k6="o"; knop6.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k8.compareTo ("x")==0){ k6="o"; knop6.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k9.compareTo ("x")==0){ k6="o"; knop6.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0 && k2.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0 && k4.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0 && k6.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0 && k9.compareTo ("x")==0){ k3="o"; knop3.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0 && k2. compareTo ("x")==0){ k4="o"; knop4.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0 && k3.compareTo ("x")==0){ k4="o"; knop4.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0 && k7.compareTo ("x")==0){ k4="o"; knop4.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0 && k8.compareTo ("x")==0){ k4="o"; knop4.setLabel("o"); } stap += 1; } public void stap8() { // computer if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k7.compareTo ("x")==0 && k6.compareTo("x")==0 ){ k8="o"; knop8.setLabel("o"); } if (k1.compareTo("x")==0 && k3.compareTo("x")==0 && k8.compareTo ("x")==0 && k6.compareTo("x")==0 ){ k9="o"; knop9.setLabel("o"); } if (k1.compareTo("x")==0 && k4.compareTo("x")==0 && k3.compareTo ("x")==0 && k8.compareTo("x")==0 ){ k6="o"; knop6.setLabel("o"); } if (k1.compareTo("x")==0 && k6.compareTo("x")==0 && k7.compareTo ("x")==0&& k8.compareTo("x")==0 ){ k9="o"; knop9.setLabel("o"); } if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k6.compareTo ("x")==0&& k8.compareTo("x")==0 ){ k9="o"; knop9.setLabel("o"); } if (k1.compareTo("x")==0 && k8.compareTo("x")==0 && k3.compareTo ("x")==0&& k6.compareTo("x")==0 ){ k9="o"; knop9.setLabel("o"); } if (k1.compareTo("x")==0 && k9.compareTo("x")==0 && k4.compareTo ("x")==0&& k3.compareTo("x")==0 ){ k2="o"; knop2.setLabel("o"); } stap += 1; } }
There has to be an order of sequence because I need to see which button was clicked on first.
For example those 2 lines don't work properly
if (k1.compareTo("x")==0 && k2.compareTo("x")==0 && k7.compareTo ("x")==0){
k4="o";
knop4.setLabel("o");
}
and
if (k1.compareTo("x")==0 && k7.compareTo("x")==0 && k2.compareTo ("x")==0){
k6="o";
knop6.setLabel("o");
}
The computer cannot see which button is clicked on first.
I need a way to make an order of sequenceLast edited by dannyy; 04-12-2011 at 10:39 AM.
- 04-06-2011, 01:34 PM #2
You haven't really posted enough information for us to answer your question. What do you mean by "order of sequence"?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-06-2011, 01:38 PM #3
Also, your code would be more readable in [ code][/code] tags. That way your indentations stay in place.
- 04-06-2011, 02:34 PM #4
... always assuming there were indentations.
db
- 04-12-2011, 07:30 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Sorry for no reaction for a few days i was away and couldn't reply. I have posted my full code so you can see what the problem is.
- 04-12-2011, 07:40 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Finding the actual problem is usually left up to you. We merely help you find it. To do this, we need very specific questions. Also, as stated previously, with large amounts of code, most people won't read it if you haven't put it in code tags.
- 04-12-2011, 08:06 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
- 04-12-2011, 08:43 AM #8
When you put your code in code tags, it doesn't autoformat it. It just leaves the indentations in the code. Putting [code] tags around the unformatted code still makes it just as hard to read.
Unless however you don't indent your code at all, and at that idea I'm appauled.
- 04-12-2011, 08:57 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The code tags are better, you might want to consider re-copy and pasting it with code tags so it is properly indented.
For the next point, a lot of the work needs to be done by you. Be specific about your question.
- 04-12-2011, 10:43 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
My problem is that when i get to stap6 and i click on a button two extra "o's" appear on the screen. I know what is wrong with my code but I don't know how to solve it.
When i press k1 then k2 and then k7 the computer sets 2 "o's" this is because my code says to set an o on a place when k1,k2,k7 are pressed. the order of clicking is of importance because i could press k1 then k2 and then k7, but also k1 then k7 and then k2. I hope i was clear enough :)
- 04-12-2011, 12:37 PM #11
I don't really understand what those stap methods are supposed to be doing.
Why don't you just write a method that checks for a win, and call that each turn? Just set the text of the JButton based on whose turn it is, then call the checkWin() method.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-13-2011, 11:44 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Stap is step in english It basically says that how many turns have passed.
For example
1st stap is me placing a x upper left. second stap is the computer placing an o
third stap would be me placing a x somewhere etc.
The order of me clicking is of importance and i need a way to make that clear to the computer.
- 04-13-2011, 01:29 PM #13
I gathered that "stap" meant "step", I just don't understand why you're doing it that way. When you look at a tic tac toe board, do you need to know what order the squares were filled to figure out whether X or O won? I don't think you do. So I don't know why your program does, either.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-13-2011, 10:20 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
- 04-13-2011, 10:52 PM #15
Member
- Join Date
- Apr 2011
- Posts
- 7
- Rep Power
- 0
The approach should be aligned with human and computer and that will be check recursively , what will be next value. The way of code is not so impressive.
I'll go through your code and will give you proper solution for this at this stage. But you can do better than this.
- 04-14-2011, 01:01 AM #16
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-14-2011, 01:04 AM #17
Again, does a human need to know the order of events to decide where to go on a tic tac toe board? If you jumped in mid-game, you'd still know where to go, right? The order doesn't matter, especially to the computer. Like has been suggested, the computer could use a recursive algorithm to figure out the next best move.
Google A* and minimax search. Those are probably the algorithms you're looking for.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-14-2011, 01:13 AM #18
Following on from Kevin's comments...
What would happen if the game board was extended to be 4x4 or greater? You would have to modify your program to add a whole bunch more duplicated code. Whereas if you used a generic recursive algorithm then the only change would be to the game board.
- 04-14-2011, 02:09 AM #19
@Kevin
He needs to learn A* for a simple Tic-Tac-Toe? A simple recursive loop searching for the best spot will do :D
EDIT: Actually, A* won't work anyways. A* is used for path finding, not tile placement.Last edited by ra4king; 04-14-2011 at 06:45 PM.
- 04-14-2011, 12:53 PM #20
The question is, how do you define "best spot"? It has been mentioned that a recursive algorithm would do the trick. This is A* search.
A game like tic-tac-toe can be represented as a path- from the start state (all blank squares) to a goal state (three in a row).
Minimax search, which I think of as a subset of A* search, is exactly what the OP should be doing.
I'm not sure what you even mean by "a simple loop searching for the best spot".How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
complete sequence
By aizen92 in forum New To JavaReplies: 25Last Post: 12-29-2010, 11:58 AM -
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03-25-2010, 06:59 AM -
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12-31-2009, 01:30 AM -
calling sequence
By rocky in forum Web FrameworksReplies: 0Last Post: 04-27-2009, 08:35 PM -
Escape Sequence
By Punter in forum New To JavaReplies: 4Last Post: 02-10-2009, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks