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 7 of 7

Thread: Checking for taken places in TicTacToe

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

    Default Checking for taken places in TicTacToe

    Hey, so another post in regards of the TicTacToe game. I want to implement yet another "safety" mechansim that should do the following; When a field on the board, lets say 1 and 1 (so the very upper left corner) is already taken or "filled" and the user tries to put X or O in that field I want to print out a message saying that the chosen field is taken. How could I implement something like this. I was thinking of another boolean method that has a variable lets say taken that is set to false,and after checking for the field the value of that variable changes accordingly, and print out the message if necessary.But this is just a rough idea out of my head, a more clear way of doing it in short guidlines would be of great help.Here is the code


     
    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();
    		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(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;
      }
     
     
    }

    Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking for taken places in TicTacToe

    boolean method that ... say taken
    That sounds reasonable.
    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 taken places in TicTacToe

    Okay so this is what I've got, it prints out the message if the same cell is initialized,but it also changes the value of the cell meaning if it was X it will give out the message but it wont stay X it will be O.How to prevent this? This is the code

    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();
    		if(isTaken(row,col)) {
    			Out.println("That cell is taken");
    		}
    		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(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] != '_';
    }
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking for taken places in TicTacToe

    Now you need the same kind of loop that was used in the readNumber method. Don't exit the loop until the user has chosen a valid square.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Checking for taken places in TicTacToe

    So I need a do while loop? I'll try it.


    EDIT: So I've tried it but now,after my first Input the program just stops.

     
    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.println("That cell is taken");
    			}
    		}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(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] != '_';
    }
     
    }
    Last edited by arhzz; June 6th, 2020 at 05:37 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking for taken places in TicTacToe

    When will the method change what value it returns? If its args are always the same, it should return the same value, forever.
    The loop needs to include code that changes the values of the args passed to the method so the method has a chance to return a different value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (June 6th, 2020)

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

    Default Re: Checking for taken places in TicTacToe

    Okay, I'll see what I can do

    EDIT: Didnt help, maybe I got it wrong?

     
    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.println("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(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] != '_';
    }
     
    }

    EDIT: Need to change the do while loop,the part where the while loop should end while(isTaken(row,cel)) without the negation.Works now.

    Thanks Norm, your the man!
    Last edited by arhzz; June 6th, 2020 at 05:59 PM.

Similar Threads

  1. [SOLVED] TicTacToe
    By arhzz in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2020, 04:29 PM
  2. TicTacToe error
    By monikathelover1999 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 24th, 2019, 03:52 PM
  3. Tictactoe with out GUI
    By Andile in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2012, 03:49 PM
  4. TicTacToe
    By Zerro900 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2011, 08:29 AM
  5. Tictactoe problem
    By Nostromo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 03:38 PM