1

I'm trying to make a battleship game, and this is the part where the players set their boards. When the second player to set the board makes theirs, the first player's board becomes the same as the second player's (when playing the game guessing the locations of that board are misses though, but that's another problem that will hopefully be fixed once this is). For example if I set the first player's ships as being at A1 A2 A3, B1 B2 B3, and C1 C2 C3, then set the second player's ships as D1 D2 D3, E1 E2 E3, and F1 F2 F3, when both lists of ships are printed out I get D1 D2 D3, E1 E2 E3, and F1 F2 F3 for both ships. I've found other questions on here with the same problem, but they always had the problem because they didn't make new lists every time, which I do (at least I'm pretty sure I do), so I can't find where my problem is. Here is the entire main game class, the GetShipLocations, SetShipLocations, and PlayersMakeTheirBoards, functions are what are giving me problems.

import java.util.*;
import java.lang.*;
public class MainGame {
    InputReader read = new InputReader();
    Grid p1Board = new Grid();
    Grid p2Board = new Grid();
    Grid p1ShipsBoard = new Grid();
    Grid p2ShipsBoard = new Grid();
    ArrayList<Ship> p1Ships = new ArrayList<Ship>();
    ArrayList<Ship> p2Ships = new ArrayList<Ship>();
    String activePlayer = "P1";

    public void SetUp() {
        p1Board.PrepPrintGrid();
        p2Board.PrepPrintGrid();
        Ship ship1 = new Ship();
        Ship ship2 = new Ship();
        Ship ship3 = new Ship();
        p1Ships.add(ship1);
        p1Ships.add(ship2);
        p1Ships.add(ship3);
        p2Ships.add(ship1);
        p2Ships.add(ship2);
        p2Ships.add(ship3);
    }
    public void GameIntro() {
        System.out.println("Welcome to battleship!");
        String rulesOption = read.getUserInput("Do you need to see the rules?");
        if(rulesOption.equals("Yes") || rulesOption.equals("yes"))
        {
            System.out.println("Put rules here");
        }
        System.out.println("Randomly determining which player goes first");
        int random = (int) (Math.random() * 2);
        if(random == 1)
        {
            activePlayer = "P2";
        }
        System.out.println(activePlayer + " starts!");
    }
    public ArrayList<ArrayList<String>> GetShipLocations(Grid board) {
        ArrayList<ArrayList<String>> ships = new ArrayList<ArrayList<String>>();
        ArrayList<String> ship1 = new ArrayList<String>();
        ArrayList<String> ship2 = new ArrayList<String>();
        ArrayList<String> ship3 = new ArrayList<String>();
        ships.add(ship1);
        ships.add(ship2);
        ships.add(ship3);
        String[] numbers = {"first", "second", "third"};
        board.PrepPrintGrid();
        board.PrintGrid();
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                String coordinate = read.getUserInput("Enter the " + numbers[j] + " coordinate of your " + numbers[i] + " ship (3 long):");
                ships.get(i).add(coordinate);
                board.SetGridDisplay(coordinate, "hit");
                board.PrintGrid();
            }
        }
        return ships;
    }
    public void SetShipLocations(ArrayList<Ship> ship, Grid activeBoard) {
        ArrayList<ArrayList<String>> shipLocations = GetShipLocations(activeBoard);
        for(int i = 0; i < 3; i++) {
            ship.get(i).SetCells(shipLocations.get(i));
        }
    }
    public void PlayersMakeTheirBoards() {
        if(activePlayer.equals("P1")) 
        {
            System.out.println("Hand the computer to player one.");
            System.out.println("Player one, time to set your board.");
            SetShipLocations(p1Ships, p1ShipsBoard);//the effects of this seem to maybe be overriden when the second board is set
            for(int i = 0; i < /*100*/3; i++) 
            {
                for(int j = 0; j < 3; j++)
                {
                    System.out.println(p1Ships.get(i).cells.get(j));
                }

            }
            System.out.println("Hand the computer to player two.");
            System.out.println("Player two, time to set your board.");
            SetShipLocations(p2Ships, p2ShipsBoard);
            for(int i = 0; i < /*100*/3; i++) 
            {
                for(int j = 0; j < 3; j++)
                {
                    System.out.println(p2Ships.get(i).cells.get(j));
                }

            }
            for(int i = 0; i < /*100*/3; i++) 
            {
                for(int j = 0; j < 3; j++)
                {
                    System.out.println(p1Ships.get(i).cells.get(j));
                }

            }
        }
        else
        {
            System.out.println("Hand the computer to player two.");
            System.out.println("Player two, time to set your board.");
            SetShipLocations(p2Ships, p2ShipsBoard);
            /*for(int i = 0; i < 100; i++) 
            {
                System.out.println("");
            }*/
            for(int i = 0; i < /*100*/3; i++) 
            {
                for(int j = 0; j < 3; j++)
                {
                    System.out.println(p2Ships.get(i).cells.get(j));
                }

            }
            System.out.println("Hand the computer to player one.");
            System.out.println("Player one, time to set your board.");
            SetShipLocations(p1Ships, p1ShipsBoard);
            for(int i = 0; i < /*100*/3; i++) 
            {
                for(int j = 0; j < 3; j++)
                {
                    System.out.println(p1Ships.get(i).cells.get(j));
                }

            }
            for(int i = 0; i < /*100*/3; i++) 
            {
                for(int j = 0; j < 3; j++)
                {
                    System.out.println(p2Ships.get(i).cells.get(j));
                }

            }
        }
    }
    public void PlayTheGame() {
        String guess = new String();
        String result = new String();
        for(int i = 0; i < 100; i++) 
            {
                System.out.println("");
            }
        while(!p1Ships.isEmpty() && !p2Ships.isEmpty())
        {
            if(activePlayer.equals("P1"))
            {
            //print the grid
            p1Board.PrintGrid();
            //ask user for their guess
            guess = read.getUserInput("Player one, enter your guess:");
            for(Ship boat:p2Ships)
            {
                result = boat.CheckYourself(guess);
                if(result.equals("hit"))
                {
                    System.out.println(result + "!");
                    break;
                }
                if(result.equals("kill"))
                {
                    System.out.println(result + "!");
                    p2Ships.remove(boat);
                    break;
                }
            }
            if(result.equals("miss"))
            {
                System.out.println(result);
            }
            p1Board.SetGridDisplay(guess, result);
            activePlayer = "P2";
            }
            else
            {
            p2Board.PrintGrid();
            //ask user for their guess
            guess = read.getUserInput("Player two, enter your guess:");
            for(Ship boat:p1Ships)
            {
                result = boat.CheckYourself(guess);
                if(result.equals("hit"))
                {
                    System.out.println(result + "!");
                    break;
                }
                if(result.equals("kill"))
                {
                    System.out.println(result + "!");
                    p1Ships.remove(boat);
                    break;
                }
            }
            if(result.equals("miss"))
            {
                System.out.println(result);
            }
            p2Board.SetGridDisplay(guess, result);
            activePlayer = "P1";
            }
        }
    }
    public void EndTheGame() {
        String winner = new String();
        if(p1Ships.isEmpty()) 
        {
            winner = "Player two";
            p2Board.PrintGrid();
        }
        else
        {
            winner = "Player one";
            p1Board.PrintGrid();
        }
        System.out.println("The game is over!");
        System.out.println(winner + " wins! Congratulations!");
    }
    public static void main(String[] args) {
        MainGame game = new MainGame();
        game.SetUp();
        game.GameIntro();
        game.PlayersMakeTheirBoards();
        game.PlayTheGame();
        game.EndTheGame();
    }
}

