Thread: online game
View Single Post
Old Apr 20th, 2008, 8:01 PM   #25
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 343
Rep Power: 4 cwl157 is on a distinguished road
Re: online game

I added it to the Player class in my battleship game and it does send the numbers to the other client but I do not think all of them. I think this can be fixed by changing when they are prompted and stuff. Also, it applies what the player enters to itself instead of the other player. And the third thought i have is if both players have a ship at the same location would it be possible to hit your own ship then as well as the other player's ship? Here is my Player class.
java Syntax (Toggle Plain Text)
  1. import ship.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. class Player
  7. {
  8. private Socket client;
  9. private ObjectOutputStream output;
  10. private ObjectInputStream input;
  11. String name;
  12. public int counter = 0;
  13.  
  14. /** Creates a new instance of Player */
  15. public Player(String newName)
  16. {
  17. this.name = newName;
  18. } // end Player
  19.  
  20. // requirements for setting a ship are:
  21. // all spaces have to euqal 'w'
  22. // and can not run off the board
  23. boolean canSetShip(int spacesNeeded, int startRow, int startCol, int direction, Board board)
  24. {
  25. int spacesNeededOne = 0;
  26.  
  27. try
  28. {
  29. // place ship horizontal
  30. if (direction == 0)
  31. {
  32. if (startCol + spacesNeeded > 9)
  33. return false;
  34. else
  35. {
  36. for (int i = startCol; i < startCol + spacesNeeded; i++)
  37. {
  38. if (board.ar[startRow][i] != 'w')
  39. return false;
  40. } // end for
  41. } // end else
  42. } // end if
  43.  
  44. // place ship verticle
  45. else if (direction == 1)
  46. {
  47. if (startRow + spacesNeeded > 9)
  48. return false;
  49. else
  50. {
  51. for (int i = startRow; i < startRow + spacesNeeded; i++)
  52. {
  53. if (board.ar[i][startCol] != 'w')
  54. return false;
  55. } // end for
  56. } // end else
  57. } // end else if
  58. } // end try
  59. catch(ArrayIndexOutOfBoundsException e)
  60. {
  61. System.out.println("ArrayIndexOutOfBoundsException thrown");
  62. return false;
  63. } // end catch
  64. return true;
  65. } // end canSetShip
  66.  
  67. void setSub(Board b, Sub s)
  68. {
  69. // find a random row
  70. Random startRowRand = new Random();
  71. int startRow = startRowRand.nextInt(10);
  72.  
  73. // find a random col
  74. Random startColRand = new Random();
  75. int startCol = startColRand.nextInt(10);
  76.  
  77. // find a random direction
  78. Random directionRand = new Random();
  79. int direction = directionRand.nextInt(2);
  80.  
  81. int counter = 0;
  82. boolean isSet = false;
  83.  
  84. while (!isSet)
  85. {
  86. if (canSetShip(s.getSize(), startRow, startCol, direction, b))
  87. {
  88. // place ship horizontally
  89. if (direction == 0)
  90. {
  91. for (int i = startCol; counter < s.getSize(); i++)
  92. {
  93. b.ar[startRow][i] = 'S';
  94. counter++;
  95. } // end for
  96. } // end if
  97. // place ship vertically
  98. else if (direction == 1)
  99. {
  100. for (int i = startRow; counter < s.getSize(); i++)
  101. {
  102. b.ar[i][startCol] = 'S';
  103. counter++;
  104. } // end for
  105. } // end else if
  106. isSet = true;
  107. } // end if
  108. else
  109. {
  110. direction = directionRand.nextInt(2);
  111. startRow = startRowRand.nextInt(10);
  112. startCol = startColRand.nextInt(10);
  113. } // end else
  114. } // end while
  115. } // end setSub
  116.  
  117. void setBattleShip(Board b, BattleShip bS)
  118. {
  119. // find a random row
  120. Random startRowRand = new Random();
  121. int startRow = startRowRand.nextInt(10);
  122.  
  123. // find a random col
  124. Random startColRand = new Random();
  125. int startCol = startColRand.nextInt(10);
  126.  
  127. // find a random direciton
  128. Random directionRand = new Random();
  129. int direction = directionRand.nextInt(2);
  130.  
  131. int counter = 0;
  132. boolean isSet = false;
  133.  
  134. while (!isSet)
  135. {
  136. if (canSetShip(bS.getSize(), startRow, startCol, direction, b))
  137. {
  138. // place ship horizontally
  139. if (direction == 0)
  140. {
  141. for (int i = startCol; counter < bS.getSize(); i++)
  142. {
  143. b.ar[startRow][i] = 'B';
  144. counter++;
  145. } // end for
  146. } // end if
  147. // place ship vertically
  148. else if (direction == 1)
  149. {
  150. for (int i = startRow; counter < bS.getSize(); i++)
  151. {
  152. b.ar[i][startCol] = 'B';
  153. counter++;
  154. } // end for
  155. } // end else if
  156. isSet = true;
  157. } // end if
  158. else
  159. {
  160. direction = directionRand.nextInt(2);
  161. startRow = startRowRand.nextInt(10);
  162. startCol = startColRand.nextInt(10);
  163. } // end else
  164. } // end while
  165. } // end setBattleShip
  166.  
  167.  
  168. void setPatrol(Board b, Patrol p)
  169. {
  170. // find a random row
  171. Random startRowRand = new Random();
  172. int startRow = startRowRand.nextInt(10);
  173.  
  174. // find a random col
  175. Random startColRand = new Random();
  176. int startCol = startColRand.nextInt(10);
  177.  
  178. // find a random direction
  179. Random directionRand = new Random();
  180. int direction = directionRand.nextInt(2);
  181.  
  182. int counter = 0;
  183. boolean isSet = false;
  184.  
  185. while (!isSet)
  186. {
  187. if (canSetShip(p.getSize(), startRow, startCol, direction, b))
  188. {
  189. // place ship horizontally
  190. if (direction == 0)
  191. {
  192. for (int i = startCol; counter < p.getSize(); i++)
  193. {
  194. b.ar[startRow][i] = 'P';
  195. counter++;
  196. } // end for
  197. } // end if
  198. // place ship vertically
  199. else if (direction == 1)
  200. {
  201. for (int i = startRow; counter < p.getSize(); i++)
  202. {
  203. b.ar[i][startCol] = 'P';
  204. counter++;
  205. } // end for
  206. } // end else if
  207. isSet = true;
  208. } // end if
  209. else
  210. {
  211. direction = directionRand.nextInt(2);
  212. startRow = startRowRand.nextInt(10);
  213. startCol = startColRand.nextInt(10);
  214. } // end else
  215. } // end while
  216. } // end setPatrol
  217.  
  218. void setCarrier(Board b, Carrier c)
  219. {
  220. // find a random row
  221. Random startRowRand = new Random();
  222. int startRow = startRowRand.nextInt(10);
  223.  
  224. // find a random col
  225. Random startColRand = new Random();
  226. int startCol = startColRand.nextInt(10);
  227.  
  228. // find a random direction
  229. Random directionRand = new Random();
  230. int direction = directionRand.nextInt(2);
  231.  
  232. int counter = 0;
  233. boolean isSet = false;
  234.  
  235. while (!isSet)
  236. {
  237. if (canSetShip(c.getSize(), startRow, startCol, direction, b))
  238. {
  239. // place ship horizontally
  240. if (direction == 0)
  241. {
  242. for (int i = startCol; counter < c.getSize(); i++)
  243. {
  244. b.ar[startRow][i] = 'C';
  245. counter++;
  246. } // end for
  247. } // end if
  248. // place ship vertically
  249. else if (direction == 1)
  250. {
  251. for (int i = startRow; counter < c.getSize(); i++)
  252. {
  253. b.ar[i][startCol] = 'C';
  254. counter++;
  255. } // end for
  256. } // end else if
  257. isSet = true;
  258. } // end if
  259. else
  260. {
  261. direction = directionRand.nextInt(2);
  262. startRow = startRowRand.nextInt(10);
  263. startCol = startColRand.nextInt(10);
  264. } // end else
  265. } // end while
  266. } // end setCarrier
  267.  
  268. // now add in to check if it is sunk and add a counter to keep track of sunk ships when all of a players ships are sunk or the counter is 4
  269. // then the game ends, or all of the spaces are filled with 'x' then declare a draw
  270. void takeTurn(Board b, Sub s, BattleShip bs, Patrol p, Carrier c) throws IOException
  271. {
  272. //Random tryRowRand = new Random();
  273. //int tryRow = tryRowRand.nextInt(10);
  274. //Random tryColRand = new Random();
  275. // int tryCol = tryColRand.nextInt(10);
  276. System.out.println("Please enter a row 0-9");
  277. String row = promptUser();
  278. //sendData(row);
  279. toServer(row);
  280. //System.out.println(row);
  281. //System.out.println("waiting to receive");
  282. // String tryRowS = receive();
  283. int tryRow = Integer.parseInt(row);
  284. System.out.println("Please enter a column 0-9");
  285. String col = promptUser();
  286. //sendData(col);
  287. toServer(col);
  288. //String tryColS = receive();
  289. //System.out.println(col);
  290. int tryCol = Integer.parseInt(col);
  291.  
  292. // try to hit the sub
  293. if (b.ar[tryRow][tryCol] == 'S')
  294. {
  295. b.ar[tryRow][tryCol] = 'X';
  296. System.out.println(this.name + " hit the Sub!");
  297. s.hits++;
  298. System.out.println("hits equals: " + s.hits);
  299. // see if sub is sunk
  300. if (s.isSunk(s.hits))
  301. {
  302. System.out.println(this.name + " sunk the Sub!");
  303. counter++;
  304. } // end if
  305. } // end if
  306.  
  307. // try to hit the BattleShip
  308. else if (b.ar[tryRow][tryCol] == 'B')
  309. {
  310. b.ar[tryRow][tryCol] = 'X';
  311. System.out.println(this.name + " hit the BattleShip!");
  312. bs.hits++;
  313. System.out.println("hits equals: " + bs.hits);
  314. if (bs.isSunk(bs.hits))
  315. {
  316. System.out.println(this.name + " sunk the BattleShip!");
  317. counter++;
  318. } // end if
  319. } // end else if
  320.  
  321. // try to hit the Patrol
  322. else if (b.ar[tryRow][tryCol] == 'P')
  323. {
  324. b.ar[tryRow][tryCol] = 'X';
  325. System.out.println(this.name + " hit the Patrol!");
  326. p.hits++;
  327. System.out.println("hits equals: " + p.hits);
  328. if (p.isSunk(p.hits))
  329. {
  330. System.out.println(this.name + " sunk the Patrol!");
  331. counter++;
  332. } // end if
  333. } // end else if
  334.  
  335. // try to hit the Carrier
  336. else if (b.ar[tryRow][tryCol] == 'C')
  337. {
  338. b.ar[tryRow][tryCol] = 'X';
  339. System.out.println(this.name + " hit the Carrier!");
  340. c.hits++;
  341. System.out.println("hits equals: " + c.hits);
  342. if (c.isSunk(c.hits))
  343. {
  344. System.out.println(this.name + " sunk the Carrier!");
  345. counter++;
  346. } // end if
  347. } // end else if
  348.  
  349. else if (b.ar[tryRow][tryCol] == 'w')
  350. {
  351. b.ar[tryRow][tryCol] = 'x';
  352. System.out.println(this.name + " Missed!");
  353. } // end else if
  354. } // end takeTurn
  355.  
  356. // decide endGame
  357. boolean endGame(int counter)
  358. {
  359. if (counter == 4)
  360. return true;
  361.  
  362. return false;
  363. } // end endGame
  364.  
  365. // get input from the keyboard
  366. // get input from the user to play game or exit
  367. public static String promptUser() throws IOException
  368. {
  369. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  370. String line = null;
  371. try
  372. {
  373. line = keyInput.readLine();
  374. } // end try
  375. //catches bad user input and throws exception
  376. catch (NumberFormatException ex)
  377. {
  378. System.out.print("Error bad data: ");
  379. } // end catch
  380.  
  381. return line;
  382. } // end promptUser
  383.  
  384. public void connect() throws IOException
  385. {
  386. //System.out.print("Attempting Connection\n");
  387. client = new Socket(InetAddress.getByName("192.168.1.102"), 1234);
  388. //System.out.print("Connected to: " + client.getInetAddress().getHostName());
  389. } // end connect
  390.  
  391. // establish the streams
  392. public void streams() throws IOException
  393. {
  394. output=new ObjectOutputStream(client.getOutputStream());
  395. output.flush();
  396. input=new ObjectInputStream(client.getInputStream());
  397. //System.out.print("\nStreams Estabolished\n");
  398. } // end streams
  399.  
  400. // receive the message from Server
  401. public String receive() throws IOException
  402. {
  403. String test = "-1";
  404. try
  405. {
  406. test = (String)input.readObject();
  407. System.out.println("\n" + test);
  408. } // end try
  409. catch(ClassNotFoundException classNotFoundException)
  410. {
  411. System.out.print("\nUnknown object recieved");
  412. } // end catch
  413. return test;
  414. } // end processConnection
  415.  
  416. public void closeConnection()
  417. {
  418. try
  419. {
  420. output.close();
  421. input.close();
  422. client.close();
  423. } // end try
  424. catch(IOException ioException)
  425. {
  426. ioException.printStackTrace();
  427. } // end catch
  428. } //closeConnection
  429.  
  430. public void sendData(String data)
  431. {
  432. try
  433. {
  434. output.writeObject(data);
  435. output.flush();
  436. } // end try
  437. catch(IOException ioException)
  438. {
  439. System.out.print("\nError writing object");
  440. } // end catch
  441. } // end sendData
  442.  
  443. // actually sends data to server
  444. public void toServer(String line) throws IOException
  445. {
  446. connect();
  447. streams();
  448. receive();
  449. //line = c.promptUser();
  450. //System.out.println(line);
  451. sendData(line);
  452. }
  453. } // end Player class
