10
10
public class Grader {
11
11
private static final int ROWS = 6 ;
12
12
private static final int COLS = 7 ;
13
-
13
+
14
+ static int delay = 500 ;
15
+
14
16
private static Board board ;
15
17
private static GamePanel gamePanel = new GamePanel (ROWS , COLS );
16
18
private static HomePanel homePanel = new HomePanel (gamePanel , false );
@@ -19,46 +21,68 @@ public class Grader {
19
21
static final String NAME1 = "Template" ;
20
22
static final String NAME2 = "Template" ;
21
23
22
- public static void main (String [] args ) throws Exception
23
- {
24
+ public static void main (String [] args ) throws Exception {
24
25
Class <?> bot1 = Class .forName ("connect4.contestants." + NAME1 );
25
26
Class <?> bot2 = Class .forName ("connect4.contestants." + NAME2 );
26
27
27
- Object a = bot1 .getConstructor ().newInstance ();
28
- Object b = bot2 .getConstructor ().newInstance ();
28
+ Player a = ( Player ) bot1 .getConstructor ().newInstance ();
29
+ Player b = ( Player ) bot2 .getConstructor ().newInstance ();
29
30
30
31
board = new Board (ROWS , COLS );
31
32
gamePanel .drawGrid (board );
32
33
33
34
Thread .sleep (1000 );
34
35
35
- playGame ((Player ) a , (Player ) b );
36
+ playGame (a , b );
37
+ delay = 10 ;
38
+
39
+ for (int i = 0 ; i < 100 - 1 ; i ++) {
40
+ board = new Board (ROWS , COLS );
41
+ gamePanel .drawGrid (board );
42
+ playGame (a , b );
43
+ }
44
+
45
+ String name1 = a .getPlayerName ();
46
+ String name2 = b .getPlayerName ();
47
+
48
+ if (name1 .equals (name2 )) name2 += "2" ;
49
+
50
+ debugger .println ();
51
+ debugger .println ();
52
+ debugger .println ();
53
+ debugger .println (name1 + " won " + p1Wins + " times" );
54
+ debugger .println (name2 + " won " + p2Wins + " times" );
55
+ debugger .println ("There were " + draws + " draws" );
56
+
57
+ debugger .close ();
36
58
}
37
-
38
- public static void playGame (Player player1 , Player player2 ) throws InterruptedException
39
- {
59
+
60
+ static int p1Wins = 0 ;
61
+ static int p2Wins = 0 ;
62
+ static int draws = 0 ;
63
+
64
+ public static void playGame (Player player1 , Player player2 ) throws InterruptedException {
40
65
String name1 = player1 .getPlayerName ();
41
66
String name2 = player2 .getPlayerName ();
42
67
43
- if (name1 .equals (name2 )) name2 += "2" ;
68
+ if (name1 .equals (name2 )) name2 += "2" ;
44
69
45
70
int winningPlayer = 0 ;
46
- while (winningPlayer == 0 )
47
- {
48
- Thread .sleep (500 );
49
- int move = getMove (player1 );
71
+ while (winningPlayer == 0 ) {
72
+ Thread .sleep (delay );
73
+ int move = getMove (player1 );
50
74
debugger .println (name1 + " dropped in " + move );
51
-
52
- if (!board .makeMove (move )){
75
+
76
+ if (!board .makeMove (move )) {
53
77
System .err .println (name1 + " made an illegal move" );
54
78
return ;
55
79
}
56
80
gamePanel .drawGrid (board );
57
81
winningPlayer = board .getWinningPlayer ();
58
82
if (winningPlayer == 0 ) {
59
- Thread .sleep (500 );
60
- move = getMove (player2 );
61
- if (!board .makeMove (move )){
83
+ Thread .sleep (delay );
84
+ move = getMove (player2 );
85
+ if (!board .makeMove (move )) {
62
86
System .err .println (name2 + " made an illegal move" );
63
87
return ;
64
88
}
@@ -68,36 +92,38 @@ public static void playGame(Player player1, Player player2) throws InterruptedEx
68
92
}
69
93
}
70
94
71
- if (winningPlayer == -1 )
72
- {
95
+ if (winningPlayer == -1 ) {
73
96
System .out .println ("Game over. It was a draw." );
74
97
debugger .println ("Game over. It was a draw." );
98
+
99
+ draws ++;
75
100
return ;
76
101
}
77
- String winningPlayerName =
78
- (winningPlayer == 1 ) ? name1 : name2 ;
102
+ String winningPlayerName =
103
+ (winningPlayer == 1 ) ? name1 : name2 ;
104
+
105
+ if (winningPlayer == 1 ) p1Wins ++;
106
+ else p2Wins ++;
79
107
80
108
debugger .println ("Game over. Player " + winningPlayer + ": " + winningPlayerName + " won." );
81
- debugger .close ();
82
109
}
83
-
110
+
84
111
public static int getMove (Player player ) throws InterruptedException {
85
- int move ;
86
- if (player instanceof HumanPlayer ) {
87
- move = player .getMoveColumn (board );
88
- while (move == -1 ) {
89
- move = player .getMoveColumn (board );
90
- Thread .sleep (20 );
91
- }
112
+ int move ;
113
+ if (player instanceof HumanPlayer ) {
114
+ move = player .getMoveColumn (board );
115
+ while (move == -1 ) {
116
+ move = player .getMoveColumn (board );
117
+ Thread .sleep (20 );
118
+ }
92
119
((HumanPlayer ) player ).setCol (-1 );
93
- } else {
94
- move = player .getMoveColumn (board );
95
- }
96
- return move ;
120
+ } else {
121
+ move = player .getMoveColumn (board );
122
+ }
123
+ return move ;
97
124
}
98
125
99
- public static GamePanel getGamePanel ()
100
- {
126
+ public static GamePanel getGamePanel () {
101
127
return gamePanel ;
102
128
}
103
129
}
0 commit comments