Skip to content

Commit 9cb8cec

Browse files
author
Janek
authored
Conversion to Kotlin & Improve toString methods (#204)
* Add Board prettyString method * Fix a few Tests * Introduce more Kotlin
1 parent 6223eca commit 9cb8cec

File tree

16 files changed

+174
-198
lines changed

16 files changed

+174
-198
lines changed

.idea/codeStyles/Project.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradle/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ tasks {
4949
dependsOn(doc)
5050
dependOnSubprojects()
5151
group = mainGroup
52-
description = "Zips everything up for release into build/deploy"
52+
description = "Zips everything up for release into ./build/deploy"
5353
}
5454

5555
val release by creating {
@@ -73,7 +73,7 @@ tasks {
7373
println("""
7474
===================================================
7575
Fertig! Jetzt noch folgende Schritte ausfuehren:
76-
- einen Release für die GUI erstellen
76+
- ein Release für die GUI erstellen
7777
- auf der Website (http://www.software-challenge.de/wp-admin) unter Medien die Dateien ersetzen
7878
- unter Seiten die Downloadseite aktualisieren (neue Version in Versionshistorie eintragen)
7979

plugin/src/shared/sc/plugin2019/Board.java

Lines changed: 0 additions & 103 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package sc.plugin2019
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias
4+
import com.thoughtworks.xstream.annotations.XStreamImplicit
5+
import sc.api.plugins.IBoard
6+
import sc.plugin2019.FieldState.OBSTRUCTED
7+
import sc.plugin2019.util.Constants
8+
import sc.shared.PlayerColor
9+
import java.util.*
10+
11+
/** Spielbrett für Piranhas mit [Constants.BOARD_SIZE]² Feldern. */
12+
@XStreamAlias(value = "board")
13+
class Board : IBoard {
14+
15+
@XStreamImplicit(itemFieldName = "fields")
16+
private var fields: Array<Array<Field>>
17+
18+
constructor() {
19+
this.fields = randomFields()
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(Constants.BOARD_SIZE) { x ->
32+
Array(Constants.BOARD_SIZE) { y ->
33+
generator(x, y)
34+
}
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 Constants.BOARD_SIZE - 1) {
44+
fields[0][index].setPiranha(PlayerColor.RED)
45+
fields[Constants.BOARD_SIZE - 1][index].setPiranha(PlayerColor.RED)
46+
fields[index][0].setPiranha(PlayerColor.BLUE)
47+
fields[index][Constants.BOARD_SIZE - 1].setPiranha(PlayerColor.BLUE)
48+
}
49+
50+
// Place Obstacles
51+
// only consider fields in the middle of the board
52+
var blockableFields: List<Field> = fields.slice(Constants.OBSTACLES_START..Constants.OBSTACLES_END).flatMap { it.slice(Constants.OBSTACLES_START..Constants.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 Constants.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)
63+
}
64+
}
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(Constants.BOARD_SIZE + 2)
72+
fun prettyString(): String {
73+
val map = Array(Constants.BOARD_SIZE) { StringBuilder("|") }
74+
fields.forEach {
75+
it.forEachIndexed { index, field ->
76+
map[index].append(field.state.asLetter())
77+
}
78+
}
79+
return map.joinToString("\n", line + "\n", "\n" + line) { it.append('|').toString() }
80+
}
81+
82+
override fun getField(x: Int, y: Int): Field =
83+
this.fields[x][y]
84+
85+
}
86+

plugin/src/shared/sc/plugin2019/Field.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import static sc.plugin2019.FieldState.*;
1111

1212
/**
13-
* Ein Feld des Spielfelds. Ein Spielfeld hat eine x- und y-Koordinate und einen {@link FieldState}.
13+
* Ein Feld des Spielfelds. Ein Spielfeld hat eine x- und y-Koordinate und einen {@link FieldState}, der anzeigt ob sich etwas auf diesem Feld befindet.
1414
*/
1515
@XStreamAlias(value = "field")
1616
public class Field implements IField {
@@ -89,11 +89,7 @@ else if(state == BLUE)
8989
return Optional.empty();
9090
}
9191

92-
/**
93-
* Nur für den Server (für Test) relevant.
94-
*
95-
* @param piranha Farbe des Piranhas
96-
*/
92+
/** Nur für den Server relevant. */
9793
public void setPiranha(PlayerColor piranha) {
9894
state = FieldState.from(piranha);
9995
}

plugin/src/shared/sc/plugin2019/FieldState.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sc.plugin2019
2+
3+
import sc.shared.PlayerColor
4+
5+
enum class FieldState {
6+
RED,
7+
BLUE,
8+
OBSTRUCTED,
9+
EMPTY;
10+
11+
override fun toString() = when(this) {
12+
OBSTRUCTED -> "Obstructed"
13+
RED -> "Red"
14+
BLUE -> "Blue"
15+
else -> "empty"
16+
}
17+
18+
fun asLetter() = when(this) {
19+
OBSTRUCTED -> 'O'
20+
RED -> 'R'
21+
BLUE -> 'B'
22+
else -> ' '
23+
}
24+
25+
companion object {
26+
@JvmStatic
27+
fun from(color: PlayerColor): FieldState {
28+
return when(color) {
29+
PlayerColor.RED -> RED
30+
PlayerColor.BLUE -> BLUE
31+
}
32+
}
33+
}
34+
}

plugin/src/shared/sc/plugin2019/util/Constants.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ public class Constants {
1010
public static final int GAME_STATS_SWARM_SIZE = 0;
1111
public static final int GAME_STATS_RED_INDEX = 0;
1212
public static final int GAME_STATS_BLUE_INDEX = 1;
13-
public static final int WIN_SCORE = 2;
1413
public static final int LOSE_SCORE = 0;
1514
public static final int DRAW_SCORE = 1;
15+
public static final int WIN_SCORE = 2;
1616

1717
public static final int BOARD_SIZE = 10;
1818
public static final int NUM_OBSTACLES = 2;
1919
public static final int MAX_FISH = (BOARD_SIZE - 2) * 2;
20-
public static final int OBSTACLES_START = 2; // refers to the lowest index at which a obstructed field might be placed
21-
public static final int OBSTACLES_END = 7; // refers to the highest index at which a obstructed field might be placed
20+
/** The lowest index at which a obstructed field may be placed */
21+
public static final int OBSTACLES_START = 2;
22+
/** The highest index at which a obstructed field may be placed */
23+
public static final int OBSTACLES_END = 7;
2224
}

0 commit comments

Comments
 (0)