程序运行结果如下:
本次代码,实现一个简易的三子棋游戏,基本思路是:打印菜单——选择游戏——打印初始棋盘——玩家下棋——电脑随机下棋(利用rand(srand)的库函数)——判断(棋盘是否的格子是否为空,判断哪一方赢)——结束游戏。
代码为下:
game.h
#ifndef _GAME__H__
#define _GAME__H__
#include
#include
#include
#include
#define ROWS 3 //行
#define COLS 3//列
void init_board(char board[ROWS][COLS],int row,int col); //初始化棋盘
void display_board(char board[ROWS][COLS],int row,int col); //打印棋盘
void player_move(char board[ROWS][COLS], int row, int col); //玩家移动
void compute_move(char board[ROWS][COLS], int row, int col); //电脑移动
char check_win(char board[ROWS][COLS], int row, int col); //判断输赢
#endif \\_GAME__H__
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char board[ROWS][COLS], int row, int col)
{
memset(board,' ',col*row*sizeof(char));
}
void display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i != 2)
printf("---|---|---\n");
}
}
void player_move(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标: ");
scanf("%d%d", &x, &y);
x--;
y--;
if (((x >= 0) && (x <= 2)) && ((y >= 0) && (y <= 2)))
{
if (board[x][y] == ' ')
{
board[x][y] = 'X';
break;
}
else
{
printf("坐标错误,请重新输入!");
}
}
else
{
printf("坐标错误,请重新输入!");
}
}
}
void compute_move(char board[ROWS][COLS], int row, int col)
{
while (1)
{
int x = rand() % 3;
int y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
static int is_full(char board[ROWS][COLS], 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;
}
char check_win(char board[ROWS][COLS], 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];
}
for (i = 0; i < col; i++)
{
if ((board[0][i] == board[1][i])
&& (board[1][i] == board[2][i])
&& (board[1][i] != ' '))
return board[1][i];
}
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 ' ';
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*********************\n");
printf("****1.play 0.exit****\n");
printf("*********************\n");
}
void game()
{
char board[ROWS][COLS]={ 0 };
int ret = '0';
init_board(board, ROWS, COLS);//初始化棋盘
display_board(board, ROWS, COLS);//打印棋盘
srand((unsigned int)time(NULL));
while (1)
{
player_move(board, ROWS, COLS);
ret = check_win(board, ROWS, COLS);
if (ret != ' ')
break;
display_board(board, ROWS, COLS);
compute_move(board, ROWS, COLS);
if (ret != ' ')
break;
display_board(board, ROWS, COLS);
}
if (ret == 'X')
printf("玩家赢\n");
else if (ret == '0')
printf("电脑赢\n");
else if (ret == 'q')
printf("平局\n");
display_board(board, ROWS, COLS);
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择: ");;
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}