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 does the following:
If a player wins, the program should immediately declare that player the winner and end. If a tie has occurred, the program should display an appropriate message and end. (You probably already know this, but just in case. 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. Player 2 wins when there are three Os in a row on the game board. The Os 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: Do not use global variables, except for global constants! You must create and use at least two functions, but are encouraged to modularize as much as possible. Each function must be named appropriately and obvious to what it does. The main function should have little more than an array for the board and call to the various functions. You should also define constants for the number of rows and columns and use them throughout the program. Make sure to check for valid input and ask again if necessary. Keep it simple and keep it clean! Here are some functions (not a complete list!) that I would suggest.
void displayBoard(char board[][COLS]); void playerTurn(char board[][COLS], char player); bool checkForWinner(char board[][COLS], char player); bool isBoardFull(char board[][COLS]); The output should look something like this: Columns 1 2 3 Row 1: * * * Row 2: * * * Row 3: * * * Player X's turn. Enter a row and column to place an X. Row: 1 Column: 2 Columns 1 2 3 Row 1: * X * Row 2: * * * Row 3: * * * Player O's turn. Enter a row and column to place an O. Row: 1 Column: 2 That location is not available. Select another location. Row: 4 Invalid Row! Row: 2 Column: 3 Columns 1 2 3 Row 1: * X * Row 2: * * O Row 3: * * * Player X's turn. Enter a row and column to place an X. Row: 3 Column: 1 Columns 1 2 3 Row 1: * X * Row 2: * * O Row 3: X * * Player O's turn. Enter a row and column to place an O. Row: 2 Column: 1 Columns 1 2 3 Row 1: * X * Row 2: O * O Row 3: X * * Player X's turn. Enter a row and column to place an X. Row: 2 Column: 2 Columns 1 2 3 Row 1: * X * Row 2: O X O Row 3: X * * Player O's turn. Enter a row and column to place an O. Row: 1 Column: 3 Columns 1 2 3 Row 1: * X O Row 2: O X O Row 3: X * * Player X's turn. Enter a row and column to place an X. Row: 3 Column: 2 Columns 1 2 3 Row 1: * X O Row 2: O X O Row 3: X X * Player 1 (X) WINS!!!!!
Code
#include<iostream>
using namespace std;
const int ROWS=3;
const int COLS=3;
void displayBoard(char board[][COLS]);
void playerTurn(char board[][COLS], char player);
bool checkForWinner(char board[][COLS], char player);
bool isBoardFull(char board[][COLS]);
void fillBoard(char board[][COLS]);
int main()
{
char board[ROWS][COLS];
fillBoard(board);
displayBoard(board);
char playerX='X';
char playerO='O';
while(!isBoardFull(board))
{
cout<<"\n\nPlayer X's turn:
"<<endl;
playerTurn(board,playerX);
displayBoard(board);
if(checkForWinner(board,playerX))
{
cout<<"Player 1 (X) WINS!!!!!"<<endl;
return 1;
}
cout<<"\n\nPlayer O's turn:
"<<endl;
playerTurn(board,playerO);
displayBoard(board);
if(checkForWinner(board,playerO))
{
cout<<"Player 2 (O) WINS!!!!!"<<endl;
return 1;
}
}
cout<<"Game was
tie!!!"<<endl<<endl;
return 1;
}
bool checkForWinner(char board[][COLS], char player)
{
bool win=false;
for(int i=0;i<3;i++)
{
if(board[i][0]==player
&&board[i][1]==player && board[i][2]==player)
{
win=true;
return
win;
}
if(board[0][i]==player
&&board[1][i]==player && board[2][i]==player)
{
win=true;
return
win;
}
}
if(board[0][0]==player && board[1][1]==player
&& board[2][2]==player)
{
win=true;
return win;
}
if(board[0][2]==player && board[1][1]==player
&& board[2][0]==player)
{
win=true;
return win;
}
return win;
}
void playerTurn(char board[][COLS],char player)
{
int row,col;
cout<<"Enter a row and column to place an
"<<player<<"."<<endl;
while(true)
{
while(true)
{
cout<<"Row: ";
cin>>row;
if(row>=1
&& row<=3)
break;
}
while(true)
{
cout<<"Column: ";
cin>>col;
if(col>=1
&& col<=3)
break;
}
if(board[row-1][col-1]=='*')
{
board[row-1][col-1]=player;
break;
}
else
cout<<"That postion is already taken! Try
Again."<<endl;
}
}
void fillBoard(char board[][COLS])
{
for(int i=0;i<ROWS;i++)
for(int j=0;j<COLS;j++)
board[i][j]='*';
}
void displayBoard(char board[][3])
{
cout<<" Columns"<<endl;
cout<<" 1 2 3"<<endl;
for(int i=0;i<ROWS;i++)
{
cout<<"Row
"<<(i+1)<<" ";
for(int j=0;j<COLS;j++)
{
cout<<board[i][j]<<" ";
}
cout<<endl;
}
}
bool isBoardFull(char board[][COLS])
{
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<COLS;j++)
{
if(board[i][j]=='*')
return false;
}
}
return true;
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.
Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...
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...
Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. 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...
I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String 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: a. Displays the contents of the board array. b. Allows player 1 to select a location...
Use Java language to create this program Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below): 1 2 3 4 5 6 7 8 9 The program should run a loop that Displays the contents of the board array allows player 1 to select the...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
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...
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...
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...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
(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...