In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game.
The above sounds very similiar to the basic Tic Tac Toe. Now here is where the strategic twist comes into play.
Please fill in the empty code in the following code provided.
#include #include using namespace std; /* Check whether there's a a horizontal, vertical, or diagonal row marked * Parameter: player - player id, 1 or 2 * board[][] - a 3x3 board * Return: true - if the player win this 3x3 board * false - if not */ bool checkWin(int player, const int board[][3]){ // Please fill in your codes here. } /* Check whether the board is full or not * The game board is full is all its grids are marked * Parameter: board[][] - a 3x3 board * Return: true - if all grids are not equal to 0 * false - if there exists at least one grid that's equal to 0 */ bool isBoardFull(const int board[][3]){ // Please fill in your codes here. } /* Check whether the input position x, y are valid or not * Valid means 1 - x, y are within the boundary * 2 - this position in the game board is empty (equal to 0) * Parameter: x, y - position the player want to choose * lower_x, upper_x, lower_y, upper_y - the boundary that x, y cannot exceed * board[][] - the whole 9x9 game board * Return: true - if x, y satisfy both requirements * false - if x, y violate at least one requirement */ bool isInputValid(int x, int y, int lower_x, int upper_x, int lower_y, int upper_y, const int board[][9]){ // Please fill in your codes here. } /* This function is used to decide which super grid player can mark given his/her opponent's move * The player can only mark 1 - the super grid with bottem left and top right boundary * 2 - anywhere unoccupied if the super grid has been marked * Parameter: superBoard - the 3x3 super tic tac toe x, y - position the opponent mark on the game board * lower_x, lower_y - the bottom left boundary the player can mark * upper_x, upper_y - the top right boundary the player can mark * Return: No Return but should change lower_x, lower_y, upper_x and upper_y */ void updateBoundary(const int superBoard[][3], int x, int y, int& lower_x, int& lower_y, int& upper_x, int& upper_y){ // Please fill in your codes here. } // This function is used to visualize the board, // as ' ' denote empty, 'X' - player 1, '0' - player 2, '#' denote this part draw for both players. void printBoard(const int board[][9], int boardsize){ // symbol used in the game // ' ' - empty grid, 'X' - player 1, '0' - player 2, '#' - draw const char symbol[4] = {' ', 'X', 'O','#'}; int i, j; cout<< " 0 1 2 3 4 5 6 7 8" << endl; cout << " |---------|---------|---------|" << endl; for(i = 0; i < boardsize; i++){ cout << i << "|"; for(j = 0; j < boardsize; j++){ cout << " " << symbol[board[i][j]] << " "; if ((j + 1)%3 == 0) cout << "|"; } cout << endl; if ((i + 1)%3 == 0) cout << " |---------|---------|---------|" << endl; } } int main(){ // initialization // game board // 0 empty grid, 1(2) occupied by player1(2), 3 draw int board[9][9] = {}; int superBoard[3][3] = {}; int tempBoard[3][3] = {}; //id of the player, 1 or 2, denotes it's who's turn int player; // game board boundary int lower_x = 0, upper_x = 8, lower_y = 0, upper_y = 8; // position to mark int x, y; // loop iterator int i, j; // Game Start! cout << "======================" << endl; cout << " Super Tic-Tac-Toe!" << endl; cout << "======================" << endl; printBoard(board, 9); // player 1 start first player = 1; // Game loop do{ cout << "Player " << player << "'s move: (" << ((player == 1)?'X':'O') << ")" << endl; cout << "Please give the grid position(row column): (range [" << lower_x << "," << upper_x << "] [" << lower_y << "," << upper_y << "])" << endl; cin >> x >> y; while(!isInputValid(x, y, lower_x, upper_x, lower_y, upper_y, board)){ cout << "Input not valid, please enter again: (range [" << lower_x << "," << upper_x << "] [" << lower_y << "," << upper_y << "])" << endl; cin >> x >> y; } // Mark this grid. board[x][y] = player; // Copy the enclosing super grid to temp board for(i = 0; i < 3; i++){ for (j = 0; j < 3; j++){ tempBoard[i][j] = board[x/3*3 + i][y/3*3 + j]; } } // After each move, check whether there is a row in this super grid. if (checkWin(player,tempBoard)){ // mark all 3 x 3 grids in a super grid for(i = (x/3*3);i < x/3*3 + 3;i++) for(j = y/3*3;j < y/3*3 + 3;j++) board[i][j] = player; // mark the super grid as this players' superBoard[x/3][y/3] = player; cout << "Player " << player << " has dominated this region!" << endl; // Check whether this player wins super tic-tac-toe if (checkWin(player,superBoard)){ cout << "Player " << player << " win the game!!!" << endl; printBoard(board, 9); system("pause"); return 0; } // Check whether draw in the whole super tic-tac-toe board // (If the overall 3x3 super board is full, but no one wins, it's a draw) if (isBoardFull(superBoard)){ cout << "Draw!" << endl; printBoard(board, 9); system("pause"); return 0; } } else{ // check whether draw in a single super grid // (If this 3x3 super grid is full, but no one wins it, it's a draw) if (isBoardFull(tempBoard)){ for(i = (x/3*3);i < x/3*3 + 3;i++) for(j = y/3*3;j < y/3*3 + 3;j++) board[i][j] = 3; superBoard[x/3][y/3] = 3; // Check whether draw in the whole super tic-tac-toe board if (isBoardFull(superBoard)){ cout << "Draw!" << endl; printBoard(board, 9); system("pause"); return 0; } } } // Update the boundary for next player updateBoundary(superBoard, x, y, lower_x, lower_y, upper_x, upper_y); printBoard(board, 9); // player switch between 1 & 2 player = player % 2 + 1; }while(true); system("pause"); return 0; }
``````
#include <iostream>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void board();
int main()
{
int player = 1,i,choice;
char mark;
do
{
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a
number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win
";
else
cout<<"==>\aGame draw";
cin.ignore();
cin.get();
return 0;
}
/***************
FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
O GAME IS OVER AND NO RESULT
****************/
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] ==
square[6])
return 1;
else if (square[7] == square[8] && square[8] ==
square[9])
return 1;
else if (square[1] == square[4] && square[4] ==
square[7])
return 1;
else if (square[2] == square[5] && square[5] ==
square[8])
return 1;
else if (square[3] == square[6] && square[6] ==
square[9])
return 1;
else if (square[1] == square[5] && square[5] ==
square[9])
return 1;
else if (square[3] == square[5] && square[5] ==
square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' &&
square[3] != '3'
&& square[4] != '4' && square[5] != '5' &&
square[6] != '6'
&& square[7] != '7' && square[8] != '8' &&
square[9] != '9')
return 0;
else
return -1;
}
/***********************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
************************/
void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl
<< endl;
cout << endl;
cout << " | | " << endl;
cout << " " << square[1] << " | " <<
square[2] << " | " << square[3] << endl;
cout << "__|_|__" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "__|_|__" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...
Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...
This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use 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 does the following: Write . 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...
Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board is a 2D array of size 3x3. The function will set an id for each cell of the board starting from 1 and stop at 9. The function prototype is void InitializeBoard(int m, int n , char board[][n]) void InitializeBoard(int m, int n , char board[][n]){ int c =1; for(int i =0; i<m; i++){ for(int j=0; j< n; j++){ board[i][j] = c+'0';...