![]() |
back to online battleship
Now that it is summer I have some more time to work on that online battleship game. I have it where 1 client can send moves to the server and the server executes the moves on the board and sends the board back to the client and detects when all the ships have been sunk and the game is over. Now I need to figure out how to make it turn based so 2 people running the client can take turns playing the game. If someone could look at this code and tell me how to do that. I know its probably not the cleanest or the best way to do what i am doing. This is the first time i have ever done a client server thing like this before so the organization of it is probably wrong and thats probably why i cant get 2 players to take turns. When i try to run the commented out code for the second person i get a nullPointerException on the one client and a cast exception on the other client. How should i organize this where 2 people can run this client connect to a server and exchange moves with the server and play battleship against each other? I know i can't be that far off. Thanks here is the code:
Client class :
:
|
Re: back to online battleship
I don't have the time to read all of your code and write some of my own code at the moment im revise for exams, fun fun fun. But something like this may help you.
1) Player Connects to server 2) Server checks for free players, whilst player waits for a player 3) Server sends client details to each player, sets player1's turns to true 4) Player1 then moves and returns the board to the server 5) Server Checks for a win, else 6) Server changes player1 turn to false and sets player2 turn to true, and send the board to player2 7) and so on, until the game is won. Sorry if this doesn't help at all. Chris |
Re: back to online battleship
If you think about it the server shouldn't really be sending the board to anyone. After each player sets up their battleships the program might send the sizes and coordinates of their ships to the server. Each player will know the location of their own ships but not the enemy's (the basis of the game). Now, when a player decides to attack you could just send that coordinate to the server and have it verify if its a hit/miss or if there is a winner and send the result back to the client so it could update the display. I didn't read your code so I'm not sure how it works, but you could just have the server update a player turn status. If it is a player's turn then the client would enable a button which would submit their next move or so.
Those are just some of my thoughts. |
Re: back to online battleship
wizard, i thought about this. In fact i have a version of this where all the server does is pass the moves between clients but in order for the server to verify the move it needs to have copies of the boards. I was thinking if the clients send the board and moves and the server plays the game and sends the moves and board back to the clients then it could tell when the game is over and send that as well.
Freaky Chris, As for what you are saying how do i make the server check for free players and then how do i make the turns go back and forth, right now it just sits with one player when the other one is connected. I thought calling receivePlayer1 and receivePlayer2 would make it alternate but it doesn't work that way and i think the server gets confused about what is sent from who when and stuff |
Re: back to online battleship
I'm not understanding why the client(s) would need to send the setup board to the server more than once. After the server received the two boards it should be able to figure out the results if it received the moves to be played.
|
Re: back to online battleship
O now i see what your saying. Yea i tried that and then just had the server send the board back to the client after 1 move but for some reason the client was still receiving the board with no move done, so i looked at how i was doing it when setting up the ships and i was sending the board to the server, setting the ships and sending the board back so for the moves, i ended up sending the board to the server and then the server sending the board back and then it worked. So if there is a different way this can be done i would love to know how to do it.
|
Re: back to online battleship
AS to how you would check for free players, simple use a status flag so to speak. You spawn a thread for a new 1v1 game. Set a variable to false such as, FULL = False, then when a new client joins the server check any threads running games for any False's. If one is found then join that game and set its FULL variable to true.
Chris |
Re: back to online battleship
yea i was wondering about threads for this, unfortunatly i have zero experience using threads but it would make it so more then 1 game could be going on at once right?
|
Re: back to online battleship
indeed it would. You would have to set up a few different threads, one to handle each new player and then no doubt one for games, im not entierly sure how one would set it up atm, if i have time later i'll have a think about the logic behind it and get back to you with that. Threads are reasonable easy to understand, i recomend checking threading out it could prove very useful.
there is a different approach that one could take i suppose. The server handles all connecting clients. Then it calls a different app with connection details that automatically connects to two people to play the gae and that then acts as the server for the game and the clients are no long connected to the main server. Once the game ends the clients are reconnected to the main server. All very dodgy im guessing im just thinking about things differently thats all Chris |
Re: back to online battleship
what if the server was only going to have 2 people connected at once and then it would just connect the 2 clients and then play the game with those 2 so for each game a new server would have to be running. I know its not right but for this thats what im thinking of. I got it to where 2 people connect to the server and it gets their names and boards but then for the moves the server gets confused with what comes from where or something
|
| All times are GMT -5. The time now is 4:19 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC