From 2634498599c05493bf9d9ae3cba382079e55cfe1 Mon Sep 17 00:00:00 2001
From: morkolai <nikolai-mork@live.no>
Date: Sat, 24 Apr 2021 14:29:59 +0200
Subject: [PATCH] Original moves are now working

---
 .../game/controllers/NetworkController.java   |  6 +-
 .../core/src/com/mygdx/game/model/GameMode.kt | 65 ++++++-------------
 .../model/gamemodes/rules/AbstractRules.kt    |  1 +
 .../model/gamemodes/rules/DefaultRules.kt     |  1 +
 4 files changed, 26 insertions(+), 47 deletions(-)

diff --git a/CheckersClient/core/src/com/mygdx/game/controllers/NetworkController.java b/CheckersClient/core/src/com/mygdx/game/controllers/NetworkController.java
index 92da9b2..3043914 100644
--- a/CheckersClient/core/src/com/mygdx/game/controllers/NetworkController.java
+++ b/CheckersClient/core/src/com/mygdx/game/controllers/NetworkController.java
@@ -23,8 +23,10 @@ public class NetworkController {
         this.client = new Client();
         client.start();
         try {
-            String IP4_LAN_ADDRESS = "192.168.0.136"; //122 //"192.168.87.20";
-            client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
+
+            String IP4_LAN_ADDRESS = "192.168.0.109";
+            client.connect(10000, IP4_LAN_ADDRESS, 54555);
+
         } catch (IOException e) {
             e.printStackTrace();
         }
diff --git a/CheckersClient/core/src/com/mygdx/game/model/GameMode.kt b/CheckersClient/core/src/com/mygdx/game/model/GameMode.kt
index a9d3a57..4e7308a 100644
--- a/CheckersClient/core/src/com/mygdx/game/model/GameMode.kt
+++ b/CheckersClient/core/src/com/mygdx/game/model/GameMode.kt
@@ -47,7 +47,9 @@ class GameMode(rules:AbstractRules, board:AbstractBoard) {
     }
 
     private fun isPathJumpable(position: Vector3, direction: Vector3, destination: Vector3): Boolean {
-        var v = position.cpy().add(direction)
+
+        val v = position.cpy().add(direction)
+
         while (v != destination) {
             if (isFieldClear(v) || this.board?.fieldExists(v) == false) {
                 return false
@@ -60,28 +62,11 @@ class GameMode(rules:AbstractRules, board:AbstractBoard) {
         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>,
-                                moveRange: Int,
-                                jumpRange: Int) {
+    // Populates a list with all possible basic moves
+    private fun getBasicMoves(position: Vector3, moveRange: Int, frontier: ArrayList<Vector3>) {
 
-        // Repeats algorithm for each direction
         for (direction: Vector3 in directionalUnitVectors) {
 
-            // BASIC MOVE
             var destination = position.cpy()
 
             for (step in 1..moveRange) {
@@ -92,40 +77,30 @@ class GameMode(rules:AbstractRules, board:AbstractBoard) {
                 }
                 frontier.add(destination.cpy())
             }
+        }
+    }
 
-            // JUMP MOVE
-            destination = getPosition(position, direction, jumpRange + 1)
+    // Populates a list with all possible jumps
+    private fun getDefaultJumps(position: Vector3, jumpRange: Int, frontier: ArrayList<Vector3>, maxIterations: Int,  iterationNumber: Int) {
 
-            var secondaryJumpDirection = getSecondaryJumpDirection(direction)
-            var isPrimaryJumpDirection = true
-            var jumpable = isPathJumpable(position, direction, destination)
+        if (iterationNumber < maxIterations) {
+            for (direction: Vector3 in directionalUnitVectors) {
 
-            while (jumpable) {
-                frontier.add(destination.cpy())
-                var newPosition = destination.cpy()
-                isPrimaryJumpDirection = !isPrimaryJumpDirection
-
-                // Sets direction for next possible move
-                if (isPrimaryJumpDirection) {
-                    destination = getPosition(newPosition, direction, jumpRange + 1)
-                    jumpable = isPathJumpable(newPosition,direction,destination)
-                } else {
-                    destination = getPosition(newPosition, secondaryJumpDirection, jumpRange + 1)
-                    jumpable = isPathJumpable(newPosition, secondaryJumpDirection, destination)
+                var destination = getPosition(position, direction, jumpRange + 1)
+
+                if (isPathJumpable(position, direction, destination)){
+                    frontier.add(destination.cpy())
+                    getDefaultJumps(destination, jumpRange, frontier, maxIterations, iterationNumber+1)
                 }
             }
         }
     }
 
+    // Choice of gamemode should be implemented here in the future
     fun getPossibleMoves(position: Vector3): ArrayList<Vector3> {
-        var possibleMoves: ArrayList<Vector3> = arrayListOf()
-        zigzagAlgorithm(position, possibleMoves, this.rules.moveRange , this.rules?.jumpRange)
+        val possibleMoves: ArrayList<Vector3> = arrayListOf() //val?????
+        getBasicMoves(position, this.rules.moveRange, possibleMoves)
+        getDefaultJumps(position, this.rules.jumpRange,possibleMoves, this.rules.maxJumps,0)
         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
-
diff --git a/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/AbstractRules.kt b/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/AbstractRules.kt
index f04ffa3..b573330 100644
--- a/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/AbstractRules.kt
+++ b/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/AbstractRules.kt
@@ -8,6 +8,7 @@ abstract class AbstractRules {
 
     abstract var moveRange: Int // How far a piece can be moved
     abstract var jumpRange: Int // How many pieces a piece can jump over
+    abstract var maxJumps: Int // How many jumps can a piece do after each other
 
 
     abstract fun getPlayerStartfields(boardSlot: Int) : List<Vector3>                // Returns a list of startfields for a given player
diff --git a/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/DefaultRules.kt b/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/DefaultRules.kt
index 1432884..540e77a 100644
--- a/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/DefaultRules.kt
+++ b/CheckersClient/core/src/com/mygdx/game/model/gamemodes/rules/DefaultRules.kt
@@ -9,6 +9,7 @@ class DefaultRules: AbstractRules() {
     override var startFields = generateStartFields()
     override var moveRange = 1
     override var jumpRange = 1
+    override var maxJumps = 6
 
     override fun getPlayerStartfields(boardSlot: Int): List<Vector3> {
         return startFields[boardSlot]
-- 
GitLab