Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CW4.src;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.GridLayout;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- public class Psyjl9Main {
- final static int WIDTH = 60;
- final static int HEIGHT = 60;
- final static int NUM_MINES = 10;
- final static int BOARD_SIZE = 10;
- final static int JFRAME_DIMENSION = 60;
- public JFrame guiFrame;
- public Board board;
- public static void main(String[] args) {
- new Psyjl9Main();
- }
- public Psyjl9Main(){
- guiFrame = new JFrame();
- JPanel panel1 = new JPanel();
- guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- guiFrame.setTitle("MineSweeper");
- guiFrame.setResizable(false);
- guiFrame.setLocationRelativeTo(null);
- guiFrame.setLayout(new BorderLayout(4, 4));
- panel1.setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
- panel1.setPreferredSize(new Dimension(WIDTH * BOARD_SIZE, HEIGHT * BOARD_SIZE));
- guiFrame.add(panel1, BorderLayout.CENTER);
- board = new Board(BOARD_SIZE);
- for(int y = 0; y < BOARD_SIZE; y++) {
- for(int x = 0; x < BOARD_SIZE; x++) {
- BoardSquareButton square = new BoardSquareButton(WIDTH, HEIGHT, Color.gray, x, y);
- board.storeButton(x, y, square);
- square.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- if(e.getButton() == MouseEvent.BUTTON1) {
- leftClicked(square.getXpos(), square.getYpos(), square);
- }
- if(e.getButton() == MouseEvent.BUTTON3) {
- rightClicked(square.getXpos(), square.getYpos(), square);
- }
- // System.out.println("square.isInvestigated: " + square.isInvestigated);
- // System.out.println("square.isMine: " + square.isMine);
- // System.out.println("square.isPotenMine: " + square.isPotenMine + "\n");
- if(board.hasWon()) {
- board.finished();
- JOptionPane.showMessageDialog(guiFrame, "You WIN");
- board.initaliseAll();
- }
- }
- });
- panel1.add(square);
- }
- }
- board.createMines(NUM_MINES);
- guiFrame.pack();
- guiFrame.setVisible(true);
- }
- protected void rightClicked(int x, int y, BoardSquareButton square) {
- if(square.isInvestigated) {
- return;
- }
- square.setPotenMine(!square.isPotenMine);
- if(square.isPotenMine) {
- square.setColor(Color.orange);
- }else {
- square.setColor(Color.gray);
- }
- }
- public void leftClicked(int x, int y, BoardSquareButton square) {
- if(square.isMine) {
- board.finished();
- JOptionPane.showMessageDialog(guiFrame, "You Lose");
- board.initaliseAll();
- }else {
- if(square.getText().equals("")) {
- return;
- }
- square.setColor(Color.GREEN);
- if(board.countSurrouding(x, y) == 0) {
- removeZeros(x, y, true);
- }else {
- square.setText("" + board.countSurrouding(x, y));
- }
- }
- square.isInvestigated = true;
- }
- private void removeZeros(int x, int y, boolean isZero) {
- BoardSquareButton square;
- for (int y1 = y-1; y1 <= y+1; y1++) {
- for (int x1 = x-1; x1 <= x+1; x1++) {
- if(y1 < 0 || y1 >= BOARD_SIZE) {
- continue;
- }else if(x1 < 0 || x1 >= BOARD_SIZE) {
- continue;
- }
- else if(board.getButton(x1,y1).isInvestigated) {
- continue;
- }
- square = board.getButton(x1, y1);
- square.setPotenMine(false);
- square.setInvestigated(true);
- if(isZero && board.countSurrouding(x1, y1) == 0) {
- square.setText("");
- square.setColor(Color.gray);
- removeZeros(x1, y1, true);
- }else {
- square.setText("" + board.countSurrouding(x1, y1));
- square.setColor(Color.green);
- }
- }
- }
- }
- public class Board {
- final int BOARD_SIZE;
- BoardSquareButton[][] buttonArr;
- public Board(int boardSize) {
- this.BOARD_SIZE = boardSize;
- buttonArr = new BoardSquareButton[boardSize][boardSize];
- }
- public BoardSquareButton getButton(int x, int y) {
- return buttonArr[x][y];
- }
- public void storeButton(int x, int y, BoardSquareButton button) {
- buttonArr[x][y] = button;
- }
- public void initaliseAll() {
- for (int y = 0; y < BOARD_SIZE; y++) {
- for (int x = 0; x < BOARD_SIZE; x++) {
- buttonArr[x][y].init();
- }
- }
- createMines(NUM_MINES);
- }
- public void createMines(int num) {
- int numMines = 0;
- int x = 0, y = 0;
- Random rand = new Random();
- while(numMines < num) {
- x = rand.nextInt(10000) % BOARD_SIZE;
- y = rand.nextInt(10000) % BOARD_SIZE;
- if(!buttonArr[x][y].isMine()) {
- buttonArr[x][y].setMine(true);
- numMines++;
- }
- }
- }
- public void finished() {
- for (int y = 0; y < BOARD_SIZE; y++) {
- for (int x = 0; x < BOARD_SIZE; x++) {
- if(buttonArr[x][y].isMine) {
- buttonArr[x][y].setColor(Color.RED);
- buttonArr[x][y].setText("M");
- }else {
- buttonArr[x][y].setText("" + countSurrouding(x, y));
- }
- }
- }
- }
- public boolean hasWon() {
- int countInves = 0;
- int countMines = 0;
- for (int y = 0; y < BOARD_SIZE; y++) {
- for (int x = 0; x < BOARD_SIZE; x++) {
- if(buttonArr[x][y].isInvestigated)
- countInves++;
- if(buttonArr[x][y].isPotenMine)
- countMines++;
- }
- }
- // System.out.println("countPot " + countInves);
- // System.out.println("countMines " + countMines);
- if(countInves == BOARD_SIZE * BOARD_SIZE - NUM_MINES) {
- if(countMines == NUM_MINES) {
- return true;
- }
- }
- return false;
- }
- public int countSurrouding(int x, int y){
- int total = 0;
- for (int y1 = y-1; y1 <= y+1; y1++) {
- for (int x1 = x-1; x1 <= x+1; x1++) {
- if(y1 < 0 || y1 >= BOARD_SIZE) {
- continue;
- }else if(x1 < 0 || x1 >= BOARD_SIZE) {
- continue;
- }
- if(buttonArr[x1][y1].isMine)
- total++;
- }
- }
- return total;
- }
- }
- @SuppressWarnings("serial")
- public class BoardSquareButton extends JButton {
- int xpos, ypos, mineNum;
- boolean isMine = false, isInvestigated = false, isPotenMine = false;
- Color c;
- public BoardSquareButton(int width, int height, Color c, int x, int y) {
- this.xpos = x;
- this.ypos = y;
- this.c = c;
- this.setFont(this.getFont().deriveFont(30.0f));
- this.setBackground(c);
- this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
- this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
- this.setText("?");
- }
- public void init() {
- this.setBackground(Color.GRAY); // Not sure if i need this yet
- this.setText("?");
- isMine = false;
- isInvestigated = false;
- isPotenMine = false;
- }
- public boolean isMine() {
- return isMine;
- }
- public void setMine(boolean isMine) {
- this.isMine = isMine;
- }
- public boolean isInvestigated() {
- return isInvestigated;
- }
- public void setInvestigated(boolean investigated) {
- this.isInvestigated = investigated;
- }
- public boolean isPotenMine() {
- return isPotenMine;
- }
- public void setPotenMine(boolean potenMine) {
- this.isPotenMine = potenMine;
- }
- public int getXpos() {
- return xpos;
- }
- public int getYpos() {
- return ypos;
- }
- public Color getColour() {
- return c;
- }
- public void setColor(Color c) {
- this.c = c;
- this.setBackground(c);
- }
- public int getMineNum() {
- return mineNum;
- }
- public void setMineNum(int mineNum) {
- this.mineNum = mineNum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement