7
7
import sc .shared .PlayerColor ;
8
8
9
9
import java .util .ArrayList ;
10
+ import java .util .Arrays ;
10
11
import java .util .List ;
11
12
import java .util .stream .Collectors ;
12
13
13
14
import static sc .plugin2019 .FieldState .OBSTRUCTED ;
14
15
15
- /**
16
- * Spielbrett für Piranhas mit 10x10 Feldern.
17
- */
16
+ /** Spielbrett für Piranhas mit {@link Constants#BOARD_SIZE}² Feldern. */
18
17
@ XStreamAlias (value = "board" )
19
18
public class Board implements IBoard {
20
19
21
20
@ XStreamImplicit (itemFieldName = "fields" )
22
21
private Field [][] fields ;
23
22
24
23
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 ];
27
48
}
28
49
29
50
/** 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 ++) {
33
55
fields [x ][y ] = new Field (x , y );
34
56
}
35
57
}
36
58
// place piranhas
37
- for (int index = 1 ; index < Constants .BOARD_SIZE - 1 ; index ++) {
59
+ for (int index = 1 ; index < Constants .BOARD_SIZE - 1 ; index ++) {
38
60
fields [0 ][index ].setPiranha (PlayerColor .RED );
39
61
fields [Constants .BOARD_SIZE - 1 ][index ].setPiranha (PlayerColor .RED );
40
62
fields [index ][0 ].setPiranha (PlayerColor .BLUE );
@@ -43,14 +65,12 @@ private void initialize() {
43
65
// place obstacles
44
66
// create a list of coordinates for fields which may be blocked
45
67
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 ));
50
70
}
51
71
// set fields with randomly selected coordinates to blocked
52
72
// 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 ++) {
54
74
int indexOfFieldToBlock = (int ) Math .floor (Math .random () * blockableFields .size ());
55
75
Field selectedField = blockableFields .get (indexOfFieldToBlock );
56
76
selectedField .setState (OBSTRUCTED );
@@ -61,29 +81,14 @@ private void initialize() {
61
81
field .getX () + field .getY () == selectedField .getX () + selectedField .getY ()))
62
82
).collect (Collectors .toList ());
63
83
}
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 ;
80
85
}
81
86
82
87
@ Override
83
88
public String toString () {
84
89
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 ++) {
87
92
b .append (fields [x ][y ].getPiranha ());
88
93
}
89
94
}
0 commit comments