Help for my project

Reply

Join Date: Jul 2004
Posts: 4
Reputation: Dark is an unknown quantity at this point 
Solved Threads: 0
Dark's Avatar
Dark Dark is offline Offline
Unverified User

Help for my project

 
0
  #1
Jul 29th, 2004
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

:-|
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 16
Reputation: rickste_r is an unknown quantity at this point 
Solved Threads: 0
rickste_r's Avatar
rickste_r rickste_r is offline Offline
Newbie Poster

Re: Help for my project

 
0
  #2
Jul 30th, 2004
The link only contains a picture! More information would be useful!
Only lemmings should jump to conclusions! :rolleyes: Please rate me!
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: Dark is an unknown quantity at this point 
Solved Threads: 0
Dark's Avatar
Dark Dark is offline Offline
Unverified User

Re: Help for my project

 
0
  #3
Aug 8th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 54
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: Help for my project

 
0
  #4
Aug 9th, 2004
I took this code from a thread I dont remember ... so its not on my credit.... anywayz it'll be helpful to you ...
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 5
Reputation: mav2040 is an unknown quantity at this point 
Solved Threads: 0
mav2040 mav2040 is offline Offline
Newbie Poster

Re: Help for my project

 
0
  #5
Aug 9th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: Dark is an unknown quantity at this point 
Solved Threads: 0
Dark's Avatar
Dark Dark is offline Offline
Unverified User

Re: Help for my project

 
0
  #6
Aug 10th, 2004
thanks mav2040 and nanosani
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 4
Reputation: Dark is an unknown quantity at this point 
Solved Threads: 0
Dark's Avatar
Dark Dark is offline Offline
Unverified User

Re: Help for my project

 
0
  #7
Aug 12th, 2004
nanosani, with that code you gave me do you have another code for it but it uses the code in a different frame
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC