Alternate turns using Polymorphism and abstract classes
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.
Re: Alternate turns using Polymorphism and abstract classes
Quote:
whenever I print the board after the play, I notice there are 2 different board
If there is supposed to be a single board, what class would it be in? How would each player get access to the single board?
Re: Alternate turns using Polymorphism and abstract classes
This is where I am having trouble. The board is in the Game class. Right now I have a public variable - public char[][] board, that Human and Computer can see.
Re: Alternate turns using Polymorphism and abstract classes
Quote:
I notice there are 2 different board,
What did that mean if there is only one board in the Game class? Is there more than one instance of the Game class?
Re: Alternate turns using Polymorphism and abstract classes
Well, not, but there is an instance of the Human class and an instance of the Computer class, then I am alternating by putting it in the instance of Game called battle. I can not instantiate Game with Game because it is abstact...
Re: Alternate turns using Polymorphism and abstract classes
Then the Human class has a board and the Computer class has a board, making two boards.
Re: Alternate turns using Polymorphism and abstract classes
Any ideas on how I can fix it?
--- Update ---
I want to be able to use only one board but take turns with the Human and Computer players.
Re: Alternate turns using Polymorphism and abstract classes
Can you make a small simple program that compiles, executes and shows the problem.
There needs to be one place the board is created with methods for all users to get to it to make updates etc.