I think the problem of applying it to that board is in the takeTurn method. Specifically this part:
java Syntax (Toggle Plain Text)
  1. System.out.println("Please enter a row 0-9");
  2. String row = promptUser();
  3. toServer(row);
  4. int tryRow = Integer.parseInt(row);
  5. System.out.println("Please enter a column 0-9");
  6. String col = promptUser();
  7. toServer(col);
  8. int tryCol = Integer.parseInt(col);
I use the row the user entered to check with not the row that is sent over the server from the other client. I tried to use the sendData and receive methods but when i entered a row using those I get an exception saying it couldn't change the String equal to "" into a number so like it didn't take the row or column entered. I think I am not that far off but I do not know how to get it so 1 player enters a row and column and the other player receives it and applies it to their board and then they enter a row and column. For that I will also post the playGame class that has the main method for this Battleship game.
java Syntax (Toggle Plain Text)
  1. import ship.*;
  2. import java.util.*;
  3. import java.io.*;
  4. public class PlayGame
  5. {
  6. public static void main(String[] args) throws IOException
  7. {
  8. int choice = 0;
  9. choice = promptUser();
  10. while (choice != 2)
  11. {
  12. if (choice == 1)
  13. {
  14. playGame();
  15. choice = promptUser();
  16. } // end if
  17. } // end while
  18. } // end main
  19.  
  20. // get input from the user to play game or exit
  21. public static int promptUser() throws IOException
  22. {
  23. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  24. int lineToInt = 0;
  25.  
  26. String line = null;
  27.  
  28. System.out.print("press 1 to play Battleship, 2 to exit: ");
  29. try
  30. {
  31. line = keyInput.readLine();
  32. lineToInt = Integer.parseInt(line);
  33. } // end try
  34. //catches bad user input and throws exception
  35. catch (NumberFormatException ex)
  36. {
  37. System.out.print("Error bad data: ");
  38. } // end catch
  39.  
  40. return lineToInt;
  41. } // end promptUser
  42.  
  43. // get input from the user and return as a String for the player name
  44. public static String promptUserString() throws IOException
  45. {
  46. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  47. String line = null;
  48. try
  49. {
  50. line = keyInput.readLine();
  51. } // end try
  52. //catches bad user input and throws exception
  53. catch (NumberFormatException ex)
  54. {
  55. System.out.print("Error bad data: ");
  56. } // end catch
  57.  
  58. return line;
  59. } // end promptUser
  60.  
  61. public static void playGame() throws IOException
  62. {
  63. System.out.println("Please enter a player name: ");
  64. String name = promptUserString();
  65. Player players = new Player(name);
  66. Board boards = new Board();
  67. Sub subs = new Sub();
  68. BattleShip battleShips = new BattleShip();
  69. Patrol patrols = new Patrol();
  70. Carrier carriers = new Carrier();
  71. int turn = 0;
  72.  
  73. // set players board
  74. players.setSub(boards, subs);
  75. players.setBattleShip(boards, battleShips);
  76. players.setPatrol(boards, patrols);
  77. players.setCarrier(boards, carriers);
  78. System.out.println(players.name + "'s board: ");
  79. System.out.println();
  80. boards.printBoard();
  81.  
  82. boolean playerOneWins = false;
  83. while (!playerOneWins)
  84. {
  85. System.out.println(players.name +"'s turn");
  86. System.out.println();
  87. boards.printBoard();
  88. System.out.println();
  89. players.takeTurn(boards, subs, battleShips, patrols, carriers);
  90.  
  91. // check to see if player won the game
  92. if (players.endGame(players.counter))
  93. playerOneWins = true;
  94.  
  95. } // end while
  96. if (playerOneWins)
  97. {
  98. System.out.println(players.name + " has won the game!");
  99. System.out.println();
  100. System.out.println(players.name +"'s final board:");
  101. boards.printBoard();
  102. System.out.println();
  103. System.out.println(players.name +"'s final board:");
  104. boards.printBoard();
  105. } // end if
  106. } // end playGame
  107.  
  108. } // end PlayGame class
cwl157 is offline   Reply With Quote