Commit 5d2df832 authored by morkolai's avatar morkolai
Browse files

WIP: zigzag algorithm works <3, finishing and integration remains

parent e89cd152
......@@ -18,15 +18,8 @@ import com.mygdx.game.model.Game;
import com.mygdx.game.model.GameMode;
import com.mygdx.game.model.GameState;
import com.mygdx.game.model.gamemodes.rules.DefaultRules;
import com.mygdx.game.views.CinematicView;
import com.mygdx.game.views.GameViewManager;
import com.mygdx.game.views.LoadingView;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.enums.CharacterAssets;
import com.mygdx.game.views.enums.CinematicAssets;
import com.mygdx.game.views.enums.LobbyAssets;
import com.mygdx.game.views.enums.MenuAssets;
import java.util.ArrayList;
public class UniCheckersClient extends ApplicationAdapter {
......
......@@ -36,6 +36,7 @@ public class GameController {
}
// TODO: Check if the piece is owned by this player
activePiece = this.view.getPiece(cubeCoordinates);
System.out.print(this.model.getGameState().getGameMode().getPossibleMoves(cubeCoordinates,1,1));
activePiece.setRotateHead(true);
previousCoordinateClicked = cubeCoordinates;
} else { // Field does NOT have piece
......
......@@ -2,7 +2,6 @@
package com.mygdx.game.model
import java.lang.Exception
import kotlin.system.measureTimeMillis
class Field {
......
......@@ -7,7 +7,7 @@ import com.badlogic.gdx.math.Vector3
class Game(gameState: GameState) {
private var gameState: GameState
var gameState: GameState
init {
this.gameState = gameState
......
......@@ -30,96 +30,102 @@ class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {
return this.rules
}
// 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
private fun getPosition(currentPosition: Vector3, direction: Vector3, numberOfSteps: Int = 1): Vector3 {
var v = currentPosition.cpy()
for (i in 1..numberOfSteps) {
v.add(direction)
}
return v.cpy()
}
private fun isFieldClear(field: Vector3): Boolean {
if (this.board?.fieldExists(field) == false ||
this.board?.fields?.get(field)?.hasPiece() == true) {
return false
}
return true
}
private fun isPathJumpable(position: Vector3, direction: Vector3, destination: Vector3): Boolean {
var v = position.cpy().add(direction)
while (v != destination) {
if (isFieldClear(v) || this.board?.fieldExists(v) == false) {
return false
}
v.add(direction)
}
if (isFieldClear(v)) {
return true
}
return false
}
private fun getSecondaryJumpDirection(direction: Vector3): Vector3 {
var v = Vector3(0f,0f,0f)
when (direction) {
Vector3(-1f, 1f, 0f) -> v = Vector3(0f, -1f, 1f)
Vector3(1f, -1f, 0f) -> v = Vector3(-1f, 0f, 1f)
Vector3(0f, -1f, 1f) -> v = Vector3(1f, 0f, -1f)
Vector3(0f, 1f, -1f) -> v = Vector3(1f, -1f, 0f)
Vector3(1f, 0f, -1f) -> v = Vector3(-1f, 1f, 0f)
Vector3(-1f, 0f, 1f) -> v = Vector3(0f, 1f, -1f)
}
return v.cpy()
}
// 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 ) {
moveRange: Int,
jumpRange: Int) {
// Repeats algorithm for each direction
for (direction: Vector3 in directionalUnitVectors) {
// BASIC MOVE
var destination = position.cpy()
// Starts by moving a single field
var destination = position.cpy().add(direction)
for (step in 1..moveRange) {
destination.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) {
if (!isFieldClear(destination)) {
break
}
frontier.add(destination)
destination.add(direction)
frontier.add(destination.cpy())
}
// JUMP MOVE
destination = getPosition(position, direction, jumpRange + 1)
// Sets first jump target
destination = position.cpy().add(direction.cpy().setLength(1F + jumpRange?.toFloat()!!))
// 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))
// Determines if it is possible to make the jump
var jumpable = this.board?.isJumpPossible(position, destination) ?: false
// Holds whether it is a zig or a zag jump
var secondaryJumpDirection = getSecondaryJumpDirection(direction)
var isPrimaryJumpDirection = true
var jumpable = isPathJumpable(position, direction, destination)
while (jumpable) {
frontier.add(destination)
frontier.add(destination.cpy())
var newPosition = destination.cpy()
isPrimaryJumpDirection = !isPrimaryJumpDirection
// Sets direction for next possible move
destination = if (isPrimaryJumpDirection) {
destination.cpy().add(direction)
if (isPrimaryJumpDirection) {
destination = getPosition(newPosition, direction, jumpRange + 1)
jumpable = isPathJumpable(newPosition,direction,destination)
} else {
destination.cpy().add(secondaryDirection)
destination = getPosition(newPosition, secondaryJumpDirection, jumpRange + 1)
jumpable = isPathJumpable(newPosition, secondaryJumpDirection, destination)
}
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> {
fun getPossibleMoves(position: Vector3, moveRange: Int, jumpRange: Int): ArrayList<Vector3> {
var possibleMoves: ArrayList<Vector3> = arrayListOf()
zigzagAlgorithm(position, possibleMoves, moveRange = moveRange, jumpRange = jumpRange)
zigzagAlgorithm(position, possibleMoves, moveRange, jumpRange)
return possibleMoves
}
}
// TODO 1. legge inn variabler som standardparametere
// TODO 2. gjøre gamestate og etc private medlemmer
// TODO 3. merge in default-game
// TODO 4. intigerere greier der de skal
......@@ -7,12 +7,14 @@ Holds the state of the game and all data related to this.
*/
class GameState(gameMode: GameMode? = null) {
var gameMode: GameMode?
private var boardState:AbstractBoard?
private var rules:AbstractRules?
private var isStarted: Boolean
private var winner: Player?
init {
this.gameMode = gameMode
if (gameMode == null) {
this.rules = null
this.boardState = null
......
......@@ -10,40 +10,4 @@ abstract class AbstractBoard {
abstract fun fieldExists(coordinates: Vector3): Boolean
abstract fun placePiecesAtStart(rules: AbstractRules, startFieldCoordinates: List<List<Vector3>>)
abstract fun movePiece(fromCoordinates: Vector3, toCoordinates: 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
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