Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 15th, 2008, 1:16 PM   #11
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: back to online battleship

OK, so your server must wait for two people to connect, once that is done execution can continue. It should first ask P1 to send there board and store it. Once it has recieved P1 Board it asks for and recieves P2 Board.

Once that has been done you can enter a loop, that is continues whilst gameWon = False. In the loop you want to do something like this.

while(gameWon==False){
   P1.requestMove();
   P1.getMove(); //stall until move is recieved.
   P2.boardUpdate(); //makes player ones move onto P2's board to check for hits etc
   isWin = P1.CheckWin(); //check to see if P2 has any boats left
   P1.sendMoveStatus(); //Show hits miss....
   P2.sendMoveStatus(); //result of P1's Move.
   if(isWin == True){
      P1.send("P1 Won");
      P2.send("P1 Won");
      break;
      }
   P2.requestMove();
   P2.getMove(); //stall until move is recieved.
   P1.boardUpdate(); //makes player two's move onto P1s board to check for hits etc
   isWin = P2.CheckWin(); //check to see if P1has any boats left
   P2.sendMoveStatus(); //Show hits miss....
   P1.sendMoveStatus(); //result of P2s Move.
   if(isWin == True){
      P1.send("P2 Won");
      P2.send("P2 Won");
      break;
      }
}

//close connections or whatever comes next

Is that any help to you?

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language

Last edited by Freaky Chris; May 15th, 2008 at 1:32 PM.
Freaky Chris is offline   Reply With Quote
Old May 15th, 2008, 2:10 PM   #12
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

now i am wondering about what wizard said if the clients keep their boards separate because i already have a version where each client sends the move to the server and the server passes it to the other client and executes the move, could i just then have the client send through the server if the game ends or not to the other client? As for what you said i think i am pretty much doing that now but it gets confused because server only takes every other move because it still plays with only 1 player so the player 2 move is not done or something here is the code i have:

