1.棋盘

2.各自下棋

3.输赢判断

2.游戏本体的构建

# 三子棋

## 一.准备需要的函数

### 1.棋盘

①构建

``````void intiboard(char arr[ROW][COL], int row, int col)//初始化棋盘
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}``````

②美化棋盘

``````void displayboard(char arr[ROW][COL], int row, int col)//打印棋盘
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("|");
printf(" %c ", arr[i][j]);
}
printf("n");
{
for (int k = 0; k < col; k++)
{
printf("|---");
}
printf("n");
}
}
}
``````

### 2.各自下棋

①玩家下棋

``````void playermove(char arr[][COL], int row, int col)//玩家回合
{
int x = 0;
int y = 0;
printf("玩家回合n");
while (1)//一直循环直到break
{
printf("请输入你想要下的坐标n");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)//输入合法
{
if (arr[x-1][y-1] == ' ')
{
arr[x-1][y-1] = 'X';
break;
}
else printf("棋盘重复，重新输入n");
}
else printf("超出范围，请重新输入！n");
}
}``````

②电脑下棋

``````void AImove(char arr[][COL], int row, int col)//电脑回合
{
printf("AI回合n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (arr[x][y] == ' ')
{
arr[x][y] = 'O';
break;
}
}
}``````

### 3.输赢判断

``````int Full(char arr[][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (arr[i][j] == ' ')
return 0;//返回0说明棋盘没有满
}
}
return 1;//棋盘满了，平局
}
char Winner(char arr[][COL], int row, int col)//判断输赢
{
for (int i = 0; i < row; i++)
{
if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][0] != ' ')
{
return arr[i][0];
}
else if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i] && arr[0][i] != ' ')
{
return arr[0][i];
}
if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] != ' ')
{
return arr[0][0];
}
if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[1][1] != ' ')
{
return arr[1][1];
}
}
int x = Full;
if (x == 0)
{
return 'Q';
}
else return 'C';
}``````

## 二.游戏实现

1.菜单的实现

``````void menu()
{
printf("---------------------------------------n");
printf("============1.开始  2.结束=============n");
printf("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=n");
printf("----------------------------------------n");
}//游戏的主页面；``````

``````void test()//前端游戏页面和选择
{
int input = 0;
srand((unsigned int)time(NULL));//后面rand的使用
do {
printf("请输入你的选择(1/0):n");
scanf_s("%d", &input);
if (input == 1)
{
printf("三子棋游戏开始！n");
play();
}
else if (input == 0)
{
printf("结束游戏,感谢游玩n");
}
else printf("请你重新选择qaqn");
} while (input);//假如input=0也就是input为假的时候结束循环：
}``````

### 2.游戏本体的构建

``````void play()
{
char board[ROW][COL];
char ret = 0;
intiboard(board, ROW, COL);
displayboard(board, ROW, COL);
while (1)
{
playermove(board, ROW, COL);
displayboard(board, ROW, COL);
ret=Winner(board, ROW, COL);
if (ret != 'C')//棋盘满了，或者决出胜负了！
{
break;
}
AImove(board, ROW, COL);
displayboard(board, ROW, COL);
ret = Winner(board, ROW, COL);
if (ret != 'C')//棋盘满了，或者决出胜负了！
{
break;
}
}
if (ret == 'X')
{
printf("玩家获胜！n");
}
else if (ret == 'O')
{
printf("电脑获胜！n");
}
else if(ret=='Q')
printf("平局.n");
}``````

# 总结

CSDN writings/三子棋 · banbanni/banni - 码云 - 开源中国 (gitee.com)

THE END