CSE 110: Principles of Programming Languages Assignment 8 Overview You have been given a group project to implement a simple version of the game Battleship.
CSE 110: Principles of Programming Languages
Assignment 8
Overview
You have been given a group project to implement a simple version of the game Battleship. Your group partner has repeatedly told you that they will start soon and send you it when theyre finished, and has refused to show it to you despite claiming that it is going well. Finally, a day before the assignment is due, your partner emails the code to you saying that it works, except for a couple small things. and catches a flight to Vegas. You play the game and very quickly discover that it is quite broken. You will need to fix and finish the assignment on your own.
As a programmer you, obviously, write a lot of code. But a surprising amount of your time actually can get spent fixing, updating, or rewriting code written by others. This assignment reflects t hat and you will be provided a version of the Battleship program that compiles, but has several issues. Your job is to edit what is already there and end with a functional version of the game. This means you will write some methods that are not implemented and fix logic errors.
Requirements
Your program must do the following in order to receive full credit on this assignment.
1. For starters, the game never ends, even after the ship has been sunk and all of the spots have been shot at. Correct the while loop condition in main to make the game end when one player wins.
2. Now that the game can end, you might notice that Player 2 always wins. Also, the two players cant fire at the same space, itll say that they already fired there, even though it was the other pl ayer who shot there. Player 1 also cant hit Player 2s ships, but they can hit their own ships! Player 1 is firing on their own grid! Find where in main Player 1 takes their shots and correct it to fire at Player 2 instead of themselves.
3. When you play, P layer 1 actually sees exactly where Player 2s ship is when firing. Fix the printP1Fire method in the BattleShipGrid class to have it print the correct array instead.
a. There are four arrays, two for each player. The one called p1/p2grid holds their ship and the locations the other player has shot at. The second array called p1/p2fire is the one that shows only where they shot, and not where the enemy ship is, though they can see where theyve hit it.
i. Basically, the one called grid is the one on the bottom wh en playing actual Battleship and the array called fire is the top one. See pic below.
ii.
4. Youll also notice that Player 2 does not see a grid at all when firing. The printP2Fire method in BattleShipGrid is only a stub so you need to write it out.
a. A stub is a quick version of a method that only works in the sense that it compiles properly, so you will have to fill in the needed code.
b. This will be almost exactly like printP1Fire method that you just fixed, except it will need to print out the proper array for Player 2.
5. Finally, there is one last visual bug. When Player 1 hits Player 2s ship, the spot that was hit shows the value that represents a miss. Likewise, when they miss, it shows the value that represents a hit. Correct the fireAtPlayerTwo method in Bat tleShipGrid to display the correct value instead.
Battleshipgrid java public class BattleShipGrid { /* * Both players have two arrays, one called grid and one called fire. * The one called grid holds their ship and is where the other player * fires on. The fire array is the one that player sees during the game * and it shows where their shots land and where they've hit the enemy * ship. */ private int[][] p1Grid; // Player 1's board which contains their ship private int[][] p1Fire; // This is what Player 1 sees and where their shots appear private int[][] p2Grid; // This would be the bottom grid in actual Battleship for Player 2 private int[][] p2Fire; // And this would be their top grid // Constants for what each int represents in the arrays private final int EMPTY = 0; private final int MISS = 1; private final int SHIP = 2; private final int HIT = 3; public BattleShipGrid() // Don't change this { p1Grid = new int[5][5]; p1Fire = new int[5][5]; p2Grid = new int[5][5]; p2Fire = new int[5][5]; p1Grid[1][0] = SHIP; p1Grid[2][0] = SHIP; p1Grid[3][0] = SHIP; p2Grid[4][2] = SHIP; p2Grid[4][3] = SHIP; p2Grid[4][4] = SHIP; } public String printP1Fire() // This is the method to fix for Step 3 { String out = ""; for(int i = 0; i < p1Fire[0].length; i++) { for(int j = 0; j < p1Fire.length; j++) { out += p2Grid[j][i]; } if(i != p1Fire[0].length - 1) { out += " "; } } return out; } public String printP2Fire() // STUB, write this for step 4 { String out = ""; return out; } // If there are any ship spots in the array, the game can't be over yet. private boolean gameOver(int[][] grid) { boolean over = true; for(int i = 0; i < grid.length; i++) { for(int j = 0; j < grid[0].length; j++) { if(grid[j][i] == SHIP) { over = false; } } } return over; } public boolean playerOneWon() { return gameOver(p2Grid); } public boolean playerTwoWon() { return gameOver(p1Grid); } // Should be called when Player 2 fires at Player 1 public int fireAtPlayerOne(int x, int y) { int result = p1Grid[x][y]; if(result == EMPTY) { p1Grid[x][y] = MISS; p2Fire[x][y] = MISS; } else if(result == SHIP) { p1Grid[x][y] = HIT; p2Fire[x][y] = HIT; } return result; } public int fireAtPlayerTwo(int x, int y) // Fix this method for Step 5 { int result = p2Grid[x][y]; if(result == EMPTY) { p2Grid[x][y] = HIT; p1Fire[x][y] = HIT; } else if(result == SHIP) { p2Grid[x][y] = MISS; p1Fire[x][y] = MISS; } return result; } }
Assignment8 java import java.util.Scanner; public class Assignment8 { public static void main(String[] args) { BattleShipGrid grid = new BattleShipGrid(); boolean player1Turn = true; Scanner scan = new Scanner(System.in); String input; String[] coordinates; int x; int y; int fire; System.out.println("Welcome to BattleShip!"); System.out.println("0 means unknown ocean, 1 means you shot there and missed,"); System.out.println("2 is a ship, and 3 is a ship that's been hit."); System.out.println("The top left corner is 0 0, and the bottom right is 4 4"); // Loop that will run until one player or the other has won while(!grid.playerOneWon() || !grid.playerTwoWon()) { if(player1Turn) { System.out.println(" Player 1: Type in the spot to shoot at as \"X Y\""); System.out.println(" " + grid.printP1Fire()); } else { System.out.println(" Player 2: Type in the spot to shoot at as \"X Y\""); System.out.println(" " + grid.printP2Fire()); } // Handle player input and slpit it from one line into two numbers input = scan.nextLine(); coordinates = input.split(" "); x = Integer.parseInt(coordinates[0]); y = Integer.parseInt(coordinates[1]); if(player1Turn) { fire = grid.fireAtPlayerOne(x, y); } else { fire = grid.fireAtPlayerOne(x, y); } if(fire == 0) { System.out.println("Miss!"); } else if(fire == 2) { System.out.println("====================================="); System.out.println("Hit!"); if(player1Turn) { System.out.println(grid.printP1Fire()); } else { System.out.println(grid.printP2Fire()); } System.out.println("====================================="); } else { System.out.println("You already shot there!"); } player1Turn = !player1Turn; } if(grid.playerOneWon()) { System.out.println("Player 1 is the winner!"); } else { System.out.println("Player 2 is the winner!"); } } }
Example Inputs
Below are five example runs of the program with the inputs and outputs. Remember, the graders will be testing your program against these as well as their own, so make sure you test these and come up with your own before submitting your program.
#1
Welcome to BattleShip!
0 means unknown ocean, 1 means you shot there and missed,
2 is a ship, and 3 is a ship that's been hit.
The top left corner is 0 0, and the bottom right is 4 4
Player 1: Type in the spot to shoot at as "X Y"
The fire array
The grid array
00000
00000
00000
00000
00000
4 2
=====================================
Hit!
00000
00000
00003
00000
00000
=====================================
Player 2: Type in the spot to shoot at as "X Y"
00000
0000 0
00000
00000
00000
0 0
Miss!
Player 1: Type in the spot to shoot at as "X Y"
00000
00000
00003
00000
00000
4 3
=====================================
Hit!
00000
00000
00003
00003
00000
=====================================
Player 2: Type in the spot to shoot at as "X Y"
10000
00000
00000
00000
00000
0 1
Miss!
Player 1: Type in the spot to shoot at as "X Y"
00000
00000
00003
00003
00000
4 4
=====================================
Hit!
00000
00000
00003
00003
00003
=====================================
Player 1 is the winner!
#2
Welcome to BattleShip!
0 means unknown ocean, 1 means you shot there and missed,
2 is a ship, and 3 is a ship that's been hit.
The top left corner is 0 0, and the bottom right is 4 4
Player 1: Type in the spot to shoot at as "X Y"
00000
00000
00000
00000
00000
0 0
Miss!
Player 2: Type in the spot to shoot at as "X Y"
00000
00000
00000
00000
00000
1 0
=====================================
Hit!
03000
00000
00000
00000
00000
=====================================
Player 1: Type in the spot to shoot at as "X Y"
10000
00000
00000
00000
00000
0 1
Miss!
Player 2: Type in the spot to shoot at as "X Y"
03000
00000
00000
00000
00000
2 0
=====================================
Hit!
03300
00000
00000
00000
00000
=====================================
Player 1: Type in the spot to shoot at as "X Y"
10000
10000
00000
00000
00000
0 2
Miss!
Player 2: Type in the spot to shoot at as "X Y"
03300
00000
00000
00000
00000
3 0
=====================================
Hit!
03330
00000
00000
00000
00000
=====================================
Player 2 is the winner!
#3
Welcome to BattleShip!
0 means unknown ocean, 1 means you shot there and missed,
2 is a ship, and 3 is a ship that's been hit.
The top left corner is 0 0, and the bottom right is 4 4
Player 1: Type in the spot to shoot at as "X Y"
00000
00000
00000
00000
00000
4 2
=====================================
Hit!
00000
00000
00003
00000
00000
=====================================
Player 2: Type in the spot to shoot at as "X Y"
00000
00000
00000
00000
00000
0 0
Miss!
Player 1: Type in the spot to shoot at as "X Y"
00000
00000
00003
00000
00000
4 3
=================================== ==
Hit!
00000
00000
00003
00003
00000
=====================================
Player 2: Type in the spot to shoot at as "X Y"
10000
00000
00000
00000
00000
0 0
You already shot there!
Player 1: Type in the spot to shoot at as "X Y"
00000
00000
00003
00003
00000
4 4
=====================================
Hit!
00000
00000
00003
00003
00003
=====================================
Player 1 is the winner!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started