client
java Syntax (Toggle Plain Text)
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import ship.*;
  5.  
  6. public class Client
  7. {
  8. private static Socket connection;
  9. private static ObjectOutputStream output;
  10. private static ObjectInputStream input;
  11. static Board b;
  12.  
  13. public static void main(String[] args) throws IOException
  14. {
  15. int counter = 0;
  16. initializePlayer();
  17. boolean playerWins = false;
  18. while (!playerWins)
  19. {
  20. takeTurn();
  21. /* counter = (Integer) receiveObject();
  22.   if (counter == 4)
  23.   {
  24.   playerWins = true;
  25.   } // end if
  26.   */
  27. } // end while
  28. /* if (playerWins)
  29.   {
  30.   receive();
  31.   b = (Board) receiveObject();
  32.   System.out.println();
  33.   b.printBoard();
  34.   System.out.println();
  35.   } // end if
  36.   */
  37. } // end main
  38.  
  39. // this initializes 1 player by sending its board and playerName to the Server
  40. static void initializePlayer()
  41. {
  42. b = new Board();
  43. try
  44. {
  45. connection = new Socket(InetAddress.getByName("192.168.1.102"), 1234);
  46. streams();
  47.  
  48. // receiving to send name
  49. receive();
  50. String getName = promptUser();
  51. send(getName);
  52. send(b);
  53. b.printBoard();
  54. b = ((Board) receiveObject());
  55. System.out.println();
  56. System.out.println("Board after ships are added");
  57. b.printBoard();
  58. } // end try
  59. catch(IOException e)
  60. {
  61. System.out.println("Caught IOException in initializePlayer " + e);
  62. } // end catch
  63. } // end initializePlayer
  64. static void takeTurn()
  65. {
  66. try
  67. {
  68. // receiving to take turn
  69. String receive = receive();
  70. String rowCol = promptUser();
  71. send(rowCol);
  72. send(b);
  73. receive();
  74. b = ((Board) receiveObject());
  75. System.out.println();
  76. System.out.println("Board after move");
  77. b.printBoard();
  78. } // end try
  79. catch(IOException e)
  80. {
  81. System.out.println("Caught IOException in takeTurn" + e);
  82. } // end catch
  83. } // end takeTurn
  84. // establish the streams
  85. private static void streams() throws IOException
  86. {
  87. output=new ObjectOutputStream(connection.getOutputStream());
  88. output.flush();
  89. input=new ObjectInputStream(connection.getInputStream());
  90. } // end streams
  91.  
  92. private static void send(Object b)
  93. {
  94. try
  95. {
  96. output.writeObject(b);
  97. output.flush();
  98. } // end try
  99. catch(IOException e)
  100. {
  101. System.out.println("Caught IOException in send " + e);
  102. } // end catch
  103. } // end send
  104.  
  105. private static String receive() throws IOException
  106. {
  107. String test = "";
  108. try
  109. {
  110. test = (String) input.readObject();
  111. System.out.print("\n" + test);
  112. } // end try
  113. catch(ClassNotFoundException classNotFoundException)
  114. {
  115. System.out.print("\nUnknown object recieved");
  116. } // end catch
  117. return test;
  118. } // end receiveObject
  119.  
  120. private static Object receiveObject() throws IOException
  121. {
  122. Object test = new Object();
  123. try
  124. {
  125. test = input.readObject();
  126. } // end try
  127. catch(ClassNotFoundException classNotFoundException)
  128. {
  129. System.out.print("\nUnknown object recieved");
  130. } // end catch
  131. return test;
  132. } // end receiveObject
  133.  
  134. // take the row line from the client
  135. private static String getRow(String line)
  136. {
  137. String row = "";
  138. // take the first char in the string which is the row
  139. char rowC = line.charAt(0);
  140. // convert it to a string and return it.
  141. row = Character.toString(rowC);
  142. System.out.println(row);
  143. return row;
  144. } // end getRow
  145.  
  146. // take the column line from the client
  147. private static String getCol(String line)
  148. {
  149. String col = "";
  150. // take the first char in the string which is the row
  151. char colC = line.charAt(2);
  152. // convert it to a string and return it.
  153. col = Character.toString(colC);
  154. System.out.println(col);
  155. return col;
  156. } // end getRow
  157.  
  158. // get input from the user to play game or exit
  159. public static String promptUser() throws IOException
  160. {
  161. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  162. int lineToInt = 0;
  163.  
  164. String line = null;
  165. try
  166. {
  167. line = keyInput.readLine();
  168. } // end try
  169. //catches bad user input and throws exception
  170. catch (NumberFormatException ex)
  171. {
  172. System.out.print("Error bad data: ");
  173. } // end catch
  174.  
  175. return line;
  176. } // end promptUser
  177.  
  178. } // end Client

Here is the server
java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2. import java.net.*;
  3. import ship.*;
  4.  
  5. // this is to test sending an entire board over the network
  6.  
  7. public class testRun
  8. {
  9. private static Socket connection;
  10. private static ObjectOutputStream output;
  11. private static ObjectInputStream input;
  12. static Player p1;
  13. static Player p2;
  14. static Board b1;
  15. static Board b2;
  16. static Sub subs;
  17. static BattleShip battleShips;
  18. static Patrol patrols;
  19. static Carrier carriers;
  20.  
  21. public static void main(String[] args) throws IOException
  22. {
  23. boolean playerWins = false;
  24. boolean playerOneTurn = true;
  25. ServerSocket s = new ServerSocket(1234);
  26. receivePlayer1(s);
  27. receivePlayer2(s);
  28.  
  29.  
  30. // this is when there is only 1 player
  31. /* while (!playerWins)
  32.   {
  33.   takeTurn1();
  34.   send((Integer)p1.counter);
  35.   if (p1.endGame(p1.counter))
  36.   {
  37.   playerWins = true;
  38.   } // end if
  39.   } // end while
  40.   */
  41.  
  42. while (!playerWins)
  43. {
  44. if (playerOneTurn)
  45. {
  46. takeTurn1();
  47. // check to see if player won the game
  48. if (p1.endGame(p1.counter))
  49. {
  50. playerWins = true;
  51. } // end if
  52. playerOneTurn = false;
  53. } // end playerOneTurn
  54. else if (!playerOneTurn)
  55. {
  56. System.out.println("inside player2 if");
  57. takeTurn2();
  58. // check to see if player won the game
  59. if (p2.endGame(p2.counter))
  60. {
  61. playerWins = true;
  62. } // end if
  63. playerOneTurn = true;
  64. } // end else if
  65. } // end while
  66. if (playerWins)
  67. {
  68. String wonGameInfo = p1.name + " has won the game!\n" + p1.name + "'s final board";
  69. send(wonGameInfo);
  70. send(b1);
  71. System.out.println();
  72. } // end if
  73. } // end main
  74.  
  75. static void receivePlayer1(ServerSocket s)
  76. {
  77. // initialize Player 1
  78. b1 = new Board();
  79. String namePrompt = "Please enter a Player name: ";
  80. String name = "";
  81. try
  82. {
  83. connection = s.accept();
  84. streams();
  85. send(namePrompt);
  86. name = (String) receive();
  87. p1 = new Player(name);
  88. System.out.println("Player's name is " + p1.name);
  89. b1 = (Board)receive();
  90. System.out.println("The empty board is ");
  91. b1.printBoard();
  92. // set the board
  93. subs = new Sub();
  94. battleShips = new BattleShip();
  95. patrols = new Patrol();
  96. carriers = new Carrier();
  97. // set board
  98. p1.setSub(b1, subs);
  99. p1.setBattleShip(b1, battleShips);
  100. p1.setPatrol(b1, patrols);
  101. p1.setCarrier(b1, carriers);
  102. System.out.println(p1.name + "'s board: ");
  103. System.out.println();
  104. b1.printBoard();
  105. send((Board) b1);
  106. } // end try
  107. catch(IOException e)
  108. {
  109. System.out.println("Caught IOException in receivePlayer " + e);
  110. } // end catch
  111. } // end receivePlayer1
  112.  
  113. static void receivePlayer2(ServerSocket s)
  114. {
  115. // initialize Player 2
  116. b2 = new Board();
  117. String namePrompt = "Please enter a Player name: ";
  118. String name = "";
  119. try
  120. {
  121. //ServerSocket s = new ServerSocket(1234);
  122. connection = s.accept();
  123. streams();
  124. send(namePrompt);
  125. name = (String) receive();
  126. p2 = new Player(name);
  127. System.out.println("Player's name is " + p2.name);
  128. b2 = (Board)receive();
  129. System.out.println("The empty board is ");
  130. b2.printBoard();
  131. // set the board
  132. subs = new Sub();
  133. battleShips = new BattleShip();
  134. patrols = new Patrol();
  135. carriers = new Carrier();
  136. // set board
  137. p2.setSub(b2, subs);
  138. p2.setBattleShip(b2, battleShips);
  139. p2.setPatrol(b2, patrols);
  140. p2.setCarrier(b2, carriers);
  141. System.out.println(p2.name + "'s board: ");
  142. System.out.println();
  143. b2.printBoard();
  144. send((Board) b2);
  145. } // end try
  146. catch(IOException e)
  147. {
  148. System.out.println("Caught IOException in receivePlayer " + e);
  149. } // end catch
  150. } // end receivePlayer2
  151.  
  152.  
  153. // get the move from the user and take turn for player 1
  154. // player 1 wants to take turn on player 2's board
  155. static void takeTurn1() throws IOException
  156. {
  157. String getRowCol = "Please enter a row and col 0-9 so it looks like this 1,2: ";
  158. int row = 0;
  159. int col = 0;
  160. send((String) getRowCol);
  161. String receivedRowCol = (String) receive();
  162. row = Integer.parseInt(getRow((String) receivedRowCol));
  163. col = Integer.parseInt(getCol((String) receivedRowCol));
  164. String rowColAre = "The row is " + row + "\nThe Col is " + col + "\n";
  165. send((String) rowColAre);
  166. System.out.println("The row is " + row);
  167. System.out.println("The col is " + col);
  168. b1 = (Board) receive();
  169. p1.takeTurn(b1, subs, battleShips, patrols, carriers, row, col);
  170. b1.printBoard();
  171. send((Board) b1);
  172. //send((Board) b1);
  173. } // end takeTurn1
  174.  
  175. // get the move from the user and take turn for player 2
  176. // player 2 wants to take turn on player 1's board
  177. static void takeTurn2() throws IOException
  178. {
  179. String getRowCol = "Please enter a row and col 0-9 so it looks like this 1,2: ";
  180. int row = 0;
  181. int col = 0;
  182. send((String) getRowCol);
  183. String receivedRowCol = (String) receive();
  184. row = Integer.parseInt(getRow((String) receivedRowCol));
  185. col = Integer.parseInt(getCol((String) receivedRowCol));
  186. String rowColAre = "The row is " + row + "\nThe Col is " + col + "\n";
  187. send((String) rowColAre);
  188. System.out.println("The row is " + row);
  189. System.out.println("The col is " + col);
  190. b2 = (Board) receive();
  191. p2.takeTurn(b1, subs, battleShips, patrols, carriers, row, col);
  192. b2.printBoard();
  193. send((Board) b2);
  194. //send((Board) b1);
  195. } // end takeTurn1
  196.  
  197.  
  198. private static void send(Object b)
  199. {
  200. try
  201. {
  202. output.writeObject(b);
  203. output.flush();
  204. } // end try
  205. catch(IOException ioException)
  206. {
  207. }
  208. } // end send
  209.  
  210. // establish the streams
  211. private static void streams() throws IOException
  212. {
  213. output=new ObjectOutputStream(connection.getOutputStream());
  214. output.flush();
  215. input=new ObjectInputStream(connection.getInputStream());
  216. } // end streams
  217.  
  218. private static Object receive() throws IOException
  219. {
  220. Object test = new Board();
  221. try
  222. {
  223. test = input.readObject();
  224. } // end try
  225. catch(ClassNotFoundException classNotFoundException)
  226. {
  227. System.out.print("\nUnknown object recieved");
  228. } // end catch
  229. return test;
  230. } // end receive
  231.  
  232. // take the row line from the client
  233. private static String getRow(String line)
  234. {
  235. String row = "";
  236. // take the first char in the string which is the row
  237. char rowC = line.charAt(0);
  238. // convert it to a string and return it.
  239. row = Character.toString(rowC);
  240. System.out.println(row);
  241. return row;
  242. } // end getRow
  243.  
  244. // take the column line from the client
  245. private static String getCol(String line)
  246. {
  247. String col = "";
  248. // take the first char in the string which is the row
  249. char colC = line.charAt(2);
  250. // convert it to a string and return it.
  251. col = Character.toString(colC);
  252. System.out.println(col);
  253. return col;
  254. } // end getCol
  255.  
  256. } // end testRun

Last edited by cwl157; May 15th, 2008 at 2:20 PM.
cwl157 is offline   Reply With Quote
Old May 15th, 2008, 2:21 PM   #13
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: back to online battleship

indeed you can. To do that simple send the players move to the server, have the server send it on to the other player. The player then checks the mova against his board and returns the result back to the server and then back to the original player.

What happened to the code you posted?

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old May 15th, 2008, 2:24 PM   #14
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

yea thats what i kinda had, but it only worked for the move so when the game was over the other player had no way of knowing the game ended but i guess i could have the client send that the game is over to the server and then the server would send it to the clients and end the game
cwl157 is offline   Reply With Quote
Old May 15th, 2008, 2:33 PM   #15
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: back to online battleship

It should go from 1 player to the next fine, your while loop doesn't appear to be the problem how ever it may be something to do with how your are changing boards. You call p1.takeTurn(b1,.....) when player1 makes a move and then you call p2.takeTurn(b1,...) when player 2 makes a move. So your only changing 1 board, this maybe the problem you are having.


Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old May 15th, 2008, 2:42 PM   #16
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

ok i fixed that and now that and now every move works but only with 1 player and 1 board. The other player enters their name and gives their board but then never gets to move
cwl157 is offline   Reply With Quote
Old May 15th, 2008, 2:47 PM   #17
Freaky Chris
Professional Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 270
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: back to online battleship

does takeTurn2() get called?

I think you may find that the reason player to never gets to play is because in takeTurn2() you call
send(), which is sending it to player one and not player 2...

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old May 15th, 2008, 2:49 PM   #18
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

how do i get it to send it to player 2 i thought send worked for both of them
cwl157 is offline   Reply With Quote