Fix adjacency count being incorrect due to underlying grid structure in GridWrapper
This commit is contained in:
parent
07ab0b898f
commit
c72efa8b44
@ -3,6 +3,7 @@ package com.shr4pnel.minesweeper;
|
||||
import java.net.URL;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
@ -55,9 +56,23 @@ public class Controller {
|
||||
blankButton.setGraphic(blankImage);
|
||||
blankButton.setMinSize(16, 16);
|
||||
blankButton.setOnMouseClicked(this::buttonClicked);
|
||||
blankButton.setOnMousePressed(this::mouseHeld);
|
||||
blankButton.setOnMouseReleased(this::mouseReleased);
|
||||
return blankButton;
|
||||
}
|
||||
|
||||
private void mouseHeld(MouseEvent mouseEvent) {
|
||||
if (gameOver)
|
||||
return;
|
||||
setImage(smiley, "img/face_ooh.png");
|
||||
}
|
||||
|
||||
private void mouseReleased(MouseEvent mouseEvent) {
|
||||
if (gameOver)
|
||||
return;
|
||||
setImage(smiley, "img/face_smile.png");
|
||||
}
|
||||
|
||||
private void buttonClicked(MouseEvent e) {
|
||||
if (gameOver) {
|
||||
return;
|
||||
@ -81,26 +96,33 @@ public class Controller {
|
||||
handlePrimaryClick(clicked, column, row);
|
||||
}
|
||||
|
||||
private void setBombIfFirstTileIsBomb(int column, int row) {
|
||||
private int[] setBombIfFirstTileIsBomb(int column, int row) {
|
||||
for (int c = 0; c < 30; ++c) {
|
||||
for (int r = 0; r < 16; ++r) {
|
||||
if (!wrapper.atColumn(c).atRow(r).isBomb()) {
|
||||
wrapper.setBomb();
|
||||
wrapper.updateGrid(column, row, c, r);
|
||||
System.out.println("erm");
|
||||
return;
|
||||
return new int[]{c, r};
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void handlePrimaryClick(Button clicked, int column, int row) {
|
||||
if (wrapper.atColumn(column).atRow(row).isBomb() && !isFirstClick) {
|
||||
gameOver(clicked);
|
||||
return;
|
||||
} else if (wrapper.isBomb() && isFirstClick) {
|
||||
}
|
||||
if (wrapper.isBomb() && isFirstClick) {
|
||||
int[] chosenColumnAndRow = setBombIfFirstTileIsBomb(column, row);
|
||||
// assertions are evil but i dont care
|
||||
assert chosenColumnAndRow != null;
|
||||
int columnMovedTo = chosenColumnAndRow[0];
|
||||
int rowMovedTo = chosenColumnAndRow[1];
|
||||
wrapper.atColumn(column).atRow(row).switchBomb(columnMovedTo, rowMovedTo);
|
||||
recursiveExpandTiles(column, row);
|
||||
isFirstClick = false;
|
||||
setBombIfFirstTileIsBomb(column, row);
|
||||
}
|
||||
int adjacentBombs = wrapper.adjacentBombCount();
|
||||
setAdjacentCount(clicked, adjacentBombs);
|
||||
@ -154,6 +176,7 @@ public class Controller {
|
||||
|
||||
@FXML
|
||||
private void reinitialize() {
|
||||
gameOver = false;
|
||||
bombCount = 99;
|
||||
updateBombCounter();
|
||||
resetTimer();
|
||||
@ -284,7 +307,6 @@ public class Controller {
|
||||
|
||||
@FXML
|
||||
private void smileyReleased() {
|
||||
gameOver = false;
|
||||
setImage(smiley, "img/face_smile.png");
|
||||
reinitialize();
|
||||
}
|
||||
|
@ -28,6 +28,13 @@ public class GridWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
public void switchBomb(int destinationColumn, int destinationRow) {
|
||||
if (isValid(currentColumn, currentRow) && isValid(destinationColumn, destinationRow)) {
|
||||
grid[destinationColumn][destinationRow] = true;
|
||||
grid[currentColumn][currentRow] = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isBomb() {
|
||||
return isValid(currentColumn, currentRow) && grid[currentColumn][currentRow];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user