Question: Create ONLY TWO java classes (client and server). You will need to run two classes simultaneously for this activity. Make sure you have a compiler
Create ONLY TWO java classes (client and server). You will need to run two classes simultaneously for this activity. Make sure you have a compiler that is capable of this.
The name of the game is BattleShip. Classically, the nature of the game is having two players construct a small fleet of ships in a grid and then try to sink the opponents. For this project however, were going to modify the rules for simplicity. Heres how it will work the Server will start off by creating a 5x5 grid in which it will randomly place 7 ships (each taking up a single tile in the grid). Then, players will take turns choosing grid tiles at which to fire. If they hit one of the Servers ships, that player scores a point. Then the next player goes. This continues until all 7 ships have been hit by either player. The player with the most points wins, then the Server disconnects.
The guidelines are as follows:
Both players should be running the same Client class. Each player should have their own console. This can be done by running the same Client class twice, each with a single Socket to connect to the Server.
The Server should accept both players connections before setting up the game. The Server should assign the users as Player 1 and Player 2.
The Server should keep track of the board and the score of both players. The ships should be randomly placed on the board after both players connect.
Players should take turns. While it is not the players turn, the console should inform the player that theyre waiting on the other player.
The grid layout should be as follows:
| A1 | A2 | A3 | A4 | A5 |
| B1 | B2 | B3 | B4 | B5 |
| C1 | C2 | C3 | C4 | C5 |
| D1 | D2 | D3 | D4 | D5 |
| E1 | E2 | E3 | E4 | E5 |
When the players turn begins, they should be shown a visual representation of the current state of the board, including which spots have been hit and which ones have not. In addition, this board should show where ships that have been hit were. Hint: create a String from the board and send that instead of sending the whole board.
The player should be asked to input a grid coordinate in the format A1 always 2 characters with the first being a letter (capital or lowercase) A-E and the second should be an integer 1-5. Data validation should be done on the input to make sure the users entry follows this format. If it does not, an invalid entry prompt should be shown and the player should be asked to re-enter.
Data Validation for valid entries should be performed on the Client-side.
Once an entry has been confirmed as valid, it should be sent to the Server and the grid should be updated for the players move.
When a player has chosen a move and the board has been updated, both players should receive a message detailing whether the move was a hit or a miss.
Once the message has been sent, the Server should allow the other player to take their turn in the same manner.
Once all 7 ships have been hit, the Server should finish the current players turn and then end the game. The final scores should then be displayed to both players, along with a short message telling both players which one won.
Important Things to Note:
Due to the turn-based nature of this program, I highly suggest not using Threads for this application. This program can be done much easier in a linear structure.
No GUI applications are allowed for this program. Your submission must be console-based.
Your program must include data validation for any input. In the case that invalid data is entered, the user must be told to re-enter without throwing an exception.
Send regular messages to the servers console. At the very least, send a message when the server connects, when either Client connects, when input is received from the client, and when the server disconnects.
Make sure that in the natural runtime of your program when the server is disconnected that all sockets, scanners, and any other form of I/O is closed leaving these open can cause a security leak that, while harmless on a localhost, can be dangerous on other IPs.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
