|
发表于 2018-3-27 18:40:34
|
显示全部楼层
我又改了一下这样好像就可以自动打开了
- #include<bits/stdc++.h>
- #include<windows.h>
- #include <conio.h>
- using namespace std;
- const int map_x = 16;
- const int map_y = 16;
- struct Mapelem{
- bool status = false;
- bool is_mine = false;
- int course = 0;
- }Map[map_x][map_y];
- void Hidecursor()
- {
- CONSOLE_CURSOR_INFO cci;
- cci.bVisible = FALSE;
- cci.dwSize = sizeof(cci);
- HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorInfo(handle, &cci);
- }
- void gotoxy(int x,int y){
- COORD cod;
- cod.X = x * 2;
- cod.Y = y;
- SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cod );
- }
- void print_course(int x,int y){
- if(!Map[x][y].course){
- printf(" ");
- return;
- }
- printf("%d",Map[x][y].course);
- }
- void drawmap(){
- for(int i=0; i<map_x; i++){
- for(int j=0; j<map_y; j++){
- gotoxy(i,j);
- if(Map[i][j].status){
- if(Map[i][j].is_mine)
- printf("★");
- else
- print_course(i,j);
- }
- else
- printf("■");
- }
- }
- }
- void UpdateCursor(POINT po)
- {
- drawmap();
- gotoxy(po.x, po.y);
- printf("□");
- }
- void setmine(int num){
- int x,y;
- for(int i=0; i<num; i++){
- do{
- x = rand()%map_x;
- y = rand()%map_y;
- }while(Map[x][y].is_mine);
- Map[x][y].is_mine = true;
- }
- }
- int GetCount(int x, int y)
- {
- int count = 0;
- // вСио╫г -> срио╫г
- for(int i = 0; i < 3; ++i)
- if((x - 1 + i >= 0) && (y - 1 >= 0))
- if(Map[x - 1 + i][y - 1].is_mine)
- ++count;
- // вСоб╫г -> сроб╫г
- for(int i = 0; i < 3; ++i)
- if((x - 1 + i >= 0) && (y + 1 < map_y))
- if(Map[x - 1 + i][y + 1].is_mine)
- ++count;
- // вС╠ъ
- if(x - 1 >= 0) // спвС╠ъбП?
- if(Map[x - 1][y].is_mine)
- ++count;
- // ср╠ъ
- if(x + 1 < map_x) // спср╠ъбП?
- if(Map[x + 1][y].is_mine)
- ++count;
- return count;
- }
- void GenerateCount(void)
- {
- for(int i = 0; i < map_x; ++i)
- {
- for(int j = 0; j < map_y; ++j)
- {
- if(!Map[i][j].is_mine)
- Map[i][j].course = GetCount(i, j);
- }
- }
- }
- void show_mine(POINT p ){
- if (Map[p.x][p.y].status)
- return;
- else if (Map[p.x][p.y].course>0){
- Map[p.x][p.y].status = true;
- return;
- }
- else{//空格只需要上下左右四个方向检测
- Map[p.x][p.y].status = true;
- Map[p.x-1][p.y-1].status = true;
- Map[p.x+1][p.y-1].status = true;
- Map[p.x-1][p.y+1].status = true;
- Map[p.x+1][p.y-1].status = true;
- POINT k;
- k.x = p.x;
- if(p.y + 1 < map_y){
- k.y = p.y + 1;
- show_mine(k);
- }
- if(p.y - 1 > 0){
- k.y = p.y - 1;
- show_mine(k);
- }
- k.y = p.y;
- if(p.x + 1 < map_x){
- k.x = p.x + 1;
- show_mine(k);
- }
- if(p.x - 1 > 0){
- k.x = p.x - 1;
- show_mine(k);
- }
- }
- }
- bool SelectTarget(POINT p){
- if(Map[p.x][p.y].status)
- return false;
- if(Map[p.x][p.y].is_mine)
- return true;
- //print_course(p.x,p.y);
- show_mine(p);
- drawmap();
- return false;
- }
- void showmap(){
- for(int i=0; i<map_x; i++){
- for(int j=0; j<map_y; j++){
- if(!Map[i][j].status){
- gotoxy(i,j);
- if(Map[i][j].is_mine)
- printf("★");
- else
- print_course(i,j);
- }
- }
- }
- }
- int main(){
- srand((unsigned int)time(0));
- Hidecursor();
- drawmap();
- setmine(30);
- GenerateCount();
- POINT p = {0,0};
- UpdateCursor(p);
- while(1){
- char c = _getch();
- switch(c){
- case 'q':
- case 'Q':
- goto EXIT;
- break;
- case 'a':
- case 'A':
- if(p.x>0){
- p.x--;
- UpdateCursor(p);
- }break;
- case 's':
- case 'S':
- if(p.y<15){
- p.y++;
- UpdateCursor(p);
- }break;
- case 'W':
- case 'w':
- if(p.y>0){
- p.y--;
- UpdateCursor(p);
- }break;
- case 'd':
- case 'D':
- if(p.x<15){
- p.x++;
- UpdateCursor(p);
- }break;
- case ' ':
- if(SelectTarget(p))
- goto EXIT;
- break;
- }
- }
- EXIT:
- showmap();
- return 0;
- }
复制代码 |
-
|