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: Checking for draw in TicTacToe

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Checking for draw in TicTacToe

    So after some thought about my code, I forgot that a draw could also occur,and I havent implemented that in my code.So here I am.Now the way of determining if a draw has happened should be pretty simple.First check if the board is full (all the fields are initialised) and second check if there is a winner, if not a draw happend.So I tried to do exactly that I created a method to check if the board was full;

     
    public static boolean isBoardFull() {
    	for(int i = 0; i < col; i++) {
    		for(int j = 0; j < row; j++) {
    			if(board[i][j] == EMPTY) {
    				return false;
    			}
    		}
    	}
    	return true;
    }

    And I went to implement this method in the rest of my code.Now since I'm here you can assume that something went wrong and this is what is going wrong.After every input it prints out draw.Although it should only do it when the board is full.So my guess is something is wrong with this method, but I'll post the entire code so that you can see how I'm implementing it

     
    public class TicTacToe {
    	public static int row,col;
    	public static char[][] board = new char[3][3];
    	public static char turn = 'X';
    	public static final int EMPTY = 0;
     
    	public static void main(String[] args) {
    		for(int i = 0; i < 3; i++) {
    			for(int j = 0; j < 3; j++) {
    				board[i][j] = '_';
    			}
    		}
    		Play();
    	}
     
    	public static void printBoard() {
    	for(int i = 0; i < 3; i++) {
    		Out.println();
    		for(int j = 0; j < 3; j++) {
    			if(j == 0){
    				Out.print("| ");
    			}
    			Out.print(board[i][j] + " | ");
    		}
    	}
    	Out.println();
     
    }
     
     
    public static void Play() {
    	boolean playing = true;
    	printBoard();
    	Out.println();
    	int nOfTurns = 1;
    	Out.println("Turn: " + nOfTurns);
     
    	while(playing){
    		Out.println();
    		Out.println("Player:" +" "+ turn);
    		Out.print("Enter a row:");
    		row = readNumber();
    		Out.print("Enter a coloumn:");
    		col = readNumber();
    		do {
    			if(isTaken(row,col)) {
    				Out.print("That cell is taken,please enter another cell:");
    				row = readNumber();
    				col = readNumber();
    			}
    		}while(isTaken(row,col));
    		board[row][col] = turn;
    		nOfTurns++;
    		Out.println();
    		Out.println("Turn: "+ nOfTurns);
    		if(GameOver(row,col)) {
    			playing = false;
    			Out.println("Player" +" "+turn +" "+"has won!");
    		}
    		printBoard();
     
    		if(isBoardFull()) {
    			Out.println("Draw");
    		}
     
    		if(turn == 'X') {
    			turn = 'O';
    		}else{
    			turn = 'X';
    		}
     
    }
    }
    public static boolean GameOver(int rowMove,int colMove) {
    	if(board[0][colMove] == board[1][colMove] && board[0][colMove] == board[2][colMove]){
    		return true;
    	}else if(board[rowMove][0] == board[rowMove][1] && board[rowMove][0] == board[rowMove][2]) {
    		return true;
    	}else if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != '_') {
    		return true;
    	}else if(board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '_') {
    		return true;
    	}else
    		return false;
    }
     
     
    public static int readNumber() {
    	boolean isValid = false;
    	int number;
    	do {	
     
    	number = In.readInt() - 1;
     
    	if (!In.done()) {
            Out.println("Input must be a valid number");
            In.readLine(); 
          } else if (number < 0 || number > 2) {
            Out.println("The number is not in the specified range: 1-3");
          } else {
            isValid = true;
          }
        } while (!isValid);
     
        return number;
      }
     
    public static boolean isTaken(int row, int col) {
    	return board[row][col] != '_';
    }
     
    public static boolean isBoardFull() {
    	for(int i = 0; i < col; i++) {
    		for(int j = 0; j < row; j++) {
    			if(board[i][j] == EMPTY) {
    				return false;
    			}
    		}
    	}
    	return true;
    }
     
    }

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

    Default Re: Checking for draw in TicTacToe

    Time for some debugging to see what the code is doing. Add some print statements inside the loop that print out the values of i and j and the contents of the board at i and j.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Checking for draw in TicTacToe

    Aight I'll give it a shot

  4. #4
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Checking for draw in TicTacToe

    So after a good night sleep,and a few hours of coding.I have come up with this;

     
    public class TicTacToe {
    	public static int row,col;
    	public static char[][] board = new char[3][3];
    	public static char turn = 'X';
     
    	public static void main(String[] args) {
    		for(int i = 0; i < 3; i++) {
    			for(int j = 0; j < 3; j++) {
    				board[i][j] = '_';
    			}
    		}
    		play();
    	}
     
    	public static void printBoard() {
    	for(int i = 0; i < 3; i++) {
    		Out.println();
    		for(int j = 0; j < 3; j++) {
    			if(j == 0){
    				Out.print("| ");
    			}
    			Out.print(board[i][j] + " | ");
    		}
    	}
    	Out.println();
     
    }
     
     
    public static void play() {
    	boolean playing = true;
    	printBoard();
    	Out.println();
    	int nOfTurns = 1;
    	Out.println("Turn: " + nOfTurns);
     
    	while(playing){
    		Out.println();
    		Out.println("Player:" +" "+ turn);
    		Out.print("Enter a row:");
    		row = readNumber();
    		Out.print("Enter a coloumn:");
    		col = readNumber();
    		do {
    			if(isTaken(row,col)) {
    				Out.print("That cell is taken,please enter another cell:");
    				row = readNumber();
    				col = readNumber();
    			}
    		}while(isTaken(row,col));
    		board[row][col] = turn;
    		nOfTurns++;
    		Out.println();
    		Out.println("Turn: "+ nOfTurns);
    		if(gameOver(row,col)) {
    			playing = false;
    			Out.println("Player" +" "+turn +" "+"has won!");
    		}
    		printBoard();
    		if(gameOver(row,col) && isTaken(row,col)) {
    			Out.println("Draw!");
    			playing = false;
    		}
    		if(turn == 'X') {
    			turn = 'O';
    		}else{
    			turn = 'X';
    		}
     
    }
    }
    public static boolean gameOver(int rowMove,int colMove) {
    	if(board[0][colMove] == board[1][colMove] && board[0][colMove] == board[2][colMove]){
    		return true;
    	}else if(board[rowMove][0] == board[rowMove][1] && board[rowMove][0] == board[rowMove][2]) {
    		return true;
    	}else if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != '_') {
    		return true;
    	}else if(board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '_') {
    		return true;
    	}else {
    		return false;
    }
     
    }
    public static int readNumber() {
    	boolean isValid = false;
    	int number;
    	do {	
     
    	number = In.readInt() - 1;
     
    	if (!In.done()) {
            Out.println("Input must be a valid number");
            In.readLine(); 
          } else if (number < 0 || number > 2) {
            Out.println("The number is not in the specified range: 1-3");
          } else {
            isValid = true;
          }
        } while (!isValid);
     
        return number;
      }
     
    public static boolean isTaken(int row, int col) {
    	return board[row][col] != '_';
    }
     
    public static boolean isBoardFull() {
    	if(isTaken(row,col)) {
    		return true;
    	}else{
    		return false;
    	}
    }
     
    }

    As you can see I've changed my boardIsFull method.Now the issue is not that it prints out draw after every move it is that it doent print draw at all.Even when I reached the end(all the fields are covered) the game does not stop.What am I doing wrong?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Checking for draw in TicTacToe

    There is a basic problem in the code using the global variables: row and col. These variables should NOT be global. By global I mean at the class level. Their values are dependent on the context where they are used. They should be declared at the local level inside a method where their values are used. If a method needs their values, they should be passed to the method as arguments.

    Change the code by removing row and col as global variables and have them be declared where they are needed and passed as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Checking for draw in TicTacToe

    Did it, but how is that gonna help?

     
    public class TicTacToe {
    	public static char[][] board = new char[3][3];
    	public static char turn = 'X';
     
    	public static void main(String[] args) {
    		for(int i = 0; i < 3; i++) {
    			for(int j = 0; j < 3; j++) {
    				board[i][j] = '_';
    			}
    		}
    		play();
    	}
     
    	public static void printBoard() {
    	for(int i = 0; i < 3; i++) {
    		Out.println();
    		for(int j = 0; j < 3; j++) {
    			if(j == 0){
    				Out.print("| ");
    			}
    			Out.print(board[i][j] + " | ");
    		}
    	}
    	Out.println();
     
    }
     
     
    public static void play() {
    	int row,col;
    	boolean playing = true;
    	printBoard();
    	Out.println();
    	int nOfTurns = 1;
    	Out.println("Turn: " + nOfTurns);
     
    	while(playing){
    		Out.println();
    		Out.println("Player:" +" "+ turn);
    		Out.print("Enter a row:");
    		 row = readNumber();
    		Out.print("Enter a coloumn:");
    		 col = readNumber();
    		do {
    			if(isTaken(row,col)) {
    				Out.print("That cell is taken,please enter another cell:");
    				row = readNumber();
    				col = readNumber();
    			}
    		}while(isTaken(row,col));
    		board[row][col] = turn;
    		nOfTurns++;
    		Out.println();
    		Out.println("Turn: "+ nOfTurns);
    		if(gameOver(row,col)) {
    			playing = false;
    			Out.println("Player" +" "+turn +" "+"has won!");
    		}
    		printBoard();
    		if(!gameOver(row,col) && isBoardFull()) {
    			Out.println("Draw!");
    			playing = false;
    		}
    		if(turn == 'X') {
    			turn = 'O';
    		}else{
    			turn = 'X';
    		}
     
    }
    }
    public static boolean gameOver(int rowMove,int colMove) {
    	if(board[0][colMove] == board[1][colMove] && board[0][colMove] == board[2][colMove]){
    		return true;
    	}else if(board[rowMove][0] == board[rowMove][1] && board[rowMove][0] == board[rowMove][2]) {
    		return true;
    	}else if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != '_') {
    		return true;
    	}else if(board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '_') {
    		return true;
    	}else {
    		return false;
    }
     
    }
    public static int readNumber() {
    	boolean isValid = false;
    	int number;
    	do {	
     
    	number = In.readInt() - 1;
     
    	if (!In.done()) {
            Out.println("Input must be a valid number");
            In.readLine(); 
          } else if (number < 0 || number > 2) {
            Out.println("The number is not in the specified range: 1-3");
          } else {
            isValid = true;
          }
        } while (!isValid);
     
        return number;
      }
     
    public static boolean isTaken(int row, int col) {
    	return board[row][col] != '_';
    }
     
    public static boolean isBoardFull() {
            boolean isFull = true;
     
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (board[i][j] == '-') {
                        isFull = false;
                    }
                }
            }
     
            return isFull;
        }
     
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Checking for draw in TicTacToe

    how is that gonna help?
    It forced you to rewrite the isBoardFull method.
    I see that more changes were made than just the global variables.
    What happens now when the code is executed?
    If there are problems, copy the full contents of the command prompt window and paste it here.
    Add some comments where the problems are describing what is wrong and what should be different.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Checking for draw in TicTacToe

    I've got it now, here is the code should work.Thanks Norm,once again.This is my pre-last (I dont know if that word exists but I think you get it) assignment in my
    programming class and if I get a positive Note, I will pass so wish me luck!

    Here is the code;

     
    public class TicTacToe {
    	public static char[][] board = new char[3][3];
    	public static char turn = 'X';
     
    	public static void main(String[] args) {
    		for(int i = 0; i < 3; i++) {
    			for(int j = 0; j < 3; j++) {
    				board[i][j] = '_';
    			}
    		}
    		play();
    	}
     
    	public static void printBoard() {
    	for(int i = 0; i < 3; i++) {
    		Out.println();
    		for(int j = 0; j < 3; j++) {
    			if(j == 0){
    				Out.print("| ");
    			}
    			Out.print(board[i][j] + " | ");
    		}
    	}
    	Out.println();
     
    }
     
     
    public static void play() {
    	int row,col;
    	boolean playing = true;
    	printBoard();
    	Out.println();
    	int nOfTurns = 1;
    	Out.println("Turn: " + nOfTurns);
     
    	while(playing){
    		Out.println();
    		Out.println("Player:" +" "+ turn);
    		Out.print("Enter a row:");
    		 row = readNumber();
    		Out.print("Enter a coloumn:");
    		 col = readNumber();
    		do {
    			if(isTaken(row,col)) {
    				Out.print("That cell is taken,please enter another cell:");
    				row = readNumber();
    				col = readNumber();
    			}
    		}while(isTaken(row,col));
    		board[row][col] = turn;
    		nOfTurns++;
    		Out.println();
    		Out.println("Turn: "+ nOfTurns);
    		if(gameOver(row,col)) {
    			playing = false;
    			Out.println("Player" +" "+turn +" "+"has won!");
    		}
    		printBoard();
    		if(!gameOver(row,col) && isBoardFull()) {
    			Out.println("Draw!");
    			playing = false;
    		}
    		if(turn == 'X') {
    			turn = 'O';
    		}else{
    			turn = 'X';
    		}
     
    }
    }
    public static boolean gameOver(int rowMove,int colMove) {
    	if(board[0][colMove] == board[1][colMove] && board[0][colMove] == board[2][colMove]){
    		return true;
    	}else if(board[rowMove][0] == board[rowMove][1] && board[rowMove][0] == board[rowMove][2]) {
    		return true;
    	}else if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[1][1] != '_') {
    		return true;
    	}else if(board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '_') {
    		return true;
    	}else {
    		return false;
    }
     
    }
    public static int readNumber() {
    	boolean isValid = false;
    	int number;
    	do {	
     
    	number = In.readInt() - 1;
     
    	if (!In.done()) {
            Out.println("Input must be a valid number");
            In.readLine(); 
          } else if (number < 0 || number > 2) {
            Out.println("The number is not in the specified range: 1-3");
          } else {
            isValid = true;
          }
        } while (!isValid);
     
        return number;
      }
     
    public static boolean isTaken(int row, int col) {
    	return board[row][col] != '_';
    }
     
    public static boolean isBoardFull() {
            boolean isFull = true;
     
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (board[i][j] == '_') {
                        isFull = false;
                    }
                }
            }
     
            return isFull;
        }
     
    }

Similar Threads

  1. [SOLVED] Checking for taken places in TicTacToe
    By arhzz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 6th, 2020, 05:51 PM
  2. [SOLVED] TicTacToe
    By arhzz in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2020, 04:29 PM
  3. Replies: 1
    Last Post: October 20th, 2013, 11:54 PM
  4. checking for draw by repetition in chess app
    By aisthesis in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 16th, 2011, 02:40 AM
  5. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM