设计总体思路
实现游戏可以一直玩,先打印棋盘,玩家和电脑下棋,最后分出胜负。
如果编写较大的程序,我们可以分不同模块
例如这个三子棋,我们可以创建三个文件
分别为:
- game.h 函数的声明
- game.c 函数的实现
- test.c 测试游戏逻辑
一、打印游戏菜单界面
首先要有开始界面并且有选项选择,开始你的游戏,代码以及效果图如下:
void menu()
{printf("*********************************************\n");printf("************* 1. play *********\n");printf("************* 0. exit *********\n");printf("*********************************************\n");
}int main()
{srand((unsigned int)time(NULL)); //随机数 无符号整型 时间戳 //头文件:stdlib.h time.hint 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;
}
二、打印棋盘
创建并初始化数组,再打印棋盘,代码以及效果图如下:
game.h
#pragma once//头文件的包含
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h> //我们可以将头文件都包含在game.h,//其余使用只需包含一下game.h即可,无需重复写头文件#define ROW 3 //定义一下行和列,未来所有使用行和列直接使用ROW和COL,
#define COL 3 //如果要更改行和列即可全篇修改//函数的声明
//
//初始化棋盘
void InitBoard(char board[ROW][COL],int row,int col);//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"void InitBoard(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++){board[i][j] = ' ';}}//初始化 地址 初始化的内容 字节大小 头文件:string.h//memset(&board[0][0],' ',row*col*sizeof(board[0][0])); //这个也是初始化棋盘
}void DisplayBoard(char board[ROW][COL], int row, int col)
{int i = 0;int j = 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");}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"void game()
{//存放数据需要一个3*3的二维数组char board[ROW ][COL] = {0};//初始化棋盘InitBoard(board, ROW, COL);//显示棋盘DisplayBoard(board, ROW, COL);
}
三、玩家下棋
'*' 代表玩家下的棋子
代码思路,第一步创建玩家下棋的函数PlayerMove()并输入坐标,代码图如下:
game.c
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] == ' ') //因为数组下标是从0开始{board[x - 1][y - 1] = '*';break;}else{printf("坐标被占用,重新输入\n");}}else{printf("坐标非法,请重新输入\n");}}
}
四、电脑下棋
'#' 代表电脑下的棋子
代码思路,第一步创建电脑下棋的函数ComputerMove()并输入坐标,代码图如下:
game.c
//电脑随机下棋
void ComputerMove(char board[ROW][COL], int row, int col)
{while (1){int x = 0;int y = 0;printf("电脑下棋>:\n");x = rand() % row;y = rand() % col;if (board[x][y] == ' '){board[x][y] = '#';break;}}
}
五、判断输赢
创建判断输赢的IsWin()函数,while()循环实现玩家和电脑可以轮流下直到一方胜利,
如果有三个 '*' 符号连成一条线,代表玩家获胜,
如果有三个 '#' 符号连成一条线,代表电脑获胜,
如果九个格子填满,但无三个相同符号连成一条线,则平局。
IsWin()函数中:
- 玩家赢返回'*'
- 电脑赢返回'#'
- 平局返回'Q'
- 继续游戏返回'C'
game.c
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;}//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
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][0] != ' '){return board[i][0];}}for (i = 0; i < col; i++){if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' '){return board[0][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 (IsFull(board,row,col)){return 'Q';}//游戏继续return 'C';
}
test.c
void game()
{//存放数据需要一个3*3的二维数组char board[ROW ][COL] = {0};//初始化棋盘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");}if ('#' == ret){printf("电脑赢\n");}if ('Q' == ret){printf("平局\n");}
}
六、最后整理代码(完整代码)
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"void InitBoard(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++){board[i][j] = ' ';}}//初始化 地址 初始化的内容 字节大小 头文件:string.h//memset(&board[0][0],' ',row*col*sizeof(board[0][0]));
}void DisplayBoard(char board[ROW][COL], int row, int col)
{int i = 0;int j = 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;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)
{while (1){int x = 0;int y = 0;printf("电脑下棋>:\n");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 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;}//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
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][0] != ' '){return board[i][0];}}for (i = 0; i < col; i++){if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' '){return board[0][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 (IsFull(board,row,col)){return 'Q';}//游戏继续return 'C';
}
game.h
#pragma once//头文件的包含
#include<string.h>
#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);//打印棋盘
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);//判断输赢
char IsWin(char board[ROW][COL], int row, int col);
test.c
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void menu()
{printf("*********************************************\n");printf("************* 1. play *********\n");printf("************* 0. exit *********\n");printf("*********************************************\n");printf("*********************************************\n");}void game()
{//存放数据需要一个3*3的二维数组char board[ROW ][COL] = {0};//初始化棋盘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");}if ('#' == ret){printf("电脑赢\n");}if ('Q' == ret){printf("平局\n");}
}int main()
{srand((unsigned int)time(NULL)); //随机数 无符号整型 时间戳 //头文件:stdlib.h time.hint 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;
}
七、游戏展示
如果各位玩了之后会发现(因为电脑是随机下的,所以可能要被电脑气死)
- 赢是特别简单
- 电脑赢有点难度
- 平局究极难
所以我们可以把平局作为目标去尝试,你会发现还是有点意思的!
最后希望对你们有帮助