Added a Board class with methods for tile generation and manipulation
This commit is contained in:
parent
e6a6c22151
commit
cf32276388
111
src/dev/kske/minesweeper/Board.java
Normal file
111
src/dev/kske/minesweeper/Board.java
Normal file
@ -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: <strong>Minesweeper</strong><br>
|
||||
* File: <strong>Board.java</strong><br>
|
||||
* Created: <strong>22.03.2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,12 @@ import java.awt.EventQueue;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
|
||||
/**
|
||||
* Project: <strong>Minesweeper</strong><br>
|
||||
* File: <strong>Minesweeper.java</strong><br>
|
||||
* Created: <strong>21.03.2019</strong><br>
|
||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||
*/
|
||||
public class Minesweeper {
|
||||
|
||||
private JFrame mframe;
|
||||
|
Loading…
Reference in New Issue
Block a user