【C语言】三子棋和扫雷游戏

目录

一、三子棋

1、运行结果

 2、代码

(1)game.h

(2)game.c

(3)main.c

二、扫雷游戏

1、运行结果(部分)

2、代码

(1)game.h

(2)game.c

(3)main.c


一、三子棋

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;
}

个人笔记,如有错漏还要麻烦各位看官指正。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
三子棋是一种简单的棋类游戏,玩家和电脑轮流在一个3x3的棋盘上下棋。玩家输入的坐标会在棋盘上标记为"*",而电脑会自动选择一个合适的位置下棋。下面是一个简单的C语言实现的例子: 首先,我们需要初始化一个3x3的二维数组来表示棋盘,并用空格字符来表示棋盘上的空位。\[1\] 然后,我们可以编写一个函数来让玩家输入下棋的坐标,并将其标记在棋盘上。\[2\]这个函数会检查玩家输入的坐标是否合法,即在1-3的范围内,并且对应的位置在棋盘上是空的。如果是合法的,就将该位置标记为"*",否则要求玩家重新输入。 接下来,我们可以编写一个函数来让电脑下棋。这个函数可以随机选择一个合法的位置,并将其标记在棋盘上。\[3\]同样,这个函数也会检查选择的位置是否合法,并且对应的位置在棋盘上是空的。 最后,我们可以使用一个while循环来实现玩家和电脑轮流下棋的过程。每次落子后,都会打印当前的棋盘状态。当棋盘上出现三个连续的"*"或"O"时,游戏结束。 这是一个简单的C语言实现的三子棋游戏。希望对你有帮助! #### 引用[.reference_title] - *1* *2* [C语言——简单三子棋](https://blog.csdn.net/weixin_46263870/article/details/123002439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [c语言三子棋](https://blog.csdn.net/wastedyouth/article/details/124169006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值