Results 1 to 4 of 4
  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default Inheritance method problem

    Hi, I am re-making a TicTacToe board game. It inherits the first game I made. Right now I am trying to override the move() method. The new board allows players to choose what to use if they don't want to use X and/or O. The problem is that whenever I use the move method, a System.err comes up saying "ERROR: The ch variable is neither X nor O."

    That is the correct message for the superclass, but my subclass is different. But even weirder is I put a System.out saying "SUPERCLASSERRORCALLED" that never prints when that error message comes up. So I'm pretty lost as to why that message is popping up. The subclass System.err will still output also if it applies. So I'll get two System.err messages sometimes.

    Here is the move() method for the sub-class

    Java Code:
        //override
        public boolean move(char ch, int row, int col) {
            boolean result = false;
            if(ch == getX() || ch == getO()) {    //check ch for X or O
                if(getLastMoved() == ch) //check if player is moving twice
                    System.err.println("Sorry, you cannot move twice.");
                else {
                        if(board[row][col] != getX() && board[row][col] != getO()) {  //if the space is unoccupied
                            board[row][col] = ch;  //make the move
                            result = true;          //change result and print outcome
                            System.out.println("The move was legal.");
                            setLastMoved(ch);
                            if(checkForWin(ch)) //if the game has been won, print the winner
                              System.out.println("The game has been won by " + ch + "!");
                        }
                        else  {  //if the spot is taken, print an error message
                            System.err.println("ERROR: That spot is already taken!! Please chose again.");
                        }
    
                    }
                }
                else  {  //if ch isn't xChar or oChar, print an error message
                    System.err.println("ERROR: The ch variable is neither " + getX() + " nor " + getO() +".");
                    System.out.println("SUBCLASSERROR");
                }
                return result;
            }
    Here is the main I am using for test

    Java Code:
        public static void main(String args[]) {
            BetterTTTBoard game = new BetterTTTBoard('S', 'G');
            game.move('X', 1, 1);
            game.move('G', 2, 0);
            game.move('S', 0, 0);
            System.out.println(game.toString());
        }
    The output I get is --

    Java Code:
    ERROR: The ch variable is neither S nor G.
    SUBCLASSERROR
    The move was legal.
    ERROR: The ch variable is neither X nor O.
    The move was legal.
    ERROR: The ch variable is neither X nor O.
    S
    
    G
    This is the error message from the base class

    Java Code:
     else  {  //if ch is illegal, print error message
                System.err.println("ERROR: The ch variable is neither X nor O.");
                System.out.println("SUPERCLASSERROR");
            }
    Any help would be appreciated.

  2. #2
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Nevermind! Found the problem!

  3. #3
    AlbertoPL is offline Member
    Join Date
    Sep 2009
    Posts
    22
    Rep Power
    0

    Default

    We are missing a lot of information here.

    First of all, is the subclass called BetterTTTBoard? Or is that the name of the superclass. If its the superclass, then that is immediately the problem.

    We'd also need to see what the getX and getO methods do, as well as what move looks like in the superclass. A look at your constructor for your subclass wouldn't hurt, either.

    Edit: Ah you figured it out, mind explaining what you did wrong for others to learn from?

  4. #4
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Yeah well in the super checkForWin(char ch) method, I had an if statement to check if ch was X or O. So in the move(...) method of BetterTTTBoard, it was calling my checkForWin(char ch) in the super class. All I needed to do was override the checkForWin(char ch) and change the check to
    Java Code:
     if(ch == getX() || ch == getO())

Similar Threads

  1. Problem with Inheritance
    By KronikAlkoholik in forum New To Java
    Replies: 4
    Last Post: 08-25-2009, 01:13 AM
  2. Method problem
    By Jeff6461 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 07:48 AM
  3. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  4. Method inheritance
    By JT4NK3D in forum New To Java
    Replies: 7
    Last Post: 06-02-2008, 07:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •