Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Alternate turns using Polymorphism and abstract classes

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Alternate turns using Polymorphism and abstract classes

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Alternate turns using Polymorphism and abstract classes

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Abstract Classes?
    By jean28 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2013, 07:13 AM
  2. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  3. what is the use of interfaces and abstract classes?
    By sagar474 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 18th, 2011, 02:34 PM
  4. Interfaces Vs Abstract classes
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:49 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM

Tags for this Thread