#include "Game.hpp" #include #include #include #include void Game::initDiff(int diff){ switch(diff){ case 0: boardWidth = 9; boardHeight = 9; mines = 9; break; case 1: boardWidth = 16; boardHeight = 16; mines = 12; break; case 2: boardWidth = 30; boardHeight = 16; mines = 16; break; case 3: std::cout << "Editing game parameters\n"; break; } } 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; } } // 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; } } } // initializing tile map for display for(int i=0; i < boardHeight; i++){ for(int j=0; j < boardWidth; j++){ tileMap[i][j] = 0; // DEBUG: seeing bombs // if(bombMap[i][j] == true){ // tileMap[i][j] = 11; // } } } } void Game::updateBoard(){ for(int x=0; x < boardWidth; x++){ for(int y=0; y < boardHeight; y++){ if(Game::bombCheck(x, y) == 0){ tileMap[x][y] = 1; } } } } int Game::editDiff(){ std::cout << "Now editing custom difficulty" << std::endl; std::cout << "Board width: "; std::cin >> boardWidth; if(boardWidth > MAX_W){ std::cout << "Board width can't be more than " << MAX_W << " tiles." << std::endl; return 1; } std::cout << "Board height: "; std::cin >> boardHeight; if(boardHeight > MAX_H){ std::cout << "Board height can't be more than " << MAX_H << " tiles." << std::endl; return 1; } std::cout << "Mines: "; std::cin >> mines; if(mines > 8 || mines > (boardWidth * boardHeight)){ std::cout << "There can't be more than 8 or " << (boardWidth * boardHeight) << "mines." << std::endl; return 1; } return 0; } /* Tile map: 0 - unrevealed tile 1 - revealed tile 2 - revealed tile with 1 bomb around 3 - revealed tile with 2 bomb around 4 - revealed tile with 3 bomb around 5 - revealed tile with 4 bomb around 6 - revealed tile with 5 bomb around 7 - revealed tile with 6 bomb around 8 - revealed tile with 7 bomb around 9 - revealed tile with 8 bomb around 10 - flag 11 - bomb (for DEBUGGING) */ void Game::displayBoard() { #ifdef _WIN32 system("cls"); #else 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'); } } putchar('\n'); } } int Game::bombCheck(int x, int y){ int bombsAround = 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++; } } return bombsAround; } void Game::input(){ std::string input; std::cout << "game>"; std::cin >> input; if(input == "q"){ inGame = false; } 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"; } }