1010public class Grader {
1111 private static final int ROWS = 6 ;
1212 private static final int COLS = 7 ;
13-
13+
14+ static int delay = 500 ;
15+
1416 private static Board board ;
1517 private static GamePanel gamePanel = new GamePanel (ROWS , COLS );
1618 private static HomePanel homePanel = new HomePanel (gamePanel , false );
@@ -19,46 +21,68 @@ public class Grader {
1921 static final String NAME1 = "Template" ;
2022 static final String NAME2 = "Template" ;
2123
22- public static void main (String [] args ) throws Exception
23- {
24+ public static void main (String [] args ) throws Exception {
2425 Class <?> bot1 = Class .forName ("connect4.contestants." + NAME1 );
2526 Class <?> bot2 = Class .forName ("connect4.contestants." + NAME2 );
2627
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 ();
2930
3031 board = new Board (ROWS , COLS );
3132 gamePanel .drawGrid (board );
3233
3334 Thread .sleep (1000 );
3435
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 ();
3658 }
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 {
4065 String name1 = player1 .getPlayerName ();
4166 String name2 = player2 .getPlayerName ();
4267
43- if (name1 .equals (name2 )) name2 += "2" ;
68+ if (name1 .equals (name2 )) name2 += "2" ;
4469
4570 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 );
5074 debugger .println (name1 + " dropped in " + move );
51-
52- if (!board .makeMove (move )){
75+
76+ if (!board .makeMove (move )) {
5377 System .err .println (name1 + " made an illegal move" );
5478 return ;
5579 }
5680 gamePanel .drawGrid (board );
5781 winningPlayer = board .getWinningPlayer ();
5882 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 )) {
6286 System .err .println (name2 + " made an illegal move" );
6387 return ;
6488 }
@@ -68,36 +92,38 @@ public static void playGame(Player player1, Player player2) throws InterruptedEx
6892 }
6993 }
7094
71- if (winningPlayer == -1 )
72- {
95+ if (winningPlayer == -1 ) {
7396 System .out .println ("Game over. It was a draw." );
7497 debugger .println ("Game over. It was a draw." );
98+
99+ draws ++;
75100 return ;
76101 }
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 ++;
79107
80108 debugger .println ("Game over. Player " + winningPlayer + ": " + winningPlayerName + " won." );
81- debugger .close ();
82109 }
83-
110+
84111 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+ }
92119 ((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 ;
97124 }
98125
99- public static GamePanel getGamePanel ()
100- {
126+ public static GamePanel getGamePanel () {
101127 return gamePanel ;
102128 }
103129}
0 commit comments