and here is the Ship class

import java.util.*;
public class Ship {
    ArrayList<String> cells = new ArrayList<String>();
    //String name = new String();
    public void SetCells(ArrayList<String> locations) {
        cells = locations;
    }
    /*public void SetName(String word) {
        name = word;
    }*/
    public String CheckYourself(String guess) {
            if(cells.contains(guess)) 
            {
                cells.remove(guess);
                if(cells.isEmpty()) 
                {
                    return "kill";
                }
                else
                {
                    return "hit";
                }
            }
            return "miss";
    }
}

The grid and reader classes are working perfectly so I didn't include them. (This is all based off the dotcom battleship game in headfirst java)

11
  • Note that you should follow the Java Naming Conventions: method names and variabele names should be written in camelCase. Commented Apr 25, 2020 at 20:20
  • Thanks for the note, I always used to do that but in the book I noticed they used capitals for the first word too, I'll switch back Commented Apr 25, 2020 at 20:27
  • 1
    Can you please edit your question to make it clearer what exactly is the problem you are facing? I couldn't understand what you are trying to say because there is no clear description of the problem rather other side stories too. Commented Apr 25, 2020 at 20:39
  • How are you instantiating the p1Ships, p2Ships, p1ShipsBoard, p2ShipsBoard? Commented Apr 25, 2020 at 20:41
  • Also, since you're getting misses when targeting the same locations, are you sure your printing code is correct? Commented Apr 25, 2020 at 20:44

1 Answer 1

2

In your setup function:

public void SetUp() {
        p1Board.PrepPrintGrid();
        p2Board.PrepPrintGrid();
        Ship ship1 = new Ship();
        Ship ship2 = new Ship();
        Ship ship3 = new Ship();
        p1Ships.add(ship1);
        p1Ships.add(ship2);
        p1Ships.add(ship3);
        p2Ships.add(ship1);
        p2Ships.add(ship2);
        p2Ships.add(ship3);
    }

You are adding the same instances of ship1-3 to both p1Ships and p2Ships. So when you change the ships of player 2, the p1Ships ArrayList is still pointing to the same ships as p2Ships, and thus will always be the same.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow I can't believe I missed this, thank you so much. What a dumb mistake haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.