#include
#define ROW 3
#define COL 3
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void gamer_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断谁赢
char is_win(char board[ROW][COL], int row, int col);
老规矩,废话补多少先看代码:
void menu()
{
printf("******************\n");
printf("******1.play******\n");
printf("******0.exit******\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
//菜单
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏");
break;
case 1:
game();
break;
default:
break;
}
} while (input);
return 0;
}
先看代码:
//初始化数组
void init_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 display_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 gamer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
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("该位置已有棋子,请重新选择");
}
}
else
{
printf("坐标不合法,请重新输入");
}
}
}
先看代码:
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col)
{
printf("电脑下棋:>\n");
while (1)
{
int x = rand() % row; //0-2
int y = rand() % col; //0-2
//判断棋盘位置是否为空
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
先看代码:
//判断谁赢
//1.玩家赢 - "*"
//2.电脑赢了 - "#"
//3.平局了 - "Q"
//4.游戏继续 - "C"
char is_win(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][1] != ' ')
{
return board[i][1];
}
}
//列
int j = 0;
for (j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
}
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
//没有人赢,就要平局
if (is_full(board, row, col))
{
return 'Q';
}
//游戏继续
return 'C';
}
//判断平局
//如果棋盘满了,返回1
//不满,返回0
int is_full(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++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
先看代码:
void game()
{
//数组的存储需要一个3*3的二维数组
char board[ROW][COL] = {0};
char ret = 0;
//初始化棋盘(数组)
init_board(board, ROW, COL);
//展示棋盘
display_board(board, ROW, COL);
//玩游戏
while (1)
{
//玩家下棋
gamer_move(board, ROW, COL);
//判断棋局
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
//电脑下棋
computer_move(board, ROW, COL);
//判断棋局
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家赢了\n");
}
else if (ret == '#')
{
printf("电脑赢了\n");
}
else if(ret == 'Q')
{
printf("平局了\n");
}
display_board(board, ROW, COL);
}