#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
#define ROW 3
#define COL 3
void chushihua_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void dayin_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家走,请输入坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//下棋
//看是否被占用
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标违法,请重新输入\n");
}
}
else
{
printf("坐标违法,请重新输入\n");
}
}
}
void computermove(char board[ROW][COL], int row, int col)
{
printf("电脑走:>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (x >= 0 && x < row && y >= 0 && y < col)
{
//下棋
//看是否被占用
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
else
{
printf("坐标违法,请重新输入\n");
}
}
}
int is_full(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[0][0];
}
int ret = is_full(board, row, col);
if (ret == 1)
return 'Q';
return 'c';
}
//菜单
void menu()
{
printf("********************************************************\n");
printf(" **************** 0.exit **************** \n");
printf(" **************** 1.play **************** \n");
printf("********************************************************\n");
}
//游戏函数
void game()
{
//1.创建棋盘
char board[ROW][COL];
//2.初始化棋盘
chushihua_board(board, ROW, COL);
//3.打印棋盘
dayin_board(board, ROW, COL);
char ret = 0;
while (1)
{
//4,玩家下棋
playermove(board, ROW, COL);
dayin_board(board, ROW, COL);
//判断输赢,是否继续
ret=Iswin(board, ROW, COL);
if (ret != 'c')
break;
//5.电脑下棋
computermove(board, ROW, COL);
dayin_board(board, ROW, COL);
ret=Iswin(board, ROW, COL);
if (ret != 'c')
break;
}
printf("********************************\n");
if (ret == '*')
{
printf("玩家赢!\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else if(ret=='Q')
{
printf("平局\n");
}
printf("********************************\n");
dayin_board(board, ROW, COL);
printf("********************************\n");
}
//主函数
int main()
{
int input = 0;
srand(time(0));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
printf("\n");
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,重新选择:>");
break;
}
} while (input);
}
可以自己玩玩。