-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardLoader.java
More file actions
82 lines (74 loc) · 1.85 KB
/
Copy pathBoardLoader.java
File metadata and controls
82 lines (74 loc) · 1.85 KB
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.StringTokenizer;
public class BoardLoader
{
private int[][] board;
public BoardLoader()
{
this("boards");
}
public BoardLoader(String boardDirectory)
{
File dir = new File(boardDirectory);
File[] files = dir.listFiles();
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
StringTokenizer st;
int randomFile = (int)(files.length * Math.random());
int row = 0;
int col = 0;
board = new int[6][7];
try {
reader = new BufferedReader(new FileReader(files[randomFile]));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
st = new StringTokenizer(text, ", ");
while (st.hasMoreTokens())
{
board[row][col] = Integer.parseInt(st.nextToken());
col++;
}
col = 0;
row++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public int[][] getBoard()
{
return board;
}
public void printBoard()
{
for (int r = 0; r < board.length; r++)
{
for (int c = 0; c < board[0].length; c++)
{
System.out.print(board[r][c] + " ");
}
System.out.println();
}
}
/*public static void main(String args[])
{
BoardLoader test = new BoardLoader();
test.printBoard();
}*/
}