GameMode.kt 1.89 KB
Newer Older
1
2
3
package com.mygdx.game.model

import com.badlogic.gdx.math.Vector3
4
5
6
import com.sun.org.apache.xpath.internal.operations.Bool
import java.lang.Exception
import java.lang.NullPointerException
7
8

//Holds a ruleset and a board
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

class GameMode(rules:AbstractRules? = null, board:AbstractBoard? = null) {

    private var rules: AbstractRules?
    private var board: AbstractBoard?

    init {
        this.rules = rules
        this.board = board
    }

    fun setBoard(board:AbstractBoard) {
        this.board = board
    }

    fun getBoard(): AbstractBoard? {
        return this.board
    }

    fun setRules(rules: AbstractRules) {
        this.rules = rules
    }

    fun getRules(): AbstractRules? {
        return this.rules
    }
35

36
    fun getPossibleMoves(position: Vector3): ArrayList<Vector3> {
37

38
39
40
41
42
43
44
45
46
47
48
49
50
        fun isInBoard(pos: Vector3): Boolean {
            var inBoard: Boolean
            try {
                inBoard = this.board!!.fieldExists(pos)
            } catch (e: NullPointerException) {
                inBoard = false
            }
            return inBoard
        }

        fun hasPiece(pos: Vector3): Boolean {
            return (this.board?.fields?.get(pos)?.hasPiece()) ?: false
        }
51

52
        fun algorithm(pos: Vector3, frontier: ArrayList<Vector3>, iterationNumber: Int = 0) {
53

54
            val iterationCount: Int = 1
55

56
57
58
59
60
61
62
63
64
65
            for (dir:Vector3 in directionalUnitVectors) {

                var newPos:Vector3 = pos.add(dir)

                if (isInBoard(newPos) && hasPiece(newPos) && iterationNumber < iterationCount) {
                    algorithm(newPos,frontier, iterationNumber+1)
                } else if (isInBoard(newPos) && !hasPiece(newPos)) {
                    frontier.add(newPos)
                }
            }
66
67
        }

68
69
70
71
        var possibleMoves: ArrayList<Vector3> = arrayListOf()
        algorithm(position, possibleMoves)
        return possibleMoves

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    }
}