Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Plansza.h"
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- Plansza::Plansza(int wysokosc, int szeroksc) : koniec_gry(false) {
- srand(time(0));
- if(wysokosc < 1 || szeroksc < 1) {
- M = 10;
- N = 10;
- }
- M = wysokosc;
- N = szeroksc;
- plansza = new Pole*[M];
- for(int i = 0; i < M; i++) {
- plansza[i] = new Pole[N];
- }
- }
- Plansza::~Plansza() {
- for(int i = 0; i < M; i++) {
- delete[] plansza[i];
- }
- delete[] plansza;
- }
- void Plansza::deployMines(int n, bool random) {
- int ostatnie_x = 0;
- int ostatnie_y = 0;
- int rand_x = rand() % N;
- int rand_y = rand() % M;
- // losowe
- if(random) {
- for(int i = 0; i < n; i++) {
- do {
- rand_x = rand() % N;
- rand_y = rand() % M;
- } while ((rand_x == ostatnie_x) && (rand_y == ostatnie_y));
- plansza[rand_y][rand_x].ustawMina(true);
- ostatnie_x = rand_x;
- ostatnie_y = rand_y;
- }
- } else { // nielosowe
- for(int i = 0; i < N; i++) {
- plansza[0][i].ustawMina(true);
- }
- for(int i = 0; i < M; i++) {
- for(int j = 0; j < N; j++) {
- plansza[i][i].ustawMina(true);
- }
- }
- }
- }
- void Plansza::debug_display() {
- for(int i = 0; i < M; i++) {
- for(int j = 0; j < N; j++) {
- plansza[i][j].info();
- }
- std::cout << std::endl;
- }
- }
- bool Plansza::hasMine(int x, int y) {
- if(x >= 0 && x < N && y >= 0 && y < M)
- return plansza[y][x].odczytajMina();
- else {
- //std::cout << "Niepoprawna wspolrzedna: x: " << x << ", y:" << y << std::endl;
- false;
- }
- }
- int Plansza::countMines(int x, int y) {
- int licznik = 0;
- for (int i = y - 1; i <= y + 1; i++) {
- for (int j = x - 1; j <= x + 1; j++) {
- //std::cout << "X: " << j << "\tY: " << i << std::endl;
- if (hasMine(j, i)) {
- //std::cout << "X: " << j << ", Y: " << i << " has mine\n";
- std::cout.flush();
- licznik++;
- if (i == y && j == x)
- licznik--;
- }
- }
- }
- //std::cout << "Licznik: " << licznik << std::endl;
- return licznik;
- }
- void Plansza::display() {
- int min_w_sasiedztwie = 0;
- for(int i = 0; i < M; i++) {
- for(int j = 0; j < N; j++) {
- min_w_sasiedztwie = countMines(j, i);
- //std::cout << "X: " << j << ", Y: " << i << " min w sasiedztwie: " << min_w_sasiedztwie << std::endl;
- if(!plansza[i][j].odczytajFlaga() && plansza[i][j].odczytajUkryte()) {
- std::cout << "[.]";
- }
- else if(plansza[i][j].odczytajFlaga() && plansza[i][j].odczytajUkryte()) {
- std::cout << "[?]";
- }
- else if(!plansza[i][j].odczytajUkryte() && hasMine(j, i)) {
- std::cout << "[x]";
- }
- else if(!plansza[i][j].odczytajUkryte()) {
- if(countMines(j, i) == 0)
- std::cout << "[ ]";
- else
- std::cout << "[" << min_w_sasiedztwie << "]";
- }
- //std::cout << j;
- min_w_sasiedztwie = 0;
- }
- std::cout << std::endl;
- }
- }
- */
- bool Plansza::reveal(int x, int y) {
- if(koniec_gry) {
- std::cout << "Przegrales" << std::endl;
- return false;
- }
- plansza[y][x].ustawUkryte(false);
- if(plansza[y][x].odczytajMina()) {
- koniec_gry = true;
- return false;
- }
- return true;
- }
Add Comment
Please, Sign In to add comment