| | |
Tic Tac Toe Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Solved Threads: 0
Hey
I'm trying to write a program to make a simple Tic-Tac-Toe Program. I just started coding it, so this is my rough draft version, i haven't worked on lots of checks i know i need to do later. However I have a big problem I cant seem to figure out how to solve.
I have three types of classes but 11 in total (The actual Frame class which makes the panels, 9 panel classes which are each just a box in the TicTacToe, and a PanelObject class which has all the methods to do stuff)
--All the code for these will be provided below--
In each Panel class (ex panel0.java) i instanciate a new PanelObject so I can use the PanelObject's methods. For example: PanelObject xyz = new PanelObject(); (Then in the panel0.java class I can do: xyz.getPlayerTurn(); ) The problem with this is that everytime the program uses a different panel it makes a different PanelObject. I need to keep track of what player went so I need a way for just one PanelObject to be instanciated for all 9 panel classes. For example, when user1 clicks panel 1, i track that user1 picked, but then when user2 clicks panel2, panel2 makes a new object(doesn't use the same) so it displays the user1 stuff instead of user2.
Hope that makes sense..
Heres the code..
Frame class:
Heres code for ONE of the 9 panel classes I made:
Heres code for PanelObject
Note that I use this BreezySwing thing which is similar to Swing so just ignore the different syntax between them.
I'm trying to write a program to make a simple Tic-Tac-Toe Program. I just started coding it, so this is my rough draft version, i haven't worked on lots of checks i know i need to do later. However I have a big problem I cant seem to figure out how to solve.
I have three types of classes but 11 in total (The actual Frame class which makes the panels, 9 panel classes which are each just a box in the TicTacToe, and a PanelObject class which has all the methods to do stuff)
--All the code for these will be provided below--
In each Panel class (ex panel0.java) i instanciate a new PanelObject so I can use the PanelObject's methods. For example: PanelObject xyz = new PanelObject(); (Then in the panel0.java class I can do: xyz.getPlayerTurn(); ) The problem with this is that everytime the program uses a different panel it makes a different PanelObject. I need to keep track of what player went so I need a way for just one PanelObject to be instanciated for all 9 panel classes. For example, when user1 clicks panel 1, i track that user1 picked, but then when user2 clicks panel2, panel2 makes a new object(doesn't use the same) so it displays the user1 stuff instead of user2.
Hope that makes sense..
Heres the code..
Frame class:
Java Syntax (Toggle Plain Text)
import javax.swing.*; import BreezySwing.*; import java.awt.*; public class TicTacToeFrame extends GBFrame { private JLabel header; private GBPanel box0,box1,box2,box3,box4,box5,box6,box7,box8; private Panel0 panel0; private Panel1 panel1; private Panel2 panel2; private Panel3 panel3; private Panel4 panel4; private Panel5 panel5; private Panel6 panel6; private Panel7 panel7; private Panel8 panel8; public TicTacToeFrame() { header = addLabel("TicTacToe", 1,1,1,1); panel0 = new Panel0(); panel1 = new Panel1(); panel2 = new Panel2(); panel3 = new Panel3(); panel4 = new Panel4(); panel5 = new Panel5(); panel6 = new Panel6(); panel7 = new Panel7(); panel8 = new Panel8(); box0 = addPanel(panel0, 2,1,1,1); box1 = addPanel(panel1, 2,2,1,1); box2 = addPanel(panel2, 2,3,1,1); box3 = addPanel(panel3, 3,1,1,1); box4 = addPanel(panel4, 3,2,1,1); box5 = addPanel(panel5, 3,3,1,1); box6 = addPanel(panel6, 4,1,1,1); box7 = addPanel(panel7, 4,2,1,1); box8 = addPanel(panel8, 4,3,1,1); box0.setBackground(Color.gray); box1.setBackground(Color.lightGray); box2.setBackground(Color.gray); box3.setBackground(Color.lightGray); box4.setBackground(Color.gray); box5.setBackground(Color.lightGray); box6.setBackground(Color.gray); box7.setBackground(Color.lightGray); box8.setBackground(Color.gray); public static void main(String [] args) { TicTacToeFrame GUI = new TicTacToeFrame(); GUI.setSize(500,500); GUI.setVisible(true); }
Java Syntax (Toggle Plain Text)
import BreezySwing.*; import java.awt.*; public class Panel3 extends GBPanel { PanelObject xyz = new PanelObject(); public void mousePressed(int x, int y) { Graphics g = getGraphics(); xyz.addPick("3"); if(xyz.getPlayerTurn() == 1) { g.drawOval(10,10,10,10); } else { g.drawRect(10,10,10,10); } if (xyz.checkIfWon() == true) { g.drawString("Congratulations Player " + xyz.getPlayerTurn() + " Won", 10 , 10); } xyz.changePlayer(); } }
Heres code for PanelObject
Java Syntax (Toggle Plain Text)
import javax.swing.*; import BreezySwing.*; import java.awt.*; import java.util.ArrayList; public class PanelObject { private int player1=1, player2; ArrayList player1Picks = new ArrayList(); ArrayList player2Picks = new ArrayList(); //return 1 if player 1 turn, else return 2 if player 2 turn public int getPlayerTurn() { if(player1 == 1 ) return 1; else return 2; } //If player 1 turn over , change to player 2 turn public void changePlayer() { if(getPlayerTurn() == 1) { this.player1= 0; this.player2= 1; } else { this.player1=1; this.player2=0; } } public void addPick(String pick) { if(getPlayerTurn() == 1) { player1Picks.add(pick); } else { player2Picks.add(pick); } } public boolean checkIfWon() { String[] picks = new String[9]; picks[0]="0"; picks[1]="1"; picks[2]="2"; picks[3]="3"; picks[4]="4"; picks[5]="5"; picks[6]="6"; picks[7]="7"; picks[8]="8"; int[] check = new int[9]; check[0]=0; check[1]=0; check[2]=0; check[3]=0; check[4]=0; check[5]=0; check[6]=0; check[7]=0; check[8]=0; if(getPlayerTurn() == 1) { for(int i = 0; i < player1Picks.size() ; i ++) { for(int k=0; k< picks.length ; k++) { if(player1Picks.get(i).equals(picks[k])) { check[k]=1; // sort in order } } } if(check[0]==1 && check[1]==1 && check[2]==1 || check[0]==1 && check[4]==1 && check[8]==1 || check[0]==1 && check[3]==1 && check[6]==1 || check[1]==1 && check[4]==1 && check[7]==1 || check[2]==1 && check[5]==1 && check[8]==1 || check[2]==1 && check[4]==1 && check[6]==1 || check[3]==1 && check[4]==1 && check[5]==1 || check[6]==1 && check[7]==1 && check[8]==1) { return true; } else return false; } else { for(int b = 0; b < player1Picks.size() ; b ++) { for(int c=0; c< picks.length ; c++) { if(player1Picks.get(b).equals(picks[c])) { check[c]=1; } } } if(check[0]==1 && check[1]==1 && check[2]==1 || check[0]==1 && check[4]==1 && check[8]==1 || check[0]==1 && check[3]==1 && check[6]==1 || check[1]==1 && check[4]==1 && check[7]==1 || check[2]==1 && check[5]==1 && check[8]==1 || check[2]==1 && check[4]==1 && check[6]==1 || check[3]==1 && check[4]==1 && check[5]==1 || check[6]==1 && check[7]==1 && check[8]==1) { return true; } else return false; } } }
Note that I use this BreezySwing thing which is similar to Swing so just ignore the different syntax between them.
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
- Tic Tac Toe Homework (C++)
- Tic Tac toe help (C)
- Tic Tac Toe AI help, where to reset variables. (C++)
Other Threads in the Java Forum
- Previous Thread: SCJP suggestions
- Next Thread: Suggest me what other resources should i need to have?
Views: 3923 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addball addressbook android api append apple applet application arguments array arrays automation binary bluetooth button chat class classes client code component css csv database draw eclipse ee error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaarraylist javaprojects jmf jni jpanel julia jvm key linux list loan loop map method methods mobile netbeans newbie number object oracle output packets phone print problem program programming project recursion reporting robot scanner screen se server service set size sms socket software sort sql stream string swing test threads time transfer tree ubuntu windows wrong






