简单扫雷游戏

简单扫雷游戏实现

game.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
void Initmap( char map[ROWS][COLS],int rows,int cols,char set );
void Displaymap(char map[ROWS][COLS], int row, int col);
void Setminemap(char mine[ROWS][COLS], int row, int col);
void Findminemap(char show[ROWS][COLS],char mine[ROWS][COLS], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game10.h"
void Initmap(char map[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++){
			map[i][j] = set;
		}
	}
}
void Displaymap(char map[ROWS][COLS], int row, int col){
	int i = 0;
	int j = 0;
	printf("--------------------------------\n");
	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 ", map[i][j]);
		}printf("\n");
	}printf("--------------------------------\n");	
}

void Setminemap(char mine[ROWS][COLS], int row, int col){
	int count = COUNT;
	while (count){
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0'){
			mine[x][y] = '1';
			count--;
		}
	}
}
int  Getminemap(char mine[ROWS][COLS], int i, int j){
	return mine[i - 1][j] + mine[i - 1][j - 1] + mine[i][j - 1] + mine[i + 1][j - 1]
		+ mine[i + 1][j] + mine[i + 1][j + 1] + mine[i][j + 1] + mine[i - 1][j + 1]-8*'0';
}

void Findminemap(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col){
	int i = 0;
	int j = 0;
	int win = 0;
	while (win<ROW*COL-COUNT){
		printf("请输入要翻开位置的坐标:\n");
		scanf("%d %d", &i, &j);
		if (i >= 1 && i <= row&&j >= 1 && j <= col){
			if (mine[i][j] == '1'){
				printf("踩雷了,游戏失败!\n");
				Displaymap(mine, i, j);
				break;
			}
			else{
				int count = Getminemap(mine, i, j);
				show[i][j] = count + '0';
				Displaymap(show, ROW, COL);
				win++;
				
			}
		}
		else{
			printf("输入了非法坐标\n");
		}
	}if (win == ROW*COL - COUNT){
		printf("恭喜你,扫雷成功!");
	}
}
saolei.c
#define _CRT_SECURE_NO_WARNINGS
#include"game10.h"
void menu(){
	printf("********************\n");
	printf("******1.play********\n");
	printf("******0.exit********\n");
	printf("********************\n");
}
void game(){
	char show[ROWS][COLS] = { 0 };
	char mine[ROWS][COLS] = { 0 };
	Initmap(show, ROWS, COLS,'*');
	Displaymap(show, ROW, COL);
	Initmap(mine, ROWS, COLS, '0');
	//Displaymap(mine, ROW, COL);
	Setminemap(mine, ROW, COL);
	Displaymap(mine, ROW, COL);
	Findminemap(show, mine, ROW, COL);
}
int main(){
	srand((unsigned int)time(NULL));
	int input = 0;
	do{
		menu();
		printf("请输入你的选择:");
		scanf("%d", &input);
		switch (input){
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏!\n");
			break;
		default:
			printf("输入有误,请重新输入:");
		}
	} while (input);

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单扫雷游戏的 Python 代码,注释已经尽量写得详细了,希望对您有所帮助: ```python import random # 定义常量 BOARD_ROWS = 10 BOARD_COLS = 10 MINE_NUM = 10 # 定义图标 HIDDEN_ICON = '■' MINE_ICON = 'x' FLAG_ICON = 'F' # 定义游戏状态 GAME_STATE_WIN = 0 GAME_STATE_LOSE = 1 GAME_STATE_PLAYING = 2 # 初始化游戏面板 def init_board(rows, cols, num): board = [[0 for i in range(cols)] for j in range(rows)] mines = random.sample(range(rows * cols), num) for m in mines: row = m // cols col = m % cols board[row][col] = MINE_ICON return board # 计算周边地雷数量 def count_mines_around(row, col, board): count = 0 for i in range(max(0, row - 1), min(row + 2, len(board))): for j in range(max(0, col - 1), min(col + 2, len(board[0]))): if board[i][j] == MINE_ICON: count += 1 return count # 显示游戏面板 def display_board(board, visible): print(' ', end='') for j in range(len(board[0])): print(j, end=' ') print() for i in range(len(board)): print(i, end=' ') for j in range(len(board[0])): if visible[i][j]: print(board[i][j], end=' ') else: print(HIDDEN_ICON, end=' ') print() # 处理玩家输入 def handle_input(board, visible): while True: try: row, col = map(int, input('请输入行列号(空格隔开):').split()) if row < 0 or row >= BOARD_ROWS or col < 0 or col >= BOARD_COLS: print('输入错误,请重新输入!') elif visible[row][col]: print('该位置已经翻开,请重新输入!') else: return row, col except ValueError: print('输入错误,请重新输入!') # 翻开一个格子 def flip(row, col, board, visible): if board[row][col] == MINE_ICON: return GAME_STATE_LOSE else: visible[row][col] = True if count_mines_around(row, col, board) == 0: for i in range(max(0, row - 1), min(row + 2, len(board))): for j in range(max(0, col - 1), min(col + 2, len(board[0]))): if not visible[i][j]: flip(i, j, board, visible) return GAME_STATE_PLAYING # 标记一个格子 def flag(row, col, visible): visible[row][col] = not visible[row][col] # 判断游戏是否胜利 def check_win(board, visible): for i in range(len(board)): for j in range(len(board[0])): if not visible[i][j] and board[i][j] != MINE_ICON: return False return True # 主函数 def main(): board = init_board(BOARD_ROWS, BOARD_COLS, MINE_NUM) visible = [[False for i in range(BOARD_COLS)] for j in range(BOARD_ROWS)] game_state = GAME_STATE_PLAYING while game_state == GAME_STATE_PLAYING: display_board(board, visible) row, col = handle_input(board, visible) if input('请输入操作(f: 标记;r: 翻开):') == 'f': flag(row, col, visible) else: game_state = flip(row, col, board, visible) display_board(board, visible) if game_state == GAME_STATE_WIN: print('恭喜你,游戏胜利!') else: print('很遗憾,游戏失败!') if __name__ == '__main__': main() ``` 这份代码实现了一个简单扫雷游戏,包括初始化游戏面板、计算周边地雷数量、显示游戏面板、处理玩家输入、翻开格子、标记格子、判断游戏是否胜利等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值