timer fixes, bomb fixes
This commit is contained in:
parent
9fa0b42239
commit
749fd17a90
3
.gitignore
vendored
3
.gitignore
vendored
@ -39,4 +39,5 @@ bin/
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
.DS_Store
|
||||
.idea
|
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
@ -63,7 +63,10 @@ jar {
|
||||
)
|
||||
}
|
||||
from {
|
||||
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
|
||||
configurations.compileClasspath.filter { it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
|
||||
}
|
||||
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
|
||||
|
||||
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
|
||||
}
|
||||
|
||||
mainClassName = "com.shr4pnel.minesweeper.Main"
|
||||
|
@ -1,14 +1,13 @@
|
||||
package com.shr4pnel.minesweeper;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.MouseButton;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.GridPane;
|
||||
|
||||
@ -28,9 +27,13 @@ public class Controller {
|
||||
@FXML
|
||||
ImageView time_3;
|
||||
|
||||
@FXML
|
||||
ImageView bomb_2;
|
||||
|
||||
@FXML
|
||||
ImageView bomb_3;
|
||||
|
||||
|
||||
// i already said i'm sorry!
|
||||
Set<String> visitedTiles = new HashSet<>();
|
||||
Grid gridHandler;
|
||||
GridWrapper wrapper;
|
||||
boolean gameOver = false;
|
||||
@ -38,6 +41,7 @@ public class Controller {
|
||||
static Timer timer = new Timer();
|
||||
int time = 0;
|
||||
long startTime;
|
||||
int bombCount = 99;
|
||||
|
||||
@FXML
|
||||
private void initialize() {
|
||||
@ -47,13 +51,19 @@ public class Controller {
|
||||
|
||||
|
||||
private void reinitialize() {
|
||||
bombCount = 99;
|
||||
updateBombCounter();
|
||||
URL zeroSecondURL = getClass().getResource("img/0_seconds.png");
|
||||
time_1.setImage(new Image(String.valueOf(zeroSecondURL)));
|
||||
time_2.setImage(new Image(String.valueOf(zeroSecondURL)));
|
||||
time_3.setImage(new Image(String.valueOf(zeroSecondURL)));
|
||||
timer.cancel();
|
||||
isFirstLoad = true;
|
||||
timer = new Timer();
|
||||
URL blank = getClass().getResource("img/blank.png");
|
||||
for (Node n : grid.getChildren()) {
|
||||
ImageView nodeAsImage = (ImageView) n;
|
||||
nodeAsImage.setImage(new Image(blank.toString()));
|
||||
nodeAsImage.setImage(new Image(String.valueOf(blank)));
|
||||
}
|
||||
gridHandler = new Grid();
|
||||
wrapper = gridHandler.grid;
|
||||
@ -65,74 +75,117 @@ public class Controller {
|
||||
return;
|
||||
}
|
||||
if (isFirstLoad) {
|
||||
startTime = System.currentTimeMillis();
|
||||
TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (time >= 999) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
long elapsedTimeMillis = currentTimeMillis - startTime;
|
||||
time = (int) (elapsedTimeMillis / 1000);
|
||||
String timeString = String.valueOf(time);
|
||||
char[] timeStringArray = timeString.toCharArray();
|
||||
int length = timeString.length();
|
||||
if (length == 3) {
|
||||
char hundred = timeStringArray[0];
|
||||
char tens = timeStringArray[1];
|
||||
char unit = timeStringArray[2];
|
||||
URL hundredURL = getClass().getResource("img/" + hundred + "_seconds.png");
|
||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_1.setImage(new Image(String.valueOf(hundredURL)));
|
||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
} else if (length == 2) {
|
||||
char tens = timeStringArray[0];
|
||||
char unit = timeStringArray[1];
|
||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
} else if (length == 1) {
|
||||
char unit = timeStringArray[0];
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
}
|
||||
}
|
||||
};
|
||||
timer.schedule(task, 0, 1000);
|
||||
scheduleTimer();
|
||||
}
|
||||
isFirstLoad = false;
|
||||
Node tileClicked = event.getPickResult().getIntersectedNode();
|
||||
int column = GridPane.getColumnIndex(tileClicked);
|
||||
int row = GridPane.getRowIndex(tileClicked);
|
||||
if (event.getButton() == MouseButton.SECONDARY) {
|
||||
flag(tileClicked);
|
||||
return;
|
||||
}
|
||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||
gameOver = true;
|
||||
System.out.println("DEAD!!!");
|
||||
URL smileyURL = getClass().getResource("img/face_dead.png");
|
||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
||||
ImageView tileClickedImage = (ImageView) tileClicked;
|
||||
URL deathBombURL = getClass().getResource("img/bomb_death.png");
|
||||
tileClickedImage.setImage(new Image(String.valueOf(deathBombURL)));
|
||||
showAllBombs();
|
||||
gameOver(tileClicked);
|
||||
return;
|
||||
}
|
||||
setAdjacentCount(tileClicked);
|
||||
expandGrid(column, row);
|
||||
}
|
||||
|
||||
void showAllBombs() {
|
||||
Node[] children = grid.getChildren().toArray(new Node[0]);
|
||||
int column, row;
|
||||
for (column = 0; column < 30; ++column) {
|
||||
for (row = 0; row < 16; ++row) {
|
||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||
ImageView currentBombTile = (ImageView) children[column + row * 30];
|
||||
URL bombRevealedURL = getClass().getResource("img/bomb_revealed.png");
|
||||
currentBombTile.setImage(new Image(String.valueOf(bombRevealedURL)));
|
||||
void gameOver(Node tileClicked) {
|
||||
gameOver = true;
|
||||
timer.cancel();
|
||||
System.out.println("DEAD!!!");
|
||||
URL smileyURL = getClass().getResource("img/face_dead.png");
|
||||
smiley.setImage(new Image(String.valueOf(smileyURL)));
|
||||
ImageView tileClickedImage = (ImageView) tileClicked;
|
||||
URL deathBombURL = getClass().getResource("img/bomb_death.png");
|
||||
tileClickedImage.setImage(new Image(String.valueOf(deathBombURL)));
|
||||
showAllBombs(GridPane.getColumnIndex(tileClicked), GridPane.getRowIndex(tileClicked));
|
||||
}
|
||||
|
||||
void flag(Node tileClicked) {
|
||||
bombCount--;
|
||||
URL flagURL = getClass().getResource("img/bomb_flagged.png");
|
||||
Image flagImage = new Image(String.valueOf(flagURL));
|
||||
ImageView tile = (ImageView) tileClicked;
|
||||
if (tile.getImage().equals(flagImage)) {
|
||||
bombCount++;
|
||||
}
|
||||
updateBombCounter();
|
||||
tile.setImage(new Image(String.valueOf(flagURL)));
|
||||
}
|
||||
|
||||
void updateBombCounter() {
|
||||
String bombCountString = String.valueOf(bombCount);
|
||||
char[] bombCountCharArray = bombCountString.toCharArray();
|
||||
if (bombCountString.length() == 2) {
|
||||
URL bombCountTensURL =
|
||||
getClass().getResource("img/" + bombCountCharArray[0] + "_seconds.png");
|
||||
URL bombCountUnitsURL =
|
||||
getClass().getResource("img/" + bombCountCharArray[1] + "_seconds.png");
|
||||
bomb_2.setImage(new Image(String.valueOf(bombCountTensURL)));
|
||||
bomb_3.setImage(new Image(String.valueOf(bombCountUnitsURL)));
|
||||
return;
|
||||
}
|
||||
URL bombCountUnitsURL =
|
||||
getClass().getResource("img/" + bombCountCharArray[1] + "_seconds.png");
|
||||
bomb_3.setImage(new Image(String.valueOf(bombCountUnitsURL)));
|
||||
}
|
||||
|
||||
void scheduleTimer() {
|
||||
startTime = System.currentTimeMillis();
|
||||
TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (time >= 999) {
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
long elapsedTimeMillis = currentTimeMillis - startTime;
|
||||
time = (int) (elapsedTimeMillis / 1000);
|
||||
String timeString = String.valueOf(time);
|
||||
char[] timeStringArray = timeString.toCharArray();
|
||||
int length = timeString.length();
|
||||
if (length == 3) {
|
||||
char hundred = timeStringArray[0];
|
||||
char tens = timeStringArray[1];
|
||||
char unit = timeStringArray[2];
|
||||
URL hundredURL = getClass().getResource("img/" + hundred + "_seconds.png");
|
||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_1.setImage(new Image(String.valueOf(hundredURL)));
|
||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
} else if (length == 2) {
|
||||
char tens = timeStringArray[0];
|
||||
char unit = timeStringArray[1];
|
||||
URL tensURL = getClass().getResource("img/" + tens + "_seconds.png");
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_2.setImage(new Image(String.valueOf(tensURL)));
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
} else if (length == 1) {
|
||||
char unit = timeStringArray[0];
|
||||
URL unitURL = getClass().getResource("img/" + unit + "_seconds.png");
|
||||
time_3.setImage(new Image(String.valueOf(unitURL)));
|
||||
}
|
||||
}
|
||||
};
|
||||
timer.schedule(task, 0, 1000);
|
||||
}
|
||||
|
||||
void showAllBombs(int clickedColumn, int clickedRow) {
|
||||
URL bombRevealedURL = getClass().getResource("img/bomb_revealed.png");
|
||||
for (Node node : grid.getChildren()) {
|
||||
int column = GridPane.getColumnIndex(node);
|
||||
int row = GridPane.getRowIndex(node);
|
||||
if (column == clickedColumn && row == clickedRow)
|
||||
continue;
|
||||
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||
ImageView imageView = (ImageView) node;
|
||||
imageView.setImage(new Image(String.valueOf(bombRevealedURL)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
package com.shr4pnel.minesweeper;
|
||||
|
||||
import javax.swing.text.html.ImageView;
|
||||
|
||||
public class GridWrapper {
|
||||
private final boolean[][] grid = new boolean[30][16];
|
||||
final boolean[][] grid = new boolean[30][16];
|
||||
private int currentColumn;
|
||||
private int currentRow;
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user