Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --------------------
- // Tree of project:
- //
- // jsx.Constants.java
- // jsx.Minesweeper.java
- // jsx.state.GameState.java
- // jsx.state.Flag.java
- // jsx.state.Direction.java
- // jsx.obj.Cell.java
- // jsx.obj.Field.java
- // --------------------
- // TODO (maybe):
- // - add mouse in second thread
- // - set UI
- // - draw images
- // - set auto opening empty cells at usual open and when doubleckicking on numbers
- //-------------------------------------------------------------------------------
- package jsx;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.WindowConstants;
- import jsx.obj.Cell;
- import jsx.obj.Field;
- import jsx.state.Direction;
- import jsx.state.Flag;
- import jsx.state.GameState;
- public class Minesweeper extends JPanel {
- Field field;
- public Minesweeper() {
- setPreferredSize(new Dimension(342, 364));
- setFocusable(true);
- addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- init();
- } else {
- if (field.getGameState() == GameState.GAME) {
- if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- field.click(false);
- repaint();
- } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
- field.click(true);
- repaint();
- } else if (e.getKeyCode() == KeyEvent.VK_UP) {
- field.move(Direction.UP);
- repaint();
- } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
- field.move(Direction.RIGHT);
- repaint();
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- field.move(Direction.DOWN);
- repaint();
- } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
- field.move(Direction.LEFT);
- repaint();
- }
- } else {
- init();
- }
- }
- }
- });
- init();
- }
- void init() {
- this.field = new Field();
- repaint();
- }
- public int getTextColor(Cell cel) {
- if (cel.getFlag() == Flag.MARKED)
- return 0xffffff;
- switch (cel.getValue()) {
- case 1:
- return 0x002db7;
- case 2:
- return 0x167a00;
- case 3:
- return 0xb70000;
- case 4:
- return 0x000aa3;
- case 5:
- return 0xb70000;
- case 6:
- return 0x8cb8ff;
- case 7:
- return 0xb70000;
- case 8:
- return 0x9300a0;
- }
- return 0;
- }
- public int getTileColor(Cell cel) {
- switch (cel.getFlag()) {
- case CRUSHED:
- return 0xba2500;
- case ERROR:
- return 0xfffb1e;
- case MARKED:
- case NONE:
- return 0x2b66c4;
- case OPEN:
- return 0xddeaff;
- case VERIFIED:
- return 0x16bc00;
- }
- return 0;
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(new Color(255, 223, 94));
- g.fillRect(0, 0, getWidth(), getHeight());
- if (field.getGameState() == GameState.WIN) {
- field.show(true);
- } else if (field.getGameState() == GameState.LOSE) {
- field.show(false);
- }
- for (int x = 0; x < Constants.XGRD; x++) {
- int oX = x * (Constants.SIZE + Constants.DIST) + Constants.BORD;
- for (int y = 0; y < Constants.YGRD; y++) {
- int oY = y * (Constants.SIZE + Constants.DIST) + Constants.BORD;
- drawTile((Graphics2D) g, field.get(x, y), oX, oY, field.isSelected(x, y));
- }
- }
- if (field.getGameState() != GameState.GAME) {
- g.setColor(new Color(255, 255, 255, 2));
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(new Color(44, 44, 44));
- Font f = new Font("Arial", Font.BOLD, 80);
- g.setFont(f);
- FontMetrics fm = getFontMetrics(f);
- int w = fm.stringWidth(field.getGameState() == GameState.WIN ? "Win!" : "Lose!");
- g.drawString(field.getGameState() == GameState.WIN ? "Win!" : "Lose!", (getWidth() - w) / 2, 188);
- }
- }
- void drawTileText(Graphics2D g, int oX, int oY, String text, Color color) {
- g.setColor(color);
- Font font = new Font("Cambria", Font.BOLD, 20);
- g.setFont(font);
- FontMetrics fm = getFontMetrics(font);
- int w = fm.stringWidth(text);
- int h = -(int) fm.getLineMetrics(text, g).getBaselineOffsets()[2];
- g.drawString(text, oX + (Constants.SIZE - w) / 2, oY + Constants.SIZE - (Constants.SIZE - h) / 2 - 2);
- }
- void drawTile(Graphics2D g, Cell cel, int x, int y, boolean b) {
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
- if (b) {
- g.setColor(new Color(0x000000));
- g.fillRect(x, y, Constants.SIZE, Constants.SIZE);
- g.setColor(new Color(getTileColor(cel)));
- g.fillRect(x + 2, y + 2, Constants.SIZE - 4, Constants.SIZE - 4);
- } else {
- g.setColor(new Color(getTileColor(cel)));
- g.fillRect(x, y, Constants.SIZE, Constants.SIZE);
- }
- if (cel.getFlag() == Flag.MARKED) {
- drawTileText(g, x, y, "?", new Color(getTextColor(cel)));
- } else if (cel.getFlag() == Flag.OPEN && cel.getValue() > 0) {
- drawTileText(g, x, y, String.valueOf(cel.getValue()), new Color(getTextColor(cel)));
- }
- }
- public static void main(String[] args) {
- JFrame game = new JFrame();
- game.setTitle("Minesweeper 2");
- game.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- game.setSize(342, 364);
- game.setResizable(false);
- game.add(new Minesweeper());
- game.setLocationRelativeTo(null);
- game.setVisible(true);
- }
- }
- //-------------------------------------------------------------------------------
- package jsx;
- public class Constants {
- public static final int DIST = 2; // margin
- public static final int SIZE = 32; // tile size
- public static final int BORD = 16; // border
- public static final int XGRD = 9; // width of grid
- public static final int YGRD = 9; // height of grid
- public static final int MINS = 12; // mines in grid
- }
- //-------------------------------------------------------------------------------
- package jsx.state;
- public enum GameState {
- GAME, WIN, LOSE;
- }
- //-------------------------------------------------------------------------------
- package jsx.state;
- public enum Flag {
- NONE, OPEN, MARKED, CRUSHED, ERROR, VERIFIED;
- }
- //-------------------------------------------------------------------------------
- package jsx.state;
- public enum Direction {
- UP, RIGHT, DOWN, LEFT;
- }
- //-------------------------------------------------------------------------------
- package jsx.obj;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import jsx.Constants;
- import jsx.state.Direction;
- import jsx.state.Flag;
- import jsx.state.GameState;
- public class Field {
- Cell[][] field = new Cell[Constants.XGRD][Constants.YGRD];
- int condition = 0;
- Random random = new Random();
- GameState state = GameState.GAME;
- int x = 0; // x of selection
- int y = 0; // y of selection
- public Field() {
- // creating field cells
- for (int x = 0; x < Constants.XGRD; x++) {
- for (int y = 0; y < Constants.YGRD; y++) {
- field[x][y] = new Cell();
- }
- }
- // inserting mines
- for (int i = 0; i < Constants.MINS + 1; i++) {
- int x = random.nextInt(Constants.XGRD);
- int y = random.nextInt(Constants.YGRD);
- if (field[x][y].isMined()) {
- i--;
- } else {
- field[x][y].setMined();
- }
- }
- // getting win condition
- for (int x = 0; x < Constants.XGRD; x++)
- for (int y = 0; y < Constants.YGRD; y++)
- if (!field[x][y].isMined()) {
- List<Cell> temp = getNearby(x, y);
- for (Cell cell : temp)
- if (cell.isMined())
- field[x][y].addValue();
- }
- for (int x = 0; x < Constants.XGRD; x++)
- for (int y = 0; y < Constants.YGRD; y++)
- if (field[x][y].getValue() > 0)
- condition++;
- }
- public Cell get(int x, int y) {
- return field[x][y];
- }
- public void checkWin() {
- int counter = 0;
- for (int x = 0; x < Constants.XGRD; x++)
- for (int y = 0; y < Constants.YGRD; y++)
- if (field[x][y].getFlag() == Flag.OPEN && field[x][y].getValue() > 0)
- counter++;
- if (counter == condition)
- state = GameState.WIN;
- }
- public GameState getGameState() {
- return state;
- }
- List<Cell> getNearby(int x, int y) {
- List<Cell> out = new ArrayList<Cell>();
- for (int i = -1; i < 2; i++)
- for (int j = -1; j < 2; j++)
- if (x + i > -1 && x + i < Constants.XGRD && y + j > -1 && y + j < Constants.XGRD)
- out.add(field[x + i][y + j]);
- return out;
- }
- // b: true - win, false - lose
- public void show(boolean b) {
- for (int x = 0; x < Constants.XGRD; x++)
- for (int y = 0; y < Constants.YGRD; y++)
- if (b) {
- if (field[x][y].isMined()) {
- field[x][y].setFlag(Flag.VERIFIED);
- } else if (field[x][y].getFlag() == Flag.NONE) {
- field[x][y].setFlag(Flag.OPEN);
- }
- } else {
- if (field[x][y].isMined() && field[x][y].getFlag() == Flag.MARKED) {
- field[x][y].setFlag(Flag.VERIFIED);
- } else if (!field[x][y].isMined() && field[x][y].getFlag() == Flag.MARKED) {
- field[x][y].setFlag(Flag.ERROR);
- } else if (field[x][y].isMined()) {
- field[x][y].setFlag(Flag.CRUSHED);
- } else {
- field[x][y].setFlag(Flag.OPEN);
- }
- }
- }
- // b: true - RMC, left - LMC
- public void click(boolean b) {
- click(x, y, b);
- }
- // b: true - RMC, left - LMC
- public void click(int x, int y, boolean b) {
- if (b) {
- if (field[x][y].getFlag() == Flag.NONE) {
- field[x][y].setFlag(Flag.MARKED);
- } else if (field[x][y].getFlag() == Flag.MARKED) {
- field[x][y].setFlag(Flag.NONE);
- }
- } else {
- if (field[x][y].getFlag() == Flag.NONE)
- if (field[x][y].isMined()) {
- state = GameState.LOSE;
- field[x][y].setFlag(Flag.CRUSHED);
- } else {
- field[x][y].setFlag(Flag.OPEN);
- checkWin();
- }
- }
- }
- public int getSelectionX() {
- return x;
- }
- public int getSelectionY() {
- return y;
- }
- public void move(int ox, int oy) {
- x = ox < 0 ? 0 : (ox > Constants.XGRD ? Constants.XGRD : ox);
- y = oy < 0 ? 0 : (oy > Constants.YGRD ? Constants.YGRD : oy);
- }
- public void move(Direction d) {
- x = x < 0 ? 0 : x;
- y = y < 0 ? 0 : y;
- if (d == Direction.UP && y > 0)
- y--;
- if (d == Direction.RIGHT && x < Constants.XGRD)
- x++;
- if (d == Direction.DOWN && y < Constants.YGRD)
- y++;
- if (d == Direction.LEFT && x > 0)
- x--;
- }
- public boolean isSelected(int ox, int oy) {
- return x == ox && y == oy;
- }
- }
- //-------------------------------------------------------------------------------
- package jsx.obj;
- import jsx.state.Flag;
- public class Cell {
- int value;
- boolean mine;
- Flag flag;
- public Cell() {
- value = 0;
- mine = false;
- flag = Flag.NONE;
- }
- public void addValue() {
- this.value += 1;
- }
- public void setValue(int i) {
- this.value += i;
- }
- public int getValue() {
- return value;
- }
- public void setFlag(Flag f) {
- this.flag = f;
- }
- public Flag getFlag() {
- return flag;
- }
- public void setMined() {
- this.mine = true;
- }
- public boolean isMined() {
- return mine;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement