Results 1 to 7 of 7
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    Is there a reason why this error is occurring? I can't identify what's causing it to happen.


    Java Code:
    package tictac1;
    
    import java.util.*;
    
    public class TicTacToe{
    
      //Instance variables
      private char[][] board; //2D array of chars for the board
      public TicTacToe(){ //creates a board where each space holds whitespace
        board = new char[3][3];
        for (int row = 0; row < 3; row ++){
          for (int col =  0; col < 3; col++){
            board[row][col] = ' ';
          } // end of inner loop
        } // end of outer loop
      } 
     
      // Methods
     
      public boolean set(int row, int col, char c){ //sets (row, col) to c
        if (row >= 3 || row < 0) 
          return false;
        if (col >= 3 || col < 0) 
          return false; 
        if (board[row][col] != ' ')
        if ( !(c == 'X' || c == 'O'))
          return false;
        // assertion: row, col, c are valid
        board[row][col] = c;
        return true; 
      }
      /*if this returns true, the character c (X or O) is put 
       * at the board's [row][col] position.
       * this happens only if the variables row, col and c are valid, and 
       * the place where [row][col] refers to is not already
       * occupied (already assigned a value), 
       * other than the default whitespace.
       * this returns false if not true. */
    
    	 
      public char get(int row, int col){ 
    
        return board[row][col];
      }
    
      /*this is a getter method that 
       * retrieves the character assigned to (rol,col)
       * the variable row is an int from 0 to board.length.
       * the variable col is an int from 0 to board[0].length
       * this returns the character that is at the referred position*/
     
      public boolean isFull(){ 
        for (int row = 0; row < 3; row ++){
        	for (int col = 0; col < 3; col ++){
        		if (board[row][col] != ' '){
        			return false;
        		}
        	}
        }
        return true;
      }
    /*this is a method that returns whether or not the board is fully occupied.
     *this returns false if any space in the board is whitespace.  otherwise it is true.
     **/
     public boolean isWinner(int row, int col){
    	 String Player = Character.toString(board[row][col]);	 
    	 boolean onDiagonal = (row ==col)||(col== -1* row+ (board.length-1));
    	 boolean HorizontalWin = true; 
    	 boolean VerticalWin = true;
    	 boolean DiagonalWinOne = true; 
    	 boolean DiagonalWinTwo = true;
    	for (int n = 0; n<board.length; n++){ //Checks rows and columns
    		if(!Character.toString(board[row][n]).equals(Player))
    			HorizontalWin = false;
    		if(!Character.toString(board[n][col]).equals(Player))
    			VerticalWin = false;		
    	}
    	if(onDiagonal){ //Checks only if move was on a diagonal
    		//Checks the diagonals
    		for(int n = 0; n < board.length; n++){
    			if(!Character.toString(board[n][n]).equals(Player))
    				DiagonalWinOne = false;
    			if(!Character.toString(board[n][-1*n+(board.length-1)]).equals(Player))
    				DiagonalWinTwo = false;
    		}
    	}
    	else{
    		DiagonalWinOne = false;
    		DiagonalWinTwo = false;
    	}
    	boolean hasWon = (HorizontalWin || VerticalWin || DiagonalWinOne || DiagonalWinTwo);
    	return hasWon;
     }
    
     /*This method takes the row and column coordinates of the last move made and checks to see
      * if that move caused the player to win. This method checks the row, column and diagonal
      * (if the move was made on a diagonal) that the move was played on to see if 
      * the winning condition was met there. 
       */
    public boolean whoIsWinner(int row, int col, boolean hasWon){
    	boolean xWon = false;
    	boolean oWon = false;
    	if (hasWon == true)
    		if (board[row][col] == 'X')
    			xWon = true;
    		if (board[row][col] == 'O')
    			oWon = true;
    	if (xWon = true)
    		return xWon;
    	if (oWon = true);
    		return oWon;
    }
    [B] private char winner(boolean hasWon, boolean xWon, boolean oWon){ 
    	    if (hasWon != false) 
    	      if (xWon == true)
    	    	 return 'X';
     		 if (oWon == true)
     			 return 'O';
     		 else 
     			 return ' ';[/B]
     }
      public void play(){
        Scanner input = new Scanner(System.in);
        int row, col;
        char currPlayer = 'X';
        while(!isFull() && winner() != 'X' && winner() != 'O'){
    	      try{
    	        print();
    	        System.out.println("Player "+currPlayer+"'s Turn");
    	        System.out.print("Enter your move. (e.g. 1,2):");
    	        Scanner tokens = new Scanner(input.nextLine());
    	        tokens.useDelimiter(",");
    	        	row = tokens.nextInt();
    	        	col = tokens.nextInt();
    	        if (!set(row,col,currPlayer)){
    	               throw new Exception("Error Setting.");
    	        }
    	        	}catch(Exception error){
    	              if(error instanceof InputMismatchException){
    	                input.nextLine();
    	              }else{
    	          System.out.println("Bad input");
    }
            if(currPlayer == 'X'){
              currPlayer = 'O';
            }
            else{
              currPlayer = 'X';
            }
          }
          
          if(currPlayer == 'X'){
            currPlayer = 'O';
          }
          else{
            currPlayer = 'X';
          }
        }
     
        input.close();
        print();
        if (winner() == 'X'){
          System.out.println("Player X wins!!");
        }
        else if (winner() == 'O'){
          System.out.println("Player O wins!!");
        }
        else{
          System.out.println("Everybody wins!! (Sort of)");
        }
      }
    /*this method plays a full game of tic-tac-toe. The method assumes the board is empty.
       * (the variables are initialized, but no moves have been made.)
       */
    
      public void print(){ //this method prints the current state of the board
        int col;
        System.out.print("    col");
        System.out.print("      ");
        for (col = 0; col < 3; col++){
        }
        System.out.println();
        for (int row = 0; row < 3; row ++){
          if(row == 0){
        	System.out.println("    +-+-+-+");
            System.out.println("row "+ "|" +" ");
            System.out.print("    +-+-+-+");
          }
          else {
            System.out.println("    "+"|" +" ");
            System.out.print("    +-+-+-+");
          }
          for (col =  0; col < 3; col++){
            System.out.print(board[row][col] + " ");
          } // end of inner loop
          System.out.println();
        } // end of outer loop   
      }
    }
    Last edited by Propinquity; 01-16-2014 at 01:26 PM.

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    We need to know what line you are getting the error on. It may also be because you have the winner() method commented out?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    winner(boolean, boolean, boolean) is the only private method as far as I can see. Frum where and how do you call it?

    @pj6444 : it isn't commented out, the syntax highlighting doesn't like block comments.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    I'm getting the error at 126, 162, and 165.
    The winner method is
    Java Code:
    private char winner(boolean hasWon, boolean xWon, boolean oWon){ 
            if (hasWon != false) 
              if (xWon == true)
                 return 'X';
             if (oWon == true)
                 return 'O';
             else 
                 return ' ';
    I'm trying to get it to return a char with the symbol of the winner of the game, so it can be used to say which player won the game.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    You're calling winner() without args. That doesn't exist.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    How do I call it with args?
    I don't really know how else to do it.

    I tried changing the result of the winner method to a String, so I could successfully run the while loops and if statements that previously had the winner method called.
    That just made an error in my main method. I'm guessing it's the same problem. Do you have any advice on how to fix this? It's completely going over my head.
    Java Code:
    package tictac1;
    public class MainMethod {
    	public static void main(String[] args){
    		TicTacToe game = new TicTacToe();
    		game.play();
    	}
    }
    error: The method play(String) in the Type TicTacToe is not applicable in the arguments ()

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: The method winner(boolean, boolean, boolean) is undefined for the Type TicTacToe?

    Quote Originally Posted by Propinquity View Post
    How do I call it with args?
    I don't really know how else to do it.
    The problem here is that you are not triggered to go find out. How have you been learning Java so far? I hope not by just copy/pasting stuff on the net together. I hope you have a book or some other type of guide; use it! At least work through the Java tutorial.

    The Java™ Tutorials

    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 4
    Last Post: 08-17-2013, 05:20 AM
  2. Replies: 5
    Last Post: 07-12-2013, 01:22 AM
  3. Replies: 3
    Last Post: 05-11-2013, 06:27 AM
  4. Replies: 3
    Last Post: 11-18-2011, 08:15 AM
  5. Replies: 3
    Last Post: 09-13-2011, 12:01 AM

Posting Permissions

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