C++ realizes tic tac toe game
- 2020-10-31 21:53:49
- OfStack
This article is an example of C++ to share the implementation of tic-tac-toe game specific code for your reference, the specific content is as follows
Initial implementation of double player input, operation of the game.
The next step is to implement the man-machine game.
#include<iostream>
using namespace std;
void Player1(void); // The player 1 The input ( operation ) function
void Player2(void); // The player 2 The input ( operation ) function
void game_judge(void); // Winning or losing judgment
void game_start(void); // The game start
int rows = 3,cols = 3; // The board size
bool win1_flag = false; // The player 1 Winning marker
bool win2_flag = false; // The player 2 Winning marker
char pieces[3][3] = {{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}};; // Initialize the
void draw(void) // Print out the checkerboard
{
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cout<<pieces[i][j];
if(j< cols-1)
cout<<" | ";
}
cout<<"\n";
if (i<rows-1)
cout <<" -- -- -- -- -- "<<endl;
}
cout <<"\n";
}
void game_start(void) // The game start
{
int n=0; // The total 3*3=9 Input a , Used to determine whether or not to fill out
bool flag = false; // The player switches flag bits
cout << " The game start "<<endl;
while(n<9 & win1_flag == false & win2_flag == false) // Missing out , Not a winner to draw, then continue the game
{
if(flag == false){ // Switch to the player 1
cout << " The player 1 The input :"<<endl;
Player1(); // The player 1 Input chess position
flag = true; // Under the 1 Step is the player 2
}
else{ // Switch to the player 2
cout << " The player 2 The input :"<<endl;
Player2(); // The player 2 Input chess position
flag = false; // Under the 1 Step is the player 1
}
game_judge(); // Determine the outcome
draw(); // Print out the checkerboard
n++; // The chessboard was added again 1 pawn
}
}
void game_judge(void) // Determine the outcome
{
for(int i=0;i<rows;i++){
if(pieces[i][0] == pieces[i][1]& pieces[i][0] == pieces[i][2] & pieces[i][0] != ' ') // Judge horizontal direction full 3 Same piece
{
if(pieces[i][0] == 'O') // It's the player 1 The chessmen are still players 2
win1_flag = true; // The player 1 win
else
win2_flag = true; // The player 2 win
}
if(pieces[0][i] == pieces[1][i]& pieces[1][i] == pieces[2][i] & pieces[0][i] != ' ') // Judge vertical direction full 3 Same piece
{
if(pieces[0][i] == 'O') // It's the player 1 The chessmen are still players 2
win1_flag = true;
else
win2_flag = true;
}
if((pieces[0][0] == pieces[1][1]& pieces[1][1] == pieces[2][2] & pieces[0][0] != ' ') // Judge oblique direction full 3 Same piece
|(pieces[0][2] == pieces[1][1]& pieces[1][1] == pieces[2][0] & pieces[2][0] != ' '))
{
if(pieces[1][1] == 'O') // It's the player 1 The chessmen are still players 2
win1_flag = true;
else
win2_flag = true;
}
}
}
void Player1(void)
{
int row0,col0;
cin>>row0>>col0; // The player 1 The input position
while(pieces[row0-1][col0-1] != ' ') // Determines if the position has a chess piece , Or there is no
{
cout<<" Incorrect input for this position , Please re-enter "<<endl;
cout<<" Please enter the number of rows and columns (1-3), Space off :";
cin >>row0>>col0; // Reenter position
}
pieces[row0-1][col0-1] = 'O'; // Place players on the board 1 pieces
}
void Player2(void)
{
int row1,col1;
cin>>row1>>col1; // The player 1 The input position
while(pieces[row1-1][col1-1] != ' ') // Determines if the position has a chess piece , Or there is no
{
cout<<" Incorrect input for this position , Please re-enter "<<endl;
cout<<" Please enter the number of rows and columns (1-3), Space off :";
cin >>row1>>col1; // Reenter position
}
pieces[row1-1][col1-1] = 'X'; // Place players on the board 2 pieces
}
int main(int argc,char** argv)
{
cout<<" In-game loading "<<endl;
draw(); // Print initial checkerboard
game_start(); // Start the game
if(win1_flag == true) // The player 1 win
cout<<" The player 1 win !"<<endl;
if(win2_flag == true) // The player 2 win
cout<<" The player 2 win !"<<endl;
if(win1_flag == win2_flag) // Did not win , A draw
cout<<" A draw !"<<endl;
return 0;
}