Skip to content

Commit de63902

Browse files
committed
Merge branch 'master' of github.com:CAU-Kiel-Tech-Inf/socha
2 parents 07689b6 + d0a78f0 commit de63902

File tree

8 files changed

+84
-81
lines changed

8 files changed

+84
-81
lines changed

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

+37-32
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,56 @@
77
import sc.shared.PlayerColor;
88

99
import java.util.ArrayList;
10+
import java.util.Arrays;
1011
import java.util.List;
1112
import java.util.stream.Collectors;
1213

1314
import static sc.plugin2019.FieldState.OBSTRUCTED;
1415

15-
/**
16-
* Spielbrett für Piranhas mit 10x10 Feldern.
17-
*/
16+
/** Spielbrett für Piranhas mit {@link Constants#BOARD_SIZE}² Feldern. */
1817
@XStreamAlias(value = "board")
1918
public class Board implements IBoard {
2019

2120
@XStreamImplicit(itemFieldName = "fields")
2221
private Field[][] fields;
2322

2423
public Board() {
25-
this.fields = new Field[Constants.BOARD_SIZE][Constants.BOARD_SIZE];
26-
initialize();
24+
this.fields = randomFields();
25+
}
26+
27+
public Board(Board boardToClone) {
28+
this.fields = emptyFields();
29+
for(int x = 0; x < Constants.BOARD_SIZE; x++) {
30+
for(int y = 0; y < Constants.BOARD_SIZE; y++) {
31+
fields[x][y] = boardToClone.fields[x][y].clone();
32+
}
33+
}
34+
}
35+
36+
@Override
37+
public Board clone() {
38+
return new Board(this);
39+
}
40+
41+
@Override
42+
public boolean equals(Object obj) {
43+
return obj instanceof Board && Arrays.equals(((Board) obj).fields, this.fields);
44+
}
45+
46+
private static Field[][] emptyFields() {
47+
return new Field[Constants.BOARD_SIZE][Constants.BOARD_SIZE];
2748
}
2849

2950
/** Erstellt eine zufälliges Spielbrett. */
30-
private void initialize() {
31-
for (int x = 0; x < Constants.BOARD_SIZE; x++) {
32-
for (int y = 0; y < Constants.BOARD_SIZE; y++) {
51+
private static Field[][] randomFields() {
52+
Field[][] fields = emptyFields();
53+
for(int x = 0; x < Constants.BOARD_SIZE; x++) {
54+
for(int y = 0; y < Constants.BOARD_SIZE; y++) {
3355
fields[x][y] = new Field(x, y);
3456
}
3557
}
3658
// place piranhas
37-
for (int index = 1; index < Constants.BOARD_SIZE - 1; index++) {
59+
for(int index = 1; index < Constants.BOARD_SIZE - 1; index++) {
3860
fields[0][index].setPiranha(PlayerColor.RED);
3961
fields[Constants.BOARD_SIZE - 1][index].setPiranha(PlayerColor.RED);
4062
fields[index][0].setPiranha(PlayerColor.BLUE);
@@ -43,14 +65,12 @@ private void initialize() {
4365
// place obstacles
4466
// create a list of coordinates for fields which may be blocked
4567
List<Field> blockableFields = new ArrayList<>();
46-
for (int x = Constants.OBSTACLES_START; x < Constants.OBSTACLES_END; x++) {
47-
for (int y = Constants.OBSTACLES_START; y < Constants.OBSTACLES_END; y++) {
48-
blockableFields.add(this.getField(x, y));
49-
}
68+
for(int x = Constants.OBSTACLES_START; x < Constants.OBSTACLES_END; x++) {
69+
blockableFields.addAll(Arrays.asList(fields[x]).subList(Constants.OBSTACLES_START, Constants.OBSTACLES_END));
5070
}
5171
// set fields with randomly selected coordinates to blocked
5272
// coordinates may not lay on same horizontal, vertical or diagonal lines with other selected coordinates
53-
for (int i = 0; i < Constants.NUM_OBSTACLES; i++) {
73+
for(int i = 0; i < Constants.NUM_OBSTACLES; i++) {
5474
int indexOfFieldToBlock = (int) Math.floor(Math.random() * blockableFields.size());
5575
Field selectedField = blockableFields.get(indexOfFieldToBlock);
5676
selectedField.setState(OBSTRUCTED);
@@ -61,29 +81,14 @@ private void initialize() {
6181
field.getX() + field.getY() == selectedField.getX() + selectedField.getY()))
6282
).collect(Collectors.toList());
6383
}
64-
}
65-
66-
/**
67-
* erzeugt eine Deepcopy dieses Objekts
68-
*
69-
* @return ein neues Objekt mit gleichen Eigenschaften
70-
*/
71-
@Override
72-
public Board clone() {
73-
Board clone = new Board();
74-
for (int x = 0; x < Constants.BOARD_SIZE; x++) {
75-
for (int y = 0; y < Constants.BOARD_SIZE; y++) {
76-
clone.fields[x][y] = fields[x][y].clone();
77-
}
78-
}
79-
return clone;
84+
return fields;
8085
}
8186

8287
@Override
8388
public String toString() {
8489
StringBuilder b = new StringBuilder("Board {");
85-
for (int x = 0; x < Constants.BOARD_SIZE; x++) {
86-
for (int y = 0; y < Constants.BOARD_SIZE; y++) {
90+
for(int x = 0; x < Constants.BOARD_SIZE; x++) {
91+
for(int y = 0; y < Constants.BOARD_SIZE; y++) {
8792
b.append(fields[x][y].getPiranha());
8893
}
8994
}

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

+28-33
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import static sc.plugin2019.FieldState.*;
1111

1212
/**
13-
* Ein Feld des Spielfelds. Ein Spielfeld ist durch den index eindeutig identifiziert.
14-
* Das type Attribut gibt an, um welchen Feldtyp es sich handelt.
13+
* Ein Feld des Spielfelds. Ein Spielfeld hat eine x- und y-Koordinate und einen {@link FieldState}.
1514
*/
1615
@XStreamAlias(value = "field")
1716
public class Field implements IField {
@@ -25,37 +24,44 @@ public class Field implements IField {
2524
@XStreamAsAttribute
2625
private FieldState state;
2726

28-
public Field(int x, int y) {
27+
public Field(int x, int y, FieldState state) {
2928
this.x = x;
3029
this.y = y;
31-
this.state = EMPTY;
30+
this.state = state;
3231
}
3332

34-
public Field(int x, int y, FieldState state) {
35-
this(x, y);
36-
this.state = state;
33+
public Field(int x, int y) {
34+
this(x, y, EMPTY);
3735
}
3836

3937
public Field(int x, int y, PlayerColor piranha) {
40-
this(x, y);
41-
if (piranha == PlayerColor.RED)
42-
this.state = RED;
43-
else
44-
this.state = BLUE;
38+
this(x, y, FieldState.from(piranha));
4539
}
4640

4741
public Field(int x, int y, boolean isObstructed) {
48-
this(x, y);
49-
if (isObstructed) {
50-
this.state = OBSTRUCTED;
51-
} else {
52-
this.state = EMPTY;
53-
}
42+
this(x, y, isObstructed ? OBSTRUCTED : EMPTY);
43+
}
44+
45+
public Field(Field fieldToClone) {
46+
this(fieldToClone.x, fieldToClone.y, fieldToClone.state);
5447
}
5548

5649
@Override
5750
public Field clone() {
58-
return new Field(this.x, this.y, this.state);
51+
return new Field(this);
52+
}
53+
54+
@Override
55+
public boolean equals(Object obj) {
56+
if(!(obj instanceof Field))
57+
return false;
58+
Field field = (Field) obj;
59+
return x == field.x && y == field.y && state == field.state;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return String.format("Field(%d|%d){%s}", x, y, state);
5965
}
6066

6167
public int getX() {
@@ -75,9 +81,9 @@ void setY(int y) {
7581
}
7682

7783
public Optional<PlayerColor> getPiranha() {
78-
if (state == RED)
84+
if(state == RED)
7985
return Optional.of(PlayerColor.RED);
80-
else if (state == BLUE)
86+
else if(state == BLUE)
8187
return Optional.of(PlayerColor.BLUE);
8288

8389
return Optional.empty();
@@ -89,13 +95,7 @@ else if (state == BLUE)
8995
* @param piranha Farbe des Piranhas
9096
*/
9197
public void setPiranha(PlayerColor piranha) {
92-
if (piranha == PlayerColor.RED) {
93-
state = RED;
94-
} else if (piranha == PlayerColor.BLUE) {
95-
state = BLUE;
96-
} else {
97-
throw new IllegalStateException("The given PlayerColor does not exist");
98-
}
98+
state = FieldState.from(piranha);
9999
}
100100

101101
public boolean isObstructed() {
@@ -106,11 +106,6 @@ public FieldState getState() {
106106
return state;
107107
}
108108

109-
@Override
110-
public String toString() {
111-
return String.format("Field(%d|%d){%s}", x, y, state);
112-
}
113-
114109
public void setState(FieldState state) {
115110
this.state = state;
116111
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package sc.plugin2019;
22

3+
import sc.shared.PlayerColor;
4+
35
public enum FieldState {
46
RED,
57
BLUE,
68
OBSTRUCTED,
7-
EMPTY
9+
EMPTY;
10+
public static FieldState from(PlayerColor color) {
11+
if(color == PlayerColor.RED)
12+
return RED;
13+
else
14+
return BLUE;
15+
}
816
}

plugin/src/shared/sc/plugin2019/GameState.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ class GameState(
2020
startPlayerColor = state.startPlayerColor
2121
}
2222

23-
override fun clone() = GameState(this)
23+
public override fun clone() = GameState(this)
2424

2525
@XStreamAsAttribute
2626
override var turn = 0
2727
set(value) {
2828
val turnLimit = Constants.ROUND_LIMIT * 2
29-
if (value > turnLimit) throw InvalidGameStateException("Turn $value exceeded maxTurn $turnLimit")
29+
if(value > turnLimit) throw InvalidGameStateException("Turn $value exceeds turnLimit($turnLimit)")
3030
field = value
3131
}
3232

3333
fun getField(x: Int, y: Int) = board.getField(x, y)
3434

3535
/** wechselt den Spieler, der aktuell an der Reihe ist, anhand der Zugzahl [turn] */
3636
fun switchCurrentPlayer() {
37-
currentPlayerColor = if (turn % 2 == 0) PlayerColor.RED else PlayerColor.BLUE
37+
currentPlayerColor = if(turn % 2 == 0) PlayerColor.RED else PlayerColor.BLUE
3838
}
3939

4040
override fun getPointsForPlayer(playerColor: PlayerColor) = GameRuleLogic.greatestSwarmSize(board, playerColor)
@@ -49,7 +49,7 @@ class GameState(
4949
*
5050
*/
5151
fun getPlayerStats(playerColor: PlayerColor): IntArray =
52-
getGameStats()[if (playerColor == PlayerColor.RED) Constants.GAME_STATS_RED_INDEX else Constants.GAME_STATS_BLUE_INDEX]
52+
getGameStats()[if(playerColor == PlayerColor.RED) Constants.GAME_STATS_RED_INDEX else Constants.GAME_STATS_BLUE_INDEX]
5353

5454
/**
5555
* Liefert Statusinformationen zum Spiel. Diese sind ein Array der
@@ -74,9 +74,9 @@ class GameState(
7474
* Spielclient i.A. nicht aufgerufen werden!
7575
*/
7676
fun addPlayer(player: Player) {
77-
if (player.color == PlayerColor.RED) {
77+
if(player.color == PlayerColor.RED) {
7878
red = player
79-
} else if (player.color == PlayerColor.BLUE) {
79+
} else if(player.color == PlayerColor.BLUE) {
8080
blue = player
8181
}
8282
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class Constants {
55
public static final int ROUND_LIMIT = 30;
66
public static final String WINNING_MESSAGE = "Das Spiel ist beendet.\nEin Spieler hat seinen Schwarm vereint.";
77
public static final String ROUND_LIMIT_MESSAGE = "Das Rundenlimit wurde erreicht.";
8+
89
// for gamestats array
910
public static final int GAME_STATS_SWARM_SIZE = 0;
1011
public static final int GAME_STATS_RED_INDEX = 0;

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ public static boolean isValidToMove(GameState state, int x, int y, Direction dir
6464

6565
List<Field> fieldsInDirection = getFieldsInDirection(board, x, y, direction);
6666

67-
FieldState opponentFieldColor;
68-
if (state.getCurrentPlayerColor() == PlayerColor.RED) {
69-
opponentFieldColor = FieldState.BLUE;
70-
} else {
71-
opponentFieldColor = FieldState.RED;
72-
}
67+
FieldState opponentFieldColor = FieldState.from(state.getCurrentPlayerColor().opponent());
7368

7469
for (Field f : fieldsInDirection) {
7570
if (f.getState() == opponentFieldColor) {
@@ -139,7 +134,6 @@ private static Set<Field> getSwarm(Board board, Set<Field> found, Set<Field> swa
139134
if (swarm.size() != tmpSwarm.size())
140135
tmpSwarm = getSwarm(board, found, tmpSwarm);
141136

142-
143137
swarm.addAll(tmpSwarm);
144138

145139
found.removeAll(swarm);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package sc.api.plugins;
22

3-
public interface IBoard {
3+
public interface IBoard extends Cloneable {
44
IField getField(int x, int y);
55
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package sc.api.plugins;
22

3-
public interface IField {
3+
public interface IField extends Cloneable {
44
}

0 commit comments

Comments
 (0)