C++扫雷游戏代码

代码

#include<iostream>
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
int main(){
	cout<<"范围:";
	int y;
	for(;;){
		cin>>y;
		if(y>=5)
			break;
		else
			cout<<"太小,请重试:";
	}
	int i;
	while(1+1==2){
    	srand( (unsigned)time( NULL ) ); 
		i=rand()%y-1;
		if(i>=3)
			break;
	}
	int n;
	int min=1,max=y;
	cout<<min<<"~"<<max<<"之间:"; 
	while(n!=i){
		cin>>n;
		if(n>=max||n<=min){
			cout<<"数字越界,请重试:";
			continue;
		}
		if(n>i){
			max=n;
			cout<<min<<"~"<<max<<"之间:"; 
		}
		if(n<i){
			min=n;
			cout<<min<<"~"<<max<<"之间:"; 
		}
	}
	cout<<"Boom!!!!";
	return 0;
}

运行

范围:1000
1~1000之间:500
1~500之间:400
1~400之间:600
数字越界,请重试:300
1~300之间:0
数字越界,请重试:100
1~100之间:34
34~100之间:56
56~100之间:78
78~100之间:90
78~90之间:88
78~88之间:79
79~88之间:85
Boom!!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个简单的扫雷游戏代码示例: #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 10 #define COLS 10 #define MINES 10 void init_board(char board[ROWS][COLS], int rows, int cols, char ch); void print_board(char board[ROWS][COLS], int rows, int cols); void set_mines(char board[ROWS][COLS], int rows, int cols, int num_mines); void set_numbers(char board[ROWS][COLS], int rows, int cols); void play_game(char board[ROWS][COLS], char show_board[ROWS][COLS], int rows, int cols); int main() { char board[ROWS][COLS]; char show_board[ROWS][COLS]; srand(time(NULL)); init_board(board, ROWS, COLS, '*'); init_board(show_board, ROWS, COLS, '-'); set_mines(board, ROWS, COLS, MINES); set_numbers(board, ROWS, COLS); print_board(show_board, ROWS, COLS); play_game(board, show_board, ROWS, COLS); return ; } void init_board(char board[ROWS][COLS], int rows, int cols, char ch) { int i, j; for (i = ; i < rows; i++) { for (j = ; j < cols; j++) { board[i][j] = ch; } } } void print_board(char board[ROWS][COLS], int rows, int cols) { int i, j; printf(" "); for (i = ; i < cols; i++) { printf("%d ", i); } printf("\n"); for (i = ; i < rows; i++) { printf("%d ", i); for (j = ; j < cols; j++) { printf("%c ", board[i][j]); } printf("\n"); } } void set_mines(char board[ROWS][COLS], int rows, int cols, int num_mines) { int i, j, count = ; while (count < num_mines) { i = rand() % rows; j = rand() % cols; if (board[i][j] != '*') { board[i][j] = '*'; count++; } } } void set_numbers(char board[ROWS][COLS], int rows, int cols) { int i, j, count; for (i = ; i < rows; i++) { for (j = ; j < cols; j++) { if (board[i][j] == '*') { continue; } count = ; if (i > && j > && board[i-1][j-1] == '*') { count++; } if (i > && board[i-1][j] == '*') { count++; } if (i > && j < cols-1 && board[i-1][j+1] == '*') { count++; } if (j > && board[i][j-1] == '*') { count++; } if (j < cols-1 && board[i][j+1] == '*') { count++; } if (i < rows-1 && j > && board[i+1][j-1] == '*') { count++; } if (i < rows-1 && board[i+1][j] == '*') { count++; } if (i < rows-1 && j < cols-1 && board[i+1][j+1] == '*') { count++; } board[i][j] = count + ''; } } } void play_game(char board[ROWS][COLS], char show_board[ROWS][COLS], int rows, int cols) { int i, j, x, y, count = ; char ch; while (count < rows * cols - MINES) { printf("请输入坐标(x,y)和操作(扫雷:S 标记:M):"); scanf("%d%d %c", &x, &y, &ch); if (x < || x >= rows || y < || y >= cols) { printf("坐标不合法,请重新输入!\n"); continue; } if (show_board[x][y] != '-') { printf("该位置已经操作过,请重新输入!\n"); continue; } if (ch == 'S') { if (board[x][y] == '*') { printf("你踩到地雷了,游戏结束!\n"); print_board(board, rows, cols); return; } else { show_board[x][y] = board[x][y]; count++; } } else if (ch == 'M') { show_board[x][y] = 'M'; } else { printf("操作不合法,请重新输入!\n"); continue; } print_board(show_board, rows, cols); } printf("恭喜你,扫雷成功!\n"); print_board(board, rows, cols); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值