在接触到c语言一段时间后,大家可以尝试一些简单的游戏实现来增加学习的乐趣.今天,我来带大家来了解一下三子棋游戏的实现.
首先,先确定该程序的整体思路 打印棋盘->下棋,保存数据->判断输赢,这三个部分,所以我将沿着这三个部分进行详细讲解
在这之前,需要的准备工作就是创建一个开始界面和创建二维数组.
void menu()
{
printf("******************************\n");
printf("*** 1.play 0.exit ***\n");
printf("******************************\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
1.打印棋盘
作为一个游戏的基础,棋盘是必不可少的东西.在这里我提供我简单的设计棋盘的思路
void DisplayBoard(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");
}
}
}
再打印完棋盘之后,我们便可以安心的进入到下一步了!
2.下棋,保存数据
先上源代码
人下棋部分
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("玩家走: >\n");
printf("请输入下棋坐标:>\n");
scanf("%d%d", &x, &y);
//判断合法性
while (1)
{
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
电脑下棋部分`
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
在下棋这部分,需要注意的是下棋的位置,坐标的合法性已经该位置是否有棋子.所以只要判断二维是否可以下棋即可.对于电脑,此时可以通过随机数的形式来得到电脑下棋坐标.当然,电脑的水平也可以更新,在这里就不细说了.
3.判断输赢
还是先上源代码
int IsFull(char board[ROW][COL], int row, int col)
{
int j = 0;
int i = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
if (board[i][0] == ' ')
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][1] != ' ')
{
return board[i][0];
}
}
//列判断
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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
//判断是否平局
if (1 == IsFull(board, ROW, COL))
return 'Q';
return 'C';
}
判断输赢就三种情况,平局,电脑赢,玩家赢.其中平局是最好判断,只需要判断棋盘是否下满即可.玩家赢与电脑赢的情况一样,只要判断是否有三子连在一起即可.
总代码展示
游戏框架
#include "game.h.cpp"
void menu()
{
printf("******************************\n");
printf("*** 1.play 0.exit ***\n");
printf("******************************\n");
}
//游戏实现函数
void game()
{
char ret;
//数组存放棋子信息
char board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
//下棋
while (1)
{
//玩家
PlayerMove(board,ROW,COL);
DisplayBoard(board, ROW, COL);
ret=IsWin(board, ROW, COL);
if (ret != 'C')
break;
//电脑
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
printf("玩家赢\n");
else if(ret =='#')
printf("电脑赢\n");
else
printf("平局\n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("三子棋\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
游戏实现方面
#include "game.h.cpp"
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 DisplayBoard(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");
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("玩家走: >\n");
printf("请输入下棋坐标:>\n");
scanf("%d%d", &x, &y);
//判断合法性
while (1)
{
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该坐标被占用\n");
break;
}
}
else
{
printf("输入非法坐标,请重新输入!\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0, y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[ROW][COL], int row, int col)
{
int j = 0;
int i = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
if (board[i][0] == ' ')
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][1] != ' ')
{
return board[i][0];
}
}
//列判断
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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ')
{
return board[1][1];
}
//判断是否平局
if (1 == IsFull(board, ROW, COL))
return 'Q';
return 'C';
}
游戏头文件的实现
#define _CRT_SECURE_NO_WARNINGS
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//函数声明
void DisplayBoard(char board[ROW][COL], int row, int col);
void InitBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
谢谢大家观看