//Tic Tac Toe
#include
#include
using namespace std;
char chessboard[5][11]; //用于打印棋盘
int chess[3][3]; //用于存放落子位置的数组
void Initialize_the_chessboard() //初始化棋盘
{
for (int i = 0; i < 5; i++)
for (int j = 0; j < 11; j++)
chessboard[i][j] = ' '; //先将所有棋盘初始化为空格
for (int i = 0; i < 5; i++)
{
chessboard[i][3] = '|';
chessboard[i][7] = '|'; //初始化两列竖线
}
for (int i = 0; i < 11; i++)
{
chessboard[1][i] = '-';
chessboard[3][i] = '-'; //初始化两条横线
}
chessboard[1][3] = '+', chessboard[1][7] = '+';
chessboard[3][3] = '+', chessboard[3][7] = '+'; //初始化竖线与横线交叉的地方
int k = 1;
for (int i = 0; i < 5; i += 2)
for (int j = 1; j < 11; j += 4)
{
chessboard[i][j] = k + '0'; //填入数字
k++;
}
}
void Print_the_chessboard() //打印棋盘
{
system("cls"); //打印之前先清屏
//打印棋盘头部
printf("%20s", "Tic Tac Toe"); //打印题目
cout << '\n';
cout << "Player1 (X) - Player2 (O)"; //两位玩家
cout << '\n';
//打印棋盘主体, 用循环结构完成即可
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 11; j++)
cout << chessboard[i][j];
cout << endl;
}
}
int judge(int s) //判断输入数字对应位置是否存在数字
{
if (s <= 0 || s >= 10)
return 0;
int i, j, k = 1;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
if (k == s)
goto A; //如果此处使用"break"来终止循环的话只能终止第一层循环, 所以用"goto"语句直接跳出循环
k++;
}
A: if (chess[i][j] != 0)
return -1;
return 1;
}
int check() //判断是否获胜
{
int flag = 2, flag2 = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (chess[i][j] == 0)
flag2++; //判断还有几个空格
for (int i = 0; i < 3; i++) //判断行是否相等
if (chess[i][0] == chess[i][1] && chess[i][0] == chess[i][2] && chess[i][1] == chess[i][2])
flag = chess[i][0];
for (int i = 0; i < 3; i++) //判断列是否相等
if (chess[0][i] == chess[1][i] && chess[0][i] == chess[2][i] && chess[1][i] == chess[2][i])
flag = chess[0][i];
if (chess[0][0] == chess[1][1] && chess[0][0] == chess[2][2] && chess[1][1] == chess[2][2]) //判断对角线是否相等
flag = chess[0][0];
if (chess[0][2] == chess[1][1] && chess[0][2] == chess[2][0] && chess[1][1] == chess[2][0]) //判断对角线是否相等
flag = chess[0][2];
if (flag2 == 0 && flag == 2)
return 3; //空格为零, 且没有获胜者
else
return flag; //flag返回值的可能值为:1,-1,0,2
}
void Player(int x)
{
if(x==1)
cout << "Player1, enter a number:";
else
cout<< "Player2, enter a number:";
char c;
int s;
cin >> c; //输入
if (c <= '0' || c > '9') //判断输入是否合法
{
cout << "Your enter is not a number, please try again.\n";
Player(x);
}
else
{
s = c - '0'; //将字符转化为数字
int a = judge(s);
if (a == 0)
{
cout << "Your number is out of range, please try again.\n";
Player(x);
}
else if (a == -1)
{
cout << "Your number has been occupied, please try again.\n";
Player(x);
}
else if (a == 1)
{
int i, j, k = 1,num;
char ch;
if(x==1)
num=1,ch='X';
else
num=-1,ch='O';
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
if (k == s)
{
chess[i][j] = num;
goto B;
}
k++;
}
B: k = 1;
for (i = 0; i < 5; i += 2)
for (j = 1; j < 11; j += 4)
{
if (k == s)
{
chessboard[i][j] = ch;
return;
}
k++;
}
}
}
}
int main()
{
Initialize_the_chessboard();
Print_the_chessboard();
int p = 1;
while (true)
{
Player(p % 2);
Print_the_chessboard();
p++;
int a = check();
if (a == 1)
{
cout << "The winner is Player1.";
break;
}
else if (a == -1)
{
cout << "The winner is Player2.";
break;
}
else if (a == 3)
{
cout << "The game is over. No one is a winner.";
break;
}
else
continue;
}
system("pause");
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)