Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.11 KB | None | 0 0
  1. package minesweeper;
  2.  
  3. import java.util.Observable;
  4. import java.util.Observer;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10. public class GameTAlpha extends Observable implements Game {
  11.  
  12.     private int bombs, cols, rows, timer;
  13.     private Field[][] fields;
  14.     private boolean locked;
  15.     private GameState state = GameState.running;
  16.  
  17.     @Override
  18.     public void start(int cols, int rows, int bombs) {
  19.         this.bombs = bombs;
  20.         this.cols = cols;
  21.         this.rows = rows;
  22.         if (cols < 1) {
  23.             error("Zu wenig Spalten - mindestens 1");
  24.         }
  25.         if (cols > 1000) {
  26.             error("Zu viele Spalten - maximal 1000");
  27.         }
  28.         if (rows > 1000) {
  29.             error("Zu viele Zeilen - maximal 1000");
  30.         }
  31.        
  32.         if (rows < 1) {
  33.             error("Zu wenig Zeilen - mindestens 1");
  34.         }
  35.         if (bombs > cols * rows) {
  36.             error("Zu viele Bomben - maximal " + (rows * cols-1));
  37.         }
  38.         if (bombs < 0) {
  39.             error("Minesweeper ohne Bomben? Dein Ernst?");
  40.         }
  41.         timer = 0;
  42.         Timer t = new Timer();
  43.         t.schedule(new TimerTask() {
  44.             @Override
  45.             public void run() {
  46.                 addToTimer();
  47.             }
  48.         }, 1000, 1000);
  49.  
  50.         setRandomMines();
  51.         update();
  52.  
  53.     }
  54.  
  55.     void setLost() {
  56.         state = GameState.lost;
  57.         for (int i = 0; i < cols; i++)
  58.             for (int k = 0; k < rows; k++) {
  59.             fields[i][k].uncover();
  60.             }
  61.         locked=true;
  62.     }
  63.  
  64.     void setWon() {
  65.         state = GameState.won;
  66.         locked=true;
  67.     }
  68.  
  69.     private void setRandomMines() {
  70.  
  71.         fields = new Field[cols][rows];
  72.         int btp = bombs;
  73.         // init field
  74.         for (int i = 0; i < cols; i++)
  75.             for (int k = 0; k < rows; k++) {
  76.                 fields[i][k] = new Field(false);
  77.             }
  78.         // set random mines
  79.         while (btp > 0) {
  80.             int rx = (int) (Math.random() * cols);
  81.             int ry = (int) (Math.random() * rows);
  82.  
  83.             if (!fields[rx][ry].isMine()) {
  84.                 fields[rx][ry] = new Field(true);
  85.                 btp--;
  86.             }
  87.         }
  88.         System.out.println("All Mines could be placed");
  89.  
  90.     }
  91.  
  92.     protected void addToTimer() {
  93.         if (!locked) {
  94.             timer++;
  95.             update();
  96.         }
  97.  
  98.     }
  99.  
  100.     private void update() {
  101.         setChanged();
  102.         notifyObservers();
  103.     }
  104.  
  105.     @Override
  106.     public int getBombsLeft() {
  107.         // bombs=bombs
  108.         int allflags = 0;
  109.         for (Field[] f : fields) {
  110.             for (Field ff : f) {
  111.                 if (ff.getState() == FieldState.flagged)
  112.                     allflags++;
  113.             }
  114.         }
  115.         return bombs - allflags;
  116.     }
  117.  
  118.     private void checkVictory() {
  119.         int covered=0;
  120.         for (Field[] f : fields) {
  121.             for (Field ff : f) {
  122.                 if (ff.getState()==FieldState.covered||ff.getState()==FieldState.flagged)
  123.                 {
  124.                     covered++;
  125.                 }
  126.             }
  127.         }
  128.         if (covered==bombs&&state.equals(GameState.running))
  129.         {
  130.             setWon();
  131.             update();
  132.         }
  133.     }
  134.  
  135.     @Override
  136.     public int getElapsedSeconds() {
  137.         return timer;
  138.     }
  139.  
  140.     public static void main(String[] args) {
  141.  
  142.         int cols = Integer.parseInt(JOptionPane.showInputDialog("Breite eingeben!", "20"));
  143.         int rows = Integer.parseInt(JOptionPane.showInputDialog("Höhe eingeben!", "20"));
  144.         int bombs = Integer.parseInt(JOptionPane.showInputDialog("Anzahl Minen eingeben!", "40"));
  145.         // fehlerbehandlung
  146.  
  147.         GameTAlpha main = new GameTAlpha();
  148.  
  149.         main.start(cols, rows, bombs);
  150.  
  151.     }
  152.  
  153.     private static void error(String message) {
  154.         throw new IllegalArgumentException(message);
  155.     }
  156.  
  157.     @Override
  158.     public void addStateObserver(Observer observer) {
  159.        
  160.         addObserver(observer);
  161.     }
  162.  
  163.     @Override
  164.     public boolean isMine(int col, int row) {
  165.         return fields[col][row].isMine();
  166.     }
  167.  
  168.     @Override
  169.     public FieldState getFieldState(int col, int row) {
  170.         return fields[col][row].getState();
  171.     }
  172.  
  173.     @Override
  174.     public void uncover(int col, int row) {
  175.         //TODO: flip adjacent if empty
  176.         if (!locked)
  177.         {
  178.         if (fields[col][row].getState()==FieldState.covered)
  179.         {
  180.         fields[col][row].uncover();
  181.         if (fields[col][row].isMine())
  182.             setLost();
  183.        
  184.         if (getNeighborMineCount(col, row)==0)
  185.         {
  186.             try{
  187.             uncoverplain(col-1,row-1);
  188.             } catch(IndexOutOfBoundsException e)
  189.             {}
  190.             try{
  191.             uncoverplain(col-1,row);
  192.             } catch(IndexOutOfBoundsException e)
  193.             {}
  194.             try{
  195.             uncoverplain(col-1,row+1);
  196.             } catch(IndexOutOfBoundsException e)
  197.             {}
  198.             try{
  199.             uncoverplain(col,row-1);
  200.             } catch(IndexOutOfBoundsException e)
  201.             {}
  202.             try{
  203.             uncoverplain(col,row+1);
  204.             } catch(IndexOutOfBoundsException e)
  205.             {}
  206.             try{
  207.             uncoverplain(col+1,row-1);
  208.             } catch(IndexOutOfBoundsException e)
  209.             {}
  210.             try{
  211.             uncoverplain(col+1,row);
  212.             } catch(IndexOutOfBoundsException e)
  213.             {}
  214.             try{
  215.             uncoverplain(col+1,row+1);
  216.             } catch(IndexOutOfBoundsException e)
  217.             {}
  218.            
  219.         }
  220.         }
  221.         checkVictory();
  222.         update();
  223.         }
  224.  
  225.     }
  226.     public void uncoverplain(int col, int row) {
  227.         //TODO: flip adjacent if empty
  228.         if (!locked)
  229.         {
  230.         if (fields[col][row].getState()==FieldState.covered)
  231.         {
  232.         fields[col][row].uncover();
  233.         if (fields[col][row].isMine())
  234.             setLost();
  235.        
  236.         if (getNeighborMineCount(col, row)==0)
  237.         {
  238.             try{
  239.             uncoverplain(col-1,row-1);
  240.             } catch(IndexOutOfBoundsException e)
  241.             {}
  242.             try{
  243.             uncoverplain(col-1,row);
  244.             } catch(IndexOutOfBoundsException e)
  245.             {}
  246.             try{
  247.             uncoverplain(col-1,row+1);
  248.             } catch(IndexOutOfBoundsException e)
  249.             {}
  250.             try{
  251.             uncoverplain(col,row-1);
  252.             } catch(IndexOutOfBoundsException e)
  253.             {}
  254.             try{
  255.             uncoverplain(col,row+1);
  256.             } catch(IndexOutOfBoundsException e)
  257.             {}
  258.             try{
  259.             uncoverplain(col+1,row-1);
  260.             } catch(IndexOutOfBoundsException e)
  261.             {}
  262.             try{
  263.             uncoverplain(col+1,row);
  264.             } catch(IndexOutOfBoundsException e)
  265.             {}
  266.             try{
  267.             uncoverplain(col+1,row+1);
  268.             } catch(IndexOutOfBoundsException e)
  269.             {}
  270.            
  271.         }
  272.         }
  273.         checkVictory();
  274.         }
  275.  
  276.     }
  277.  
  278.     @Override
  279.     public void toggleFlag(int col, int row) {
  280.         fields[col][row].toggleFlag();
  281.         update();
  282.     }
  283.  
  284.     @Override
  285.     public GameState getState() {
  286.         return state;
  287.     }
  288.  
  289.     @Override
  290.     public int getColCount() {
  291.         return cols;
  292.     }
  293.  
  294.     @Override
  295.     public int getRowCount() {
  296.         return rows;
  297.     }
  298.  
  299.     @Override
  300.     public int getNeighborMineCount(int col, int row) throws IndexOutOfBoundsException {
  301.         if (col<0||col>cols||row<0||row>rows)
  302.             throw new IndexOutOfBoundsException("");
  303.         int c=0;
  304.         // xoooooooo
  305.         try {
  306.             if (fields[col - 1][row-1].isMine()) {
  307.                 c++;
  308.             }
  309.         } catch (IndexOutOfBoundsException e) {
  310.         }
  311.         // xoooooooo
  312.                 try {
  313.                     if (fields[col - 1][row].isMine()) {
  314.                         c++;
  315.                     }
  316.                 } catch (IndexOutOfBoundsException e) {
  317.                 }
  318.                 // xoooooooo
  319.                 try {
  320.                     if (fields[col - 1][row+1].isMine()) {
  321.                         c++;
  322.                     }
  323.                 } catch (IndexOutOfBoundsException e) {
  324.                 }// xoooooooo
  325.                 try {
  326.                     if (fields[col][row-1].isMine()) {
  327.                         c++;
  328.                     }
  329.                 } catch (IndexOutOfBoundsException e) {
  330.                 }// xoooooooo
  331.                 try {
  332.                     if (fields[col][row+1].isMine()) {
  333.                         c++;
  334.                     }
  335.                 } catch (IndexOutOfBoundsException e) {
  336.                 }// xoooooooo
  337.                 try {
  338.                     if (fields[col+1][row-1].isMine()) {
  339.                         c++;
  340.                     }
  341.                 } catch (IndexOutOfBoundsException e) {
  342.                 }// xoooooooo
  343.                 try {
  344.                     if (fields[col+1][row].isMine()) {
  345.                         c++;
  346.                     }
  347.                 } catch (IndexOutOfBoundsException e) {
  348.                 }// xoooooooo
  349.                 try {
  350.                     if (fields[col+1][row+1].isMine()) {
  351.                         c++;
  352.                     }
  353.                 } catch (IndexOutOfBoundsException e) {
  354.                 }
  355.                 return c;
  356.            
  357.     }
  358.  
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement