这是我的代码:
#include<iostream>
using namespace std;
void drawBoard();
void startGame();
void player2();
void limit();
char turn;
char turn1;
char board[9] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' };
void main()
{
cout << endl << " *** TIC-TAC-TOE ***" << endl
<< "--------------------------------------------------------------------------------" << endl;
cout << "**Start the game please**\n" << endl;
cout << "\t\t\tPlayer 1 <x> - Player 2 <o>" << endl << endl;
drawBoard();
limit();
system("pause");
}
void drawBoard()
{
cout << "The playing board is here for you!!" << endl << endl;
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
void startGame()
{
int choice;
int cell = 0;
turn = 'x';
if (turn == 'x')
{
cout << "Player's 1 turn [x]: ";
}
else if (turn == 'o')
{
cout << "Player's 2 turn [o]: ";
}
cin >> choice;
cout << "enter the cell number: ";
cin >> cell;
if (choice == 0 && board[cell] == '0')
{
cout << " | | " << endl;
cout << " " << turn << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 1 && board[cell] == '1')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << turn << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 2 && board[cell] == '2')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << turn << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 3 && board[cell] == '3')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << turn << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 4 && board[cell] == '4')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << turn << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 5 && board[cell] == '5')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << turn << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 6 && board[cell] == '6')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << turn << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 7 && board[cell] == '7')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << turn << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 8 && board[cell] == '8')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << turn << endl;
cout << " | | " << endl;
}
else
{
cout << "This cell number is already used! Enter the number again.";
}
}
void player2()
{
int choice;
int cell = 0;
turn1 = 'o';
if (turn1 == 'x')
{
cout << "Player's 1 turn [x]: ";
}
else if (turn1 == 'o')
{
cout << "Player's 2 turn [o]: ";
}
cin >> choice;
cout << "enter the cell number: ";
cin >> cell;
if (choice == 0 && board[cell] == '0')
{
cout << " | | " << endl;
cout << " " << turn1 << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 1 && board[cell] == '1')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << turn1 << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 2 && board[cell] == '2')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << turn1 << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 3 && board[cell] == '3')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << turn1 << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 4 && board[cell] == '4')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << turn1 << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 5 && board[cell] == '5')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << turn1 << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 6 && board[cell] == '6')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << turn1 << " | " << board[7] << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 7 && board[cell] == '7')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << turn1 << " | " << board[8] << endl;
cout << " | | " << endl;
}
else if (choice == 8 && board[cell] == '8')
{
cout << " | | " << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << turn1 << endl;
cout << " | | " << endl;
}
else
{
cout << "This cell number is already used! Enter the number again." << endl;
}
}
void limit()
{
for (int i = 1; i <= 4; i++)
{
startGame();
player2();
}
}
它有效,但我遇到了一些困难。当everey玩家轮到我时,我无法恢复棋盘。我该如何恢复价值?
答案 0 :(得分:0)
每次搬家后都要更新电路板
board[choice]=turn;
然后显示电路板。