目录
一、三子棋
1、运行结果
2、代码
(1)game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3 //符号的定义
#define COL 3
#define ROWS ROW+2
#define COLS COL+2
void Init_Game(char board[ROWS][COLS], int row, int col);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void PlayerMove(char board[ROWS][COLS], int row, int col);
void ComputerMove(char board[ROWS][COLS], int row, int col);
int JudgeWin(char board[ROWS][COLS], int row, int col);//玩家赢:*;电脑赢:#;平局:Q;游戏继续:C
(2)game.c
#include "game.h"
void Init_Game(char board[ROWS][COLS], 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[ROWS][COLS], 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(" |");
}
putchar('\n');
if(i < row - 1)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if(j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void PlayerMove(char board[ROWS][COLS], int row, int col)//玩家走
{
printf("玩家走:>");
int i = 0;
int j = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d %d", &i, &j);
if (i >= 1 && i < row + 1 && j >= 1 && j < col + 1)//判断坐标合法性
{
if (board[i - 1][j - 1] == ' ') //坐标未被占用
{
board[i - 1][j - 1] = '*';
break;
}
else
{
printf("坐标被占用,请重新输入。");
}
}
else
{
printf("坐标非法,请重新输入。");
}
}
}
void ComputerMove(char board[ROWS][COLS], int row, int col)//电脑走
{
printf("电脑走:>\n");
while(1)
{
int x = rand() % row; //随机数字范围:0~row-1
int y = rand() % col; //随机数字范围:0~col-1
if (board[x][y] == ' ') //坐标未被占用
{
board[x][y] = '#';
break;
}
}
}
int JudgeFull(char board[ROWS][COLS], int row, int col)//判断是否满格
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
int JudgeWin(char board[ROWS][COLS], int row, int col)//判断胜负——玩家赢:*;电脑赢:#;平局:Q;游戏继续:C
{
int i = 0;
int j = 0;
//输赢判断
//扫描三行
for (i = 0; i < row; i++)
{
if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j] != ' ')
{
return board[i][j];
}
}
//扫描三列
for (i = 0; i < col; i++)
{
j = 0;
if (board[j][i] == board[j + 1][i] && board[j + 1][i] == board[j + 2][i] && board[j][i] != ' ')
{
return board[j][i];
}
}
//扫描斜行
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
if (board[i][j] == board[i - 1][j - 1] && board[i][j] == board[i + 1][j + 1] && board[i][j] != ' ')
return board[i][j];
}
}
//平局判断
int ret = JudgeFull(board, ROW, COL);
if (ret == 1)
{
return 'Q';
}
//继续判断
return 'C';
}
(3)main.c
#include "game.h"
void menu()
{
printf("-------------------------\n");
printf("------ 1.begin -------\n");
printf("------ 0.exit -------\n");
printf("-------------------------\n");
}
void game()
{
char board[ROWS][COLS]; //存储数据——二维数组
char ret = 0;
Init_Game(board, ROW, COL); //初始化棋盘——初始化空格
putchar('\n');
DisplayBoard(board, ROW, COL); //打印棋盘——打印数组内容
putchar('\n');
while (1)
{
PlayerMove(board, ROW, COL); //玩家走
DisplayBoard(board, ROW, COL);
putchar('\n');
ret = JudgeWin(board, ROW, COL);
if (ret != 'C')
break;
ComputerMove(board, ROW, COL); //电脑走
DisplayBoard(board, ROW, COL);
putchar('\n');
ret = JudgeWin(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*')
{
printf("\n玩家获胜!\n");
}
else if (ret == '#')
{
printf("\n电脑获胜!\n");
}
else
{
printf("\n平局!\n");
}
DisplayBoard(board, ROW, COL);
putchar('\n');
}
int main()
{
srand((unsigned int)time(NULL));//生成随机数字
int select = 0;
do
{
menu();
putchar('\n');
printf("请选择:>");
scanf("%d", &select);
switch (select)
{
case 1:
game();
break;
case 0:
break;
default:
printf("请重新输入:>");
break;
}
} while (select);
return 0;
}
二、扫雷游戏
1、运行结果(部分)
-------------------------
------ 1.begin -------
------ 0.exit -------
-------------------------
请选择:>1
-----------扫雷游戏----------
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * * * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
-----------扫雷游戏----------
请输入坐标:> 5 2
-----------扫雷游戏----------
0 1 2 3 4 5 6 7 8 9
1 * * * * * * * * *
2 * * * * * * * * *
3 * * * * * * * * *
4 * * * * * * * * *
5 * 1 * * * * * * *
6 * * * * * * * * *
7 * * * * * * * * *
8 * * * * * * * * *
9 * * * * * * * * *
-----------扫雷游戏----------
请输入坐标:> 5 6
-----------扫雷游戏----------
0 1 2 3 4 5 6 7 8 9
1 * * * * * * 1
2 * * * * * * 1
3 * * * * * 2 1
4 * * * * 2 1
5 * 1 * 1 1
6 * * * 1
7 * * * 1 1 1
8 * 2 1 1 2 *
9 * 1 2 *
-----------扫雷游戏----------
请输入坐标:> 4 4
很遗憾,你被炸死了。
-----------扫雷游戏----------
0 1 2 3 4 5 6 7 8 9
1 0 0 0 0 1 0 0 0 0
2 0 0 0 0 0 1 0 0 0
3 0 0 1 0 1 0 0 0 0
4 0 0 0 1 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 1 0 0 0 0 0 0 0
7 0 0 1 0 0 0 0 0 0
8 1 0 0 0 0 0 0 0 1
9 0 0 0 0 0 0 0 0 1
-----------扫雷游戏----------
-------------------------
------ 1.begin -------
------ 0.exit -------
-------------------------
请选择:>0
D:\c\text-1\text.c\game2\x64\Debug\game2.exe (进程 4840)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
2、代码
(1)game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9 //棋盘行数
#define COL 9 //棋盘列数
#define ROWS ROW+2 //扫描行数
#define COLS COL+2 //扫描列数
#define mine_count 10 //雷数
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret);
void Display(char board[ROWS][COLS], int rows, int cols);
void SetMine(char board[ROWS][COLS], int row, int col);
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void AutoShow(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* count);
(2)game.c
#include "game.h"
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
//展示棋盘
void Display(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
putchar('\n');
puts("-----------扫雷游戏----------");
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
putchar('\n');
for (i = 1; i <= row; i++)
{
printf("%d", i);
for (j = 1; j <= col; j++)
{
printf(" %c", board[i][j]);
}
putchar('\n');
}
puts("-----------扫雷游戏----------");
putchar('\n');
}
//布雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int a = 0;
while(a < mine_count)//布置mine_count个雷
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if(board[i][j] != '1')
{
board[i][j] = '1';
a++;
}
}
}
//扫描坐标周边
static int GetCount(char mine[ROWS][COLS], int x, int y)
{
int a = 0;
int b = 0;
int count = 0;
for(a = -1; a < 2; a++)
{
for (b = -1; b < 2; b++)
{
if (mine[x+a][y+b] == '1')
{
count++;
}
}
}
return count;//返回坐标周边雷的数量
/*return mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';*/
}
//自动显示坐标坐标
void AutoShow(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* count)
{
int i = 0;
int j = 0;
//判断坐标:1、超出极限 2、是雷 3、已被查找
if (1 <= x && x <= 9 && 1 <= y && y <= 9 && mine[x][y] == '0' && show[x][y] == '*')
{
if (GetCount(mine, x, y))//坐标周边有雷
{
show[x][y] = '0' + GetCount(mine, x, y);//将整数转换成字符
(*count)--;
}
else
{
show[x][y] = ' ';//无雷显示空格
(*count)--;
for (i = -1; i <= 1; i++)//扫描(x,y)周围八个格子
{
for (j = -1; j <= 1; j++)
{
AutoShow(mine, show, x + i, y + j, count);//进行递归
}
}
}
}
}
//扫雷
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = row * col - mine_count;//记录排查的次数
while (count)
{
printf("请输入坐标:> ");
scanf("%d%d", &i, &j);
//判断坐标合法性:(1)是否在范围内 (2)是否未被查找
if (i >= 1 && i <= row && j >= 1 && j <= col && show[i][j] == '*')
{
if (mine[i][j] == '1')//踩雷
{
printf("很遗憾,你被炸死了。\n");
Display(mine, row, col);//显示雷区
break;
}
else
{
AutoShow(mine, show, i, j, &count);//扫描周边并显示雷的个数
Display(show, row, col);
}
}
else
{
puts("坐标不合法,请重新输入。\n");
}
}
if (count == 0)//全部排查完成且没有踩雷
{
printf("恭喜你,排雷成功!");
Display(mine, row, col);
}
}
(3)main.c
#include "game.h"
//打印菜单
void menu()
{
printf("-------------------------\n");
printf("------ 1.begin -------\n");
printf("------ 0.exit -------\n");
printf("-------------------------\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//展示棋盘
Display(show, ROW, COL);
//埋雷
SetMine(mine, ROW, COL);
//Display(mine, ROW, COL);
//扫雷
SweepMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));//生成随机数字
int select = 0;
do
{
menu();
putchar('\n');
printf("请选择:>");
scanf("%d", &select);
switch (select)
{
case 1:
game();
break;
case 0:
break;
default:
printf("\n请重新输入:>\n");
break;
}
} while (select);
return 0;
}
个人笔记,如有错漏还要麻烦各位看官指正。