Initial commit
This commit is contained in:
56
src/main/java/com/shr4pnel/minesweeper/Controller.java
Normal file
56
src/main/java/com/shr4pnel/minesweeper/Controller.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.shr4pnel.minesweeper;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.GridPane;
|
||||
|
||||
public class Controller {
|
||||
@FXML
|
||||
GridPane grid;
|
||||
|
||||
|
||||
// i already said i'm sorry!
|
||||
Set<String> visitedTiles = new HashSet<>();
|
||||
Grid gridHandler;
|
||||
GridWrapper wrapper;
|
||||
|
||||
@FXML
|
||||
private void initialize() {
|
||||
gridHandler = new Grid();
|
||||
wrapper = gridHandler.grid;
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void gridClicked(MouseEvent event) {
|
||||
Node tileClicked = event.getPickResult().getIntersectedNode();
|
||||
int column = GridPane.getColumnIndex(tileClicked);
|
||||
int row = GridPane.getRowIndex(tileClicked);
|
||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||
System.out.println("DEAD!!!");
|
||||
return;
|
||||
// todo do stuff!!
|
||||
}
|
||||
|
||||
setAdjacentCount(tileClicked);
|
||||
expandGrid(column, row);
|
||||
}
|
||||
|
||||
void setAdjacentCount(Node tileClicked) {
|
||||
int column = GridPane.getColumnIndex(tileClicked);
|
||||
int row = GridPane.getRowIndex(tileClicked);
|
||||
ImageView image = (ImageView) tileClicked;
|
||||
int adjacentBombs = wrapper.atColumn(column).atRow(row).adjacentBombCount();
|
||||
URL imageURL = getClass().getResource("img/num_" + adjacentBombs + ".png");
|
||||
image.setImage(new Image(imageURL.toString()));
|
||||
}
|
||||
|
||||
void expandGrid(int baseColumn, int baseRow) {
|
||||
|
||||
}
|
||||
}
|
33
src/main/java/com/shr4pnel/minesweeper/Grid.java
Normal file
33
src/main/java/com/shr4pnel/minesweeper/Grid.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.shr4pnel.minesweeper;
|
||||
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Grid {
|
||||
final GridWrapper grid = new GridWrapper();
|
||||
|
||||
public Grid() {
|
||||
// todo fix beginner mode and intermediate!
|
||||
// sorry :3
|
||||
// 99 bombs in expert:
|
||||
generateBombs(99);
|
||||
}
|
||||
|
||||
private void generateBombs(int bombMax) {
|
||||
int i;
|
||||
boolean success = false;
|
||||
for (i = 0; i < bombMax; ++i) {
|
||||
System.out.println("Pass " + i);
|
||||
success = false;
|
||||
while (!success) {
|
||||
int column = ThreadLocalRandom.current().nextInt(30);
|
||||
int row = ThreadLocalRandom.current().nextInt(16);
|
||||
if (grid.atColumn(column).atRow(row).isBomb()) {
|
||||
continue;
|
||||
}
|
||||
grid.atColumn(column).atRow(row).setBomb();
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
115
src/main/java/com/shr4pnel/minesweeper/GridWrapper.java
Normal file
115
src/main/java/com/shr4pnel/minesweeper/GridWrapper.java
Normal file
@@ -0,0 +1,115 @@
|
||||
package com.shr4pnel.minesweeper;
|
||||
|
||||
import javax.swing.text.html.ImageView;
|
||||
|
||||
public class GridWrapper {
|
||||
private final boolean[][] grid = new boolean[30][16];
|
||||
private int currentColumn;
|
||||
private int currentRow;
|
||||
|
||||
public GridWrapper() {
|
||||
this.currentColumn = 0;
|
||||
this.currentRow = 0;
|
||||
}
|
||||
|
||||
GridWrapper atColumn(int column) {
|
||||
this.currentColumn = column;
|
||||
return this;
|
||||
}
|
||||
|
||||
GridWrapper atRow(int row) {
|
||||
this.currentRow = row;
|
||||
return this;
|
||||
}
|
||||
|
||||
void setBomb() {
|
||||
grid[this.currentColumn][this.currentRow] = true;
|
||||
}
|
||||
|
||||
boolean isBomb() {
|
||||
return grid[currentColumn][currentRow];
|
||||
}
|
||||
|
||||
int adjacentBombCount() {
|
||||
boolean checkLeft, checkRight, checkUp, checkDown;
|
||||
int count = 0;
|
||||
checkLeft = currentColumn > 0;
|
||||
checkRight = currentColumn < 29;
|
||||
checkUp = currentRow > 0;
|
||||
checkDown = currentRow < 15;
|
||||
|
||||
if (checkLeft) {
|
||||
count += left();
|
||||
}
|
||||
if (checkRight) {
|
||||
count += right();
|
||||
}
|
||||
if (checkUp) {
|
||||
count += top();
|
||||
}
|
||||
if (checkDown) {
|
||||
count += bottom();
|
||||
}
|
||||
if (checkUp && checkLeft) {
|
||||
count += topLeft();
|
||||
}
|
||||
|
||||
if (checkUp && checkRight) {
|
||||
count += topRight();
|
||||
}
|
||||
if (checkDown && checkRight) {
|
||||
count += bottomRight();
|
||||
}
|
||||
if (checkDown && checkLeft) {
|
||||
count += bottomLeft();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
private int topRight() {
|
||||
return grid[currentColumn + 1][currentRow - 1] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int top() {
|
||||
return grid[currentColumn][currentRow - 1] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int topLeft() {
|
||||
return grid[currentColumn - 1][currentRow - 1] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int left() {
|
||||
return grid[currentColumn - 1][currentRow] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int bottomLeft() {
|
||||
return grid[currentColumn - 1][currentRow + 1] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int bottom() {
|
||||
return grid[currentColumn][currentRow + 1] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int bottomRight() {
|
||||
return grid[currentColumn + 1][currentRow + 1] ? 1 : 0;
|
||||
}
|
||||
|
||||
private int right() {
|
||||
return grid[currentColumn + 1][currentRow] ? 1 : 0;
|
||||
}
|
||||
|
||||
boolean isValid() {
|
||||
return this.currentColumn >= 0 && this.currentColumn < 30 && this.currentRow >= 0 && this.currentRow < 16;
|
||||
}
|
||||
|
||||
void printGrid() {
|
||||
int i, j;
|
||||
for (i = 0; i < 30; ++i) {
|
||||
for (j = 0; j < 16; ++j) {
|
||||
System.out.printf(this.atColumn(i).atRow(j).isBomb() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
23
src/main/java/com/shr4pnel/minesweeper/Main.java
Normal file
23
src/main/java/com/shr4pnel/minesweeper/Main.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.shr4pnel.minesweeper;
|
||||
|
||||
import java.io.IOException;
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class Main extends Application {
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException {
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("minesweeper.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
stage.setTitle("Minesweeper");
|
||||
stage.setScene(scene);
|
||||
stage.setResizable(false);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user