-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver.java
66 lines (63 loc) · 1.83 KB
/
Driver.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
import java.awt.Color;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Driver {
public static void main (String [ ] args) {
Board myBoard = new Board ( );
BufferedReader playerMoves = new BufferedReader (new InputStreamReader (System.in));
Display myDisplay = new Display (myBoard);
while (true) {
// It's the player's turn to move, playing RED.
// There has to be an uncolored connector, since it's the user's turn
// and there are 15 connectors.
System.out.print ("Your move? ");
String s = "";
try {
s = playerMoves.readLine ( );
} catch (Exception exc) {
//This shouldn't happen.
exc.printStackTrace ( );
System.exit (2);
}
Connector cnctr;
try {
cnctr = Connector.toConnector (s);
} catch (IllegalFormatException exc) {
System.out.println (exc.getMessage ( ));
continue;
}
if (myBoard.colorOf (cnctr) == Color.RED || myBoard.colorOf (cnctr) == Color.BLUE) {
System.out.println ("ERROR: " + cnctr + " has already been colored.");
continue;
}
if (myBoard.formsTriangle (cnctr, Color.RED)) {
System.out.println ("You lose!");
try {
Thread.sleep (4000);
} catch (InterruptedException e1) {
}
System.exit (1);
}
myBoard.add (cnctr, Color.RED);
assert myBoard.isOK ( ) : "NOT OK";
// Now it's the computer's turn.
Connector computerMove = myBoard.choice ( );
System.out.println ("Computer takes " + computerMove);
if (myBoard.formsTriangle (computerMove, Color.BLUE)) {
System.out.println ("You win!");
try {
Thread.sleep (4000);
} catch (InterruptedException e1) {
}
System.exit (0);
}
myBoard.add (computerMove, Color.BLUE);
assert myBoard.isOK ( );
myDisplay.repaint ( );
try {
Thread.sleep (1000);
} catch (InterruptedException e1) {
}
}
}
}