Skip to content

Commit 8404a1f

Browse files
committed
feat(plugin/Board): update generation algorithm
1 parent bc13210 commit 8404a1f

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ The version should always be in sync with the [GUI](https://github.com/software-
1010
A `y` version of 0 marks the beta of the current year
1111
and likely contains breaking changes between patches.
1212

13+
### [23.0.2](https://github.com/software-challenge/backend/commits/23.0.2) New Board generation - 2022-08-21
14+
- Update Board generation:
15+
The Board is now mirrored with holes,
16+
with more holes in the middle and more fish at the edge.
17+
- Improve automated checks
18+
1319
### [23.0.1](https://github.com/software-challenge/backend/commits/23.0.1) Proper Penguins - 2022-08-06
1420
- Proper rule implementation for Penguins with some helpers
21+
- Improve XML
1522

1623
### [23.0.0](https://github.com/software-challenge/backend/commits/23.0.0) Rough Penguins - 2022-07-28
1724
- Generify some plugin functionality to the SDK

plugin/src/main/kotlin/sc/plugin2023/Board.kt

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,10 @@ class Board(fields: TwoDBoard<Field> = generateFields()): RectangularBoard<Field
1616

1717
constructor(board: Board): this(board.gameField.clone())
1818

19-
/**
20-
* Setzt einen Pinguin an gewählte Koordinaten. Diese Methode ist nur für
21-
* den Server relevant, da keine vollständige Überprüfung auf korrekte Züge
22-
* durchgeführt wird. Um für einen Spieler einen neuen Pinguin zu setzen,
23-
* die [perform][sc.plugin2015.SetMove.perform]
24-
* -Methode benutzen
25-
*
26-
* @param x
27-
* X-Koordinate
28-
* @param y
29-
* Y-Koordinate
30-
* @param penguin
31-
* Pinguin, der gesetzt werden soll.
32-
*/
33-
//@Throws(IllegalArgumentException::class)
34-
//fun putPenguin(x: Int, y: Int, penguin: Penguin?) {
35-
// require((x < 0 || y < 0 || x >= Constants.BOARD_SIZE || y >= Constants.BOARD_SIZE || fields[x][y].fish !== 1 || fields[x][y].penguin) == null)
36-
// fields[x][y].putPenguin(penguin)
37-
//}
38-
///**
39-
// * nur für den Server relevant
40-
// */
41-
//@Throws(IllegalArgumentException::class)
42-
//private fun putPenguinMove(x: Int, y: Int, penguin: Penguin) {
43-
// require((x < 0 || y < 0 || x >= Constants.BOARD_SIZE || y >= Constants.BOARD_SIZE || fields[x][y].fish <= 0 || fields[x][y].penguin) == null)
44-
// fields[x][y].putPenguin(penguin)
45-
//}
46-
4719
override fun isValid(coordinates: Coordinates) =
4820
(coordinates.x + coordinates.y) % 2 == 0 &&
49-
coordinates.x >= 0 &&
50-
super.isValid(coordinates.copy(coordinates.x / 2))
21+
coordinates.x >= 0 &&
22+
super.isValid(coordinates.copy(coordinates.x / 2))
5123

5224
/** Gibt das Feld an den gegebenen Koordinaten zurück. */
5325
override operator fun get(x: Int, y: Int) =
@@ -64,11 +36,11 @@ class Board(fields: TwoDBoard<Field> = generateFields()): RectangularBoard<Field
6436
}
6537

6638
fun possibleMovesFrom(pos: Coordinates) =
67-
Vector.DoubledHex.directions.flatMap { vector ->
68-
(1 until Constants.BOARD_SIZE).map {
69-
Move.run(pos, vector * it)
70-
}.takeWhile { getOrEmpty(it.to).fish > 0 }
71-
}
39+
Vector.DoubledHex.directions.flatMap { vector ->
40+
(1 until Constants.BOARD_SIZE).map {
41+
Move.run(pos, vector * it)
42+
}.takeWhile { getOrEmpty(it.to).fish > 0 }
43+
}
7244

7345
/** Returns a list of the non-null filter outputs */
7446
fun <T> filterFields(filter: (Field, Coordinates) -> T?): Collection<T> =
@@ -86,22 +58,29 @@ class Board(fields: TwoDBoard<Field> = generateFields()): RectangularBoard<Field
8658
fun getOrEmpty(key: Coordinates?) = key?.let { getOrNull(it) } ?: Field()
8759

8860
override val entries: Set<Map.Entry<Coordinates, Field>>
89-
get() = filterFields { field, coordinates -> FieldPosition(coordinates, field) }.toSet()
61+
get() = filterFields { f, coordinates -> FieldPosition(coordinates, f) }.toSet()
9062

9163
override fun clone(): Board = Board(this)
9264

9365
companion object {
9466
/** Generiert ein neues Spielfeld mit zufällig auf dem Spielbrett verteilten Fischen. */
9567
private fun generateFields(seed: Int = Random.nextInt()): TwoDBoard<Field> {
96-
var remainingFish = 100
68+
var remainingFish = Constants.BOARD_SIZE * (Constants.BOARD_SIZE + 1)
9769
val random = Random(seed)
98-
// TODO val holes =
99-
return List(Constants.BOARD_SIZE) {
70+
println("Board seed: $seed")
71+
return List(Constants.BOARD_SIZE / 2) {
10072
MutableList(Constants.BOARD_SIZE) {
101-
val fish = random.nextInt(remainingFish) / 30 + 1
73+
val rand = random.nextInt(remainingFish)
74+
if(rand < 5)
75+
return@MutableList Field()
76+
val fish = rand / 20 + 1
10277
remainingFish -= fish
10378
Field(fish)
10479
}
80+
}.let {
81+
it + it.asReversed().map { list ->
82+
MutableList(Constants.BOARD_SIZE) { index -> list[Constants.BOARD_SIZE - index - 1].clone() }
83+
}
10584
}
10685
}
10786

plugin/src/test/kotlin/sc/plugin2023/BoardTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import sc.helpers.testXStream
1515
import sc.plugin2023.util.PluginConstants
1616

1717
class BoardTest: FunSpec({
18-
context("Board generation") {
18+
context("Board") {
1919
val generatedBoard = Board()
20-
test("works properly") {
20+
test("generates properly") {
2121
generatedBoard shouldHaveSize PluginConstants.BOARD_SIZE * PluginConstants.BOARD_SIZE
2222
generatedBoard.forAll {
2323
it.penguin.shouldBeNull()
24-
it.fish shouldBeInRange 1..4
24+
it.fish shouldBeInRange 0..4
2525
}
2626

2727
generatedBoard.getPenguins() shouldHaveSize 0
@@ -34,7 +34,7 @@ class BoardTest: FunSpec({
3434
}
3535
(0 until PluginConstants.BOARD_SIZE).map { (it * 2) y 2 }.forAll {
3636
val field = generatedBoard[it]
37-
field.fish shouldBeInRange 1..4
37+
field.fish shouldBeInRange 0..4
3838
generatedBoard[it.x, it.y] shouldBe field
3939
}
4040
}
@@ -48,7 +48,7 @@ class BoardTest: FunSpec({
4848
clone shouldBe makeBoard(0 y 0 to 1)
4949
}
5050
}
51-
context("Board generates Moves") {
51+
context("Board calculates Moves") {
5252
val board = makeBoard(0 y 0 to 0)
5353
test("many possible moves") {
5454
// right, right down

0 commit comments

Comments
 (0)