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: Noughts and Crosses/Tic Tac Toe code problem

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Noughts and Crosses/Tic Tac Toe code problem

    I'm trying to create a noughts and crosses game, that runs without the need for any GUI components, with the following code. (I've marked in bold where the errors occur)

    (Some pointers: I've used 1 and 2 to denote the players rather than x and o as I could then denote the turn number in conjunction with the player who's go it is)

    package noughts.and.crosses;
    /**
     *
     * @author Alex
     */
    import java.util.Scanner;
     
    public class NoughtsAndCrosses {
     
        public static class GameBoard {
            private int[][] theBoard;
            private int currentTurn;
     
            public void GameBoard(int firstTurn){
                currentTurn = firstTurn;
                theBoard = new int[3][3];
     
                //Initialise board
                for (int j = 0; j < 3; j++) {
                    for (int i = 0; i < 3; i++) {
                        theBoard[i][j] = 0;
                    }
                }
            }
            public void showBoard() {
                for (int j = 0; j < 3; j++){
                    System.out.println(theBoard[0][j] + "/n"); 
                }
                for (int j = 0; j < 3; j++){
                    System.out.println(theBoard[1][j] + "/n");
                }
                for (int j = 0; j < 3; j++){
                    System.out.println(theBoard[2][j] + "/n");
                }
            }
            //Define two players as 1 and 2
            //1 = x, 2 = o
            public void nextTurn(){
                if (currentTurn == 1){
                    currentTurn = 2;
                  }   
                else {
                    currentTurn = 1;
                }
            }
            public int getCurrentTurn(){
                return currentTurn;
            }
            public boolean isComplete(){
                boolean complete = true;
                for (int j = 0; j < 3; j++) {
                    for (int i = 0; i < 3; i++) {
                        theBoard[i][j] = 0;
                    complete = false;
                }
             }
                return complete;
           }
            public void makeMove(int row, int col){
                int move = theBoard[row][col];
                if (theBoard[row][col] != 0){
                    System.out.println("That space is already occupied, make another selection.");
            } 
                else { 
                    theBoard[row][col] = currentTurn;
                }
            }
            public int isWinner(){
                int winner = 0;
                //Check for a winner
                for (int i = 0; i < 3; i++){
                    if (theBoard[i][0] == theBoard[i][1] && theBoard[i][0] == theBoard[i][2]){
                        if (theBoard[i][0] != 0){
                        winner = theBoard[i][0];
                        }
                    }
                }
                for (int j = 0; j < 3; j++){
                    if (theBoard[0][j] == theBoard[1][j] && theBoard[0][j] == theBoard[2][j]){
                        if (theBoard[0][j] != 0){
                            winner = theBoard[0][j];
                        }
                    }
                }
                if (theBoard[3][3] == theBoard[2][2] && theBoard[3][3] == theBoard[1][1]){
                    if (theBoard[3][3] !=0){
                        winner = theBoard[3][3];
                    }
                }
                if (theBoard[3][1] == theBoard[2][2] && theBoard[3][1] == theBoard[1][3]){
                    if (theBoard[3][1] != 0){
                        winner = theBoard[3][1];
                    }
                }
                return winner;
            }
        }    
     
        public static void main(String[] args) {
            GameBoard board = new GameBoard(1);  
            Scanner input = new Scanner(System.in);
            int turn;
            int inputRow;
            int inputCol;
            int number;
     
          while (board.isWinner() == 0 && board.isComplete() = false){
                board.showBoard();
                turn = board.getCurrentTurn();
                System.out.println("Player" + turn + "it's your move.");
     
                System.out.print("Enter a row number 1-3");
                number = input.nextInt();
                inputRow = number - 1;
                System.out.print("Enter a column number 1-3");
                number = input.nextInt();
                inputCol = number - 1;
                board.makeMove(inputRow, inputCol);
                board.nextTurn();
            }
            turn = board.getCurrentTurn();
            System.out.println("Player" + turn + "make a valid move.");
            System.out.println("To move enter a row and then a column number");
            System.out.print("Enter a row number 1-3");
            number = input.nextInt();
            inputRow = number - 1;
            System.out.print("Enter a column number 1-3");
            number = input.nextInt();
            inputCol = number - 1;
            board.makeMove(inputRow, inputCol);
     
            System.out.println("The winner is player" + board.isWinner());
     
        }
    }

    The command windows displays, however, the following errors:

    NoughtsAndCrosses.java:100: error: constructor GameBoard in class GameBoard cannot be applied to given types;
            GameBoard board = new GameBoard(1);
                              ^
      required: no arguments
      found: int
      reason: actual and formal argument lists differ in length
    NoughtsAndCrosses.java:107: error: unexpected type
            while (board.isWinner() == 0 && board.isComplete() = false){
                                         ^
      required: variable
      found:    value
    2 errors

    However for the first error, I believe that I need an integer within the parentheses since otherwise the game won't know which turn to start on? I also have defined the constructor "GameBoard(int firstTurn)" and so don't understand why it asking for no arguments.
    The second error I am also none the wiser since as it is not clear to me what it is asking me to change.

    I'm very much a beginner to Java so I apologise if I come across as not knowing much at all.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    This is not a constructor but rather a "pseudo-constructor":

    public void GameBoard(int firstTurn){
      //....
    }

    Note that constructors declare no return type, not void, not anything.

    Cheers!

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Maukkv (January 27th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    Ah of course, that was silly of me. Thanks!
    I now just have the problem with board.isComplete() = false.
    It states "unexpected type, required variable, found value" but I'm not sure what it refers to.

    --- Update ---

    I've realised the problem, it was simply me putting "=" instead of "==".

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    Quote Originally Posted by Maukkv View Post
    Ah of course, that was silly of me. Thanks!
    I now just have the problem with board.isComplete() = false.
    It states "unexpected type, required variable, found value" but I'm not sure what it refers to.

    --- Update ---

    I've realised the problem, it was simply me putting "=" instead of "==".
    And I'll suggest that you use neither. More succinct and clear than:

    if (board.isComplete() == false) {
      // ...
    }

    is to simply do:

    if (!board.isComplete()) {
      // ...
    }

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    Okay thanks again.
    I am actually having problems getting the game to work now when I make a move.
    When I try to input rows and columns in order to activate the makeMove() method nothing happens and I am still presented with an array output of zeros. For example, this is the output I get from my code:

    000
    000
    000
    Player 1 it's your move.
    Enter a row number 1-3: 1
    Enter a column number 1-3: 2
    current turn 2
    000
    000
    000
    Player 2 it's your move.
    Enter a row number 1-3: 1
    Enter a column number 1-3: 2
    current turn 1
    000
    000
    000

    What I was hoping that would happen is that if I entered a row and column number it would replace the corresponding zero with the number of the current turn. I'm specifically new to the scanner, so I'm not sure if the way I've implemented that is wrong or if it is my code that is wrong in some way?

    My code is:
    public class NoughtsAndCrosses {
     
        public static class GameBoard {
            private int[][] theBoard;
            private int currentTurn;
     
            public GameBoard(int firstTurn){
                currentTurn = firstTurn;
                theBoard = new int[3][3];
     
                //Initialise board
                for(int j = 0; j < 3; j++) {
                    for(int i = 0; i < 3; i++) {
                        theBoard[i][j] = 0;
                    }
                }
            }
            public void showBoard() {
                System.out.print(theBoard[0][0]);
                System.out.print(theBoard[0][1]);
                System.out.println(theBoard[0][2]);
                System.out.print(theBoard[1][0]);
                System.out.print(theBoard[1][1]);
                System.out.println(theBoard[1][2]);
                System.out.print(theBoard[2][0]);
                System.out.print(theBoard[2][1]);
                System.out.println(theBoard[2][2]);
                }
     
     
            //Define two players as 1 and 2
            //1 = x, 2 = o
            public void nextTurn(){
                if(currentTurn == 1){
                    currentTurn = 2;
                  }   
                else {
                    currentTurn = 1;
                }
            }
            public int getCurrentTurn(){
                return currentTurn;
            }
            public boolean isComplete(){
                boolean complete = true;
                for(int j = 0; j < 3; j++) {
                    for(int i = 0; i < 3; i++) {
                        theBoard[i][j] = 0;
                    complete = false;
                }
             }
                return complete;
           }
            public void makeMove(int row, int col){
                if(theBoard[row][col] != 0){
                    System.out.println("That space is already occupied, make another selection.");
                } 
                else { 
                    theBoard[row][col] = currentTurn;
                }
            }
            public int isWinner(){
                int winner = 0;
                //Check for a winner
                for(int i = 0; i < 3; i++){
                    if(theBoard[i][0] == theBoard[i][1] && theBoard[i][0] == theBoard[i][2]){
                        if(theBoard[i][0] != 0){
                        winner = theBoard[i][0];
                        }
                    }
                }
                for(int j = 0; j < 3; j++){
                    if(theBoard[0][j] == theBoard[1][j] && theBoard[0][j] == theBoard[2][j]){
                        if(theBoard[0][j] != 0){
                            winner = theBoard[0][j];
                        }
                    }
                }
                if(theBoard[2][2] == theBoard[1][1] && theBoard[2][2] == theBoard[0][0]){
                    if(theBoard[2][2] !=0){
                        winner = theBoard[2][2];
                    }
                }
                if(theBoard[2][0] == theBoard[1][1] && theBoard[2][0] == theBoard[0][2]){
                    if(theBoard[2][0] != 0){
                        winner = theBoard[2][0];
                    }
                }
                return winner;
            }
        }    
     
        public static void main(String[] args) {
            GameBoard board = new GameBoard(1);
            Scanner input = new Scanner(System.in);
            int turn;
            int inputRow;
            int inputCol;
            int number;
     
            while(board.isWinner() == 0 && !board.isComplete()){
                board.showBoard();
                turn = board.getCurrentTurn();
                System.out.println("Player " + turn + " it's your move.");
                System.out.print("Enter a row number 1-3: ");
                number = input.nextInt();
                inputRow = number - 1;
                System.out.print("Enter a column number 1-3: ");
                number = input.nextInt();
                inputCol = number - 1;
                board.makeMove(inputRow, inputCol);
                board.nextTurn();
                System.out.println("current turn " + board.getCurrentTurn());
            }
            turn = board.getCurrentTurn();
            System.out.println("Player " + turn + " make a valid move.");
            System.out.println("To move enter a row, and then a column number");
            System.out.print("Enter a row number 1-3: ");
            number = input.nextInt();
            inputRow = number - 1;
            System.out.print("Enter a column number 1-3: ");
            number = input.nextInt();
            inputCol = number - 1;
            board.makeMove(inputRow, inputCol);
     
            System.out.println("The winner is player" + board.isWinner());
     
        }
    }

    Again thanks for the help!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    A suggestion: Use variables in place of the hard coded magic numbers: 1 and 2 for the players.
    Define two variables:
    final int Player1 = 1;   // define value for players
    final int Player2 = 2;
    Then you can easily see where a player is used by doing a find on "Player" vs looking for a 1 or 2.

    Better will be an enum when you have gotten to that lesson
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    Look very closely at your isComplete() method, where you call it, and what *exactly* it does to the board int array.

  9. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Noughts and Crosses/Tic Tac Toe code problem

    Ah Excellent! It works as planned. Thanks guys! I didn't realise that in that instance the isComplete() method was setting the value of each entry back to zero each time.

Similar Threads

  1. Tic Tac Toe problem help!
    By vess28 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 29th, 2012, 06:59 PM
  2. help needed (tic tac toe problem)
    By vess28 in forum Java Networking
    Replies: 1
    Last Post: September 21st, 2012, 05:14 PM
  3. Let's build tic-tac-toe (noughts and crosses) together...
    By 2by4 in forum What's Wrong With My Code?
    Replies: 34
    Last Post: December 30th, 2011, 10:52 PM
  4. tic tac toe game problem with X and O
    By woohooXX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2011, 06:34 AM
  5. Tic-Tac-Toe program problem help
    By MuffinMcFluffin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2009, 09:14 PM