Hello! I have this code here
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 = In.readInt() -1; Out.print("Enter a coloumn:"); col = In.readInt() -1; 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; }
And it works fine.Now I want to implement a "safety" mechanicsm in the code.What I mean is I want to make sure that no wrong inputs (regarding rows and coloumns) can be made. So the range is from 1 - 3 and I want when the users puts in lets say 4, a message pops on on the screen saying that that is not a valid input. I want to execute this WITHOUT exceptions.I was thinking of creating a method (boolean) that will take an input compare it to a lower and upperbound and return true or false accordingly.My only issue is how will I implement that in the play method. Any help?
Thanks!