Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The game TTT is a two player game where each player trades moves across a 3 x 3

ID: 3689625 • Letter: T

Question

The game TTT is a two player game where each player trades moves across a 3 x 3 game grid. One player designates their move with an X whereas the other player designates their move with an O. When one player has three of their marks placed in a line, that player has won the game. If all nine squares in the grid are filled and neither player has three symbols matching in a row, that game is considered tied.  

Consider the following game scenario between a red and a blue opponent.

Figure 1: A sample TTT configuration

In the above figure, the blue player won the game since the bottom horizontal row has three of the same symbol in each square. Please note that there are eight matching rows where victory can be determined (three horizontal, three vertical and two diagonal.)

For this exercise you will implement the same game in C++. You may use either arrays or vectors to hold moves as the game board.

Points will be awarded for this exercise as follows:

10 points

generate a game board with three randomly placed symbols

output the game board with the placement of each symbol

10 points + 10 extra credit points

A playable game of TTT, where the player trades moves with the computer opponent

(players moves can be entered by asking the user for the row and column to be placed.)

10 points + 20 extra credit points

All of the above requirements except the game should be implemented so that the player will never be able to defeat the computer opponent in a game of TTT (please note that the game can be completed to a tie.)

Explanation / Answer

import java.util.Scanner; /** * Tic-Tac-Toe: Two-player console, non-graphics, non-OO version. * All variables/methods are declared as static (belong to the class) * in the non-OO version. */ public class TTTConsoleNonOO2P { // Name-constants to represent the seeds and cell contents public static final int EMPTY = 0; public static final int CROSS = 1; public static final int NOUGHT = 2; // Name-constants to represent the various states of the game public static final int PLAYING = 0; public static final int DRAW = 1; public static final int CROSS_WON = 2; public static final int NOUGHT_WON = 3; // The game board and the game status public static final int ROWS = 3, COLS = 3; // number of rows and columns public static int[][] board = new int[ROWS][COLS]; // game board in 2D array // containing (EMPTY, CROSS, NOUGHT) public static int currentState; // the current state of the game // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON) public static int currentPlayer; // the current player (CROSS or NOUGHT) public static int currntRow, currentCol; // current seed's row and column public static Scanner in = new Scanner(System.in); // the input Scanner /** The entry main method (the program starts here) */ public static void main(String[] args) { // Initialize the game-board and current status initGame(); // Play the game once do { playerMove(currentPlayer); // update currentRow and currentCol updateGame(currentPlayer, currntRow, currentCol); // update currentState printBoard(); // Print message if game-over if (currentState == CROSS_WON) { System.out.println("'X' won! Bye!"); } else if (currentState == NOUGHT_WON) { System.out.println("'O' won! Bye!"); } else if (currentState == DRAW) { System.out.println("It's a Draw! Bye!"); } // Switch player currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS; } while (currentState == PLAYING); // repeat if not game-over } /** Initialize the game-board contents and the current states */ public static void initGame() { for (int row = 0; row
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote