#include <iostream> #include <cstdio> #include <ctime> #include <cstdlib> #include <conio.h> #include <cctype> #define HEIGHT 10 #define WIDTH 10 using namespace std; int BOON_NUM = 10, health = 3; struct MAP{ bool isOpen; int groundBoon; }; int pos[8][2] = { {-1, -1}, {-1, 0}, {-1, 1}, { 0, -1}, { 0, 1}, { 1, -1}, { 1, 0}, { 1, 1} }; int dirPos[4][2] = { {-1, 0}, { 0, -1}, { 0, 1}, { 1, 0} }; MAP map[10][10]; void csh(){ for(int i = 0; i < HEIGHT; i++){ for(int j = 0; j < WIDTH; j++){ map[i][j].isOpen = false; map[i][j].groundBoon = 0; } } srand(int(time(NULL))); for(int i = 0; i < BOON_NUM; ){ int x = rand() % HEIGHT; int y = rand() % WIDTH; if(map[x][y].groundBoon == -1) continue; map[x][y].groundBoon = -1; for(int j = 0; j < 8; j++){ int nx = x + pos[j][0]; int ny = y + pos[j][1]; if(nx < 0 || nx >= HEIGHT || ny < 0 || ny >= WIDTH) continue; if(map[nx][ny].groundBoon == -1) continue; map[nx][ny].groundBoon++; } i++; } } void sc(){ printf(" "); for(int i = 0; i < WIDTH; i++) printf("%d ", i); printf("\n"); for(int i = 0; i < HEIGHT; i++){ printf("%d ", i); for(int j = 0; j < WIDTH; j++){ if(map[i][j].isOpen){ if(map[i][j].groundBoon == -1) printf("%c ", '*'); else if(map[i][j].groundBoon == 0) printf("%c ", ' '); else printf("%d ", map[i][j].groundBoon); }else printf("%c ", '@'); } printf("\n"); } printf("剩余雷的数量为: %d\n", BOON_NUM); printf("剩余生命值为: %d\n", health); } void bigOpen(int openH, int openW){ if(map[openH][openW].isOpen) return; map[openH][openW].isOpen = true; if(map[openH][openW].groundBoon != 0) return; for(int i = 0; i < 4; i++){ int nx = openH + dirPos[i][0]; int ny = openW + dirPos[i][1]; if(nx < 0 || nx >= HEIGHT || ny < 0 || ny >= WIDTH) continue; bigOpen(nx, ny); } } bool open(int openH, int openW){ if(map[openH][openW].isOpen) return false; if(map[openH][openW].groundBoon == -1){ health--; if(health == 0) return true; map[openH][openW].isOpen = true; return false; } if(map[openH][openW].groundBoon == 0) bigOpen(openH, openW); map[openH][openW].isOpen = true; return false; } bool win(){ for(int i = 0; i < HEIGHT; i++){ for(int j = 0; j < WIDTH; j++){ if(!map[i][j].isOpen && map[i][j].groundBoon != -1) return false; } } return true; } int main(){ system("title 扫雷游戏"); printf("欢迎来到扫雷游戏, 一共10个雷, 3条命; 生命值为0游戏结束\n"); system("pause"); system("cls"); printf("是否设置生命值为1?(y是/n否)"); char temp = getch(); printf("\n"); system("cls"); while(!(temp == 'y' || temp == 'n')){ printf("是否设置生命值为1?(y是/n否)"); temp = getch(); printf("\n"); system("cls"); } if(temp == 'y') health = 1; csh(); bool isEnd = false, isWin = false; char openH, openW, isAgain; while(!isEnd && !isWin){ sc(); printf("请输入你要选择的行: "); openH = getch(); printf("\n"); while(!isdigit(openH)){ printf("请输入你要选择的行: "); openH = getch(); printf("\n"); } printf("请输入你要选择的列: "); openW = getch(); printf("\n"); while(!isdigit(openW)){ printf("请输入你要选择的列: "); openW = getch(); printf("\n"); } isEnd = open(openH - 48, openW - 48); isWin = win(); if(isEnd) printf("你输了!\n"); if(isWin) printf("你赢了!\n"); printf("是否重新开始游戏? y是n否"); system("cls"); } return 0; }
下一篇
Python解释器安装
有话要说...