C语言实现三子棋小游戏(基础版)
玩家棋子:* 电脑棋子:#
具体步骤
1.创建棋盘
此处用宏定义,方便更换棋盘大小
#define ROW 3
#define COL 3
char board[ROW][COL] = { 0 };
2.打印菜单
先打印菜单,供玩家选择 1——开始游戏 0——退出游戏
void menu()
{
printf("********************\n");
printf("**** 1.开始游戏 ****\n");
printf("**** 0.退出游戏 ****\n");
printf("********************\n");
}
3.函数主体
玩家选择1则进入游戏,选择0则退出游戏; 先让玩家选择再进行判断,所以用do-while循环;
#include<stdio.h>
int main()
{
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("已退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
4.游戏game()的具体实现
1.首先初始化棋盘并打印棋盘; 2.玩家和电脑轮流下棋,然后打印棋盘,所以放在循环中; 3.if_win函数判断棋势形势: 返回*则玩家赢;返回 # 则电脑赢;返回 D 则平局;返回 C 则继续
void game()
{
char board[ROW][COL] = { 0 };
char ret = 0;
initboard(board, ROW, COL);
showboard(board, ROW, COL);
while (1)
{
playermove(board, ROW, COL);
showboard(board, ROW, COL);
ret = if_win(board, ROW, COL);
if (ret != 'C')
break;
computermove(board, ROW, COL);
showboard(board, ROW, COL);
ret = if_win(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("恭喜,玩家赢了!\n");
else if (ret == '#')
printf("很遗憾,电脑赢了!\n");
else
printf("平局\n");
}
如果此处没有看明白,请先跳过此节,往下看
5.初始化棋盘
初始化棋盘为空格
void initboard(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] = ' ';
}
}
}
6.打印棋盘
你是不是认为跟上面的思路的差不多? 只需要把board[i][j] = ’ ';改为printf(" %c ", board[i][j]);再在一层循环后加上换行符就行了?
void showboard(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]);
}
printf("\n");
}
}
实际上结果是这样的 因为我们初始化棋盘为‘ ‘(空格),所以没有分隔符,看不到棋盘的情况 重新打印加工后的棋盘 用|分隔左右棋子,用—分隔上下两行
void showboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
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");
}
}
printf("\n");
}
加工后的结果
7.玩家下棋
1.玩家不用知道数组的下标是从0开始,所以我们对玩家输入的横纵坐标-1便得到玩家实际想下的坐标; 2.玩家输入的坐标是空白的,也就是不存在棋子,否则重新输入; 3.玩家输入的坐标要合法,即在棋盘的范围之内,否则重新输入; 4.系统不断地判断玩家落子的地方,所以放在循环中进行,若玩家落子成功,则退出此循环;
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");
}
}
8.电脑下棋
1.设定电脑随机下棋,使用rand()产生随机数,但rand()每次只会产生固定的随机数,所以要在主函数中加入srand((unsigned int)time(NULL)); 或者srand((unsigned int)time(0)); 将时间作为seed产生不断变化的随机数 rand()和srand()的使用需要包括头文件#include<stdlib.h> time()函数的使用需要包括头文件#includetime.h> 2.在棋盘范围内下棋,横纵坐标对row和col取模; 3.在空白的地方下棋;
void computermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
9.判断棋盘形势
1.玩家获胜 2.电脑获胜 3.平局(需要调用is_full函数判断是否填满,这是平局的前提) 4.游戏继续 注:有些编译器if_full函数放在if_win后面会报错,最好放在前面
int if_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;
}
char if_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 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 (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 (if_full(board, ROW, COL))
return 'D';
return 'C';
}
看到此处,你可以回到4.重新整理一遍思路
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void initboard(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 showboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
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");
}
}
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)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int if_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;
}
char if_win(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 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 (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 (if_full(board, ROW, COL))
return 'D';
return 'C';
}
void menu()
{
printf("********************\n");
printf("**** 1.开始游戏 ****\n");
printf("**** 0.退出游戏 ****\n");
printf("********************\n");
}
void game()
{
char board[ROW][COL] = { 0 };
char ret = 0;
initboard(board, ROW, COL);
showboard(board, ROW, COL);
while (1)
{
playermove(board, ROW, COL);
showboard(board, ROW, COL);
ret = if_win(board, ROW, COL);
if (ret != 'C')
break;
computermove(board, ROW, COL);
showboard(board, ROW, COL);
ret = if_win(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("恭喜,玩家赢了!\n");
else if (ret == '#')
printf("很遗憾,电脑赢了!\n");
else
printf("平局\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("已退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
|