C语言编程实现三子棋和扫雷

三子棋


一.创建大概结构

首先创建三个文件

C语言编程实现三子棋和扫雷_第1张图片

 声明类放在game.h,自定义函数放在game.c,运行主体放在test.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] = { 0 };
	InitBoard(board, ROW, COL);
    //初始化棋盘
	DisplayBoard(board, ROW, COL);
    //打印棋盘
	char ret = 0;
	while (1)
	{
		Player_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
        //判断是否获胜
		if (ret != 'C')
		{
			break;
		}
		Computer_move(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
        //判断是否获胜
		if (ret != 'C')
		{
			break;
		}
		
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else
	{
		printf("平局\n");
	}
}

void test()
{
	srand((unsigned int)time(NULL));//时间戳,创造随机数
	int n = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &n);
		switch (n)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏");
			break;
		default:
			printf("选择错误");
			break;
		}
	} while (n);//当n为0(假)时,退出循环
	return 0;
}
int main()
{
	test();
	return 0;
}

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;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		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 Player_move(char board[ROW][COL], int row, int col)//玩家操作
{
	printf("玩家下棋:>");
	int x = 0;
	int y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')//因为数组下标是从0开始的,但是并不是人人都知道
			{                              //所以使坐标正常化,就每个都-1
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("坐标已被占用,请重新输入\n");
			}
		}
		else
		{
			printf("坐标非法,请重新输入!\n");
		}
	}
}
void Computer_move(char board[ROW][COL], int row, int col)电脑操作(随机)
{
	int x = 0;
    int y = 0;
	printf("电脑下棋:>\n");
	while (1)
	{
		x = rand() % row;//随机数%3 ->范围:0~2之间
		y = rand() % col;//随机数%3 ->范围: 0~2之间 符合坐标限制
		if (board[x - 1][y - 1] == ' ')
		{
			board[x - 1][y - 1] = '#';
			break;
		}
	}
}
int is_full(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 is_win(char board[ROW][COL], int row, int col)//判断胜利
{
	int i = 0;
	for (i = 0; i < row; i++)//一列相同胜利
	{
		if (board[i][1] == board[i][2] && board[i][2] == board[i][3]&&board[i][1]!=' ')
		{
			return board[i][1];
		}
	}
	for (i = 0; i < col; i++)//一行相同胜利
	{
		if (board[1][i] == board[2][i] && board[3][i] == board[2][i]&&board[1][i]!=' ')
		{
			return board[1][i];
		}
	}
	if (board[1][1] == board[0][0] && board[2][2] == board[1][1]&&board[1][1]!=' ')
	{
		return board[1][1];//斜线相同胜利
	}
	if (board[0][2] == board[1][1] && board[2][0] == board[1][1] && board[1][1] != ' ')
	{
		return board[1][1];//斜线相同胜利
	}
	if (1 == is_full(board, row, col))
	{
		return 'Q';//平局返回'Q'作为标记
	}
	return 'C';//倘若未发生上述情况,则返回'C'游戏继续
}

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 Player_move(char board[ROW][COL], int row, int col);

void Computer_move(char board[ROW][COL], int row, int col);

char is_win(char board[ROW][COL], int row, int col);

三.效果展示

C语言编程实现三子棋和扫雷_第2张图片


扫雷


一.创建头文件,源文件

C语言编程实现三子棋和扫雷_第3张图片

这里和上面三子棋一样 


二.进行编程

test.c

#include "game.h"
void menu()
{
	printf("*********************\n");
    printf("*****   1.PLAY   ****\n");
	printf("*****   2.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, '*');
	//Displayboard(mine, ROW, COL);
	Setmine(mine, ROW, COL);
	Displayboard(show, ROW, COL);
	Findmine(mine, show, ROW, COL);
}
void test()
{
	int i = 0;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("请选择:>");
		scanf("%d", &i);
		switch (i)
		{
		case 1:
		{
			game();
			break;
		}
		case 0:
			printf("退出游戏");
			break;
		default:
			printf("输入错误,请重新输入");
		}
	} while (i);
}
int main()
{
	test();
	return 0;
}

game.c

#include "game.h"

void Initboard(char board[ROWS][COLS], int rows, int cols,char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < ROWS; i++)
	{
		for (j = 0; j < COLS; j++)
		{
			board[i][j] = set;
		}
	}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= col; i++)
	{
		printf(" %d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf(" %d ", i);
		for (j = 1; j <= col; j++)
		{
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
}
void Setmine(char mine[ROWS][COLS], int row, int col)
{
	int count = MINE;

	while (count)
	{
		int x = rand() % 9 + 1;
		int y = rand() % 9 + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}
static int Get_minecount(char mine[ROWS][COLS], int x, int y)
{
	return mine[x][y - 1] +
		mine[x][y + 1] +
		mine[x + 1][y] +
		mine[x - 1][y] +
		mine[x + 1][y - 1] +
		mine[x + 1][y + 1] +
		mine[x - 1][y - 1] +
		mine[x - 1][y + 1] - 8 * '0';
}
void Findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - MINE)
	{
		printf("请输入你要查看的坐标:>");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				Displayboard(mine, row, col);
				break;
			}
			else
			{
				int n = Get_minecount(mine,x,y);
				show[x][y] = n + '0';
				Displayboard(show, row, col);
				win++;
			}
		}
		else
		{
			printf("坐标非法,请重新输入\n");
		}
	}
	if (win == row * col - MINE)
	{
		printf("恭喜你,排雷成功,游戏胜利\n");
		Displayboard(mine, row, col);
	}
}

game.h

#include 
#include 

#define ROW  9
#define COL  9

#define ROWS ROW+2
#define COLS COL+2
#define MINE 10

void Initboard(char board[ROWS][COLS], int rows, int cols, char set);

void Displayboard(char board[ROWS][COLS], int row, int col);

void Setmine(char board[ROWS][COLS], int row, int col);

void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

三.效果展示

C语言编程实现三子棋和扫雷_第4张图片

C语言编程实现三子棋和扫雷_第5张图片

你可能感兴趣的