-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
129 lines (99 loc) · 5.01 KB
/
Copy pathBoard.java
File metadata and controls
129 lines (99 loc) · 5.01 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
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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Board {
//default board is blank
public static void initializeWithPredefinedBoard(char[][] board, int size) {
size = 9;
GoGame.board = new char[][] {
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', 'X', 'O', '.', '.', '.', '.'},
{'.', '.', '.', 'O', 'X', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'}
};
}
public static void initializeManually(Scanner scanner, char[][] board, int size) {
System.out.println("Enter the initial board state (use '.' for empty spaces, 'X' for White, 'O' for Black):");
for (int i = 0; i < GoGame.size; i++) {
String rowInput = scanner.next();
for (int j = 0; j < GoGame.size; j++) {
GoGame.board[i][j] = rowInput.charAt(j);
}
}
}
public static void initializeFromFile(char[][] board, int size) {
try {
File file = new File("gopatterns.txt");
Scanner scanner = new Scanner(file);
List<String[]> patterns = new ArrayList<>(); // A list to store all patterns.
List<String> currentPattern = new ArrayList<>();
List<String> patternNames = new ArrayList<>(); // A list to store pattern names.
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// Assuming each pattern starts with 'P', let's split the patterns based on this:
if (line.length() > 0 && line.charAt(0) == 'P') {
patternNames.add(line); // Store the pattern name.
if(!currentPattern.isEmpty()) {
patterns.add(currentPattern.toArray(new String[0]));
currentPattern.clear();
}
} else if (!line.isEmpty()) {
currentPattern.add(line);
}
}
if(!currentPattern.isEmpty()) {
patterns.add(currentPattern.toArray(new String[0]));
}
scanner.close();
// Now, let's randomly choose one pattern:
Random rand = new Random();
int randomPatternIndex = rand.nextInt(patterns.size());
String[] chosenPattern = patterns.get(randomPatternIndex);
System.out.println(patternNames.get(randomPatternIndex)); // Print only the chosen pattern's name
for (int i = 0; i < size && i < chosenPattern.length; i++) {
String rowInput = chosenPattern[i];
for (int j = 0; j < size && j < rowInput.length(); j++) {
GoGame.board[i][j] = rowInput.charAt(j);
}
}
} catch (FileNotFoundException e) {
System.out.println("Failed to load the file. Initializing with a predefined board instead.");
initializeWithPredefinedBoard(board, size);
}
}}
/* {'.', '.', '.', 'X', 'O', '.', '.', '.', '.'},
{'X', 'X', 'X', 'X', 'O', '.', '.', '.', '.'},
{'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'}
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', 'X', 'O', '.', '.', '.', '.'},
{'.', '.', '.', 'O', 'X', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'}
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', 'X', 'O', '.', '.', '.', '.', '.', '.'},
{'.', 'X', 'O', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.'}
*/