-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicBoard.java
149 lines (136 loc) · 5.32 KB
/
BasicBoard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package model.board;
import model.exceptions.CorruptedFileException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BasicBoard implements BoardInterface {
/***
* Model of a board.
* Uses -1 for fields not available for players.
* Uses 0 for fields without an owner.
* Uses 1 to 6 for fields of particular players.
*/
private int[][] boardFields;
/***
* Positions of pieces.
* Uses 0 for nothing.
* Uses 1 to 6 for position of particular player's pieces.
*/
private int[][] positions;
/***
* Loads board from a file.
* @param file Should be a text file, using spaces and new lines to form an array-like structure.
* Should consist of 0 - 6 and "n" (for -1), see {@link #boardFields}.
* @throws CorruptedFileException When files does not conform to given expectations, an exception is thrown.
*/
public void loadBoard(File file) throws CorruptedFileException {
try {
Scanner ss = new Scanner(file);
int lineCounter = 0;
while (ss.hasNextLine()) {
lineCounter++;
ss.nextLine();
}
ss.close();
this.boardFields = new int[lineCounter][];
this.positions = new int[lineCounter][];
lineCounter = 0;
Scanner s = new Scanner(file);
while (s.hasNextLine()) {
String[] line = s.nextLine().split(" ");
this.boardFields[lineCounter] = new int[line.length];
this.positions[lineCounter] = new int[line.length];
for (int i = 0; i < line.length; i++) {
if (line[i].equals("n")) { //n is for null
boardFields[lineCounter][i] = -1;
positions[lineCounter][i] = -1;
continue;
}
try {
int field = Integer.parseInt(line[i]);
if (field > 6) {
throw new CorruptedFileException("This game supports up to six players only");
}
if (field < 0) {
throw new CorruptedFileException("Values should start at 0");
}
this.boardFields[lineCounter][i] = field;
this.positions[lineCounter][i] = field;
} catch (NumberFormatException e) {
throw new CorruptedFileException("Values should be an integer");
}
}
lineCounter++;
}
} catch (FileNotFoundException e) {
throw new CorruptedFileException("File could not be found");
}
}
/***
* Loads the board from an array.
* @param boardArray Array to be loaded from, similar to file in {@link BasicBoard#loadBoard(File)}.
* @throws CorruptedFileException If the array isn't a board array.
*/
public void loadBoard(String[][] boardArray) throws CorruptedFileException {
boardFields = new int[boardArray.length][];
positions = new int[boardArray.length][];
for (int i = 0; i < boardArray.length; i++) {
boardFields[i] = new int[boardArray[i].length];
positions[i] = new int[boardArray[i].length];
for (int j = 0; j < boardArray[i].length; j++) {
if (boardArray[i][j].equals("n") || boardArray[i][j].equals("-1")) {
boardFields[i][j] = -1;
positions[i][j] = -1;
continue;
}
try {
int field = Integer.parseInt(boardArray[i][j]);
if (field > 6) {
throw new CorruptedFileException("This game supports up to six players only");
}
if (field < 0) {
throw new CorruptedFileException("Values should start at 0");
}
boardFields[i][j] = field;
positions[i][j] = field;
} catch (NumberFormatException e) {
throw new CorruptedFileException("Values should be integers");
}
}
}
}
/***
* Getter for positions table.
* @return Table of positions, see {@link #positions}.
*/
public int[][] getPositions() {
return this.positions;
}
/***
* Sets something in board's {@link #positions} table.
* @param row Row of a change.
* @param col Column of a change.
* @param val Value to be set.
*/
public void setPositions(int row, int col, int val) {
this.positions[row][col] = val;
}
/***
* Checks if a particular field is not null.
* @param row Row to check.
* @param col Column to check.
* @return True, if isn't null. False if is.
*/
public boolean fieldNotNull(int row, int col) {
int h = boardFields.length;
int j = boardFields[0].length;
return (j > col && h > row && row >= 0 && col >= 0 && boardFields[row][col] != -1);
}
/***
* Gets board fields.
* @return Board fields array.
*/
public int[][] getBoardFields() {
return this.boardFields;
}
}