Zatrzymanie działania programu + jego wywołanie

0

Witam,
jestem początkujący w C++. Muszę napisać sapera, ale mam problem z wywołaniem funkcji. Obecnie jest to tak, że wywołuję metodę wyświetlania planszy w pęli while z przerwą 60ms do czasu gdy parametr while będzie prawdą. Problemem jest to, że plansza odświeżą się co 60ms i widać to jak ona się odświeża. Co powoduję, że ekran skaczę. Nie bardzo mogę zwiększyć tych 60ms, bo wtedy wciśnięte klawisze reagują z opóżnieniem. Czy da się jakoś zrobić, żeby kod w pętli while(linia: 181-182) był wywoływany po wciśnięciu klawiszów takich jak: strzałka w lewo, prawo, w górę, w dół i enter? I przy okazji, żeby sam program nie kończył pracy gdy nie ma nic już do roboty?

Tak to wygląda:
user image

Źródło:

#include <iostream>
#include <cstdlib>
#include <windows.h>

#define xlength 40
#define ylength 20

#define level_easy 100

#define left 0x25
#define right 0x27
#define top 0x28
#define bottom 0x26
#define enter 0x0D

using namespace std;

class saper {

	struct field {
		int value;
		bool display;
	};

    int positionX = 0;
    int positionY = 0;
    bool status = false;

	field grid[xlength][ylength];

    void minesGenerate() {
        for(int i = 0;i<level_easy;i++) {
            int tmp_x = rand() % xlength;
            int tmp_y = rand() % ylength;

            if(grid[tmp_x][tmp_y].value == 9) {
               i--;
            }
            else {
                this->mines(tmp_x,tmp_y);

            }
        }
    }

    void generateGrid() {
        for(int y=0;y<ylength;y++) {
            for(int x=0;x<xlength;x++) {

                grid[x][y].value = 0;
                grid[x][y].display = false;
            }
        }

        this->minesGenerate();
    }

    void mines(int x, int y) {
        grid[x][y].value = 9;

        for(int i=-1;i<=1;i++) {
            for(int j=-1;j<=1;j++) {
                if(x+j >= 0 && x+j <= xlength-1 && y+i >= 0 && y+i <= ylength-1) {
                    if(grid[x+j][y+i].value != 9) {
                        grid[x+j][y+i].value += 1;
                    }
                }
            }
        }
    }

    void gridUpdate(int x, int y) {

        if(x-1 >= 0) {
            if(grid[x-1][y].value == 0 && grid[x-1][y].display == false) {
                grid[x-1][y].display = true;

                this->gridUpdate(x-1,y);
            }
        }
        if(x <= xlength) {
            if(grid[x+1][y].value == 0 && grid[x+1][y].display == false) {
                grid[x+1][y].display = true;

                this->gridUpdate(x+1,y);
            }
        }
        if(y-1 >= 0) {
            if(grid[x][y-1].value == 0 && grid[x][y-1].display == false) {
                grid[x][y-1].display = true;

                this->gridUpdate(x,y-1);
            }
        }
        if(y <= ylength) {
            if(grid[x][y+1].value == 0 && grid[x][y+1].display == false) {
                grid[x][y+1].display = true;

                this->gridUpdate(x,y+1);
            }
        }

    }

	public:
        saper() {
            this->generateGrid();
        }

        bool getStatus() {
            return this->status;
        }

        void setStatus(bool bl) {
            this->status = bl;
        }

        void gridView() {
            system ("cls");

            for(int y=0;y<ylength;y++) {
                for(int x=0;x<xlength;x++) {
                    if (x==positionX && y==positionY) {
                        SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE  ), 0x09);
                        cout << "X";
                    }
                    else {
                        SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE  ), 0x07);

                        if(grid[x][y].display == false) {
                            cout << "*";
                        }
                        else {
                            if(grid[x][y].value == 9) {
                                cout << "#";
                            }
                            else if(grid[x][y].value == 0) {
                                cout << " ";
                            }
                            else {
                                cout << grid[x][y].value;
                            }
                        }
                    }

                    SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE  ), 0x07);

                }
                cout << endl;
            }
        }

        void saperMain() {
            if((GetKeyState(right) & 0x8000) && positionX<xlength-1) positionX++;
            if((GetKeyState(left) & 0x8000) && positionX>0) positionX--;
            if((GetKeyState(top) & 0x8000) && positionY<ylength-1) positionY++;
            if((GetKeyState(bottom) & 0x8000) && positionY>0) positionY--;

            if(GetKeyState(enter) & 0x8000) {
                grid[positionX][positionY].display = true;

                if(grid[positionX][positionY].value == 9) {
                    //this->setStatus(true);
                }

                if(grid[positionX][positionY].value == 0) {
                    this->gridUpdate(positionX, positionY);
                }
            }

            this->gridView();
        }
};

int main(int argc, char** argv) {

	saper s;
    s.saperMain();

    while(!s.getStatus()) {
        Sleep(60);
        s.saperMain();
    }

    cout << endl << "PRZEGRALES";
    Sleep(5000);

	return 0;
}
1

nie rysuj mapy ciagle. Rysuj jedynie zmiany (a raczej nadpisuj)

1 użytkowników online, w tym zalogowanych: 0, gości: 1