C++ Tic-Tac-Toe Game Write a program that allows two players to play a game of t
ID: 3763802 • Letter: C
Question
C++
Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that • Displays the contents of the board array • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. • Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. • Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner. Note: You may declare the winner if you cannot figure out how to have the computer figure it out.
Explanation / Answer
import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7', '8', '9'}}; // assign player to char value of X's only int play; char player = 'X'; char cpu = 'O'; int rowNumber; int columnNumber; int playerORow; int playerOcolumn; System.out.println("Welcome to tic, tac, toe! "); System.out.println("Do you wish to play? 1 for yes, 2 for no "); Scanner input = new Scanner(System.in); play = input.nextInt(); if(play != 1) { System.out.print("Invalid input, game will now EXIT thanks for playing!"); System.exit(0); } // end if displayBoard(board); System.out.println("You are limited to X's only, good luck!"); System.out.println("Please enter row (0-3) of your move: "); rowNumber = input.nextInt(); System.out.println("Please enter column (1-3); of your move: "); columnNumber = input.nextInt(); System.out.println("Please enter row (0-3) for player O: "); playerORow = input.nextInt(); System.out.println("Please enter column (1-3); of your move: "); playerOcolumn = input.nextInt(); if(board[rowNumber][columnNumber] != 'X' && board[rowNumber][columnNumber] != 'O') { board[rowNumber][columnNumber] = player; } // end if else { } makeAMove(board, player); hasWon(board, player); boardFull(board); } // end main method // displays only the tic tac toe board public static void displayBoard(char[][] board) { // loop for each row System.out.println(board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " ---------"); System.out.println(board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " ---------"); System.out.println(board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " "); } // end display board method // takes board array of values and updates it with valid row and column selected by player..does not return anything public static void makeAMove(char[][] board, char player) { displayBoard(board); } // end makeAMove method // compare each element in board to see if the char value of 'X' exists // if exists then then return true, else return false public static boolean hasWon(char[][] board, char player) { // Check if the player has won by checking winning conditions. if (board[0][0] == player && board[0][1] == player && board[0][2] == player || // 1st row board[1][0] == player && board[1][1] == player && board[1][2] == player || // 2nd row board[2][0] == player && board[2][1] == player && board[2][2] == player || // 3rd row board[0][0] == player && board[1][0] == player && board[2][0] == player || // 1st col. board[0][1] == player && board[1][1] == player && board[2][1] == player || // 2nd col. board[0][2] == player && board[1][2] == player && board[2][2] == player || // 3rd col. board[0][0] == player && board[1][1] == player && board[2][2] == player || // Diagonal board[2][0] == player && board[1][1] == player && board[0][2] == player) // Diagonal / return true; else { return false; } } // end hasWon method public static boolean boardFull(char [][] board) { if (board[0][0] != '1' && board[0][1] != '2' && board[0][2] != '3' && board[1][0] != '4' && board[1][1] != '5' && board[1][2] != '6' && board[2][0] != '7' && board[2][1] != '8' && board[2][2] != '9') return true; else { return false; } // end else }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.