Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.54 KB | None | 0 0
  1. package CW4.src;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.GridLayout;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15.  
  16. public class Psyjl9Main {
  17.    
  18.     final static int WIDTH = 60;
  19.     final static int HEIGHT = 60;
  20.     final static int NUM_MINES = 10;
  21.     final static int BOARD_SIZE = 10;
  22.    
  23.     final static int JFRAME_DIMENSION = 60;
  24.     public JFrame guiFrame;
  25.     public Board board;
  26.    
  27.     public static void main(String[] args) {
  28.         new Psyjl9Main();
  29.  
  30.     }
  31.    
  32.     public Psyjl9Main(){
  33.         guiFrame = new JFrame();
  34.  
  35.         JPanel panel1 = new JPanel();
  36.        
  37.         guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.         guiFrame.setTitle("MineSweeper");
  39.         guiFrame.setResizable(false);
  40.         guiFrame.setLocationRelativeTo(null);
  41.        
  42.         guiFrame.setLayout(new BorderLayout(4, 4));
  43.         panel1.setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
  44.         panel1.setPreferredSize(new Dimension(WIDTH * BOARD_SIZE, HEIGHT * BOARD_SIZE));
  45.        
  46.         guiFrame.add(panel1, BorderLayout.CENTER);
  47.        
  48.         board = new Board(BOARD_SIZE);
  49.         for(int y = 0; y < BOARD_SIZE; y++) {
  50.             for(int x = 0; x < BOARD_SIZE; x++) {
  51.                 BoardSquareButton square = new BoardSquareButton(WIDTH, HEIGHT, Color.gray, x, y);
  52.                 board.storeButton(x, y, square);
  53.                 square.addMouseListener(new MouseAdapter() {
  54.                     @Override
  55.                      public void mousePressed(MouseEvent e) {
  56.                         if(e.getButton() == MouseEvent.BUTTON1) {
  57.                             leftClicked(square.getXpos(), square.getYpos(), square);
  58.                         }
  59.                         if(e.getButton() == MouseEvent.BUTTON3) {
  60.                             rightClicked(square.getXpos(), square.getYpos(), square);
  61.                         }
  62.                     //  System.out.println("square.isInvestigated: " + square.isInvestigated);
  63.                     //  System.out.println("square.isMine: " + square.isMine);
  64.                     //  System.out.println("square.isPotenMine: " + square.isPotenMine + "\n");
  65.                        
  66.                         if(board.hasWon()) {
  67.                             board.finished();
  68.                             JOptionPane.showMessageDialog(guiFrame, "You WIN");
  69.                             board.initaliseAll();
  70.                         }
  71.                      }
  72.                 });
  73.                 panel1.add(square);
  74.             }
  75.         }
  76.        
  77.         board.createMines(NUM_MINES);
  78.         guiFrame.pack();
  79.         guiFrame.setVisible(true);
  80.     }
  81.    
  82.     protected void rightClicked(int x, int y, BoardSquareButton square) {
  83.         if(square.isInvestigated) {
  84.             return;
  85.         }
  86.         square.setPotenMine(!square.isPotenMine);
  87.         if(square.isPotenMine) {
  88.             square.setColor(Color.orange);
  89.         }else {
  90.             square.setColor(Color.gray);
  91.         }
  92.     }
  93.  
  94.     public void leftClicked(int x, int y, BoardSquareButton square) {
  95.         if(square.isMine) {
  96.             board.finished();
  97.             JOptionPane.showMessageDialog(guiFrame, "You Lose");
  98.             board.initaliseAll();
  99.         }else {
  100.             if(square.getText().equals("")) {
  101.                 return;
  102.             }
  103.             square.setColor(Color.GREEN);
  104.             if(board.countSurrouding(x, y) == 0) {
  105.                 removeZeros(x, y, true);
  106.             }else {
  107.                 square.setText("" + board.countSurrouding(x, y));
  108.             }
  109.            
  110.         }
  111.        
  112.         square.isInvestigated = true;
  113.     }
  114.    
  115.     private void removeZeros(int x, int y, boolean isZero) {
  116.         BoardSquareButton square;
  117.         for (int y1 = y-1; y1 <= y+1; y1++) {
  118.             for (int x1 = x-1; x1 <= x+1; x1++) {
  119.                 if(y1 < 0 || y1 >= BOARD_SIZE) {
  120.                     continue;
  121.                 }else if(x1 < 0 || x1 >= BOARD_SIZE) {
  122.                     continue;
  123.                 }
  124.                 else if(board.getButton(x1,y1).isInvestigated) {
  125.                     continue;
  126.                 }
  127.                 square = board.getButton(x1, y1);
  128.                 square.setPotenMine(false);
  129.                 square.setInvestigated(true);
  130.                 if(isZero && board.countSurrouding(x1, y1) == 0) {
  131.                     square.setText("");
  132.                     square.setColor(Color.gray);
  133.                     removeZeros(x1, y1, true);
  134.                 }else {
  135.                     square.setText("" + board.countSurrouding(x1, y1));
  136.                     square.setColor(Color.green);
  137.                    
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     public class Board {
  144.         final int BOARD_SIZE;
  145.         BoardSquareButton[][] buttonArr;
  146.        
  147.         public Board(int boardSize) {
  148.             this.BOARD_SIZE = boardSize;
  149.              buttonArr = new BoardSquareButton[boardSize][boardSize];
  150.         }
  151.        
  152.         public BoardSquareButton getButton(int x, int y) {
  153.             return buttonArr[x][y];
  154.         }
  155.        
  156.         public void storeButton(int x, int y, BoardSquareButton button) {
  157.             buttonArr[x][y] = button;
  158.         }
  159.        
  160.         public void initaliseAll() {
  161.             for (int y = 0; y < BOARD_SIZE; y++) {
  162.                 for (int x = 0; x < BOARD_SIZE; x++) {
  163.                     buttonArr[x][y].init();
  164.                    
  165.                 }
  166.             }
  167.             createMines(NUM_MINES);
  168.         }
  169.        
  170.         public void createMines(int num) {
  171.             int numMines = 0;
  172.             int x = 0, y = 0;
  173.             Random rand = new Random();
  174.             while(numMines < num) {
  175.                 x = rand.nextInt(10000) % BOARD_SIZE;
  176.                 y = rand.nextInt(10000) % BOARD_SIZE;
  177.                 if(!buttonArr[x][y].isMine()) {
  178.                     buttonArr[x][y].setMine(true);
  179.                     numMines++;
  180.                 }
  181.             }
  182.         }
  183.        
  184.         public void finished() {
  185.             for (int y = 0; y < BOARD_SIZE; y++) {
  186.                 for (int x = 0; x < BOARD_SIZE; x++) {
  187.                     if(buttonArr[x][y].isMine) {
  188.                         buttonArr[x][y].setColor(Color.RED);
  189.                         buttonArr[x][y].setText("M");
  190.                     }else {
  191.                         buttonArr[x][y].setText("" + countSurrouding(x, y));
  192.                     }
  193.                 }
  194.             }
  195.         }
  196.        
  197.         public boolean hasWon() {
  198.             int countInves = 0;
  199.             int countMines = 0;
  200.             for (int y = 0; y < BOARD_SIZE; y++) {
  201.                 for (int x = 0; x < BOARD_SIZE; x++) {
  202.                     if(buttonArr[x][y].isInvestigated)
  203.                         countInves++;
  204.                     if(buttonArr[x][y].isPotenMine)
  205.                         countMines++;
  206.                 }
  207.             }
  208.         //  System.out.println("countPot " + countInves);
  209.         //  System.out.println("countMines " + countMines);
  210.            
  211.             if(countInves == BOARD_SIZE * BOARD_SIZE - NUM_MINES) {
  212.                 if(countMines == NUM_MINES) {
  213.                     return true;
  214.                 }
  215.             }
  216.             return false;
  217.         }
  218.        
  219.         public int countSurrouding(int x, int y){
  220.             int total = 0;
  221.             for (int y1 = y-1; y1 <= y+1; y1++) {
  222.                 for (int x1 = x-1; x1 <= x+1; x1++) {
  223.                     if(y1 < 0 || y1 >= BOARD_SIZE) {
  224.                         continue;
  225.                     }else if(x1 < 0 || x1 >= BOARD_SIZE) {
  226.                         continue;
  227.                     }
  228.                     if(buttonArr[x1][y1].isMine)
  229.                         total++;
  230.                 }
  231.             }
  232.             return total;
  233.         }
  234.        
  235.        
  236.     }
  237.    
  238.     @SuppressWarnings("serial")
  239.     public class BoardSquareButton extends JButton {
  240.         int xpos, ypos, mineNum;
  241.         boolean isMine = false, isInvestigated = false, isPotenMine = false;
  242.         Color c;
  243.        
  244.         public BoardSquareButton(int width, int height, Color c, int x, int y) {
  245.             this.xpos = x;
  246.             this.ypos = y;
  247.             this.c = c;
  248.             this.setFont(this.getFont().deriveFont(30.0f));
  249.             this.setBackground(c);
  250.             this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
  251.             this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  252.             this.setText("?");
  253.         }
  254.        
  255.         public void init() {
  256.             this.setBackground(Color.GRAY); // Not sure if i need this yet
  257.             this.setText("?");
  258.             isMine = false;
  259.             isInvestigated = false;
  260.             isPotenMine = false;
  261.         }
  262.  
  263.         public boolean isMine() {
  264.             return isMine;
  265.         }
  266.  
  267.         public void setMine(boolean isMine) {
  268.             this.isMine = isMine;
  269.         }
  270.  
  271.         public boolean isInvestigated() {
  272.             return isInvestigated;
  273.         }
  274.  
  275.         public void setInvestigated(boolean investigated) {
  276.             this.isInvestigated = investigated;
  277.         }
  278.  
  279.         public boolean isPotenMine() {
  280.             return isPotenMine;
  281.         }
  282.  
  283.         public void setPotenMine(boolean potenMine) {
  284.             this.isPotenMine = potenMine;
  285.         }
  286.  
  287.         public int getXpos() {
  288.             return xpos;
  289.         }
  290.  
  291.         public int getYpos() {
  292.             return ypos;
  293.         }
  294.        
  295.         public Color getColour() {
  296.             return c;
  297.         }
  298.        
  299.         public void setColor(Color c) {
  300.             this.c = c;
  301.             this.setBackground(c);
  302.         }
  303.  
  304.         public int getMineNum() {
  305.             return mineNum;
  306.         }
  307.  
  308.         public void setMineNum(int mineNum) {
  309.             this.mineNum = mineNum;
  310.         }
  311.        
  312.     }
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement