Tic Tac Toe Help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 2
Reputation: Surfline is an unknown quantity at this point 
Solved Threads: 0
Surfline Surfline is offline Offline
Newbie Poster

Tic Tac Toe Help

 
0
  #1
Jun 6th, 2005
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:
  1. import javax.swing.*;
  2. import BreezySwing.*;
  3. import java.awt.*;
  4.  
  5.  
  6. public class TicTacToeFrame extends GBFrame
  7. {
  8.  
  9. private JLabel header;
  10. private GBPanel box0,box1,box2,box3,box4,box5,box6,box7,box8;
  11. private Panel0 panel0;
  12. private Panel1 panel1;
  13. private Panel2 panel2;
  14. private Panel3 panel3;
  15. private Panel4 panel4;
  16. private Panel5 panel5;
  17. private Panel6 panel6;
  18. private Panel7 panel7;
  19. private Panel8 panel8;
  20.  
  21.  
  22. public TicTacToeFrame()
  23. {
  24. header = addLabel("TicTacToe", 1,1,1,1);
  25. panel0 = new Panel0();
  26. panel1 = new Panel1();
  27. panel2 = new Panel2();
  28. panel3 = new Panel3();
  29. panel4 = new Panel4();
  30. panel5 = new Panel5();
  31. panel6 = new Panel6();
  32. panel7 = new Panel7();
  33. panel8 = new Panel8();
  34.  
  35.  
  36. box0 = addPanel(panel0, 2,1,1,1);
  37. box1 = addPanel(panel1, 2,2,1,1);
  38. box2 = addPanel(panel2, 2,3,1,1);
  39. box3 = addPanel(panel3, 3,1,1,1);
  40. box4 = addPanel(panel4, 3,2,1,1);
  41. box5 = addPanel(panel5, 3,3,1,1);
  42. box6 = addPanel(panel6, 4,1,1,1);
  43. box7 = addPanel(panel7, 4,2,1,1);
  44. box8 = addPanel(panel8, 4,3,1,1);
  45.  
  46. box0.setBackground(Color.gray);
  47. box1.setBackground(Color.lightGray);
  48. box2.setBackground(Color.gray);
  49. box3.setBackground(Color.lightGray);
  50. box4.setBackground(Color.gray);
  51. box5.setBackground(Color.lightGray);
  52. box6.setBackground(Color.gray);
  53. box7.setBackground(Color.lightGray);
  54. box8.setBackground(Color.gray);
  55. public static void main(String [] args)
  56. {
  57. TicTacToeFrame GUI = new TicTacToeFrame();
  58. GUI.setSize(500,500);
  59. GUI.setVisible(true);
  60.  
  61. }
Heres code for ONE of the 9 panel classes I made:
  1. import BreezySwing.*;
  2. import java.awt.*;
  3.  
  4. public class Panel3 extends GBPanel
  5. {
  6. PanelObject xyz = new PanelObject();
  7. public void mousePressed(int x, int y)
  8. {
  9. Graphics g = getGraphics();
  10. xyz.addPick("3");
  11. if(xyz.getPlayerTurn() == 1)
  12. {
  13. g.drawOval(10,10,10,10);
  14. }
  15. else
  16. {
  17. g.drawRect(10,10,10,10);
  18. }
  19. if (xyz.checkIfWon() == true)
  20. {
  21. g.drawString("Congratulations Player " + xyz.getPlayerTurn() + " Won", 10 , 10);
  22. }
  23. xyz.changePlayer();
  24. }
  25. }

Heres code for PanelObject
  1. import javax.swing.*;
  2. import BreezySwing.*;
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5.  
  6. public class PanelObject
  7. {
  8.  
  9. private int player1=1, player2;
  10. ArrayList player1Picks = new ArrayList();
  11. ArrayList player2Picks = new ArrayList();
  12.  
  13.  
  14. //return 1 if player 1 turn, else return 2 if player 2 turn
  15. public int getPlayerTurn()
  16. {
  17. if(player1 == 1 )
  18. return 1;
  19. else
  20. return 2;
  21. }
  22.  
  23. //If player 1 turn over , change to player 2 turn
  24. public void changePlayer()
  25. {
  26. if(getPlayerTurn() == 1)
  27. {
  28. this.player1= 0;
  29. this.player2= 1;
  30. }
  31. else
  32. {
  33. this.player1=1;
  34. this.player2=0;
  35. }
  36.  
  37. }
  38. public void addPick(String pick)
  39. {
  40. if(getPlayerTurn() == 1)
  41. {
  42. player1Picks.add(pick);
  43.  
  44. }
  45. else
  46. {
  47. player2Picks.add(pick);
  48. }
  49. }
  50.  
  51. public boolean checkIfWon()
  52. {
  53.  
  54. String[] picks = new String[9];
  55. picks[0]="0";
  56. picks[1]="1";
  57. picks[2]="2";
  58. picks[3]="3";
  59. picks[4]="4";
  60. picks[5]="5";
  61. picks[6]="6";
  62. picks[7]="7";
  63. picks[8]="8";
  64.  
  65. int[] check = new int[9];
  66. check[0]=0;
  67. check[1]=0;
  68. check[2]=0;
  69. check[3]=0;
  70. check[4]=0;
  71. check[5]=0;
  72. check[6]=0;
  73. check[7]=0;
  74. check[8]=0;
  75.  
  76. if(getPlayerTurn() == 1)
  77. {
  78. for(int i = 0; i < player1Picks.size() ; i ++)
  79. {
  80. for(int k=0; k< picks.length ; k++)
  81. {
  82. if(player1Picks.get(i).equals(picks[k]))
  83. {
  84. check[k]=1; // sort in order
  85. }
  86. }
  87. }
  88. if(check[0]==1 && check[1]==1 && check[2]==1 || check[0]==1 && check[4]==1 && check[8]==1 ||
  89. check[0]==1 && check[3]==1 && check[6]==1 || check[1]==1 && check[4]==1 && check[7]==1 ||
  90. check[2]==1 && check[5]==1 && check[8]==1 || check[2]==1 && check[4]==1 && check[6]==1 ||
  91. check[3]==1 && check[4]==1 && check[5]==1 || check[6]==1 && check[7]==1 && check[8]==1)
  92. {
  93. return true;
  94. }
  95. else
  96. return false;
  97. }
  98. else
  99. {
  100. for(int b = 0; b < player1Picks.size() ; b ++)
  101. {
  102. for(int c=0; c< picks.length ; c++)
  103. {
  104. if(player1Picks.get(b).equals(picks[c]))
  105. {
  106. check[c]=1;
  107. }
  108. }
  109. }
  110. if(check[0]==1 && check[1]==1 && check[2]==1 || check[0]==1 && check[4]==1 && check[8]==1 ||
  111. check[0]==1 && check[3]==1 && check[6]==1 || check[1]==1 && check[4]==1 && check[7]==1 ||
  112. check[2]==1 && check[5]==1 && check[8]==1 || check[2]==1 && check[4]==1 && check[6]==1 ||
  113. check[3]==1 && check[4]==1 && check[5]==1 || check[6]==1 && check[7]==1 && check[8]==1)
  114. {
  115. return true;
  116. }
  117. else
  118. return false;
  119.  
  120. }
  121.  
  122. }
  123. }

Note that I use this BreezySwing thing which is similar to Swing so just ignore the different syntax between them.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2
Reputation: Surfline is an unknown quantity at this point 
Solved Threads: 0
Surfline Surfline is offline Offline
Newbie Poster

Re: Tic Tac Toe Help

 
0
  #2
Jun 7th, 2005
Also note: You ignore 90% of the code I just need to figure out how make the panel classes work with the panelObject class without making a new object every time. I just showed all the code to help understand.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Tic Tac Toe Help

 
0
  #3
Jun 7th, 2005
Personally, I would'nt use all those classes. You could use one class and it still be just as efficent. Plus, I would use buttons and set the label of the button to either x or o, which will make it much easier to tell wether someone has won or not.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3923 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC