|
1 | 1 | package sc.plugin2026
|
2 | 2 |
|
3 | 3 | import com.thoughtworks.xstream.annotations.XStreamAlias
|
4 |
| -import com.thoughtworks.xstream.annotations.XStreamImplicit |
5 |
| -import sc.api.plugins.IBoard |
| 4 | +import sc.api.plugins.MutableTwoDBoard |
| 5 | +import sc.api.plugins.RectangularBoard |
| 6 | +import sc.api.plugins.Team |
6 | 7 | import sc.plugin2026.FieldState.OBSTRUCTED
|
7 | 8 | import sc.plugin2026.util.*
|
8 |
| -import java.util.* |
| 9 | +import kotlin.math.floor |
9 | 10 |
|
10 | 11 | /** Spielbrett für Piranhas mit [PiranhaConstants.BOARD_LENGTH]² Feldern. */
|
11 | 12 | @XStreamAlias(value = "board")
|
12 |
| -class Board : IBoard { |
| 13 | +class Board(override val gameField: MutableTwoDBoard<Field> = randomFields()): RectangularBoard<Field>(gameField) { |
13 | 14 |
|
14 |
| - @XStreamImplicit(itemFieldName = "fields") |
15 |
| - private var fields: Array<Array<Field>> |
| 15 | + // TODO later |
| 16 | + //override fun toString() = |
| 17 | + // "Board " + fields.joinToString(" ", "[", "]") { column -> column.joinToString(", ", prefix = "[", postfix = "]") { //it.toString() } } |
| 18 | + // |
| 19 | + //val line = "-".repeat(PiranhaConstants.BOARD_LENGTH + 2) |
| 20 | + //fun prettyString(): String { |
| 21 | + // val map = Array(PiranhaConstants.BOARD_LENGTH) { StringBuilder("|") } |
| 22 | + // fields.forEach { |
| 23 | + // it.forEachIndexed { index, field -> |
| 24 | + // map[index].append(field.state.asLetter()) |
| 25 | + // } |
| 26 | + // } |
| 27 | + // return map.joinToString("\n", line + "\n", "\n" + line) { it.append('|').toString() } |
| 28 | + //} |
16 | 29 |
|
17 |
| - constructor() { |
18 |
| - this.fields = randomFields() |
19 |
| - } |
20 |
| - |
21 |
| - |
22 |
| - constructor(boardToClone: Board) { |
23 |
| - this.fields = generateFields { x, y -> boardToClone.fields[x][y].clone() } |
24 |
| - } |
25 |
| - |
26 |
| - public override fun clone(): Board = Board(this) |
27 |
| - |
28 |
| - override fun equals(other: Any?): Boolean = other is Board && Arrays.equals(other.fields, this.fields) |
29 |
| - |
30 |
| - private fun generateFields(generator: (Int, Int) -> Field): Array<Array<Field>> { |
31 |
| - return Array(PiranhaConstants.BOARD_LENGTH) { x -> |
32 |
| - Array(PiranhaConstants.BOARD_LENGTH) { y -> |
33 |
| - generator(x, y) |
| 30 | + companion object { |
| 31 | + /** Erstellt eine zufälliges Spielbrett. */ |
| 32 | + private fun randomFields(): Array<Array<Field>> { |
| 33 | + val fields = generateFields { x, y -> Field(x, y) } |
| 34 | + |
| 35 | + // Place Piranhas |
| 36 | + for(index in 1 until PiranhaConstants.BOARD_LENGTH - 1) { |
| 37 | + fields[0][index].setPiranha(Team.ONE) |
| 38 | + fields[PiranhaConstants.BOARD_LENGTH - 1][index].setPiranha(Team.ONE) |
| 39 | + fields[index][0].setPiranha(Team.TWO) |
| 40 | + fields[index][PiranhaConstants.BOARD_LENGTH - 1].setPiranha(Team.TWO) |
34 | 41 | }
|
35 |
| - } |
36 |
| - } |
37 |
| - |
38 |
| - /** Erstellt eine zufälliges Spielbrett. */ |
39 |
| - private fun randomFields(): Array<Array<Field>> { |
40 |
| - val fields = generateFields { x, y -> Field(x, y) } |
41 |
| - |
42 |
| - // Place Piranhas |
43 |
| - for(index in 1 until PiranhaConstants.BOARD_LENGTH - 1) { |
44 |
| - fields[0][index].setPiranha(Team.ONE) |
45 |
| - fields[PiranhaConstants.BOARD_LENGTH - 1][index].setPiranha(Team.ONE) |
46 |
| - fields[index][0].setPiranha(Team.TWO) |
47 |
| - fields[index][PiranhaConstants.BOARD_LENGTH - 1].setPiranha(Team.TWO) |
48 |
| - } |
49 |
| - |
50 |
| - // Place Obstacles |
51 |
| - // only consider fields in the middle of the board |
52 |
| - var blockableFields: List<Field> = fields.slice(PiranhaConstants.OBSTACLES_START..PiranhaConstants.OBSTACLES_END).flatMap { it.slice(PiranhaConstants.OBSTACLES_START..PiranhaConstants.OBSTACLES_END) } |
53 |
| - // set fields with randomly selected coordinates to blocked |
54 |
| - // coordinates may not lay on same horizontal, vertical or diagonal lines with other selected coordinates |
55 |
| - for(i in 0 until PiranhaConstants.NUM_OBSTACLES) { |
56 |
| - val indexOfFieldToBlock = Math.floor(Math.random() * blockableFields.size).toInt() |
57 |
| - val selectedField = blockableFields[indexOfFieldToBlock] |
58 |
| - selectedField.state = OBSTRUCTED |
59 |
| - blockableFields = blockableFields.filter { field -> |
60 |
| - !(field.x == selectedField.x || field.y == selectedField.y || |
61 |
| - field.x - field.y == selectedField.x - selectedField.y || |
62 |
| - field.x + field.y == selectedField.x + selectedField.y) |
| 42 | + |
| 43 | + // Place Obstacles |
| 44 | + // only consider fields in the middle of the board |
| 45 | + var blockableFields: List<Field> = fields.slice(PiranhaConstants.OBSTACLES_START..PiranhaConstants.OBSTACLES_END).flatMap { it.slice(PiranhaConstants.OBSTACLES_START..PiranhaConstants.OBSTACLES_END) } |
| 46 | + // set fields with randomly selected coordinates to blocked |
| 47 | + // coordinates may not lay on same horizontal, vertical or diagonal lines with other selected coordinates |
| 48 | + for(i in 0 until PiranhaConstants.NUM_OBSTACLES) { |
| 49 | + val indexOfFieldToBlock = floor(Math.random() * blockableFields.size).toInt() |
| 50 | + val selectedField = blockableFields[indexOfFieldToBlock] |
| 51 | + selectedField.state = OBSTRUCTED |
| 52 | + blockableFields = blockableFields.filter { field -> |
| 53 | + !(field.x == selectedField.x || field.y == selectedField.y || |
| 54 | + field.x - field.y == selectedField.x - selectedField.y || |
| 55 | + field.x + field.y == selectedField.x + selectedField.y) |
| 56 | + } |
63 | 57 | }
|
| 58 | + return fields |
64 | 59 | }
|
65 |
| - return fields |
66 |
| - } |
67 |
| - |
68 |
| - override fun toString() = |
69 |
| - "Board " + fields.joinToString(" ", "[", "]") { column -> column.joinToString(", ", prefix = "[", postfix = "]") { it.toString() } } |
70 |
| - |
71 |
| - val line = "-".repeat(PiranhaConstants.BOARD_LENGTH + 2) |
72 |
| - fun prettyString(): String { |
73 |
| - val map = Array(PiranhaConstants.BOARD_LENGTH) { StringBuilder("|") } |
74 |
| - fields.forEach { |
75 |
| - it.forEachIndexed { index, field -> |
76 |
| - map[index].append(field.state.asLetter()) |
| 60 | + |
| 61 | + private fun generateFields(generator: (Int, Int) -> Field): Array<Array<Field>> { |
| 62 | + return Array(PiranhaConstants.BOARD_LENGTH) { x -> |
| 63 | + Array(PiranhaConstants.BOARD_LENGTH) { y -> |
| 64 | + generator(x, y) |
| 65 | + } |
77 | 66 | }
|
78 | 67 | }
|
79 |
| - return map.joinToString("\n", line + "\n", "\n" + line) { it.append('|').toString() } |
80 | 68 | }
|
81 |
| - |
82 |
| - fun getField(x: Int, y: Int): Field = |
83 |
| - this.fields[x][y] |
84 |
| - |
85 | 69 | }
|
86 | 70 |
|
0 commit comments