From cf322763880c3be545101737330fe47aea36b0f2 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 22 Mar 2019 17:54:18 +0100 Subject: [PATCH] Added a Board class with methods for tile generation and manipulation --- src/dev/kske/minesweeper/Board.java | 111 ++++++++++++++++++++++ src/dev/kske/minesweeper/Minesweeper.java | 7 +- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/dev/kske/minesweeper/Board.java diff --git a/src/dev/kske/minesweeper/Board.java b/src/dev/kske/minesweeper/Board.java new file mode 100644 index 0000000..ff2185c --- /dev/null +++ b/src/dev/kske/minesweeper/Board.java @@ -0,0 +1,111 @@ +package dev.kske.minesweeper; + +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.image.BufferedImage; +import java.util.Random; + +/** + * Project: Minesweeper
+ * File: Board.java
+ * Created: 22.03.2019
+ * Author: Kai S. K. Engelbart + */ +public class Board { + + private int tileSize, width, height; + private Rectangle screen; + private BufferedImage[] icons; + + private GameState gameState; + private int mines, activeTiles, flaggedTiles; + private Tile[][] board; + + public Board(int x, int y, int width, int height, int tileSize, int mines) { + this.tileSize = tileSize; + this.width = width; + this.height = height; + screen = new Rectangle(x, y, x + width * tileSize, y + height * tileSize); + // TODO: Init icons + + gameState = GameState.ACTIVE; + this.mines = mines; + activeTiles = width * height; + flaggedTiles = 0; + + board = new Tile[width][height]; + for (int i = 0; i < width; i++) + for (int j = 0; j < height; j++) + board[i][j] = new Tile(); + } + + public Point getTilePos(int x, int y) { + return new Point((x - screen.x) / tileSize, (y - screen.y) / tileSize); + } + + public void initMines() { + int remaining = mines; + Random random = new Random(); + while (remaining > 0) { + // Randomly select a tile + int n = random.nextInt(width); + int m = random.nextInt(height); + + // Check if the selected tile already is a mine + if (!board[n][m].isMine()) { + // Decrement the counter + remaining--; + + // Place the mine + board[n][m].setMine(true); + + // Adjust surrounding mine counters + for (int i = Math.max(0, n - 1); i < Math.min(n + 1, board.length); i++) + for (int j = Math.max(0, m - 1); j < Math.min(m + 1, board[i].length); j++) + if (i != n || j != m) board[i][j].setSurroundingMines(board[i][j].getSurroundingMines() + 1); + } + } + } + + public void touchTile(int n, int m) { + Tile tile = board[n][m]; + if (!tile.isTouched()) { + tile.setTouched(true); + activeTiles--; + tile.setDrawSurroundingMines(true); + + // Adjust the number of flagged tiles if the tile was flagged + if (tile.isFlagged()) { + tile.setFlagged(false); + flaggedTiles--; + } + + // Test if the game is won or lost + if (tile.isMine()) gameState = GameState.LOST; + else if (mines == activeTiles) gameState = GameState.WON; + + // Touch surrounding tiles when there are zero surrounding mines + else if (tile.getSurroundingMines() == 0) + for (int i = Math.max(0, n - 1); i < Math.min(n + 1, board.length); i++) + for (int j = Math.max(0, m - 1); j < Math.min(m + 1, board[i].length); j++) + touchTile(i, j); + + // Redraw the touched tile + // TODO: Draw tile + } + } + + public void flagTile(Point tilePos) { + Tile tile = board[tilePos.x][tilePos.y]; + if (!tile.isTouched()) { + if (tile.isFlagged()) { + tile.setFlagged(false); + flaggedTiles--; + } else { + tile.setFlagged(true); + flaggedTiles++; + } + // TODO: Draw tile + } + } +} diff --git a/src/dev/kske/minesweeper/Minesweeper.java b/src/dev/kske/minesweeper/Minesweeper.java index a9af539..292a623 100644 --- a/src/dev/kske/minesweeper/Minesweeper.java +++ b/src/dev/kske/minesweeper/Minesweeper.java @@ -4,7 +4,12 @@ import java.awt.EventQueue; import javax.swing.JFrame; - +/** + * Project: Minesweeper
+ * File: Minesweeper.java
+ * Created: 21.03.2019
+ * Author: Kai S. K. Engelbart + */ public class Minesweeper { private JFrame mframe;