Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JMine - Game.java

Game.java

Caricato da:
Scarica il programma completo

  1. package jmine;
  2.  
  3. import java.util.Random;
  4. import jmine.exceptions.IllegalNumOfMine;
  5.  
  6. public class Game {
  7.  
  8.     private Box[][] ground;
  9.     private int width;
  10.     private int height;
  11.     private int numOfMine;
  12.     private boolean gameOver;
  13.  
  14.     public Game(int pWidth, int pHeight, int pNumOfMine) throws IllegalNumOfMine {
  15.         if (pNumOfMine > (pWidth * pHeight)) {
  16.             throw new IllegalNumOfMine();
  17.         }
  18.         this.ground = new Box[pHeight][pWidth];
  19.         this.width = pWidth;
  20.         this.height = pHeight;
  21.         this.numOfMine = pNumOfMine;
  22.         this.gameOver = false;
  23.         this.newGame();
  24.     }
  25.  
  26.     public void newGame() {
  27.         this.gameOver=false;
  28.         Random randomizer = new Random();
  29.         for (int i = 0; i < this.width; i++) {
  30.             for (int j = 0; j < this.height; j++) {
  31.                 this.ground[j][i] = new Box();
  32.             }
  33.         }
  34.         int randI = 0;
  35.         int randJ = 0;
  36.         for (int i = 0; i < this.numOfMine; i++) {
  37.             do {
  38.                 randI = randomizer.nextInt(this.height);
  39.                 randJ = randomizer.nextInt(this.width);
  40.             } while (this.ground[randI][randJ].isMine());
  41.             this.ground[randI][randJ].setMine(true);
  42.             if (randI - 1 >= 0 && randJ - 1 >= 0) {
  43.                 this.ground[randI - 1][randJ - 1].upNumOfMine();
  44.             }
  45.             if (randI - 1 >= 0) {
  46.                 this.ground[randI - 1][randJ].upNumOfMine();
  47.             }
  48.             if (randI - 1 >= 0 && randJ + 1 < this.width) {
  49.                 this.ground[randI - 1][randJ + 1].upNumOfMine();
  50.             }
  51.             if (randJ - 1 >= 0) {
  52.                 this.ground[randI][randJ - 1].upNumOfMine();
  53.             }
  54.             if (randJ + 1 < this.width) {
  55.                 this.ground[randI][randJ + 1].upNumOfMine();
  56.             }
  57.             if (randI + 1 < this.height && randJ - 1 >= 0) {
  58.                 this.ground[randI + 1][randJ - 1].upNumOfMine();
  59.             }
  60.             if (randI + 1 < this.height) {
  61.                 this.ground[randI + 1][randJ].upNumOfMine();
  62.             }
  63.             if (randI + 1 < this.height && randJ + 1 < this.width) {
  64.                 this.ground[randI + 1][randJ + 1].upNumOfMine();
  65.             }
  66.         }
  67.     }
  68.  
  69.     public Box[][] getGround() {
  70.         return this.ground;
  71.     }
  72.  
  73.     public int getHeight() {
  74.         return this.height;
  75.     }
  76.  
  77.     public int getNumOfMine() {
  78.         return this.numOfMine;
  79.     }
  80.  
  81.     public int getWidth() {
  82.         return this.width;
  83.     }
  84.  
  85.     public boolean getGameOver() {
  86.         return this.gameOver;
  87.     }
  88.  
  89.     public void flag(int pX, int pY) {
  90.         if (this.ground[pX][pY].isCover()) {
  91.             if (this.ground[pX][pY].isFlagged()) {
  92.                 this.ground[pX][pY].setFlagged(false);
  93.             } else {
  94.                 this.ground[pX][pY].setFlagged(true);
  95.             }
  96.         }
  97.     }
  98.  
  99.     public boolean isWinner(){
  100.         for(int i=0; i<this.width; i++){
  101.             for(int j=0; j<this.height; j++){
  102.                 if(this.ground[i][j].isCover()){
  103.                     if(!this.ground[i][j].isMine()){
  104.                         return false;
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         return true;
  110.     }
  111.  
  112.     public void unconver(int pX, int pY) {
  113.         if (this.ground[pX][pY].isCover() && !this.ground[pX][pY].isFlagged()) {
  114.             if (this.ground[pX][pY].isMine()) {
  115.                 this.gameOver = true;
  116.             } else {
  117.                 this.ground[pX][pY].setCover(false);
  118.                 if (this.ground[pX][pY].getNumOfMine() == 0) {
  119.                     if (pX - 1 >= 0 && pY - 1 >= 0) {
  120.                         this.unconver(pX - 1, pY - 1);
  121.                     }
  122.                     if (pX - 1 >= 0) {
  123.                         this.unconver(pX - 1, pY);
  124.                     }
  125.                     if (pX - 1 >= 0 && pY + 1 < this.width) {
  126.                         this.unconver(pX - 1, pY + 1);
  127.                     }
  128.                     if (pY - 1 >= 0) {
  129.                         this.unconver(pX, pY - 1);
  130.                     }
  131.                     if (pY + 1 < this.width) {
  132.                         this.unconver(pX, pY + 1);
  133.                     }
  134.                     if (pX + 1 < this.height && pY - 1 >= 0) {
  135.                         this.unconver(pX + 1, pY - 1);
  136.                     }
  137.                     if (pX + 1 < this.height) {
  138.                         this.unconver(pX + 1, pY);
  139.                     }
  140.                     if (pX + 1 < this.height && pY + 1 < this.width) {
  141.                         this.unconver(pX + 1, pY + 1);
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.     }
  147. }