Commit 1aaede99 authored by morkolai's avatar morkolai
Browse files

WIP rewritten whole algorithm for possible moves

parent 0265ae7c
package com.mygdx.game.model
import com.badlogic.gdx.math.Vector3
import com.sun.org.apache.xpath.internal.operations.Bool
import java.lang.Exception
import java.lang.NullPointerException
//Holds a ruleset and a board
......@@ -33,43 +30,80 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
return this.rules
}
fun getPossibleMoves(position: Vector3): ArrayList<Vector3> {
fun isInBoard(pos: Vector3): Boolean {
var inBoard: Boolean
try {
inBoard = this.board!!.fieldExists(pos)
} catch (e: NullPointerException) {
inBoard = false
// Returns zeros for ones and vice versa, otherwise returns original parameter value
private fun flip(a: Float): Float {
when (a) {
0F -> return 1F
1F -> return 0F
else -> {
return a
}
return inBoard
}
}
fun hasPiece(pos: Vector3): Boolean {
return (this.board?.fields?.get(pos)?.hasPiece()) ?: false
}
// Populates an arrayList with all possible moves according to zigzag jump rules
private fun zigzagAlgorithm(position: Vector3, frontier: ArrayList<Vector3>,
jumpRange: Int? = this.rules?.jumpRange,
moveRange: Int? = this.rules?.moveRange ) {
// Repeats algorithm for each direction
for (direction: Vector3 in directionalUnitVectors) {
// BASIC MOVE
// Starts by moving a single field
var destination = position.cpy().add(direction)
// Keeps adding fields to frontier as long as they are clear and within range
while (destination != position.cpy().add(direction.setLength(1F + moveRange?.toFloat()!!))) {
if (this.board?.isPathClear(position, destination) == false) {
break
}
frontier.add(destination)
destination.add(direction)
}
// JUMP MOVE
fun algorithm(pos: Vector3, frontier: ArrayList<Vector3>, iterationNumber: Int = 0) {
// Sets first jump target
destination = position.cpy().add(direction.cpy().setLength(1F + jumpRange?.toFloat()!!))
val iterationCount: Int = 1
// Finds the other jumping direction by switching 1 and 0, -1 is left as it is, e.g. finds
// the zag direction
var secondaryDirection = Vector3(flip(direction.x), flip(direction.y), flip(direction.z))
for (dir:Vector3 in directionalUnitVectors) {
// Determines if it is possible to make the jump
var jumpable = this.board?.isJumpPossible(position, destination) ?: false
var newPos:Vector3 = pos.add(dir)
// Holds whether it is a zig or a zag jump
var isPrimaryJumpDirection = true
if (isInBoard(newPos) && hasPiece(newPos) && iterationNumber < iterationCount) {
algorithm(newPos,frontier, iterationNumber+1)
} else if (isInBoard(newPos) && !hasPiece(newPos)) {
frontier.add(newPos)
while (jumpable) {
frontier.add(destination)
var newPosition = destination.cpy()
// Sets direction for next possible move
destination = if (isPrimaryJumpDirection) {
destination.cpy().add(direction)
} else {
destination.cpy().add(secondaryDirection)
}
jumpable = this.board?.isJumpPossible(newPosition, destination) ?: false
isPrimaryJumpDirection = !isPrimaryJumpDirection
}
}
}
fun getPossibleMoves(position: Vector3, moveRange: Int? = this.rules?.moveRange, jumpRange: Int? = this.rules?.jumpRange): ArrayList<Vector3> {
var possibleMoves: ArrayList<Vector3> = arrayListOf()
algorithm(position, possibleMoves)
zigzagAlgorithm(position, possibleMoves, moveRange = moveRange, jumpRange = jumpRange)
return possibleMoves
}
}
......@@ -88,3 +122,4 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
......@@ -8,4 +8,39 @@ abstract class AbstractBoard {
abstract fun computeFields() // Computes all fields on the board
abstract fun fieldExists(coordinates: Vector3): Boolean
// Determines if the path does not hold any pieces and exists
fun isPathClear(currentPosition: Vector3, targetPosition: Vector3): Boolean {
var direction = targetPosition.cpy().sub(currentPosition).setLength(1F)
var logicalStopPosition = targetPosition.cpy().add(direction)
while (currentPosition != logicalStopPosition) {
if (this.fields[currentPosition]?.hasPiece() == true && !fieldExists(currentPosition)) {
return false
}
currentPosition.add(direction)
}
return true
}
// Determines if a jump is possible e.g. pieces all the way except the last field and it exists
fun isJumpPossible(currentPosition: Vector3, destinationPosition: Vector3): Boolean {
var direction = destinationPosition.cpy().sub(currentPosition).setLength(1F)
currentPosition.add(direction)
while (currentPosition != destinationPosition) {
if (this.fields[currentPosition]?.hasPiece() == true || !fieldExists(currentPosition)) {
return false
}
currentPosition.add(direction)
}
if (this.fields[currentPosition]?.hasPiece() == true) {
return false
}
return true
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ abstract class AbstractRules {
abstract var startFields: List<List<Vector3>> // List containing list of startfields for each player
abstract var moveRange: Int // How far a piece can be moved
abstract var jumpRange: Int // How far a piece can jump (over other pieces)
abstract var jumpRange: Int // How many pieces a piece can jump over
abstract fun getPlayerStartfields(playerId: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerId: Int) : List<Vector3> // Returns a list of targetfields for a given player
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment