Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 18th, 2008, 8:14 PM   #41
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
Re: back to online battleship

cwl

I will be back to explain in detail how to do these things tomorrow.. however I'm studying for an exam (that is tomorrow lol) right now so I don't have time. In the meantime check out this resource, it will explain some of your concerns.

http://java.sun.com/docs/books/tutor...ect/index.html

Btw in order for the players to 'know about' what each other did, player1 will take his turn and update the Action Request object. Then he will send the object over to player2 using the ObjectOutputStream. This is done by

out.writeObject(yourObjecthere)
and
Object requestObject = in.readObject();

The link I provided above will explain why it's possible for one Player to know what the other player is sending him. Also note that readObject() will just sit there until it reads in an object. So its possible for player1 to wait for player2's response.
Fall Back Son is offline   Reply With Quote
Old May 18th, 2008, 8:23 PM   #42
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

thank you i will have a look at that and see what i can get from it. Thanks and good luck on your exam.
cwl157 is offline   Reply With Quote
Old May 20th, 2008, 11:52 PM   #43
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

Fall back son,
so I have not had a chance to look at that link as thoroughly as i would like to yet but I have changed my player class so now the constructor makes the board, and sets the players ships. So now to create a player with a name and a board full of ships you just create a new player. Now you mention an action request object that changes state when a player moves. So, i had a method inside the Player class that would take the turn for the player. I assume this method would be replaced by the action request object so to take 1 turn it is a whole different class and then using the stuff in that link you sent me it would tell the server or other client or whoever what happened. I guess im confused on how i would use that reflection stuff to send moves and other messages from one client across a server to another client.
And then based off of this:
Quote:
To approach this problem you should have classes that represent the board for each player. Then, you should have an additional class that represents an "Action request". This class would be able to interact with the two players, accepting a "strike attempt" and replying with either a hit or a miss, and requesting that each players "board" class update their board in response to this information. If you do not have this additional class, it will make the problem harder to solve. With this class, you can simply send an object of the class back and forth a couple times per turn and have each player update their board appropriately. You should not be using Client and Server to play the game. You should be using appropriate classes to represent the board. Then Player1 will take a turn, which will just update an Action Request object. Player2 will have a method, lets call it "evalAction". When Player2 receives the Action Request object, it will call Action Request object.evalAction(). evalAction should be implemented so that it will update the game board based on whatever is in the Action Request object. Then evalAction will update the Action request object again, and send it back to player 1. Lets look at a possibility of how this would work:

1) Player 1 enters information indicating he wants to hit a square, lets call is sq8
2) The Action Request object is updated, via a method (doesn't matter what it is), which will make the Action Request object basically say that Player1 is trying to hit sq8.
3)Player 2's evalAction method checks the Action Request object, and looks at varying information on its own board, such as was it a hit or a miss? Then Player2 calls its own method to update it's board, then calls a method to guess its own square.
4) This process can just be repeated on each Player.
I have another class that just creates 2 players and has the players fill out a board. I don't think i want this because each client will only have 1 player 1 board so i think i just want to make 1 player and board each time. I was going off of this in thinking that i need a class that creates the 2 players and 2 boards.
Quote:
To approach this problem you should have classes that represent the board for each player.
I don't know I think im really confused about it at this point.

Also, now that I am working full time again my time to work on this is pretty much limited to weekends but i really want to know about this reflection stuff and get it working.

Last edited by cwl157; May 21st, 2008 at 12:18 AM.
cwl157 is offline   Reply With Quote
Old May 21st, 2008, 2:41 AM   #44
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
Re: back to online battleship

You only need one class to represent the board, sorry if I made it sound different.

Reflection:
Take for example where you said Object myObj = in.readObject(). Reflection is a mechanism in Java that allows you to determine things about that object (myObj). These things include: The class the object is from, the methods the object has, etc. AND reflection allows you to not only know these things, but to make use of them - you can also call the object's methods using reflection. When do you need this? You need this in cases where you aren't sure exactly what type of object you will have at runtime. Say, for example, I am writing a program that has to do calculus. I have a class called Derivable and I have another class called Integrable. I have a Server which has to read in objects that are either of those two things. Then it has to evaluate them on an interval. How is the Server supposed to know whether it just read in an Integral or a Derivative? Reflection is how it can do this.

Now, I don't think you really have to use reflection for this project, since every time the Server receives an object, it will be an ActionRequest object. Instead, you can just make sure that both the "Action Request" class and the "Board" class is available to each player. Then, when each player receives an Action Request, it will be like was said above... Object myObj = in.readObject() and you can just cast the object that you read in. ActionRequest newRequest = (ActionRequest)myObj; then you can call whatever methods are necessary, pass it back to the other player, and repeat.
Fall Back Son is offline   Reply With Quote
Old May 21st, 2008, 1:12 PM   #45
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

so would i still be using the server / client model or would it all be one program that has players and then the ActionRequest object is how the players pass moves and other data back and forth to each other? If thats the case how does this work over a network?
cwl157 is offline   Reply With Quote
Old May 21st, 2008, 10:57 PM   #46
Fall Back Son
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 204
Rep Power: 2 Fall Back Son is on a distinguished road
Re: back to online battleship

You will still need the client/server model. The client and server will be passing the ActionRequests back and forth, and will be calling whatever methods are necessary to update the boards. Basically the client and server shouldn't itself contain the code necessary for running the program, they should just create whatever objects are necessary to run the program and call whatever methods are necessary. So client would create one player's board and server would create the other players board.
Fall Back Son is offline   Reply With Quote
Old May 21st, 2008, 11:16 PM   #47
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

o so your saying one of the players is the server and the other player is the client and then they pass the info back and forth to each other to play battleship, so then how does the actionRequest object fit in? So i would still have 2 programs, 1 client and 1 server and then they both initialize a name and board, so now player 1 is the server with a name and board with ships placed on the board and player 2 is a client with a name and board with ships placed on the board. Then player 1 (server) sends a move to player 2 (client). The move would be something to do with the actionRequest object? Then player 2 (client) sends a move back to player 1 (server) and this is something with the actionRequest object as well. Then the players would be checking when the game is over and do some sort of update to the actionRequest object for that as well. So does that sound like how the game is supposed to go? I'm still confused with how the actionRequestObject fits into it. Would it just have some booleans like if it hits a ship say true, or if the game is over say true?
cwl157 is offline   Reply With Quote
Old May 22nd, 2008, 8:10 PM   #48
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
Re: back to online battleship

I've tried to do something with it and don't know what to do. I made an ActionRequest object it looks like this
java Syntax (Toggle Plain Text)
  1. // use this class to tell what kind of action is being passed to the other player either a move or game over
  2. public class ActionRequest
  3. {
  4. private static boolean playingMove;
  5.  
  6. static void setPlayingMove(boolean newPlayingMove)
  7. {
  8. playingMove = newPlayingMove;
  9. } // end setPlayingMove
  10.  
  11. static boolean getPlayingMove()
  12. {
  13. return playingMove;
  14. } // end getPlayingMove();
  15. } // end class ActionRequest

It just returns true if the player is attempting a move, is that what you had in mind for it to do? Also, i have a takeTurn method that takes a row and column along with the board and all the ships and sees what is hit where, this info will have to be passed to the other player as well though and this method takes all the ships and board as parameters so do i just make those global variables to my Player class because right now i have them being created in the constructor of the Player class. Here is my Player class
java Syntax (Toggle Plain Text)
  1. import ship.*;
  2. import java.util.*;
  3. import java.io.*;
  4.  
  5. class Player
  6. {
  7. private String name;
  8. private Board b;
  9. public int counter = 0;
  10.  
  11.  
  12. /** Creates a new instance of Player */
  13. public Player()
  14. {
  15. System.out.println("Please enter a player name");
  16. String newName = promptUser();
  17. this.name = newName;
  18. // create the board and ships
  19. b = new Board();
  20. Sub s = new Sub();
  21. BattleShip bs = new BattleShip();
  22. Patrol p = new Patrol();
  23. Carrier c = new Carrier();
  24. b.display();
  25. System.out.println();
  26. setSub(b, s);
  27. System.out.println();
  28. b.display();
  29. System.out.println();
  30. setBattleShip(b, bs);
  31. System.out.println();
  32. b.display();
  33. System.out.println();
  34. setPatrol(b, p);
  35. System.out.println();
  36. b.display();
  37. System.out.println();
  38. setCarrier(b, c);
  39. System.out.println();
  40. b.display();
  41. } // end Player
  42.  
  43. // display the player name and their board
  44. public void display()
  45. {
  46. System.out.println(name);
  47. b.display();
  48. } // end display();
  49.  
  50. public String getName()
  51. {
  52. return name;
  53. } // end getName
  54.  
  55. // requirements for setting a ship are:
  56. // all spaces have to euqal 'w'
  57. // and can not run off the board
  58. boolean canSetShip(int spacesNeeded, int startRow, int startCol, int direction, Board board)
  59. {
  60. int spacesNeededOne = 0;
  61.  
  62. try
  63. {
  64. // place ship horizontal
  65. if (direction == 0)
  66. {
  67. if (startCol + spacesNeeded > 9)
  68. return false;
  69. else
  70. {
  71. for (int i = startCol; i < startCol + spacesNeeded; i++)
  72. {
  73. if (board.ar[startRow][i] != 'w')
  74. return false;
  75. } // end for
  76. } // end else
  77. } // end if
  78.  
  79. // place ship verticle
  80. else if (direction == 1)
  81. {
  82. if (startRow + spacesNeeded > 9)
  83. return false;
  84. else
  85. {
  86. for (int i = startRow; i < startRow + spacesNeeded; i++)
  87. {
  88. if (board.ar[i][startCol] != 'w')
  89. return false;
  90. } // end for
  91. } // end else
  92. } // end else if
  93. } // end try
  94. catch(ArrayIndexOutOfBoundsException e)
  95. {
  96. System.out.println("ArrayIndexOutOfBoundsException thrown");
  97. return false;
  98. } // end catch
  99. return true;
  100. } // end canSetShip
  101.  
  102. void setSub(Board b, Sub s)
  103. {
  104. // get the starting row
  105. System.out.println("Enter the starting row for the sub.");
  106. String startRowString = promptUser();
  107. int startRow = Integer.parseInt(startRowString);
  108.  
  109. // get the starting col
  110. System.out.println("Enter a starting column for the sub.");
  111. String startColString = promptUser();
  112. int startCol = Integer.parseInt(startColString);
  113.  
  114. // get the direction
  115. System.out.println("Enter the direction of the sub. (0 for horizontal, 1 for verticle)");
  116. String directionString = promptUser();
  117. int direction = Integer.parseInt(directionString);
  118.  
  119. int counter = 0;
  120. boolean isSet = false;
  121.  
  122. while (!isSet)
  123. {
  124. if (canSetShip(s.getSize(), startRow, startCol, direction, b))
  125. {
  126. // place ship horizontally
  127. if (direction == 0)
  128. {
  129. for (int i = startCol; counter < s.getSize(); i++)
  130. {
  131. b.ar[startRow][i] = 'S';
  132. counter++;
  133. } // end for
  134. } // end if
  135. // place ship vertically
  136. else if (direction == 1)
  137. {
  138. for (int i = startRow; counter < s.getSize(); i++)
  139. {
  140. b.ar[i][startCol] = 'S';
  141. counter++;
  142. } // end for
  143. } // end else if
  144. isSet = true;
  145. } // end if
  146. else
  147. {
  148. System.out.println("Sorry not enough room");
  149. // get another starting row
  150. System.out.println("Enter the starting row for the sub.");
  151. startRowString = promptUser();
  152. startRow = Integer.parseInt(startRowString);
  153.  
  154. // get the starting col
  155. System.out.println("Enter a starting column for the sub.");
  156. startColString = promptUser();
  157. startCol = Integer.parseInt(startColString);
  158.  
  159. // get the direction
  160. System.out.println("Enter the direction of the sub. (0 for horizontal, 1 for verticle)");
  161. directionString = promptUser();
  162. direction = Integer.parseInt(directionString);
  163. } // end else
  164. } // end while
  165. } // end setSub
  166.  
  167. void setBattleShip(Board b, BattleShip bS)
  168. {
  169. // get the starting row
  170. System.out.println("Enter the starting row for the battleShip.");
  171. String startRowString = promptUser();
  172. int startRow = Integer.parseInt(startRowString);
  173.  
  174. // get the starting col
  175. System.out.println("Enter a starting column for the battleShip.");
  176. String startColString = promptUser();
  177. int startCol = Integer.parseInt(startColString);
  178.  
  179. // get the direction
  180. System.out.println("Enter the direction of the battleShip. (0 for horizontal, 1 for verticle)");
  181. String directionString = promptUser();
  182. int direction = Integer.parseInt(directionString);
  183.  
  184. int counter = 0;
  185. boolean isSet = false;
  186.  
  187. while (!isSet)
  188. {
  189. if (canSetShip(bS.getSize(), startRow, startCol, direction, b))
  190. {
  191. // place ship horizontally
  192. if (direction == 0)
  193. {
  194. for (int i = startCol; counter < bS.getSize(); i++)
  195. {
  196. b.ar[startRow][i] = 'B';
  197. counter++;
  198. } // end for
  199. } // end if
  200. // place ship vertically
  201. else if (direction == 1)
  202. {
  203. for (int i = startRow; counter < bS.getSize(); i++)
  204. {
  205. b.ar[i][startCol] = 'B';
  206. counter++;
  207. } // end for
  208. } // end else if
  209. isSet = true;
  210. } // end if
  211. else
  212. {
  213. System.out.println("Sorry not enough room");
  214. // get the starting row
  215. System.out.println("Enter the starting row for the battleShip.");
  216. startRowString = promptUser();
  217. startRow = Integer.parseInt(startRowString);
  218.  
  219. // get the starting col
  220. System.out.println("Enter a starting column for the battleShip.");
  221. startColString = promptUser();
  222. startCol = Integer.parseInt(startColString);
  223.  
  224. // get the direction
  225. System.out.println("Enter the direction of the battleShip. (0 for horizontal, 1 for verticle)");
  226. directionString = promptUser();
  227. direction = Integer.parseInt(directionString);
  228. } // end else
  229. } // end while
  230. } // end setBattleShip
  231.  
  232.  
  233. void setPatrol(Board b, Patrol p)
  234. {
  235. // get the starting row
  236. System.out.println("Enter the starting row for the patrol boat.");
  237. String startRowString = promptUser();
  238. int startRow = Integer.parseInt(startRowString);
  239.  
  240. // get the starting col
  241. System.out.println("Enter a starting column for the patrol boat.");
  242. String startColString = promptUser();
  243. int startCol = Integer.parseInt(startColString);
  244.  
  245. // get the direction
  246. System.out.println("Enter the direction of the patrol boat. (0 for horizontal, 1 for verticle)");
  247. String directionString = promptUser();
  248. int direction = Integer.parseInt(directionString);
  249.  
  250. int counter = 0;
  251. boolean isSet = false;
  252.  
  253. while (!isSet)
  254. {
  255. if (canSetShip(p.getSize(), startRow, startCol, direction, b))
  256. {
  257. // place ship horizontally
  258. if (direction == 0)
  259. {
  260. for (int i = startCol; counter < p.getSize(); i++)
  261. {
  262. b.ar[startRow][i] = 'P';
  263. counter++;
  264. } // end for
  265. } // end if
  266. // place ship vertically
  267. else if (direction == 1)
  268. {
  269. for (int i = startRow; counter < p.getSize(); i++)
  270. {
  271. b.ar[i][startCol] = 'P';
  272. counter++;
  273. } // end for
  274. } // end else if
  275. isSet = true;
  276. } // end if
  277. else
  278. {
  279. System.out.println("Sorry not enough room");
  280. // get the starting row
  281. System.out.println("Enter the starting row for the patrol boat.");
  282. startRowString = promptUser();
  283. startRow = Integer.parseInt(startRowString);
  284.  
  285. // get the starting col
  286. System.out.println("Enter a starting column for the patrol boat.");
  287. startColString = promptUser();
  288. startCol = Integer.parseInt(startColString);
  289.  
  290. // get the direction
  291. System.out.println("Enter the direction of the patrol boat. (0 for horizontal, 1 for verticle)");
  292. directionString = promptUser();
  293. direction = Integer.parseInt(directionString);
  294. } // end else
  295. } // end while
  296. } // end setPatrol
  297.  
  298. void setCarrier(Board b, Carrier c)
  299. {
  300. // get the starting row
  301. System.out.println("Enter the starting row for the carrier.");
  302. String startRowString = promptUser();
  303. int startRow = Integer.parseInt(startRowString);
  304.  
  305. // get the starting col
  306. System.out.println("Enter a starting column for the carrier.");
  307. String startColString = promptUser();
  308. int startCol = Integer.parseInt(startColString);
  309.  
  310. // get the direction
  311. System.out.println("Enter the direction of the carrier. (0 for horizontal, 1 for verticle)");
  312. String directionString = promptUser();
  313. int direction = Integer.parseInt(directionString);
  314. int counter = 0;
  315. boolean isSet = false;
  316.  
  317. while (!isSet)
  318. {
  319. if (canSetShip(c.getSize(), startRow, startCol, direction, b))
  320. {
  321. // place ship horizontally
  322. if (direction == 0)
  323. {
  324. for (int i = startCol; counter < c.getSize(); i++)
  325. {
  326. b.ar[startRow][i] = 'C';
  327. counter++;
  328. } // end for
  329. } // end if
  330. // place ship vertically
  331. else if (direction == 1)
  332. {
  333. for (int i = startRow; counter < c.getSize(); i++)
  334. {
  335. b.ar[i][startCol] = 'C';
  336. counter++;
  337. } // end for
  338. } // end else if
  339. isSet = true;
  340. } // end if
  341. else
  342. {
  343. System.out.println("Sorry not enough room");
  344. // get the starting row
  345. System.out.println("Enter the starting row for the carrier.");
  346. startRowString = promptUser();
  347. startRow = Integer.parseInt(startRowString);
  348.  
  349. // get the starting col
  350. System.out.println("Enter a starting column for the carrier.");
  351. startColString = promptUser();
  352. startCol = Integer.parseInt(startColString);
  353.  
  354. // get the direction
  355. System.out.println("Enter the direction of the carrier. (0 for horizontal, 1 for verticle)");
  356. directionString = promptUser();
  357. direction = Integer.parseInt(directionString);
  358. } // end else
  359. } // end while
  360. } // end setCarrier
  361.  
  362. // 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
  363. // then the game ends, or all of the spaces are filled with 'x' then declare a draw
  364. void takeTurn(Board b, Sub s, BattleShip bs, Patrol p, Carrier c, int tryRow, int tryCol) throws IOException
  365. {
  366. // try to hit the sub
  367. if (b.ar[tryRow][tryCol] == 'S')
  368. {
  369. b.ar[tryRow][tryCol] = 'X';
  370. s.hits++;
  371. String subHit = "Your Sub has been hit!\nHits equals: " + s.hits;
  372. // sendData(subHit);
  373. System.out.println("hits equals: " + s.hits);
  374. // see if sub is sunk
  375. if (s.isSunk(s.hits))
  376. {
  377. System.out.println("Your Sub has sunk!");
  378. counter++;
  379. } // end if
  380. } // end if
  381.  
  382. // try to hit the BattleShip
  383. else if (b.ar[tryRow][tryCol] == 'B')
  384. {
  385. b.ar[tryRow][tryCol] = 'X';
  386. System.out.println("Your BattleShip has been hit!");
  387. bs.hits++;
  388. System.out.println("hits equals: " + bs.hits);
  389. if (bs.isSunk(bs.hits))
  390. {
  391. System.out.println("Your BattleShip has sunk!");
  392. counter++;
  393. } // end if
  394. } // end else if
  395.  
  396. // try to hit the Patrol
  397. else if (b.ar[tryRow][tryCol] == 'P')
  398. {
  399. b.ar[tryRow][tryCol] = 'X';
  400. System.out.println("Your Patrol has been hit!");
  401. p.hits++;
  402. System.out.println("hits equals: " + p.hits);
  403. if (p.isSunk(p.hits))
  404. {
  405. System.out.println("Your Patrol has sunk!");
  406. counter++;
  407. } // end if
  408. } // end else if
  409.  
  410. // try to hit the Carrier
  411. else if (b.ar[tryRow][tryCol] == 'C')
  412. {
  413. b.ar[tryRow][tryCol] = 'X';
  414. System.out.println("Your Carrier has been hit!");
  415. c.hits++;
  416. System.out.println("hits equals: " + c.hits);
  417. if (c.isSunk(c.hits))
  418. {
  419. System.out.println("Your Carrier has sunk!");
  420. counter++;
  421. } // end if
  422. } // end else if
  423.  
  424. else if (b.ar[tryRow][tryCol] == 'w')
  425. {
  426. b.ar[tryRow][tryCol] = 'x';
  427. System.out.println("Shot Missed!");
  428. } // end else if
  429. } // end takeTurn
  430.  
  431. // decide endGame
  432. boolean endGame(int counter)
  433. {
  434. if (counter == 4)
  435. return true;
  436.  
  437. return false;
  438. } // end endGame
  439.  
  440. // get input from the keyboard
  441. // get input from the user to play game or exit
  442. public static String promptUser()
  443. {
  444. String line = null;
  445. try
  446. {
  447. BufferedReader keyInput = new BufferedReader(new InputStreamReader(System.in));
  448. line = keyInput.readLine();
  449. } // end try
  450. catch(IOException e)
  451. {
  452. System.out.println("Caught IOException in Player.promptUser " + e);
  453. } // end catch
  454. //catches bad user input and throws exception
  455. catch (NumberFormatException ex)
  456. {
  457. System.out.print("Caught NumberFormatException in Player.promptUser " + ex);
  458. } // end catch
  459. return line;
  460. } // end promptUser
  461. } // end Player class