I am working on a project building a battleship game. One of the requirements is that we create an abstract class called Game, with an abstract method called play(), then 2 subclasses for each player, one for computer and one for human.
In my class Game I have a method to initialize the board and randomly generate ships.
In the main of the program I am Doing the following:
Human humanPlayer = new Human();
Computer computerPlayer = new Computer();
Game battle = humanPlayer; //this is the abstract class receiving the human subclass
battle.initBoard(); //this initializes the board
battle.putShips(); //this places the ships
battle.printBoard(); //this print the board
After this, I have a while loop that alternates turns
while(battle.humanIsWinner() || battle.computerIsWinner())
{
if (turn%2 == 0)
{
battle = humanPlayer;
battle.play();
turn++;
battle.printBattleGround();
}
else
{
battle = computerPlayer;
battle.play();
turn++;
battle.printBattleGround();
}
}
The problem I am having is that whenever I print the board after the play, I notice there are 2 different board, I guess one for the human and one for the computer, which is not correct because the human ships are at the top half of my board and the computer ships are at the bottom half of my board.
I believe my problem is with the abstract class and using polymorphism correctly.
Any help is greatly appreciated.
Thanks.