最终效果图
test.c 测试游戏逻辑
game.c 关于游戏相关函数的声明
game.c 游戏相关函数的实现
1.创建基本逻辑,菜单和重复玩
void menu() { printf("**********************************************n"); printf("**************** 1.play ****************n"); printf("**************** 2.exit ****************n"); printf("**********************************************n"); } int main() { int input = 0; do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1:printf("三子棋游戏n"); break; case 0:printf("退出游戏n"); break; default:printf("选择错误,重新选择n"); break; } } while (input != 0); return 0; }
2.实现三子棋游戏
1>棋盘创建
1)基本框架
棋盘是3 * 3 的,故应建立一个二维数组 3 * 3
存储数据,二维数组
char board[ROW][COL];//不用3 3 而用ROW COL 为了以后改变棋盘的规模而准备
game.h内
#define ROW 3 #define COL 3
#include"game.h"引用头文件,使用game.h内的数据
2)初始化棋盘
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] = ' '; } } }
3)打印一下棋盘 - 本质打印数组内容
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) { j = 0; for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("n"); } } }
2>设计玩家走和电脑走
1)玩家走
void PlayerMove(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][y] == ' ') { board[x][y] = '*'; DisplayBoard(board, ROW, COL); break; } else { printf("坐标被占用,请重新输入n"); } } else { printf("非法坐标请重新输入n"); } } }
2)电脑走
void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑走:>n"); while (1) { int x = rand() % row;//接受随机数 int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
在test.c主函数内
srand((unsigned int)time(NULL));//随机数的生成,头文件和
3>判断游戏状态
在游戏进行过成中的四种情况:玩家赢-* 电脑赢-# 平局-Q 游戏继续-C
char IsWin 用来判断游戏的状态
其中判断多行和列棋盘状态时,思考当什么时候满足输赢条件,
也就是当全行或列都相同且不为空格时,把此条件逻辑取反也就是该状态下没有
一方赢的条件
int IsFull(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 IsWin(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-1; j++) { if (!(board[i][j] == board[i][j + 1] && board[i][j] != ' ')) { break; } } if (j == col-1) { return board[i][j]; } } //判断列 for (j = 0; j < col; j++) { for (i = 0; i < row - 1; i++) { if (!(board[i][j] == board[i+1][j] && board[i][j] != ' ')) { break; } } if (i == row - 1) { return board[i][j]; } } //判断正对角线 for (i = 0,j = 0; i < row-1&&j0 && j < col - 1; i--, j++) { if (board[i][j] == board[i -1][j + 1]) { if(board[i][j]==' ') break; } else { break; } } if (i == 0 || j == col - 1) { return board[i][j]; } //1--满 0--未满 int ret = IsFull(board, ROW, COL); if (ret == 1) { return 'Q'; } else { return 'C'; } }
最终函数结果
test.c
#include"game.h" void menu() { printf("**********************************************n"); printf("**************** 1.play ****************n"); printf("**************** 2.exit ****************n"); printf("**********************************************n"); } void game() { char board[ROW][COL]; InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); char ret = 0;//接收游戏状态 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"); } DisplayBoard(board, ROW, COL); } int main() { 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 != 0); return 0; }
game.h
//头文件的包含 #include#include #include //符号的定义 #define ROW 3 #define COL 3 //函数的声明 //初始化棋盘 void InitBoard(char board[ROW][COL],int row,int col); //打印棋盘的函数 void DisplayBoard(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); //1.玩家赢 - * //2.电脑赢 - # //3.平局 - Q //4.游戏继续 - C //判断游戏是否有输赢 char IsWin(char board[ROW][COL], int row, int col);
game.c
#include"game.h" 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) { j = 0; 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; 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("坐标被占用,请重新输入n"); } } else { printf("非法坐标请重新输入n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑走:>n"); while (1) { int x = rand() % row;//接受随机数 int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } int IsFull(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 IsWin(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-1; j++) { if (!(board[i][j] == board[i][j + 1] && board[i][j] != ' ')) { break; } } if (j == col-1) { return board[i][j]; } } //判断列 for (j = 0; j < col; j++) { for (i = 0; i < row - 1; i++) { if (!(board[i][j] == board[i+1][j] && board[i][j] != ' ')) { break; } } if (i == row - 1) { return board[i][j]; } } //判断正对角线 for (i = 0,j = 0; i < row-1&&j0 && j < col - 1; i--, j++) { if (board[i][j] == board[i -1][j + 1]) { if(board[i][j]==' ') break; } else { break; } } if (i == 0 || j == col - 1) { return board[i][j]; } //判断是否平局 //1--满 0--未满 int ret = IsFull(board, ROW, COL); if (ret == 1) { return 'Q'; } else { return 'C'; } }
也可以把标识符常量3改为5就是简易版的五子棋了,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)