944,172 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5971
  • Java RSS
Nov 24th, 2004
0

URGENT Help Needed With TicTacToe Application Source Code!!!

Expand Post »
Hi,

I'm in URGENT need of anyone who can help me with the following TicTacToe code. When you run it, it lets you click a box. When you click a box, X appears. The, when it's O's player's turn, it will not print an O.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. class TicTacToe extends JFrame implements ActionListener
  6. {
  7. public String play = "X's turn.";
  8. public String one1 = " ";
  9. public String two2 = " ";
  10. public String three3 = " ";
  11. public String four4 = " ";
  12. public String five5 = " ";
  13. public String six6 = " ";
  14. public String seven7 = " ";
  15. public String eight8 = " ";
  16. public String nine9 = " ";
  17. public boolean xTurn = true;
  18.  
  19.  
  20. JButton one = new JButton(one1);
  21. JButton two = new JButton(two2);
  22. JButton three = new JButton(three3);
  23. JButton four = new JButton(four4);
  24. JButton five = new JButton(five5);
  25. JButton six = new JButton(six6);
  26. JButton seven = new JButton(seven7);
  27. JButton eight = new JButton(eight8);
  28. JButton nine = new JButton(nine9);
  29. JLabel status = new JLabel(play);
  30.  
  31.  
  32.  
  33. public TicTacToe()
  34. {
  35. super("Tic-Tac-Toe");
  36. setSize(200,200);
  37. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. setVisible(true);
  39.  
  40. Container contentArea = getContentPane();
  41. GridBagLayout flowManager = new GridBagLayout();
  42. GridBagConstraints pos = new GridBagConstraints();
  43. contentArea.setLayout(flowManager);
  44.  
  45. one.addActionListener(this);
  46. two.addActionListener(this);
  47. three.addActionListener(this);
  48. four.addActionListener(this);
  49. five.addActionListener(this);
  50. six.addActionListener(this);
  51. seven.addActionListener(this);
  52. eight.addActionListener(this);
  53. nine.addActionListener(this);
  54.  
  55.  
  56. pos.gridx=0; pos.gridy=0;
  57. contentArea.add(one,pos);
  58.  
  59. pos.gridx=1; pos.gridy=0;
  60. contentArea.add(two,pos);
  61.  
  62. pos.gridx=2; pos.gridy=0;
  63. contentArea.add(three,pos);
  64.  
  65. pos.gridx=0; pos.gridy=1;
  66. contentArea.add(four,pos);
  67.  
  68. pos.gridx=1; pos.gridy=1;
  69. contentArea.add(five,pos);
  70.  
  71. pos.gridx=2; pos.gridy=1;
  72. contentArea.add(six,pos);
  73.  
  74. pos.gridx=0; pos.gridy=2;
  75. contentArea.add(seven,pos);
  76.  
  77. pos.gridx=1; pos.gridy=2;
  78. contentArea.add(eight,pos);
  79.  
  80. pos.gridx=2; pos.gridy=2;
  81. contentArea.add(nine,pos);
  82.  
  83. pos.gridx=1; pos.gridy=3;
  84. contentArea.add(status,pos);
  85.  
  86. setContentPane(contentArea);
  87. }
  88.  
  89.  
  90. public void actionPerformed(ActionEvent event)
  91. {
  92. //O's Turn:
  93.  
  94. if(!(xTurn) && play=="O's turn." && event.getSource()==one){
  95. one.setText("O");
  96. xTurn=true;
  97. status.setText("X's turn.");
  98. }
  99.  
  100.  
  101. else if(!(xTurn) && play=="O's turn." && event.getSource()==two){
  102. two.setText("O");
  103. xTurn=true;
  104. status.setText("X's turn.");
  105. }
  106.  
  107.  
  108.  
  109. else if(!(xTurn) && play=="O's turn." && event.getSource()==three){
  110. three.setText("O");
  111. xTurn=true;
  112. status.setText("X's turn.");
  113. }
  114.  
  115.  
  116.  
  117. else if(!(xTurn) && play=="O's turn." && event.getSource()==four){
  118. four.setText("O");
  119. xTurn=true;
  120. status.setText("X's turn.");
  121. }
  122.  
  123.  
  124.  
  125. else if(!(xTurn) && play=="O's turn." && event.getSource()==five){
  126. five.setText("O");
  127. xTurn=true;
  128. status.setText("X's turn.");
  129. }
  130.  
  131.  
  132.  
  133. else if(!(xTurn) && play=="O's turn." && event.getSource()==six){
  134. six.setText("O");
  135. xTurn=true;
  136. status.setText("X's turn.");
  137. }
  138.  
  139.  
  140.  
  141.  
  142. else if(!(xTurn) && play=="O's turn." && event.getSource()==seven){
  143. seven.setText("O");
  144. xTurn=true;
  145. status.setText("X's turn.");
  146. }
  147.  
  148.  
  149.  
  150. else if(!(xTurn) && play=="O's turn." && event.getSource()==eight){
  151. eight.setText("O");
  152. xTurn=true;
  153. status.setText("X's turn.");
  154. }
  155.  
  156.  
  157. else if(!(xTurn) && play=="O's turn." && event.getSource()==nine){
  158. nine.setText("O");
  159. xTurn=true;
  160. status.setText("X's turn.");
  161. }
  162.  
  163.  
  164.  
  165.  
  166. //X's turn
  167. if(xTurn && play=="X's turn." && event.getSource()==one){
  168. one.setText("X");
  169. xTurn=false;
  170. status.setText("O's turn.");
  171. }
  172. else if(xTurn && play=="X's turn." && event.getSource()==two){
  173. two.setText("X");
  174. xTurn=false;
  175. status.setText("O's turn.");
  176. }
  177. else if(xTurn && play=="X's turn." && event.getSource()==three){
  178. three.setText("X");
  179. xTurn=false;
  180. status.setText("O's turn.");
  181. }
  182. else if(xTurn && play=="X's turn." && event.getSource()==four){
  183. four.setText("X");
  184. xTurn=false;
  185. status.setText("O's turn.");
  186. }
  187. else if(xTurn && play=="X's turn." && event.getSource()==five){
  188. five.setText("X");
  189. xTurn=false;
  190. status.setText("O's turn.");
  191. }
  192. else if(xTurn && play=="X's turn." && event.getSource()==six){
  193. six.setText("X");
  194. xTurn=false;
  195. status.setText("O's turn.");
  196. }
  197. else if(xTurn && play=="X's turn." && event.getSource()==seven){
  198. seven.setText("X");
  199. xTurn=false;
  200. status.setText("O's turn.");
  201. }
  202. else if(xTurn && play=="X's turn." && event.getSource()==eight){
  203. eight.setText("X");
  204. xTurn=false;
  205. status.setText("O's turn.");
  206. }
  207. else if(xTurn && play=="X's turn." && event.getSource()==nine){
  208. nine.setText("X");
  209. xTurn=false;
  210. status.setText("O's turn.");
  211. }
  212. }
  213.  
  214.  
  215. public static void main(String[] args)
  216. { TicTacToe eg = new TicTacToe(); }
  217. }

Thanks!

C++
Similar Threads
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Nov 24th, 2004
0

Re: URGENT Help Needed With TicTacToe Application Source Code!!!

your screaming you demand urgent help is enough reason to let your request sit for a few months before looking at it.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 24th, 2004
0

Re: URGENT Help Needed With TicTacToe Application Source Code!!!

One hint I will give you.

Where do you set the value of 'play' after each turn??
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem in finding out an array
Next Thread in Java Forum Timeline: 4Q i cannot find in my book





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC