Untitled
unknown
java
2 years ago
14 kB
4
Indexable
package Labs.Assignment8; import java.util.*; public class QuestionEightPointNine { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); /*Create an empty 3x3 array for the board */ char[][] board = new char[3][3]; char [][] gameBoard = emptyBoard(board); System.out.println("'Welcome to this game of tic-tac-toe."); printBoard(gameBoard); System.out.println("PlayerOne please select what letter you are going to be."+ "Either UPPERCASE X or UPPERCASE O"); char [] tokens = new char[2]; char playerOne; do { /*#this is a do while loop that keeps asking for a input and then checks the input until player one is either x or o */ playerOne = scanner.nextLine().charAt(0); checkTokenPlayerOne(playerOne); }while(playerOne != 'X' & playerOne!='O'); tokens[0] = playerOne; char playerTwo = checkTokenPlayerTwo(playerOne); tokens[1] = playerTwo; boolean check1; boolean check2; int moveCount =1; while(moveCount<=2) { /*this while loop does the following: * 1. Runs two do while loops until both players have made 2 moves * 2. its not possible to win the game after two moves */ do { /*this do while loop does the following : * 1. prompts player one to make a move * 2. takes in the row and column using scanner * 3. checks if it is a valid move * 4. if it is a valid move changes the gameboard * 5. prints the gameboard * 6. if it is not a valid move it repeats the while loop until it is */ System.out.println("Player " + tokens[0] + " it your turn to make a move!"); System.out.println("Enter a row(0, 1, or 2) for player " + tokens[0] + ": "); int rowPostion = scanner.nextInt(); System.out.println("Enter a column(0, 1, or 2) for player " + tokens[0] + ": "); int columnPostion = scanner.nextInt(); check1 = isValidMove(rowPostion,columnPostion, gameBoard); if(check1 == true) { gameBoard[rowPostion][columnPostion] = tokens[0]; printBoard(gameBoard); } }while(check1==false); do { /*this do while loop does the following : * 1. prompts player two to make a move * 2. takes in the row and column using scanner * 3. checks if it is a valid move * 4. if it is a valid move changes the gameboard * 5. prints the gameboard * 6. if it is not a valid move it repeats the while loop until it is */ System.out.println("Player " + tokens[1] + " it your turn to make a move!"); System.out.println("Enter a row(0, 1, or 2) for player " + tokens[1] + ": "); int rowPostion = scanner.nextInt(); System.out.println("Enter a column(0, 1, or 2) for player " + tokens[1] + ": "); int columnPostion = scanner.nextInt(); check2 = isValidMove(rowPostion,columnPostion, gameBoard); if(check2 == true) { gameBoard[rowPostion][columnPostion] = tokens[1]; printBoard(gameBoard); } }while(check2==false); moveCount+=1; } boolean check; int result; do { System.out.println("You guy have reached the 3rd move"); // Prints game board board printBoard(gameBoard); System.out.println("Player " + tokens[0] + " it your turn to make a move!"); System.out.println("Enter a row(0, 1, or 2) for player " + tokens[0] + ": "); int rowPostion = scanner.nextInt(); System.out.println("Enter a column(0, 1, or 2) for player " + tokens[0] + ": "); int columnPostion = scanner.nextInt(); check = isValidMove(rowPostion,columnPostion, gameBoard); if(check == true) { gameBoard[rowPostion][columnPostion] = tokens[0]; } // Determine game status: win,draw or continue result = gameStatus(board, tokens[0]); // If status is continue make next player take turn if (result == 2) { char temp = tokens[0]; tokens[0] = tokens[1]; tokens[1] = temp; } } while(result == 2); printBoard(gameBoard); if (result == 0) { System.out.println(); System.out.printf( "Player %c has won the game", tokens[0]); } else System.out.println("Players draw"); } public static int gameStatus(char[][] gameBoard, char token) { if (isWin(gameBoard,token)) { return 0; /* Game is over and someone won */ } else if (isDraw(gameBoard)) { return 1; /* Game is over and it is a draw */ } else { return 2; /* Game continues */ } } public static boolean isWin(char[][] gameBoard, char token) { return horizontalWin(gameBoard, token) || verticalWin(gameBoard, token) || diagonalWin(gameBoard, token); } public static boolean horizontalWin(char[][] gameBoard , char token) { /* This function checks if the given token has a horizontal win in the gameBoard 2D array. It does this by iterating over each row in the game board, counting the number of occurrences of token in each row, and checking if the count is equal to 3. If it is, the function returns true indicating that there is a horizontal win. If none of the rows have 3 tokens, the function returns false.*/ for (int i = 0; i < gameBoard.length; i++) { int count = 0; for (int j = 0; j < gameBoard[i].length; j++) { if (gameBoard[i][j] == token) { count++; } } if (count == 3) { return true; } } return false; } public static boolean verticalWin(char[][] gameBoard, char token) { /*This function checks if the given token has a vertical win in the gameBoard 2D array. It does this by iterating over each column in the game board, counting the number of occurrences of token in each column, and checking if the count is equal to 3. If it is, the function returns true indicating that there is a vertical win. If none of the columns have 3 tokens, the function returns false. */ for (int i = 0; i < gameBoard.length; i++) { int count = 0; for (int j = 0; j < gameBoard[i].length; j++) { if (gameBoard[j][i] == token) { count++; } } if (count == 3) { return true; } } return false; } public static boolean diagonalWin(char[][] gameBoard, char token) { /*This function checks if the given token has a diagonal win in the gameBoard 2D array. It does this by checking both diagonals of the game board for the presence of the token. It checks the first diagonal by iterating over the elements of the main diagonal, counting the number of occurrences of token, and checking if the count is equal to 3. If it is, the function returns true indicating that there is a diagonal win. Next, it checks the second diagonal by iterating over the elements in a reverse manner, counting the number of occurrences of token, and checking if the count is equal to 3. If it is, the function returns true indicating that there is a diagonal win. If none of the diagonals have 3 tokens, the function returns false. */ int count = 0; for (int i = 0; i < gameBoard.length; i++) { if (gameBoard[i][i] == token) { count++; } if (count == 3) { return true; } } count = 0; for (int i = 0, j = gameBoard[i].length - 1; j >= 0 ; j--, i++) { if (gameBoard[i][j] == token) { count++; } if (count == 3) { return true; } } return false; } public static boolean isDraw(char[][] gameBoard) { /*This function checks if the gameBoard 2D array represents a draw. It does this by iterating over each element in the game board and checking if there is an empty space represented by the character ' '. If there is an empty space, the function returns false indicating that the game is not a draw. If all elements have been checked and there are no empty spaces, the function returns true indicating that the game is a draw. */ for (int i = 0; i < gameBoard.length; i++) { for (int j = 0; j < gameBoard[i].length; j++) { if (gameBoard[i][j] == ' ') { return false; } } } return true; } public static boolean isTaken(int rowPostion, int columnPostion, char[][] gameBoard) { /*checks if the postion is taken or not * if it is an empty space than we classify it as empty * if not the postion is taken */ boolean isEmpty = false; if(gameBoard[rowPostion][columnPostion] == ' ') { return isEmpty = true; } return isEmpty; } public static boolean isRowValid(int rowPostion) { /* checks if the row postion is either 0,1,2*/ int[] array = {0, 1, 2}; boolean isInArray = false; for (int element : array) { if (element == rowPostion) { isInArray = true; break; } } return isInArray; } public static boolean isColValid(int columnPostion) { /* checks if the column postion is either 0,1,2*/ int[] array = {0, 1, 2}; boolean isInArray = false; for (int element : array) { if (element == columnPostion) { isInArray = true; break; } } return isInArray; } public static boolean isValidMove(int rowPostion , int columnPostion, char[][] gameBoard) { /*This code is checking if a move made by the player is valid or not. It does this by checking if the row and column positions are within the valid range, and if the spot on the game board is already taken or not. The isRowValid and isColValid methods check if the row and column positions are within the bounds of the game board, respectively. The isTaken method checks if the specified position on the game board is already occupied. If both the row and column positions are valid, and the spot on the game board is not taken, the method returns true to indicate that the move is valid. Otherwise, it returns false. */ boolean isValid = false; boolean rowValidity = isRowValid(rowPostion); boolean colValidity = isColValid(columnPostion); if(rowValidity == true & colValidity == true) { boolean taken = isTaken(rowPostion,columnPostion,gameBoard); if(taken == true) { isValid = true; } } return isValid; } public static char checkTokenPlayerTwo(char playerOne) { /*#This is a function that checks whether the input of player one is either upercase X or upercase 0 and returns the opposite token for player two*/ char playerTwo; if(playerOne == 'X') { return playerTwo = 'O'; } else { return playerTwo = 'X'; } } public static void checkTokenPlayerOne(char playerOne) { /*#This is a function that checks whether the input of player one is either upercase X or upercase 0 */ if(playerOne == 'X'| playerOne == 'O') { System.out.println(); System.out.printf("Player One has selected %c", playerOne); System.out.println(); } if(playerOne != 'X' & playerOne != 'O') { System.out.println(); System.out.println("PlayerOne please select either UpperCase X or UpperCase O"); } } public static char[][] emptyBoard(char[][] board) { /** emptyBoard returns a 3x3 array filled with blank spaces */ for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i][j] = ' '; } } return board; } public static void printBoard(char[][] gameBoard) { /*this fucntion take in the gameBoard and prints it */ System.out.println("\n-------"); for (int i = 0; i < gameBoard.length; i++) { for (int j = 0; j < gameBoard[i].length; j++) { System.out.print("|" + gameBoard[i][j]); } System.out.println("|\n-------"); } } }
Editor is loading...