-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
213 lines (159 loc) · 4.82 KB
/
Game.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import java.util.Scanner;
import java.io.*;
public class Game {
private Player first, second, current;
public Scanner scanner = new Scanner(System.in);
private Board board = new Board();
public Game () {
first = null;
second = null;
current = null;
}
//for new games
public Game(Player p1, Player p2) {
first = p1;
second = p2;
current = p1;
}
//for loaded games
public Game(Player p1, Player p2, Player cur) {
first = p1;
second = p2;
current = cur;
}
//asks user to choose starting posiiton, creates new board then calls play method
public void start() {
int start_position;
System.out.println("Awesome! Now choose the starting positions: \n\n Standard: \n\t1. 4-4 middle \n Non-Standard: \n\t2. Upper Left \n\t"
+ "3. Upper Right \n\t4. Lower Left \n\t5. Lower Right");
do
{
while (!scanner.hasNextInt()) {
System.out.println("Error: Must enter a valid integer.");
scanner.nextLine();
}
start_position = Integer.parseInt(scanner.nextLine());
System.out.println("");
if (start_position > 5 || start_position < 1)
System.out.println("Error: Please enter a number between 1-5!");
} while (start_position > 5 || start_position < 1);
board.createNewBoard(start_position);
play();
}
//loops until game is over calling take turn or save method, removes markers too
public void play() {
boolean isGameOver = false;
while (!isGameOver)
{
if (board.hasForfeited)
break;
board.takeTurn(current);
if (board.hasSaved) {
if (current.getName().equals(first.getName()))
current = first;
else
current = second;
save();
break;
}
if (current.getName().equals(first.getName()))
current = second;
else
current = first;
isGameOver = board.checkIfGameOver();
board.removeMarkers();
}
if (isGameOver) {
board.drawBoard();
System.out.println(current.getName() + " wins with " + board.countPieces(current) + " pieces!");
System.out.println("");
}
}
//opens file and updates board and creates new players, returns board
public static Board load() {
String filename, tempp1, tempp2, currentp, positions;
Game loadedOrthello;
boolean doesFileExist = false;
Scanner scanner1 = new Scanner(System.in);
System.out.println("Please enter the name of which game you would like to load: ");
filename = scanner1.nextLine();
//check if it is a legit string
try {
File file = new File(filename);
if (!file.exists()) {
doesFileExist = false;
while(!doesFileExist) {
System.out.println("File does not exist. Please enter a filename that exists: ");
filename = scanner1.nextLine();
file = new File(filename);
if (file.exists())
doesFileExist = true;
}
}
if (file.exists() || doesFileExist) {
Scanner scan2 = new Scanner(new File(filename));
if (scan2.hasNextLine()) {
tempp1 = scan2.nextLine();
tempp2 = scan2.nextLine();
currentp = scan2.nextLine();
positions = scan2.nextLine();
Player p1 = new Player(tempp1);
p1.setColor(Position.getBlack());
Player p2 = new Player(tempp2);
p2.setColor(Position.getWhite());
Player cur = new Player(currentp);
if (cur.getName().equals(p1.getName()))
cur.setColor(p1.getColor());
else
cur.setColor(p2.getColor());
loadedOrthello = new Game(p1, p2, cur);
loadedOrthello.board = new Board(positions);
loadedOrthello.play();
return loadedOrthello.board;
}
}
}
catch (IOException e)
{
System.out.println("An error occurred.");
e.printStackTrace();
}
catch (IllegalStateException e) {
System.out.println("Problem trying to read file");
}
scanner1.close();
///filler for above block
Board filler = new Board();
return filler;
}
//saves the game by saving names and positions into a file
private void save() {
String filename, thePositions = "";
System.out.println("To save the current game, enter a filename below:");
filename = scanner.nextLine();
try {
File file = new File(filename);
if(file.exists() && !file.isDirectory())
System.out.println("File already exists.");
else {
file.createNewFile();
System.out.println("");
System.out.println("File created.");
PrintWriter pw = new PrintWriter(filename);
pw.write(first.getName() + "\n" + second.getName() + "\n" + current.getName() + "\n");
thePositions = board.getPositions();
pw.write(thePositions);
pw.close();
}
}
catch (IOException e)
{
System.out.println("An error occurred.");
e.printStackTrace();
}
catch (IllegalStateException e) {
System.out.println("Problem trying to read file");
}
System.out.println("");
}
}