本文章描述了使用C语言实现三子棋游戏的思路和代码实现,适合C语言新手用来练手。相比较我上一篇博客写的扫雷游戏,三子棋游戏实现起来更为简单一些。实验环境:VS2013
本篇文章的实现代码我放在我的码云上了,有需要的小伙伴戳这里:https://gitee.com/yuan-xinyi/c-language/tree/master/japplet
三子棋游戏是使用3X3的棋盘进行游戏的,玩家和电脑轮流下棋,只要有一方的三个棋子先连成一条线即获取游戏胜利。(这里我让玩家先下棋,然后电脑下棋以此轮流下去,这里可以进行优化改进,比如让玩家和电脑轮流下棋更为公平)
实现的基本思路较为简单,大家思考一下游戏的过程就能知道。重点代码部分有四个:
代码如下(示例):
void Player_Move(char board[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
while (1)
{
printf("请输入下棋坐标:\n");
scanf("%d%d", &x, &y);
if (board[x][y] == si) //si为在头文件中定义的初始化棋盘的字符
{
board[x][y] = sp; //sp为在头文件中定义的玩家棋子代表字符
break;
}
else
{
printf("该坐标已经有棋子请重新输入!\n");
continue;
}
}
}
这里我使用随机数来生成电脑下棋的坐标(此处其实略有不妥,从游戏的公平性来说,玩家占据了优势,但是目前我还没有更好的思路)
代码如下(示例):
void Computer_Move(char board[ROWS][COLS], int row, int col)
{
while (1)
{
int x = rand() % col + 1;
int y = rand() % row + 1;
if (board[x][y] == si)
{
board[x][y] = sc;
break;
}
}
}
分为三种情况判断:行,列,交叉的两条线。
int Is_Line(char board[ROWS][COLS], int row, int col, char s)
{
int i = 0, j = 0;
for (i = 1; i <= row; i++)
{
int flag = 1; //代表是一条线
for (j = 1; j <= col; j++)
{
if (board[i][j] != s)
flag = 0;
}
if (flag == 1)
return 1;
}
for (i = 1; i <= row; i++)
{
int flag = 1; //代表是一条线
for (j = 1; j <= col; j++)
{
if (board[j][i] != s)
flag = 0;
}
if (flag == 1)
return 1;
}
if (board[1][1]== s && board[2][2]== s && board[3][3] == s)
{
return 1; //斜线
}
if (board[1][3] == s && board[2][2] == s && board[3][1] == s)
return 1;
}
int Is_Winner(char board[ROWS][COLS], int row, int col,char s)
{
if (Is_Line(board, ROW, COL, s) == 1)
return 1;
else
return 0;
}
设置time1,time2,time分别代表玩家下棋的个数,电脑下棋的个数,棋盘上的棋子总个数。当time1,time2大于等于3以后每下一次棋子都要判断是否有哪一方胜出。当time大于棋盘上能承载棋子的总个数的时候,平局。
void playgame(char board[ROWS][COLS], int row, int col)
{
int time1 = 0,time2 = 0,time = 0;
while (time <= row*col)
{
time = 0;
Player_Move(board, row, col);
time1++;
DisplayBoard(board, row, col);
if (time1 >= 3)
{
if (Is_Winner(board, row, col, sp) == 1)
{
printf("恭喜你获得胜利!\n");
break;
}
}
time = time1 + time2;
if (time == row * col)
{
printf("游戏平局!\n");
break;
}
time = 0;
Computer_Move(board, row, col);
time2++;
time = time1 + time2;
if (time == row * col)
{
printf("游戏平局!\n");
break;
}
DisplayBoard(board, row, col);
if (time2 >= 3)
{
if (Is_Winner(board, row, col, sc) == 1)
{
printf("很遗憾你输了,电脑获得了胜利!\n");
break;
}
}
}
}
game.h头文件
#include
#define ROW 3
#define COL 3
#define COLS COL+1
#define ROWS ROW+1
#define sp '*' //用户使用的代表字符
#define sc '#' //电脑使用的代表字符
#define si '0' //初始化棋盘为'0'字符
void InitBoard(char board[ROWS][COLS], int row, int col, char s); //以字符s初始化棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col); //打印棋盘
void Player_Move(char board[ROWS][COLS], int row, int col); //用户下棋
void Computer_Move(char board[ROWS][COLS], int row, int col); //电脑下棋
int Is_Winner(char board[ROWS][COLS], int row, int col); //判断谁是胜利者
void playgame(char board[ROWS][COLS], int row, int col); //游戏过程
game.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int row, int col, char s)
{
int i = 0, j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = s;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("*******************\n");
}
void Computer_Move(char board[ROWS][COLS], int row, int col)
{
while (1)
{
int x = rand() % col + 1;
int y = rand() % row + 1;
if (board[x][y] == si)
{
board[x][y] = sc;
break;
}
}
}
void Player_Move(char board[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
while (1)
{
printf("请输入下棋坐标:\n");
scanf("%d%d", &x, &y);
if (board[x][y] == si)
{
board[x][y] = sp;
break;
}
else
{
printf("该坐标已经有棋子请重新输入!\n");
continue;
}
}
}
int Is_Line(char board[ROWS][COLS], int row, int col, char s)
{
int i = 0, j = 0;
for (i = 1; i <= row; i++)
{
int flag = 1; //代表是一条线
for (j = 1; j <= col; j++)
{
if (board[i][j] != s)
flag = 0;
}
if (flag == 1)
return 1;
}
for (i = 1; i <= row; i++)
{
int flag = 1; //代表是一条线
for (j = 1; j <= col; j++)
{
if (board[j][i] != s)
flag = 0;
}
if (flag == 1)
return 1;
}
if (board[1][1]== s && board[2][2]== s && board[3][3] == s)
{
return 1; //斜线
}
if (board[1][3] == s && board[2][2] == s && board[3][1] == s)
return 1;
}
int Is_Winner(char board[ROWS][COLS], int row, int col,char s)
{
if (Is_Line(board, ROW, COL, s) == 1)
return 1;
else
return 0;
}
void playgame(char board[ROWS][COLS], int row, int col)
{
int time1 = 0,time2 = 0,time = 0;
while (time <= row*col)
{
Player_Move(board, row, col);
time1++;
DisplayBoard(board, row, col);
if (time1 >= 3)
{
if (Is_Winner(board, row, col, sp) == 1)
{
printf("恭喜你获得胜利!\n");
break;
}
}
Computer_Move(board, row, col);
time2++;
DisplayBoard(board, row, col);
if (time2 >= 3)
{
if (Is_Winner(board, row, col, sc) == 1)
{
printf("很遗憾你输了,电脑获得了胜利!\n");
break;
}
}
time = time1 + time2;
if (time == row * col)
{
printf("游戏平局!\n");
break;
}
}
}
main.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf(" (^=^) \n");
printf("**************欢迎进入三子棋游戏*************\n");
printf("请选择: \n");
printf(" 1.三子棋 \n");
printf(" 0.退出 \n");
}
void game()
{
char board[ROWS][COLS];
InitBoard(board, ROWS, COLS, si);
DisplayBoard(board, ROW, COL);
playgame(board, ROW, COL);
}
void playGame()
{
int choice = 0;
do{
menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
game();
playGame();
break;
case 0:
exit(0);
break;
default:
printf("选择错误,请重新选择正确的选项!\n");
break;
}
} while (choice != 1 && choice != 0);
}
int main()
{
srand((unsigned int)time(NULL));
playGame();
return 0;
}
写完扫雷游戏和三子棋游戏之后发现他俩有很多相似之处,只是游戏的玩法不一样,只要明确游戏的玩法写起来就比较容易了。
建议大家在写之间先整体构思一下自己需要的功能以及函数,包括游戏中每一步的过程,这样按着自己的思路来写就轻松多了。
虽然整体来看代码比较简单,但是实现过程中也会遇到很多问题的,大家感兴趣的就自己动手尝试一下吧。