Compare commits
17
Commits
2827d79556
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d95555d2af
|
||
|
|
2b18a6bd9f
|
||
|
|
ea70aa5181
|
||
|
|
ef8050a5f9
|
||
|
|
7d7e660c77
|
||
|
|
acaa18ffcc
|
||
|
|
c781b364d8
|
||
|
|
d681f20b90 | ||
|
|
ef569e1bcb | ||
|
|
74ba71d52c | ||
|
|
7708654bdb
|
||
|
|
004fe776eb | ||
|
|
c2c5ac6e68
|
||
|
|
0198ba6e56
|
||
|
|
f5ca09cc67 | ||
|
|
1187bedc5b
|
||
|
|
21fa946011
|
@@ -45,5 +45,3 @@ lib
|
|||||||
target
|
target
|
||||||
.mvn
|
.mvn
|
||||||
src/main/resources/META-INF
|
src/main/resources/META-INF
|
||||||
mvnw
|
|
||||||
mvnw.cmd
|
|
||||||
|
|||||||
@@ -2,35 +2,44 @@
|
|||||||
|
|
||||||
"named this way because i didn't realise there was another libremines!"
|
"named this way because i didn't realise there was another libremines!"
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
### Requirements:
|
### Requirements:
|
||||||
- Java 21 or newer (openjdk-21-jdk)
|
- Java 21
|
||||||
- Maven 3.8.8 (note: some versions cause gluon to freak out)
|
- Maven
|
||||||
- GraalVM 21
|
|
||||||
|
|
||||||
### Instructions:
|
### Instructions:
|
||||||
|
|
||||||
Create the maven wrapper. gluonfx does not like versions of maven that are not 3.8.8:
|
This will automatically fetch dependencies and run the project:
|
||||||
|
|
||||||
|
#### Linux:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
./mvnw javafx:run
|
||||||
|
```
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
./mvnw.cmd javafx:run
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Native Image:
|
||||||
|
|
||||||
|
##### Requirements:
|
||||||
|
- GraalVM
|
||||||
|
- Maven 3.8.8
|
||||||
|
|
||||||
|
Create the maven wrapper at version 3.8.8. GluonFX does not work with newer versions as of 2024-07-27.
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
mvn wrapper:wrapper -Dmaven=3.8.8
|
mvn wrapper:wrapper -Dmaven=3.8.8
|
||||||
```
|
```
|
||||||
|
|
||||||
This is helpful for version management, in case you have an incompatible version of maven installed.
|
Install dependencies, build and run:
|
||||||
Install dependencies:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
./mvnw install
|
./mvnw dependency:resolve gluonfx:build
|
||||||
```
|
|
||||||
|
|
||||||
Build and run:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
GRAALVM_HOME=/path/to/graalvm ./mvnw gluonfx:build
|
|
||||||
```
|
```
|
||||||
|
|
||||||
A native binary should be placed at `target/gluonfx/<your architecture>/libremines`
|
A native binary should be placed at `target/gluonfx/<your architecture>/libremines`
|
||||||
|
|||||||
@@ -0,0 +1,250 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
# or more contributor license agreements. See the NOTICE file
|
||||||
|
# distributed with this work for additional information
|
||||||
|
# regarding copyright ownership. The ASF licenses this file
|
||||||
|
# to you under the Apache License, Version 2.0 (the
|
||||||
|
# "License"); you may not use this file except in compliance
|
||||||
|
# with the License. You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing,
|
||||||
|
# software distributed under the License is distributed on an
|
||||||
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
# KIND, either express or implied. See the License for the
|
||||||
|
# specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
# Apache Maven Wrapper startup batch script, version 3.3.1
|
||||||
|
#
|
||||||
|
# Optional ENV vars
|
||||||
|
# -----------------
|
||||||
|
# JAVA_HOME - location of a JDK home dir, required when download maven via java source
|
||||||
|
# MVNW_REPOURL - repo url base for downloading maven distribution
|
||||||
|
# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
|
||||||
|
# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output
|
||||||
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
set -euf
|
||||||
|
[ "${MVNW_VERBOSE-}" != debug ] || set -x
|
||||||
|
|
||||||
|
# OS specific support.
|
||||||
|
native_path() { printf %s\\n "$1"; }
|
||||||
|
case "$(uname)" in
|
||||||
|
CYGWIN* | MINGW*)
|
||||||
|
[ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")"
|
||||||
|
native_path() { cygpath --path --windows "$1"; }
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# set JAVACMD and JAVACCMD
|
||||||
|
set_java_home() {
|
||||||
|
# For Cygwin and MinGW, ensure paths are in Unix format before anything is touched
|
||||||
|
if [ -n "${JAVA_HOME-}" ]; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ]; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||||
|
JAVACCMD="$JAVA_HOME/jre/sh/javac"
|
||||||
|
else
|
||||||
|
JAVACMD="$JAVA_HOME/bin/java"
|
||||||
|
JAVACCMD="$JAVA_HOME/bin/javac"
|
||||||
|
|
||||||
|
if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then
|
||||||
|
echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2
|
||||||
|
echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD="$(
|
||||||
|
'set' +e
|
||||||
|
'unset' -f command 2>/dev/null
|
||||||
|
'command' -v java
|
||||||
|
)" || :
|
||||||
|
JAVACCMD="$(
|
||||||
|
'set' +e
|
||||||
|
'unset' -f command 2>/dev/null
|
||||||
|
'command' -v javac
|
||||||
|
)" || :
|
||||||
|
|
||||||
|
if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then
|
||||||
|
echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# hash string like Java String::hashCode
|
||||||
|
hash_string() {
|
||||||
|
str="${1:-}" h=0
|
||||||
|
while [ -n "$str" ]; do
|
||||||
|
char="${str%"${str#?}"}"
|
||||||
|
h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296))
|
||||||
|
str="${str#?}"
|
||||||
|
done
|
||||||
|
printf %x\\n $h
|
||||||
|
}
|
||||||
|
|
||||||
|
verbose() { :; }
|
||||||
|
[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; }
|
||||||
|
|
||||||
|
die() {
|
||||||
|
printf %s\\n "$1" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
|
||||||
|
while IFS="=" read -r key value; do
|
||||||
|
case "${key-}" in
|
||||||
|
distributionUrl) distributionUrl="${value-}" ;;
|
||||||
|
distributionSha256Sum) distributionSha256Sum="${value-}" ;;
|
||||||
|
esac
|
||||||
|
done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
|
||||||
|
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
|
||||||
|
|
||||||
|
case "${distributionUrl##*/}" in
|
||||||
|
maven-mvnd-*bin.*)
|
||||||
|
MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/
|
||||||
|
case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in
|
||||||
|
*AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;;
|
||||||
|
:Darwin*x86_64) distributionPlatform=darwin-amd64 ;;
|
||||||
|
:Darwin*arm64) distributionPlatform=darwin-aarch64 ;;
|
||||||
|
:Linux*x86_64*) distributionPlatform=linux-amd64 ;;
|
||||||
|
*)
|
||||||
|
echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2
|
||||||
|
distributionPlatform=linux-amd64
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
|
||||||
|
;;
|
||||||
|
maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
|
||||||
|
*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||||
|
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
|
||||||
|
[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}"
|
||||||
|
distributionUrlName="${distributionUrl##*/}"
|
||||||
|
distributionUrlNameMain="${distributionUrlName%.*}"
|
||||||
|
distributionUrlNameMain="${distributionUrlNameMain%-bin}"
|
||||||
|
MAVEN_HOME="$HOME/.m2/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")"
|
||||||
|
|
||||||
|
exec_maven() {
|
||||||
|
unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || :
|
||||||
|
exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD"
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ -d "$MAVEN_HOME" ]; then
|
||||||
|
verbose "found existing MAVEN_HOME at $MAVEN_HOME"
|
||||||
|
exec_maven "$@"
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "${distributionUrl-}" in
|
||||||
|
*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;;
|
||||||
|
*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# prepare tmp dir
|
||||||
|
if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then
|
||||||
|
clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; }
|
||||||
|
trap clean HUP INT TERM EXIT
|
||||||
|
else
|
||||||
|
die "cannot create temp dir"
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p -- "${MAVEN_HOME%/*}"
|
||||||
|
|
||||||
|
# Download and Install Apache Maven
|
||||||
|
verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
|
||||||
|
verbose "Downloading from: $distributionUrl"
|
||||||
|
verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||||
|
|
||||||
|
# select .zip or .tar.gz
|
||||||
|
if ! command -v unzip >/dev/null; then
|
||||||
|
distributionUrl="${distributionUrl%.zip}.tar.gz"
|
||||||
|
distributionUrlName="${distributionUrl##*/}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# verbose opt
|
||||||
|
__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR=''
|
||||||
|
[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v
|
||||||
|
|
||||||
|
# normalize http auth
|
||||||
|
case "${MVNW_PASSWORD:+has-password}" in
|
||||||
|
'') MVNW_USERNAME='' MVNW_PASSWORD='' ;;
|
||||||
|
has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then
|
||||||
|
verbose "Found wget ... using wget"
|
||||||
|
wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl"
|
||||||
|
elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then
|
||||||
|
verbose "Found curl ... using curl"
|
||||||
|
curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl"
|
||||||
|
elif set_java_home; then
|
||||||
|
verbose "Falling back to use Java to download"
|
||||||
|
javaSource="$TMP_DOWNLOAD_DIR/Downloader.java"
|
||||||
|
targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||||
|
cat >"$javaSource" <<-END
|
||||||
|
public class Downloader extends java.net.Authenticator
|
||||||
|
{
|
||||||
|
protected java.net.PasswordAuthentication getPasswordAuthentication()
|
||||||
|
{
|
||||||
|
return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() );
|
||||||
|
}
|
||||||
|
public static void main( String[] args ) throws Exception
|
||||||
|
{
|
||||||
|
setDefault( new Downloader() );
|
||||||
|
java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
END
|
||||||
|
# For Cygwin/MinGW, switch paths to Windows format before running javac and java
|
||||||
|
verbose " - Compiling Downloader.java ..."
|
||||||
|
"$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java"
|
||||||
|
verbose " - Running Downloader.java ..."
|
||||||
|
"$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If specified, validate the SHA-256 sum of the Maven distribution zip file
|
||||||
|
if [ -n "${distributionSha256Sum-}" ]; then
|
||||||
|
distributionSha256Result=false
|
||||||
|
if [ "$MVN_CMD" = mvnd.sh ]; then
|
||||||
|
echo "Checksum validation is not supported for maven-mvnd." >&2
|
||||||
|
echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
|
||||||
|
exit 1
|
||||||
|
elif command -v sha256sum >/dev/null; then
|
||||||
|
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then
|
||||||
|
distributionSha256Result=true
|
||||||
|
fi
|
||||||
|
elif command -v shasum >/dev/null; then
|
||||||
|
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then
|
||||||
|
distributionSha256Result=true
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2
|
||||||
|
echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ $distributionSha256Result = false ]; then
|
||||||
|
echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2
|
||||||
|
echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# unzip and move
|
||||||
|
if command -v unzip >/dev/null; then
|
||||||
|
unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip"
|
||||||
|
else
|
||||||
|
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
|
||||||
|
fi
|
||||||
|
printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url"
|
||||||
|
mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
|
||||||
|
|
||||||
|
clean || :
|
||||||
|
exec_maven "$@"
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
<# : batch portion
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
@REM or more contributor license agreements. See the NOTICE file
|
||||||
|
@REM distributed with this work for additional information
|
||||||
|
@REM regarding copyright ownership. The ASF licenses this file
|
||||||
|
@REM to you under the Apache License, Version 2.0 (the
|
||||||
|
@REM "License"); you may not use this file except in compliance
|
||||||
|
@REM with the License. You may obtain a copy of the License at
|
||||||
|
@REM
|
||||||
|
@REM http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@REM
|
||||||
|
@REM Unless required by applicable law or agreed to in writing,
|
||||||
|
@REM software distributed under the License is distributed on an
|
||||||
|
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
@REM KIND, either express or implied. See the License for the
|
||||||
|
@REM specific language governing permissions and limitations
|
||||||
|
@REM under the License.
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
@REM Apache Maven Wrapper startup batch script, version 3.3.1
|
||||||
|
@REM
|
||||||
|
@REM Optional ENV vars
|
||||||
|
@REM MVNW_REPOURL - repo url base for downloading maven distribution
|
||||||
|
@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven
|
||||||
|
@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output
|
||||||
|
@REM ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0)
|
||||||
|
@SET __MVNW_CMD__=
|
||||||
|
@SET __MVNW_ERROR__=
|
||||||
|
@SET __MVNW_PSMODULEP_SAVE=%PSModulePath%
|
||||||
|
@SET PSModulePath=
|
||||||
|
@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @(
|
||||||
|
IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B)
|
||||||
|
)
|
||||||
|
@SET PSModulePath=%__MVNW_PSMODULEP_SAVE%
|
||||||
|
@SET __MVNW_PSMODULEP_SAVE=
|
||||||
|
@SET __MVNW_ARG0_NAME__=
|
||||||
|
@SET MVNW_USERNAME=
|
||||||
|
@SET MVNW_PASSWORD=
|
||||||
|
@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*)
|
||||||
|
@echo Cannot start maven from wrapper >&2 && exit /b 1
|
||||||
|
@GOTO :EOF
|
||||||
|
: end batch / begin powershell #>
|
||||||
|
|
||||||
|
$ErrorActionPreference = "Stop"
|
||||||
|
if ($env:MVNW_VERBOSE -eq "true") {
|
||||||
|
$VerbosePreference = "Continue"
|
||||||
|
}
|
||||||
|
|
||||||
|
# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties
|
||||||
|
$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl
|
||||||
|
if (!$distributionUrl) {
|
||||||
|
Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
|
||||||
|
"maven-mvnd-*" {
|
||||||
|
$USE_MVND = $true
|
||||||
|
$distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip"
|
||||||
|
$MVN_CMD = "mvnd.cmd"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default {
|
||||||
|
$USE_MVND = $false
|
||||||
|
$MVN_CMD = $script -replace '^mvnw','mvn'
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# apply MVNW_REPOURL and calculate MAVEN_HOME
|
||||||
|
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
|
||||||
|
if ($env:MVNW_REPOURL) {
|
||||||
|
$MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" }
|
||||||
|
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')"
|
||||||
|
}
|
||||||
|
$distributionUrlName = $distributionUrl -replace '^.*/',''
|
||||||
|
$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
|
||||||
|
$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain"
|
||||||
|
$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
|
||||||
|
$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
|
||||||
|
|
||||||
|
if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
|
||||||
|
Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME"
|
||||||
|
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
|
||||||
|
exit $?
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) {
|
||||||
|
Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl"
|
||||||
|
}
|
||||||
|
|
||||||
|
# prepare tmp dir
|
||||||
|
$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile
|
||||||
|
$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir"
|
||||||
|
$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null
|
||||||
|
trap {
|
||||||
|
if ($TMP_DOWNLOAD_DIR.Exists) {
|
||||||
|
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
|
||||||
|
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null
|
||||||
|
|
||||||
|
# Download and Install Apache Maven
|
||||||
|
Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..."
|
||||||
|
Write-Verbose "Downloading from: $distributionUrl"
|
||||||
|
Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName"
|
||||||
|
|
||||||
|
$webclient = New-Object System.Net.WebClient
|
||||||
|
if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) {
|
||||||
|
$webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD)
|
||||||
|
}
|
||||||
|
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
|
||||||
|
$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null
|
||||||
|
|
||||||
|
# If specified, validate the SHA-256 sum of the Maven distribution zip file
|
||||||
|
$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum
|
||||||
|
if ($distributionSha256Sum) {
|
||||||
|
if ($USE_MVND) {
|
||||||
|
Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties."
|
||||||
|
}
|
||||||
|
Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash
|
||||||
|
if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) {
|
||||||
|
Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# unzip and move
|
||||||
|
Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
|
||||||
|
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null
|
||||||
|
try {
|
||||||
|
Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
|
||||||
|
} catch {
|
||||||
|
if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) {
|
||||||
|
Write-Error "fail to move MAVEN_HOME"
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null }
|
||||||
|
catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" }
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD"
|
||||||
@@ -6,31 +6,23 @@
|
|||||||
|
|
||||||
<groupId>com.shr4pnel.minesweeper</groupId>
|
<groupId>com.shr4pnel.minesweeper</groupId>
|
||||||
<artifactId>libremines</artifactId>
|
<artifactId>libremines</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>21</maven.compiler.source>
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
<maven.compiler.target>21</maven.compiler.target>
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<profiles>
|
|
||||||
<profile>
|
|
||||||
<id>web</id>
|
|
||||||
<properties>
|
|
||||||
<gluonfx.target>web</gluonfx.target>
|
|
||||||
</properties>
|
|
||||||
</profile>
|
|
||||||
</profiles>
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjfx</groupId>
|
<groupId>org.openjfx</groupId>
|
||||||
<artifactId>javafx-controls</artifactId>
|
<artifactId>javafx-controls</artifactId>
|
||||||
<version>21.0.3</version>
|
<version>21.0.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjfx</groupId>
|
<groupId>org.openjfx</groupId>
|
||||||
<artifactId>javafx-fxml</artifactId>
|
<artifactId>javafx-fxml</artifactId>
|
||||||
<version>21.0.3</version>
|
<version>21.0.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -1,55 +1,168 @@
|
|||||||
package com.shr4pnel.minesweeper;
|
package com.shr4pnel.minesweeper;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.event.ActionEvent;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.fxml.FXMLLoader;
|
||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
|
import javafx.scene.Parent;
|
||||||
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.scene.control.MenuItem;
|
||||||
import javafx.scene.control.RadioMenuItem;
|
import javafx.scene.control.RadioMenuItem;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.scene.image.ImageView;
|
import javafx.scene.image.ImageView;
|
||||||
import javafx.scene.input.MouseButton;
|
import javafx.scene.input.MouseButton;
|
||||||
import javafx.scene.input.MouseEvent;
|
import javafx.scene.input.MouseEvent;
|
||||||
import javafx.scene.layout.GridPane;
|
import javafx.scene.layout.GridPane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JavaFX controller for minesweeper.fxml.
|
||||||
|
* Handles all operations performed within the GUI.
|
||||||
|
* @author shrapnelnet admin@shr4pnel.com
|
||||||
|
*/
|
||||||
public class Controller {
|
public class Controller {
|
||||||
|
/**
|
||||||
|
* The grid within the FXML holding all the tiles.
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private GridPane grid;
|
private GridPane grid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Images within the FXML structure.
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private ImageView smiley, time_1, time_2, time_3, bomb_2, bomb_3;
|
private ImageView smiley, time_1, time_2, time_3, bomb_2, bomb_3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Menu items
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private RadioMenuItem color, marks;
|
private RadioMenuItem color, marks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The menu item "about".
|
||||||
|
*/
|
||||||
|
@FXML
|
||||||
|
private MenuItem about;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The underlying 2D array representing where bombs are on the grid.
|
||||||
|
*/
|
||||||
private Grid gridHandler;
|
private Grid gridHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A wrapper to make operations on the underlying grid easier.
|
||||||
|
*/
|
||||||
private GridWrapper wrapper;
|
private GridWrapper wrapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the grid during a game over state preventing further clicking of the grid.
|
||||||
|
*/
|
||||||
private boolean gameOver = false;
|
private boolean gameOver = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A variable specifying if the game has been loaded for the first time.
|
||||||
|
*/
|
||||||
private boolean isFirstLoad = true;
|
private boolean isFirstLoad = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The game timer.
|
||||||
|
*/
|
||||||
static Timer timer = new Timer();
|
static Timer timer = new Timer();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time that has elapsed since the game started.
|
||||||
|
*/
|
||||||
private int time = 0;
|
private int time = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The time the game was started at.
|
||||||
|
*/
|
||||||
private long startTime;
|
private long startTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of bombs on the grid.
|
||||||
|
*/
|
||||||
private int bombCount = 99;
|
private int bombCount = 99;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A 2D array holding all tiles that have previously recursively been expanded.
|
||||||
|
*/
|
||||||
private boolean[][] expandedTiles;
|
private boolean[][] expandedTiles;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A boolean that shows if the user has performed their first click on the GridPane.
|
||||||
|
*/
|
||||||
private boolean isFirstClick = true;
|
private boolean isFirstClick = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets important variables on Main class initialization
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
setNotYetImplemented(color);
|
setNotYetImplemented(color);
|
||||||
setNotYetImplemented(marks);
|
setNotYetImplemented(marks);
|
||||||
|
about.setOnAction(this::openAbout);
|
||||||
setupGrid();
|
setupGrid();
|
||||||
gridHandler = new Grid();
|
gridHandler = new Grid();
|
||||||
wrapper = gridHandler.grid;
|
wrapper = gridHandler.grid;
|
||||||
expandedTiles = new boolean[30][16];
|
expandedTiles = new boolean[30][16];
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNotYetImplemented(RadioMenuItem node) {
|
/**
|
||||||
node.setOnAction((ActionEvent e) -> System.out.println("https://http.cat/images/501.jpg"));
|
* Opens the about window when the radiomenuitem is clicked
|
||||||
|
*
|
||||||
|
* @param actionEvent The node that the window opens on, on click
|
||||||
|
*/
|
||||||
|
private void openAbout(ActionEvent actionEvent) {
|
||||||
|
try {
|
||||||
|
Parent root = FXMLLoader.load(getClass().getResource("about.fxml"));
|
||||||
|
Stage stage = new Stage();
|
||||||
|
stage.setTitle("About");
|
||||||
|
stage.setMinWidth(455);
|
||||||
|
stage.setMinHeight(275);
|
||||||
|
stage.setScene(new Scene(root));
|
||||||
|
stage.show();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens a window if an unimplemented menu item is clicked
|
||||||
|
*
|
||||||
|
* @param node The node that the window opens on, on click
|
||||||
|
*/
|
||||||
|
private void setNotYetImplemented(RadioMenuItem node) {
|
||||||
|
node.setOnAction(new EventHandler<>() {
|
||||||
|
@Override
|
||||||
|
public void handle(ActionEvent actionEvent) {
|
||||||
|
try {
|
||||||
|
Parent root = FXMLLoader.load(getClass().getResource("unimplemented.fxml"));
|
||||||
|
Stage stage = new Stage();
|
||||||
|
stage.setTitle("Unimplemented!");
|
||||||
|
stage.setMinHeight(420);
|
||||||
|
stage.setMinWidth(525);
|
||||||
|
stage.setScene(new Scene(root));
|
||||||
|
stage.show();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fills the GridPane with blank buttons
|
||||||
|
*/
|
||||||
private void setupGrid() {
|
private void setupGrid() {
|
||||||
for (int column = 0; column < 30; ++column) {
|
for (int column = 0; column < 30; ++column) {
|
||||||
for (int row = 0; row < 16; ++row) {
|
for (int row = 0; row < 16; ++row) {
|
||||||
@@ -59,9 +172,13 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used during initialization to fill the board
|
||||||
|
*
|
||||||
|
* @return A blank button
|
||||||
|
*/
|
||||||
private Button createBlankButton() {
|
private Button createBlankButton() {
|
||||||
Image blank =
|
Image blank = new Image(String.valueOf(getClass().getResource("img/blank.png")), 16, 16, true, false);
|
||||||
new Image(String.valueOf(getClass().getResource("img/blank.png")), 16, 16, true, true);
|
|
||||||
ImageView blankImage = new ImageView(blank);
|
ImageView blankImage = new ImageView(blank);
|
||||||
Button blankButton = new Button();
|
Button blankButton = new Button();
|
||||||
blankButton.setGraphic(blankImage);
|
blankButton.setGraphic(blankImage);
|
||||||
@@ -72,18 +189,34 @@ public class Controller {
|
|||||||
return blankButton;
|
return blankButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes smiley face to concerned look when the mouse is held
|
||||||
|
*
|
||||||
|
* @param mouseEvent Stub, required by EventHandler interface
|
||||||
|
*/
|
||||||
private void mouseHeld(MouseEvent mouseEvent) {
|
private void mouseHeld(MouseEvent mouseEvent) {
|
||||||
if (gameOver)
|
if (gameOver)
|
||||||
return;
|
return;
|
||||||
setImage(smiley, "img/face_ooh.png");
|
setImage(smiley, "img/face_ooh.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes smiley face back to smiling after mouse release
|
||||||
|
*
|
||||||
|
* @param mouseEvent Stub, required by EventHandler interface
|
||||||
|
*/
|
||||||
private void mouseReleased(MouseEvent mouseEvent) {
|
private void mouseReleased(MouseEvent mouseEvent) {
|
||||||
if (gameOver)
|
if (gameOver)
|
||||||
return;
|
return;
|
||||||
setImage(smiley, "img/face_smile.png");
|
setImage(smiley, "img/face_smile.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the main GridPane being clicked
|
||||||
|
* Identifies the intention of the click (e.g. flag, open tile)
|
||||||
|
*
|
||||||
|
* @param e The event fired on click
|
||||||
|
*/
|
||||||
private void buttonClicked(MouseEvent e) {
|
private void buttonClicked(MouseEvent e) {
|
||||||
if (gameOver) {
|
if (gameOver) {
|
||||||
return;
|
return;
|
||||||
@@ -104,9 +237,78 @@ public class Controller {
|
|||||||
if (buttonImage.getUrl().contains("flagged.png")) {
|
if (buttonImage.getUrl().contains("flagged.png")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (buttonImage.getUrl().contains("num") && !buttonImage.getUrl().contains("num_0.png")) {
|
||||||
|
chord(clicked, buttonImage.getUrl());
|
||||||
|
return;
|
||||||
|
}
|
||||||
handlePrimaryClick(clicked, column, row);
|
handlePrimaryClick(clicked, column, row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int adjacentFlagCount(int column, int row) {
|
||||||
|
int flagCount = 0;
|
||||||
|
for (int innerColumn = column - 1; innerColumn <= column + 1; innerColumn++) {
|
||||||
|
for (int innerRow = row - 1; innerRow <= row + 1; innerRow++) {
|
||||||
|
if (innerColumn == column && innerRow == row)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!wrapper.atColumn(innerColumn).atRow(innerRow).isValid()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Button b = (Button) getNodeByColumnRowIndex(innerColumn, innerRow);
|
||||||
|
String bURL = getButtonURL(b);
|
||||||
|
if (bURL.contains("flag")) {
|
||||||
|
flagCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flagCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Button[] chordTilesToOpen(int column, int row) {
|
||||||
|
ArrayList<Button> buttons = new ArrayList<>(8);
|
||||||
|
for (int innerColumn = column - 1; innerColumn <= column + 1; innerColumn++) {
|
||||||
|
for (int innerRow = row - 1; innerRow <= row + 1; innerRow++) {
|
||||||
|
if (innerColumn == column && innerRow == row)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!wrapper.atColumn(innerColumn).atRow(innerRow).isValid()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Button b = (Button) getNodeByColumnRowIndex(innerColumn, innerRow);
|
||||||
|
String bURL = getButtonURL(b);
|
||||||
|
// if the tile is unflagged and unopened:
|
||||||
|
if (bURL.contains("blank")) {
|
||||||
|
buttons.add(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buttons.toArray(Button[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void chord(Button clicked, String clickedURL) {
|
||||||
|
int column = GridPane.getColumnIndex(clicked);
|
||||||
|
int row = GridPane.getRowIndex(clicked);
|
||||||
|
int urlLength = clickedURL.length();
|
||||||
|
char requiredAdjacentChar = clickedURL.charAt(urlLength-5);
|
||||||
|
int requiredAdjacent = Integer.parseInt(Character.toString(requiredAdjacentChar));
|
||||||
|
int actualAdjacent = adjacentFlagCount(column, row);
|
||||||
|
if (requiredAdjacent != actualAdjacent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Button[] buttons = chordTilesToOpen(column, row);
|
||||||
|
for (Button b: buttons) {
|
||||||
|
expandTile(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the first tile clicked was a bomb, move that bomb to the first available column on row 0 before opening it
|
||||||
|
*
|
||||||
|
* @param column The column of the clicked tile
|
||||||
|
* @param row The row of the clicked tile
|
||||||
|
* @return An array containing the column and row the bomb was moved to
|
||||||
|
*/
|
||||||
private int[] setBombIfFirstTileIsBomb(int column, int row) {
|
private int[] setBombIfFirstTileIsBomb(int column, int row) {
|
||||||
for (int c = 0; c < 30; ++c) {
|
for (int c = 0; c < 30; ++c) {
|
||||||
for (int r = 0; r < 16; ++r) {
|
for (int r = 0; r < 16; ++r) {
|
||||||
@@ -117,24 +319,40 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
System.out.println("If you see this, congrats! this should never happen. please email me.");
|
||||||
|
return new int[]{0, 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts the URL of a given button
|
||||||
|
*
|
||||||
|
* @param button A javafx.scene.control.Button object
|
||||||
|
* @return A string representation of the image URL
|
||||||
|
*/
|
||||||
private String getButtonURL(Button button) {
|
private String getButtonURL(Button button) {
|
||||||
ImageView graphic = (ImageView) button.getGraphic();
|
ImageView graphic = (ImageView) button.getGraphic();
|
||||||
Image image = graphic.getImage();
|
Image image = graphic.getImage();
|
||||||
return image.getUrl();
|
return image.getUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterates through the grid to find all tiles that are either blank or flagged.
|
||||||
|
* Used to check if win condition has been met
|
||||||
|
*
|
||||||
|
* @return The number of tiles that were blank or flagged
|
||||||
|
*/
|
||||||
private int numberOfUnrevealedTiles() {
|
private int numberOfUnrevealedTiles() {
|
||||||
int unrevealedTiles = 0;
|
int unrevealedTiles = 0;
|
||||||
int column, row;
|
int column, row;
|
||||||
for (column = 0; column < 30; ++column) {
|
for (column = 0; column < 30; ++column) {
|
||||||
for (row = 0; row < 16; ++row) {
|
for (row = 0; row < 16; ++row) {
|
||||||
Button current = (Button) getNodeByRowColumnIndex(row, column);
|
Button current = (Button) getNodeByColumnRowIndex(column, row);
|
||||||
assert current != null;
|
// this will hopefully never happen..... or will it ......
|
||||||
|
if (current == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
String currentURL = getButtonURL(current);
|
String currentURL = getButtonURL(current);
|
||||||
if (currentURL.contains("blank.png") || currentURL.contains("flag.png")) {
|
if (currentURL.contains("blank.png") || currentURL.contains("flagged.png")) {
|
||||||
unrevealedTiles++;
|
unrevealedTiles++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,11 +360,24 @@ public class Controller {
|
|||||||
return unrevealedTiles;
|
return unrevealedTiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the number of unrevealed tiles is equal to 99 (the win condition)
|
||||||
|
*
|
||||||
|
* @return A boolean representing if the game has been won
|
||||||
|
* @see #numberOfUnrevealedTiles()
|
||||||
|
*/
|
||||||
private boolean checkWinCondition() {
|
private boolean checkWinCondition() {
|
||||||
int unrevealedTiles = numberOfUnrevealedTiles();
|
int unrevealedTiles = numberOfUnrevealedTiles();
|
||||||
return unrevealedTiles == 99;
|
return unrevealedTiles == 99;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a left mouse click on an unopened tile
|
||||||
|
*
|
||||||
|
* @param clicked The button that was clicked
|
||||||
|
* @param column The column that it was clicked at
|
||||||
|
* @param row The row that it was clicked at
|
||||||
|
*/
|
||||||
private void handlePrimaryClick(Button clicked, int column, int row) {
|
private void handlePrimaryClick(Button clicked, int column, int row) {
|
||||||
if (wrapper.atColumn(column).atRow(row).isBomb() && !isFirstClick) {
|
if (wrapper.atColumn(column).atRow(row).isBomb() && !isFirstClick) {
|
||||||
gameOver(clicked);
|
gameOver(clicked);
|
||||||
@@ -154,13 +385,11 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
if (wrapper.isBomb() && isFirstClick) {
|
if (wrapper.isBomb() && isFirstClick) {
|
||||||
int[] chosenColumnAndRow = setBombIfFirstTileIsBomb(column, row);
|
int[] chosenColumnAndRow = setBombIfFirstTileIsBomb(column, row);
|
||||||
// assertions are evil but i dont care
|
|
||||||
assert chosenColumnAndRow != null;
|
|
||||||
int columnMovedTo = chosenColumnAndRow[0];
|
int columnMovedTo = chosenColumnAndRow[0];
|
||||||
int rowMovedTo = chosenColumnAndRow[1];
|
int rowMovedTo = chosenColumnAndRow[1];
|
||||||
wrapper.atColumn(column).atRow(row).switchBomb(columnMovedTo, rowMovedTo);
|
wrapper.atColumn(column).atRow(row).switchBomb(columnMovedTo, rowMovedTo);
|
||||||
recursiveExpandTiles(column, row);
|
recursiveExpandTiles(column, row);
|
||||||
clicked = (Button) getNodeByRowColumnIndex(row, column);
|
clicked = (Button) getNodeByColumnRowIndex(column, row);
|
||||||
}
|
}
|
||||||
isFirstClick = false;
|
isFirstClick = false;
|
||||||
int adjacentBombs = wrapper.adjacentBombCount();
|
int adjacentBombs = wrapper.adjacentBombCount();
|
||||||
@@ -174,6 +403,13 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a tile is surrounded by tiles with no adjacent bombs, open all the tiles recursively
|
||||||
|
*
|
||||||
|
* @param column The column that was clicked
|
||||||
|
* @param row The row that was clicked
|
||||||
|
* @see #expandTile(int, int)
|
||||||
|
*/
|
||||||
private void recursiveExpandTiles(int column, int row) {
|
private void recursiveExpandTiles(int column, int row) {
|
||||||
if (column < 0 || column >= 30 || row < 0 || row >= 16 ||
|
if (column < 0 || column >= 30 || row < 0 || row >= 16 ||
|
||||||
expandedTiles[column][row] && !wrapper.atColumn(column).atRow(row).isBomb()) {
|
expandedTiles[column][row] && !wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||||
@@ -192,8 +428,14 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens a tile, and adds it to an array to prevent overflow
|
||||||
|
*
|
||||||
|
* @param column The column of the button to open
|
||||||
|
* @param row The row of the button to open
|
||||||
|
*/
|
||||||
private void expandTile(int column, int row) {
|
private void expandTile(int column, int row) {
|
||||||
Node tile = getNodeByRowColumnIndex(row, column);
|
Node tile = getNodeByColumnRowIndex(column, row);
|
||||||
if (tile != null) {
|
if (tile != null) {
|
||||||
Button button = (Button) tile;
|
Button button = (Button) tile;
|
||||||
if (button.isVisible()) {
|
if (button.isVisible()) {
|
||||||
@@ -207,7 +449,31 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node getNodeByRowColumnIndex(int row, int column) {
|
private void expandTile(Button button) {
|
||||||
|
Node node = (Node) button;
|
||||||
|
int column = GridPane.getColumnIndex(node);
|
||||||
|
int row = GridPane.getRowIndex(node);
|
||||||
|
if (wrapper.atColumn(column).atRow(row).isBomb()) {
|
||||||
|
gameOver(node);
|
||||||
|
}
|
||||||
|
if (button.isVisible()) {
|
||||||
|
int adjacentBombs = wrapper.atColumn(column).atRow(row).adjacentBombCount();
|
||||||
|
setAdjacentCount(button, adjacentBombs);
|
||||||
|
if (adjacentBombs == 0 && !expandedTiles[column][row]) {
|
||||||
|
expandedTiles[column][row] = true;
|
||||||
|
recursiveExpandTiles(column, row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterates over the grid and returns the node when it's at the correct child
|
||||||
|
*
|
||||||
|
* @param row The desired row to find
|
||||||
|
* @param column The desired column to find
|
||||||
|
* @return The node at the specified position
|
||||||
|
*/
|
||||||
|
private Node getNodeByColumnRowIndex(int column, int row) {
|
||||||
for (Node node : grid.getChildren()) {
|
for (Node node : grid.getChildren()) {
|
||||||
if (GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
|
if (GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
|
||||||
return node;
|
return node;
|
||||||
@@ -216,6 +482,9 @@ public class Controller {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets important state variables on restart
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private void reinitialize() {
|
private void reinitialize() {
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
@@ -229,6 +498,11 @@ public class Controller {
|
|||||||
isFirstClick = true;
|
isFirstClick = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resets the timer to 0
|
||||||
|
*
|
||||||
|
* @see #reinitialize()
|
||||||
|
*/
|
||||||
private void resetTimer() {
|
private void resetTimer() {
|
||||||
URL zeroSecondURL = getClass().getResource("img/0_seconds.png");
|
URL zeroSecondURL = getClass().getResource("img/0_seconds.png");
|
||||||
time_1.setImage(new Image(String.valueOf(zeroSecondURL)));
|
time_1.setImage(new Image(String.valueOf(zeroSecondURL)));
|
||||||
@@ -239,6 +513,11 @@ public class Controller {
|
|||||||
timer = new Timer();
|
timer = new Timer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo figure out why createBlankButton errors here
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refills the grid with new blank buttons
|
||||||
|
*/
|
||||||
private void resetGrid() {
|
private void resetGrid() {
|
||||||
URL blank = getClass().getResource("img/blank.png");
|
URL blank = getClass().getResource("img/blank.png");
|
||||||
for (Node node : grid.getChildren()) {
|
for (Node node : grid.getChildren()) {
|
||||||
@@ -247,6 +526,11 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a bomb is clicked, change the smiley, set gameOver to true.
|
||||||
|
*
|
||||||
|
* @param tileClicked The node that was clicked, to set the correct image
|
||||||
|
*/
|
||||||
private void gameOver(Node tileClicked) {
|
private void gameOver(Node tileClicked) {
|
||||||
gameOver = true;
|
gameOver = true;
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
@@ -255,6 +539,9 @@ public class Controller {
|
|||||||
showAllBombs(GridPane.getColumnIndex(tileClicked), GridPane.getRowIndex(tileClicked));
|
showAllBombs(GridPane.getColumnIndex(tileClicked), GridPane.getRowIndex(tileClicked));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a win state, change the smiley.
|
||||||
|
*/
|
||||||
private void win() {
|
private void win() {
|
||||||
gameOver = true;
|
gameOver = true;
|
||||||
timer.cancel();
|
timer.cancel();
|
||||||
@@ -262,11 +549,14 @@ public class Controller {
|
|||||||
flagAllRemaining();
|
flagAllRemaining();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After a win, set every bomb tile that is unflagged to flagged.
|
||||||
|
*/
|
||||||
private void flagAllRemaining() {
|
private void flagAllRemaining() {
|
||||||
int column, row;
|
int column, row;
|
||||||
for (column = 0; column < 30; ++column) {
|
for (column = 0; column < 30; ++column) {
|
||||||
for (row = 0; row < 16; ++row) {
|
for (row = 0; row < 16; ++row) {
|
||||||
Button current = (Button) getNodeByRowColumnIndex(row, column);
|
Button current = (Button) getNodeByColumnRowIndex(column, row);
|
||||||
assert current != null;
|
assert current != null;
|
||||||
String currentURL = getButtonURL(current);
|
String currentURL = getButtonURL(current);
|
||||||
boolean tileIsBomb = wrapper.atColumn(column).atRow(row).isBomb();
|
boolean tileIsBomb = wrapper.atColumn(column).atRow(row).isBomb();
|
||||||
@@ -277,12 +567,17 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a flag on a tile, representing a tile that the user suspects has a bomb behind it.
|
||||||
|
* This prevents the tile being clicked on.
|
||||||
|
*
|
||||||
|
* @param tileClicked The tile that the user clicks.
|
||||||
|
*/
|
||||||
private void flag(Node tileClicked) {
|
private void flag(Node tileClicked) {
|
||||||
Button tileAsButton = (Button) tileClicked;
|
Button tileAsButton = (Button) tileClicked;
|
||||||
ImageView tileGraphic = (ImageView) tileAsButton.getGraphic();
|
ImageView tileGraphic = (ImageView) tileAsButton.getGraphic();
|
||||||
Image tileGraphicImage = tileGraphic.getImage();
|
Image tileGraphicImage = tileGraphic.getImage();
|
||||||
if (!tileGraphicImage.getUrl().contains("blank.png") &&
|
if (!tileGraphicImage.getUrl().contains("blank.png") && !tileGraphicImage.getUrl().contains("flagged.png")) {
|
||||||
!tileGraphicImage.getUrl().contains("flagged.png")) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
boolean flagged = tileGraphicImage.getUrl().contains("flagged.png");
|
boolean flagged = tileGraphicImage.getUrl().contains("flagged.png");
|
||||||
@@ -299,17 +594,30 @@ public class Controller {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On flag or reinitialization, update the bomb counter to represent the amount of currently flagged tiles
|
||||||
|
*/
|
||||||
private void updateBombCounter() {
|
private void updateBombCounter() {
|
||||||
String bombCountString = String.format("%03d", bombCount);
|
String bombCountString = String.format("%03d", bombCount);
|
||||||
setBombCounterImage(bomb_2, bombCountString.charAt(1));
|
setBombCounterImage(bomb_2, bombCountString.charAt(1));
|
||||||
setBombCounterImage(bomb_3, bombCountString.charAt(2));
|
setBombCounterImage(bomb_3, bombCountString.charAt(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to update the digits of the bomb counter.
|
||||||
|
*
|
||||||
|
* @param imageView The ImageView representing the counter digit to be changed.
|
||||||
|
* @param digit The digit the counter should be changed to.
|
||||||
|
* @see #updateBombCounter()
|
||||||
|
*/
|
||||||
private void setBombCounterImage(ImageView imageView, char digit) {
|
private void setBombCounterImage(ImageView imageView, char digit) {
|
||||||
URL imageURL = getClass().getResource("img/" + digit + "_seconds.png");
|
URL imageURL = getClass().getResource("img/" + digit + "_seconds.png");
|
||||||
imageView.setImage(new Image(String.valueOf(imageURL)));
|
imageView.setImage(new Image(String.valueOf(imageURL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the timer when the user clicks or flags their first tile.
|
||||||
|
*/
|
||||||
private void scheduleTimer() {
|
private void scheduleTimer() {
|
||||||
startTime = System.currentTimeMillis();
|
startTime = System.currentTimeMillis();
|
||||||
TimerTask task = new TimerTask() {
|
TimerTask task = new TimerTask() {
|
||||||
@@ -325,6 +633,11 @@ public class Controller {
|
|||||||
timer.schedule(task, 0, 1000);
|
timer.schedule(task, 0, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the time since the game started, and passes it on.
|
||||||
|
*
|
||||||
|
* @see #setTimerImage(ImageView, char)
|
||||||
|
*/
|
||||||
private void updateTimer() {
|
private void updateTimer() {
|
||||||
long elapsedTimeMillis = System.currentTimeMillis() - startTime;
|
long elapsedTimeMillis = System.currentTimeMillis() - startTime;
|
||||||
time = (int) (elapsedTimeMillis / 1000);
|
time = (int) (elapsedTimeMillis / 1000);
|
||||||
@@ -334,11 +647,24 @@ public class Controller {
|
|||||||
setTimerImage(time_3, timeString.charAt(2));
|
setTimerImage(time_3, timeString.charAt(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the timer digits individually based on updateTimer()
|
||||||
|
*
|
||||||
|
* @param imageView The ImageView to be updated
|
||||||
|
* @param digit The digit to update the ImageView with
|
||||||
|
* @see #updateTimer()
|
||||||
|
*/
|
||||||
private void setTimerImage(ImageView imageView, char digit) {
|
private void setTimerImage(ImageView imageView, char digit) {
|
||||||
URL imageURL = getClass().getResource("img/" + digit + "_seconds.png");
|
URL imageURL = getClass().getResource("img/" + digit + "_seconds.png");
|
||||||
imageView.setImage(new Image(String.valueOf(imageURL)));
|
imageView.setImage(new Image(String.valueOf(imageURL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On game over, show every bomb that was left on the grid, as well as every incorrectly flagged bomb.
|
||||||
|
*
|
||||||
|
* @param clickedColumn The column that caused the game over.
|
||||||
|
* @param clickedRow The row that caused the game over.
|
||||||
|
*/
|
||||||
private void showAllBombs(int clickedColumn, int clickedRow) {
|
private void showAllBombs(int clickedColumn, int clickedRow) {
|
||||||
for (Node node : grid.getChildren()) {
|
for (Node node : grid.getChildren()) {
|
||||||
Button b = (Button) node;
|
Button b = (Button) node;
|
||||||
@@ -355,27 +681,53 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a button's graphic.
|
||||||
|
*
|
||||||
|
* @param button The button to change.
|
||||||
|
* @param imagePath A path to an image to change the graphic to.
|
||||||
|
*/
|
||||||
private void setImage(Button button, String imagePath) {
|
private void setImage(Button button, String imagePath) {
|
||||||
URL imageURL = getClass().getResource(imagePath);
|
URL imageURL = getClass().getResource(imagePath);
|
||||||
button.setGraphic(new ImageView(new Image(String.valueOf(imageURL), 16, 16, true, false)));
|
button.setGraphic(new ImageView(new Image(String.valueOf(imageURL), 16, 16, true, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces an ImageView's graphic with another.
|
||||||
|
*
|
||||||
|
* @param imageView The ImageView to change.
|
||||||
|
* @param imagePath A path to an image to change the graphic to.
|
||||||
|
*/
|
||||||
private void setImage(ImageView imageView, String imagePath) {
|
private void setImage(ImageView imageView, String imagePath) {
|
||||||
URL imageURL = getClass().getResource(imagePath);
|
URL imageURL = getClass().getResource(imagePath);
|
||||||
imageView.setImage(new Image(String.valueOf(imageURL)));
|
imageView.setImage(new Image(String.valueOf(imageURL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces the smiley with a pressed down variant when it is clicked.
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private void smileyPressed() {
|
private void smileyPressed() {
|
||||||
setImage(smiley, "img/face_smile_pressed.png");
|
setImage(smiley, "img/face_smile_pressed.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases the smiley from being pressed down, starts reinitialization.
|
||||||
|
*
|
||||||
|
* @see #reinitialize()
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private void smileyReleased() {
|
private void smileyReleased() {
|
||||||
setImage(smiley, "img/face_smile.png");
|
setImage(smiley, "img/face_smile.png");
|
||||||
reinitialize();
|
reinitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the graphic of a tile to indicate how many bombs are adjacent to it.
|
||||||
|
*
|
||||||
|
* @param tileClicked The tile to update.
|
||||||
|
* @param adjacentBombs The number of bombs adjacent to the tile.
|
||||||
|
*/
|
||||||
private void setAdjacentCount(Node tileClicked, int adjacentBombs) {
|
private void setAdjacentCount(Node tileClicked, int adjacentBombs) {
|
||||||
Button button = (Button) tileClicked;
|
Button button = (Button) tileClicked;
|
||||||
setImage(button, "img/num_" + adjacentBombs + ".png");
|
setImage(button, "img/num_" + adjacentBombs + ".png");
|
||||||
|
|||||||
@@ -1,18 +1,31 @@
|
|||||||
package com.shr4pnel.minesweeper;
|
package com.shr4pnel.minesweeper;
|
||||||
|
|
||||||
|
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to fill the GridWrapper with bombs. Planned to merge into GridWrapper.
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
public class Grid {
|
public class Grid {
|
||||||
|
/**
|
||||||
|
* An instance of GridWrapper used to check generation of bombs.
|
||||||
|
*/
|
||||||
final GridWrapper grid = new GridWrapper();
|
final GridWrapper grid = new GridWrapper();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the bomb-grid structure on instantiation
|
||||||
|
*
|
||||||
|
* @see #generateBombs(int)
|
||||||
|
*/
|
||||||
public Grid() {
|
public Grid() {
|
||||||
// todo fix beginner mode and intermediate!
|
|
||||||
// sorry :3
|
|
||||||
// 99 bombs in expert:
|
|
||||||
generateBombs(99);
|
generateBombs(99);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a boolean 2D array by randomly selecting a column and row, and setting it to true to represent a bomb.
|
||||||
|
*
|
||||||
|
* @param bombMax The number of bombs to generate.
|
||||||
|
*/
|
||||||
private void generateBombs(int bombMax) {
|
private void generateBombs(int bombMax) {
|
||||||
int i;
|
int i;
|
||||||
boolean success;
|
boolean success;
|
||||||
|
|||||||
@@ -1,33 +1,76 @@
|
|||||||
package com.shr4pnel.minesweeper;
|
package com.shr4pnel.minesweeper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simplifies operations on the bomb array. Preventing direct access leads to cleaner code.
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
public class GridWrapper {
|
public class GridWrapper {
|
||||||
|
/**
|
||||||
|
* Number of columns.
|
||||||
|
*/
|
||||||
private static final int COLUMNS = 30;
|
private static final int COLUMNS = 30;
|
||||||
|
/**
|
||||||
|
* Number of rows.
|
||||||
|
*/
|
||||||
private static final int ROWS = 16;
|
private static final int ROWS = 16;
|
||||||
|
/**
|
||||||
|
* A low level 2D array representing the position of bombs.
|
||||||
|
*/
|
||||||
final boolean[][] grid = new boolean[COLUMNS][ROWS];
|
final boolean[][] grid = new boolean[COLUMNS][ROWS];
|
||||||
|
/**
|
||||||
|
* Points at a column in grid.
|
||||||
|
*/
|
||||||
private int currentColumn;
|
private int currentColumn;
|
||||||
|
/**
|
||||||
|
* Points at a row in grid.
|
||||||
|
*/
|
||||||
private int currentRow;
|
private int currentRow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On instantiation, initialize currentColumn and currentRow to 0
|
||||||
|
*/
|
||||||
public GridWrapper() {
|
public GridWrapper() {
|
||||||
this.currentColumn = 0;
|
this.currentColumn = 0;
|
||||||
this.currentRow = 0;
|
this.currentRow = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets currentColumn to a specified column.
|
||||||
|
*
|
||||||
|
* @param column The column specified.
|
||||||
|
* @return Current GridWrapper instance.
|
||||||
|
*/
|
||||||
public GridWrapper atColumn(int column) {
|
public GridWrapper atColumn(int column) {
|
||||||
this.currentColumn = column;
|
this.currentColumn = column;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets currentColumn to a specified column.
|
||||||
|
*
|
||||||
|
* @param row The row specified.
|
||||||
|
* @return Current GridWrapper instance.
|
||||||
|
*/
|
||||||
public GridWrapper atRow(int row) {
|
public GridWrapper atRow(int row) {
|
||||||
this.currentRow = row;
|
this.currentRow = row;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a bomb in grid at the position specified by currentColumn and currentRow.
|
||||||
|
*/
|
||||||
public void setBomb() {
|
public void setBomb() {
|
||||||
if (isValid(currentColumn, currentRow)) {
|
if (isValid(currentColumn, currentRow)) {
|
||||||
grid[currentColumn][currentRow] = true;
|
grid[currentColumn][currentRow] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Switches a bomb from one position to another.
|
||||||
|
*
|
||||||
|
* @param destinationColumn The new column for the bomb.
|
||||||
|
* @param destinationRow The new row for the bomb.
|
||||||
|
*/
|
||||||
public void switchBomb(int destinationColumn, int destinationRow) {
|
public void switchBomb(int destinationColumn, int destinationRow) {
|
||||||
if (isValid(currentColumn, currentRow) && isValid(destinationColumn, destinationRow)) {
|
if (isValid(currentColumn, currentRow) && isValid(destinationColumn, destinationRow)) {
|
||||||
grid[destinationColumn][destinationRow] = true;
|
grid[destinationColumn][destinationRow] = true;
|
||||||
@@ -35,10 +78,20 @@ public class GridWrapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if there is a bomb at the specified position.
|
||||||
|
*
|
||||||
|
* @return The value in grid specified by currentColumn and currentRow.
|
||||||
|
*/
|
||||||
public boolean isBomb() {
|
public boolean isBomb() {
|
||||||
return isValid(currentColumn, currentRow) && grid[currentColumn][currentRow];
|
return isValid(currentColumn, currentRow) && grid[currentColumn][currentRow];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks for bombs in every direction from the currentColumn and currentRow.
|
||||||
|
*
|
||||||
|
* @return The number of adjacent bombs.
|
||||||
|
*/
|
||||||
public int adjacentBombCount() {
|
public int adjacentBombCount() {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
@@ -69,16 +122,50 @@ public class GridWrapper {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo use switchBomb
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Swaps one bomb with another.
|
||||||
|
*
|
||||||
|
* @param oldColumn The original column of the bomb.
|
||||||
|
* @param oldRow The original row of the bomb.
|
||||||
|
* @param newColumn The destination column of the bomb.
|
||||||
|
* @param newRow The destination row of the bomb.
|
||||||
|
*/
|
||||||
public void updateGrid(int oldColumn, int oldRow, int newColumn, int newRow) {
|
public void updateGrid(int oldColumn, int oldRow, int newColumn, int newRow) {
|
||||||
grid[oldColumn][oldRow] = false;
|
grid[oldColumn][oldRow] = false;
|
||||||
grid[newColumn][newRow] = true;
|
grid[newColumn][newRow] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a bomb is at a specified position.
|
||||||
|
*
|
||||||
|
* @param column The column specified.
|
||||||
|
* @param row The row specified.
|
||||||
|
* @return A boolean AND representing a valid position and a bomb being present.
|
||||||
|
* @see #isValid(int, int)
|
||||||
|
*/
|
||||||
private boolean isBombAt(int column, int row) {
|
private boolean isBombAt(int column, int row) {
|
||||||
return isValid(column, row) && grid[column][row];
|
return isValid(column, row) && grid[column][row];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given column and row is in bounds.
|
||||||
|
*
|
||||||
|
* @param column The column provided.
|
||||||
|
* @param row The row provided.
|
||||||
|
* @return A boolean representing the tile being in bounds.
|
||||||
|
*/
|
||||||
private boolean isValid(int column, int row) {
|
private boolean isValid(int column, int row) {
|
||||||
return column >= 0 && column < COLUMNS && row >= 0 && row < ROWS;
|
return column >= 0 && column < COLUMNS && row >= 0 && row < ROWS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given column and row is in bounds using local state
|
||||||
|
*
|
||||||
|
* @return A boolean representing the tile being in bounds.
|
||||||
|
*/
|
||||||
|
public boolean isValid() {
|
||||||
|
return this.currentColumn >= 0 && this.currentColumn < COLUMNS && this.currentRow >= 0 && this.currentRow < ROWS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.shr4pnel.minesweeper;
|
package com.shr4pnel.minesweeper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javafx.application.Application;
|
import javafx.application.Application;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.fxml.FXMLLoader;
|
import javafx.fxml.FXMLLoader;
|
||||||
@@ -8,7 +9,17 @@ import javafx.scene.Scene;
|
|||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The application opening point, used to bootstrap JavaFX and open to the GUI.
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
public class Main extends Application {
|
public class Main extends Application {
|
||||||
|
/**
|
||||||
|
* JavaFX opening method. Creates the stage and bootstraps the application.
|
||||||
|
*
|
||||||
|
* @param stage The stage object passed in by JavaFX.
|
||||||
|
* @throws IOException If the FXML template or app icon are not found in resources.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws IOException {
|
public void start(Stage stage) throws IOException {
|
||||||
Image icon = new Image(String.valueOf(getClass().getResource("winmine.png")));
|
Image icon = new Image(String.valueOf(getClass().getResource("winmine.png")));
|
||||||
@@ -21,10 +32,19 @@ public class Main extends Application {
|
|||||||
stage.show();
|
stage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application opening method. Calls the JavaFX start method.
|
||||||
|
*
|
||||||
|
* @param args Optional commandline parameters, unimplemented.
|
||||||
|
* @see #start(Stage)
|
||||||
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
launch();
|
launch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels the timer on application stop.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@FXML
|
@FXML
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* The main module for the program. Required to launch to JavaFX as well as bundle into a native binary.
|
||||||
|
* @author shrapnelnet admin@shr4pnel.com
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
module libremines {
|
module libremines {
|
||||||
requires javafx.controls;
|
requires javafx.controls;
|
||||||
requires javafx.fxml;
|
requires javafx.fxml;
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.scene.image.*?>
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
<?import javafx.scene.text.*?>
|
||||||
|
|
||||||
|
<AnchorPane prefHeight="225.0" prefWidth="454.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1">
|
||||||
|
<children>
|
||||||
|
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="146.0" layoutY="31.0" pickOnBounds="true" preserveRatio="true">
|
||||||
|
<image>
|
||||||
|
<Image url="@winmine.png" />
|
||||||
|
</image>
|
||||||
|
</ImageView>
|
||||||
|
<Text layoutY="42.0" strokeType="OUTSIDE" strokeWidth="0.0" text="made with love - shrapnelnet" textAlignment="CENTER" wrappingWidth="454.0">
|
||||||
|
<font>
|
||||||
|
<Font name="Source Code Pro" size="13.0" />
|
||||||
|
</font>
|
||||||
|
</Text>
|
||||||
|
<Text layoutY="183.0" strokeType="OUTSIDE" strokeWidth="0.0" text="greeTz: b4rkod, ping, cockpit, vulon, i330 n' porcupine" textAlignment="CENTER" wrappingWidth="454.0">
|
||||||
|
<font>
|
||||||
|
<Font name="Source Code Pro" size="13.0" />
|
||||||
|
</font>
|
||||||
|
</Text>
|
||||||
|
<Text layoutY="200.0" strokeType="OUTSIDE" strokeWidth="0.0" text="next time make your own!" textAlignment="CENTER" wrappingWidth="454.0">
|
||||||
|
<font>
|
||||||
|
<Font name="Source Code Pro" size="13.0" />
|
||||||
|
</font>
|
||||||
|
</Text>
|
||||||
|
|
||||||
|
</children>
|
||||||
|
</AnchorPane>
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="350.0" prefWidth="500.0" style="-fx-border-color: black; -fx-border-width: 1px;" stylesheets="@fix-glow.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.shr4pnel.minesweeper.Controller">
|
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="350.0" prefWidth="500.0" style="-fx-border-color: black; -fx-border-width: 1px;" stylesheets="@fix-glow.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.shr4pnel.minesweeper.Controller">
|
||||||
<children>
|
<children>
|
||||||
<MenuBar style="-fx-background-color: white;">
|
<MenuBar prefWidth="498.0" style="-fx-background-color: white;">
|
||||||
<menus>
|
<menus>
|
||||||
<Menu mnemonicParsing="false" text="Game">
|
<Menu mnemonicParsing="false" text="Game">
|
||||||
<items>
|
<items>
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
<ToggleGroup fx:id="difficulty" />
|
<ToggleGroup fx:id="difficulty" />
|
||||||
</toggleGroup>
|
</toggleGroup>
|
||||||
</RadioMenuItem>
|
</RadioMenuItem>
|
||||||
<RadioMenuItem mnemonicParsing="false" text="Internmediate" toggleGroup="$difficulty" />
|
<RadioMenuItem mnemonicParsing="false" text="Intermediate" toggleGroup="$difficulty" />
|
||||||
<RadioMenuItem mnemonicParsing="false" selected="true" text="Expert" toggleGroup="$difficulty" />
|
<RadioMenuItem mnemonicParsing="false" selected="true" text="Expert" toggleGroup="$difficulty" />
|
||||||
<SeparatorMenuItem mnemonicParsing="false" />
|
<SeparatorMenuItem mnemonicParsing="false" />
|
||||||
<RadioMenuItem fx:id="marks" mnemonicParsing="false" selected="true" text="Marks (?)" />
|
<RadioMenuItem fx:id="marks" mnemonicParsing="false" selected="true" text="Marks (?)" />
|
||||||
@@ -36,7 +36,7 @@
|
|||||||
</Menu>
|
</Menu>
|
||||||
<Menu mnemonicParsing="false" text="Help">
|
<Menu mnemonicParsing="false" text="Help">
|
||||||
<items>
|
<items>
|
||||||
<MenuItem mnemonicParsing="false" text="About Minesweeper" />
|
<MenuItem fx:id="about" mnemonicParsing="false" text="About Minesweeper" />
|
||||||
</items>
|
</items>
|
||||||
</Menu>
|
</Menu>
|
||||||
</menus>
|
</menus>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.scene.image.*?>
|
||||||
|
<?import javafx.scene.layout.*?>
|
||||||
|
|
||||||
|
|
||||||
|
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="420.0" prefWidth="525.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1">
|
||||||
|
<children>
|
||||||
|
<ImageView fitHeight="420.0" fitWidth="531.0" pickOnBounds="true" preserveRatio="true">
|
||||||
|
<image>
|
||||||
|
<Image url="@img/204.jpg" />
|
||||||
|
</image>
|
||||||
|
</ImageView>
|
||||||
|
</children>
|
||||||
|
</Pane>
|
||||||
Reference in New Issue
Block a user