URGENT Help Needed With TicTacToe Application Source Code!!!

Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

URGENT Help Needed With TicTacToe Application Source Code!!!

 
0
  #1
Nov 24th, 2004
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.

  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++
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

 
0
  #2
Nov 24th, 2004
your screaming you demand urgent help is enough reason to let your request sit for a few months before looking at it.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

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

 
0
  #3
Nov 24th, 2004
One hint I will give you.

Where do you set the value of 'play' after each turn??
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC