For this assigment, you will submit two .java files. You will need to run two cl
ID: 3828766 • Letter: F
Question
For this assigment, you will submit two .java files. You will need to run two classes simultaneously for this activity. Make sure you have a compiler that is capable of this.
For the for a previus exercise, we looked at how a Server/Client program can operate an online store. Now we’re going to look at another industry for these kinds of programs: gaming. For this project, we’re going to build a game that can be played with two players.
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 opponent’s. For this project however, we’re going to modify the rules for simplicity. Here’s 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 Server’s 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 player’s turn, the console should inform the player that they’re waiting on the other player.
The grid layout should be as follows:
When the player’s 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 user’s 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 player’s 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 player’s 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 server’s 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.
Leave documentation and comments in your code to explain things step-by-step. This is just good practice. It doesn’t have to be a lot; just enough to explain your process in a simple way.
User-Friendliness is important! Make sure your program looks good on the user’s end.
Connectivity
8 points
The server opens a port on a localhost, and the clients successfully connect. The program keeps the connection open during RunTime and successfully closes it when the program is finished.
User-Input
4 points
The client program successfully obtains user-input, and data validation is done correctly to validate the input obtained. The user is asked for new input if invalid input is given.
User-Friendliness
4 points
The look-and-feel of the program runs well; the client sends good user-friendly output; the server relays information to the console.
App Reliability
6 points
The program correctly sends the user input to the server when necessary; the server correctly sends information to the user; all information sent and received is correct.
Good Practice
2 points
No dead code; no unused imports or variables, IO and Sockets are correctly closed. Good documentation.
Total
24 points
A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 C1 C2 C3 C4 C5 D1 D2 D3 D4 D5 E1 E2 E3 E4 E5Explanation / Answer
I'm not familiar with the GAE and if that interferes, but I don't have a problem running multiples. I often fire up a server and multiple clients from the same window.
A few pointers:
down voteaccepted
I'm not familiar with the GAE and if that interferes, but I don't have a problem running multiples. I often fire up a server and multiple clients from the same window.
A few pointers:
- Go to the main method of each program to run them. Once you've run them once, they show up in the drop menu on the run button.
- Make multiple consoles and pin them. It makes it much easier to see your (debug/informational) outputs in real time.
- Move the multiple consoles to separate views so you can see them at the same time.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.