View Single Post
Old May 17th, 2008, 3:47 PM   #35
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

For what its worth, I think that sending the board back and forth is a bad idea.

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.



Also, I just saw that you were using ObjectOutputStream. You should note: I think ObjectOutputStream requires any objects sent over it to implement the class 'Serialized'. If you are sending over objects that do not implement the Serialized interface, this could be causing you problems.

Last edited by Fall Back Son; May 17th, 2008 at 4:02 PM.
Fall Back Son is offline   Reply With Quote