diff options
Diffstat (limited to 'src/Game.cpp')
| -rw-r--r-- | src/Game.cpp | 190 |
1 files changed, 67 insertions, 123 deletions
diff --git a/src/Game.cpp b/src/Game.cpp index 6a75758..f230a04 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -29,68 +29,33 @@ void Game::initDiff(int diff){ } void Game::initBoard(){ - // Bomb map (boolean): 0 or false is no bomb, 1 or true is bomb; - // clearing bomb map - for(int i=0; i < boardHeight; i++){ - for(int j=0; j < boardWidth; j++){ - bombMap[i][j] = 0; + for(int y=0; y < boardHeight; y++){ + for(int x=0; x < boardWidth; x++){ + bombMap[y][x] = false; + revealed[y][x] = false; + tileMap[y][x] = 0; } } - // initializing bomb map - - for(int i=0; i < boardHeight; i++){ - for(int j=0; j < boardWidth; j++){ - for(int k = 0; k < mines; k++){ - bombMap[i][j] = rand() % 2; - } - } - } + int placed = 0; + while(placed < mines){ + int x = rand() % boardWidth; + int y = rand() % boardHeight; - // initializing tile map for display - - for(int i=0; i < boardHeight; i++){ - for(int j=0; j < boardWidth; j++){ - tileMap[i][j] = 0; - revealed[i][j] = false; - - // DEBUG: seeing bombs - // if(bombMap[i][j] == true){ - // tileMap[i][j] = 11; - //} + if(!bombMap[y][x]){ + bombMap[y][x] = true; + placed++; } } } void Game::updateBoard(){ - for(int x=0; x < boardWidth; x++){ - for(int y=0; y < boardHeight; y++){ - if(Game::bombCheck(x, y) == 0 && revealed[x][y] == true){ - revealed[x-1][y+1] = true; - revealed[x][y+1] = true; - revealed[x+1][y+1] = true; - revealed[x-1][y] = true; - revealed[x+1][y] = true; - revealed[x-1][y-1] = true; - revealed[x][y-1] = true; - revealed[x+1][y-1] = true; - } else if(Game::bombCheck(x, y) == 1 && revealed[x][y] == true){ - tileMap[x][y] = 2; - } else if(Game::bombCheck(x, y) == 2 && revealed[x][y] == true){ - tileMap[x][y] = 3; - } else if(Game::bombCheck(x, y) == 3 && revealed[x][y] == true){ - tileMap[x][y] = 4; - } else if(Game::bombCheck(x, y) == 4 && revealed[x][y] == true){ - tileMap[x][y] = 5; - } else if(Game::bombCheck(x, y) == 5 && revealed[x][y] == true){ - tileMap[x][y] = 6; - } else if(Game::bombCheck(x, y) == 6 && revealed[x][y] == true){ - tileMap[x][y] = 7; - } else if(Game::bombCheck(x, y) == 7 && revealed[x][y] == true){ - tileMap[x][y] = 8; - } else if(Game::bombCheck(x, y) == 8 && revealed[x][y] == true){ - tileMap[x][y] = 9; - } + for(int y=0; y < boardHeight; y++){ + for(int x=0; x < boardWidth; x++){ + if(!revealed[y][x]) continue; + + int bombs = bombCheck(x, y); + tileMap[y][x] = bombs == 0 ? 1 : bombs + 1; } } } @@ -149,90 +114,69 @@ void Game::displayBoard() { system("clear"); #endif - for(int i=0; i < boardHeight; i++){ - for(int j = 0; j < boardWidth; j++){ - if(tileMap[i][j] == 0){ - putchar('#'); - } else if(tileMap[i][j] == 1){ - putchar('.'); - } else if(tileMap[i][j] == 2){ - putchar('1'); - } else if(tileMap[i][j] == 3){ - putchar('2'); - } else if(tileMap[i][j] == 4){ - putchar('3'); - } else if(tileMap[i][j] == 5){ - putchar('4'); - } else if(tileMap[i][j] == 6){ - putchar('5'); - } else if(tileMap[i][j] == 7){ - putchar('6'); - } else if(tileMap[i][j] == 8){ - putchar('7'); - } else if(tileMap[i][j] == 9){ - putchar('8'); - } else if(tileMap[i][j] == 10){ - putchar('X'); - } else if(tileMap[i][j] == 11){ - putchar('B'); - } + for(int y=0; y < boardHeight; y++){ + for(int x; x < boardWidth; x++){ + if(tileMap[y][x] == 0) putchar('#'); + else if(tileMap[y][x] == 1) putchar('.'); + else putchar('0' + tileMap[y][x] - 1); } putchar('\n'); } } int Game::bombCheck(int x, int y){ - int bombsAround = 0; + int count = 0; - if(tileMap[x][y] == 1){ - if(bombMap[x-1][y-1] == true){ - bombsAround++; - } - if(bombMap[x][y-1] == true){ - bombsAround++; - } - if(bombMap[x+1][y-1] == true){ - bombsAround++; - } - if(bombMap[x-1][y] == true){ - bombsAround++; - } - if(bombMap[x+1][y] == true){ - bombsAround++; - } - if(bombMap[x-1][y+1] == true){ - bombsAround++; - } - if(bombMap[x][y+1] == true){ - bombsAround++; - } - if(bombMap[x+1][y+1] == true){ - bombsAround++; + for(int dy = -1; dy <= 1; dy++){ + for(int dx = -1; dx <= 1; dx++){ + if(dx == 0 && dy == 0) continue; + + int nx = x + dx; + int ny = y + dy; + + if(nx >= 0 && nx < boardWidth && ny >= 0 && ny < boardHeight && bombMap[ny][nx]){ + count++; + } } } - return bombsAround; + return count; } void Game::input(){ - std::string input; + std::string cmd; + std::cout << "game> "; + std::cin >> cmd; - std::cout << "game>"; - std::cin >> input; + if(cmd == "q"){ + inGame = false; + return; + } - if(input == "q"){ - inGame = false; + int x, y; + std::cin >> x >> y; - } else if(input == "d"){ - int x, y; - std::cout << "Enter column and row where to dig: "; - std::cin >> x >> y; - tileMap[x][y] = 1; - } else if(input == "f"){ - int x, y; - std::cout << "Enter column and row where to place flag: "; - std::cin >> x >> y; - tileMap[x][y] = 10; - } else { - std::cout << "Unknown command '" << input << "'.\n"; + if(x < 0 || x >= boardWidth || y < 0 || y >= boardHeight) return; + if(cmd == "d"){ + if(bombMap[y][x]){ + std::cout << "BOOM! Game over\n"; + inGame = false; + return; + } + + if(!revealed[y][x]){ + revealed[y][x] = true; + if(bombCheck(x, y) == 0){ + for(int dy = -1; dy <= 1; dy++){ + for(int dx = -1; dx <= 1; dx++){ + int nx = x + dx; + int ny = y + dy; + + if(nx >= 0 && nx < boardWidth && ny >= 0 && ny < boardHeight && !revealed[ny][nx]){ + revealed[ny][nx] = true; + } + } + } + } + } } } |
