943,696 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5017
  • Java RSS
Jul 29th, 2004
0

Help for my project

Expand Post »
Hi, if you don't know me my name is Dark, i need help with my assignment on Java. Could anyone help me. Here is what my project is about.

http://www.chevalier.nsw.edu.au/acad...sodad/axes.jpg

:-|
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
Dark is offline Offline
4 posts
since Jul 2004
Jul 30th, 2004
0

Re: Help for my project

The link only contains a picture! More information would be useful!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rickste_r is offline Offline
16 posts
since Jul 2004
Aug 8th, 2004
0

Re: Help for my project

Sorry, my bad. Don't worry about that pic, i have to do make a simple java game and i was thinking of dping Noughts and crosses, if you have a code for me to play that game could anyone give it to me because i really want to get up the class because i droped 4 places after the last 4 assessments
Reputation Points: 10
Solved Threads: 0
Unverified User
Dark is offline Offline
4 posts
since Jul 2004
Aug 9th, 2004
0

Re: Help for my project

I took this code from a thread I dont remember ... so its not on my credit.... anywayz it'll be helpful to you ...
Java Syntax (Toggle Plain Text)
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class NoughtsAndCrosses extends JPanel implements ActionListener
  7. {
  8. private boolean gameOver; // to keep track of the game status
  9. private String player; // to keep track of the current player
  10. private Panel board = new Panel(); // to hold the nine cells
  11. private Button [][]cell= new Button [3][3]; // the cells
  12. // these next two labels provide a border around the cells
  13. private Label blankL = new Label(" ");
  14. private Label blankR = new Label(" ");
  15. // the next two labels are centre alligned
  16. private Label error = new Label ("",1);
  17. private Label info = new Label ("player X to start", 1);
  18.  
  19. // the constructor
  20. public NoughtsAndCrosses()
  21. {
  22.  
  23. setBackground(Color.yellow);
  24. gameOver = false;
  25. player = "X"; // player X to start the game
  26. board.setLayout(new GridLayout(3,3)); // discussed later
  27. // creates and adds nine buttons to the board
  28. for (int i = 0; i<3; i++)
  29. {
  30. for (int j=0; j<3;j++)
  31. {
  32. cell[i][j] = new Button();
  33. cell[i][j].addActionListener(this);
  34. board.add(cell[i][j]);
  35. }
  36. }
  37. // positions the items on the screen
  38. setLayout(new BorderLayout());
  39. add("Center",board);
  40. add ("West", blankL);
  41. add("East", blankR);
  42. add("North", info);
  43. add("South",error);
  44. }
  45.  
  46. public void actionPerformed(ActionEvent e)
  47. {
  48. if (!gameOver)// process mouse click only if game is not over
  49. {
  50. for (int i = 0; i<3; i++)
  51. {
  52. for (int j=0; j<3;j++)
  53. {
  54. if (e.getSource()== cell[i][j])
  55. {
  56. processEvent(i,j); // call worker method to process event
  57. }
  58. }
  59. }
  60. }
  61. }
  62.  
  63. // worker method to process a given button press
  64. private void processEvent(int i, int j)
  65. {
  66. // check no attempt made to move into an occupied cell
  67. if (cell[i][j].getLabel().equals("X") ||
  68. cell[i][j].getLabel().equals("O"))
  69. {
  70. error.setText("This cell already taken!!");
  71. }
  72. else
  73. {
  74. // clear any error messages
  75. error.setText("");
  76. // change button label to current player
  77. cell[i][j].setLabel(player);
  78. // check whether this move results in game over
  79. if (hasWon(i, j))// process game over
  80. {
  81. info.setText(" player " + player + " has won!");
  82. gameOver = true;
  83. }
  84. else // process game not over
  85. {
  86. // change player
  87. if (player.equals("X"))
  88. {
  89. player = "O";
  90. }
  91. else
  92. {
  93. player = "X";
  94. }
  95. info.setText("player "+player+" to go next");
  96. }
  97. }
  98. }
  99.  
  100. // worker method to check if game over
  101. private boolean hasWon(int i, int j)
  102. {
  103. boolean won;
  104. // check current row
  105. won = true;
  106. for(int col = 0; col<3; col++)
  107. {
  108. if (!cell[i][col].getLabel().equals(player))
  109. {
  110. won = false;
  111. }
  112. }
  113. if (!won)
  114. {
  115. // check current column
  116. won = true;
  117. for(int row = 0; row<3; row++)
  118. {
  119. if (!cell[row][j].getLabel().equals(player))
  120. {
  121. won = false;
  122. }
  123. }
  124. }
  125. if (!won)
  126. {
  127. // check left diagonal
  128. won = true;
  129. for(int num = 0; num<3; num++)
  130. {
  131. if (!cell[num][num].getLabel().equals(player))
  132. {
  133. won = false;
  134. }
  135. }
  136. }
  137. if (!won)
  138. {
  139. // check right diagonal
  140. won = true;
  141. for(int num = 0; num<3; num++)
  142. {
  143. if (!cell[2-num][num].getLabel().equals(player))
  144. {
  145. won = false;
  146. }
  147. }
  148. }
  149. return won;
  150. }
  151.  
  152. public static void main (String [] args)
  153. {
  154. //run the program creating a new JFrame
  155. JFrame frame = new JFrame("Noughts and Crosses v1.0");
  156. NoughtsAndCrosses game = new NoughtsAndCrosses();
  157. frame.setSize(230,230);
  158. frame.getContentPane().add(game);
  159. frame.setVisible(true);
  160. }
  161. }
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Aug 9th, 2004
0

Re: Help for my project

Hey, If you want to make cool Java Games, try to read the book titled Black art of Game Programming, I forgot the author though. : ) But If you want, you can e-mail me at grifter140@yahoo.com.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mav2040 is offline Offline
5 posts
since Aug 2004
Aug 10th, 2004
0

Re: Help for my project

thanks mav2040 and nanosani
Reputation Points: 10
Solved Threads: 0
Unverified User
Dark is offline Offline
4 posts
since Jul 2004
Aug 12th, 2004
0

Re: Help for my project

nanosani, with that code you gave me do you have another code for it but it uses the code in a different frame
Reputation Points: 10
Solved Threads: 0
Unverified User
Dark is offline Offline
4 posts
since Jul 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: Java Applet and IIS
Next Thread in Java Forum Timeline: Ms Access sql problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC