Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems

Similar presentations


Presentation on theme: "Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems"— Presentation transcript:

1 Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems http://www.cs.hmc.edu/courses/2004/fall/cs5/week_11/homework.html Tutors available Fri., Sat. afternoons Lac Lab and Parsons (1-4) Sunday afternoons Lac Lab and Parsons (1-4) Sunday evenings Lac Lab and Parsons (8-12) Monday evenings Lac Lab and Parsons (8-12) names and hours linked from the CS 5 Syllabus

2 Tutors available -- contact information

3 char checker Picture of a Player object Player int lookahead Player playerForX Player(char ch, int lk, int tbk) int tiebreakType char getChecker() char me() char opp()void printScores() double evaluate(Board b) double[] ply0,1,2,3,4,N(Board b) double[] plyHuman(Board b) int breaktie(double[] s) … int go(Board b)

4 Problem 1: Methods to write class Player public double tournamentEvaluate(Board b) public double[] findScores(Board b) public int plyHuman(Board b) public double[] ply0(Board b) (ply1, ply2, ply3, ply4, (plyN) ) public int breaktie(double[] s) Methods class Board public void removeMove(int c) public boolean isOver() New Methods public double evaluate(Board b) small methods: constructor, me(), you(), printScores public void clear() extra credit no “go” extra credit

5 Problem 1: Outline mainwhile find the score for each column find the maximum score break ties return a single move create an array of 7 doubles s s 50 0 100 0 max = 100 (right, left, random) 3 (column 3) ply tie bkr

6 Hw10 class CS5App { public static void main(String[] args) { H.pl("Hi! Welcome to Connect 4..."); int R = H.ni(); int C = H.ni(); Board b = new Board(R,C); char player = 'X'; while (true) { b.print(); int c = H.ni(); // gets next move b.addMove(c,player); if (b.winsFor(player)) break; if (player == 'X') player = '0'; else player = 'X'; } // end of while } Hw11 Player px = new Player(‘X’,lk,tb); Player po = new Player(‘O’,lk,tb); Player player = px; if (player == px) player = po; else player = px; player.me() Changes player.me() 6 7

7 Player px = new Player(‘X’,lk,tb); Player po = new Player(‘O’,lk,tb); Player player = px; if (player == px) player = po; else player = px; Player px Player po Details ‘X’ ply tiebreaker Details ‘O’ ply tiebreaker Player player

8 Choosing a move 1) Find scores at appropriate lookahead… 2) Print the scores. 3) Break ties to determine the next move. ply0 : 0 ply of lookahead ply1 : 1 ply of lookahead ply2,3,4 : 2,3,4 ply of lookahead plyHuman : ask the user printScores : prints the scores to each column breaktie : chooses ONE maximum score findScores chooses one of these methods to run

9 class Player { public double[] findScores(Board b) { returns the appropriate set of seven scores char checker int lookahead int tiebreakType findScores

10 plyHuman class Player { // returns a set of scores! // with the user choosing a col public double[] plyHuman(Board b) { ??????? s 0c123456 need to prompt for input need a valid score need to support “hints”

11 class Player { public double[] ply0(Board b) { double[] s = new double[7]; for (int c=0 ; c<7 ; ++c) { if (b.allowsMove(c)) { s[c] = } else s[c] = } return s; } ply0

12 Looking ahead … 0 ply: 2 ply:3 ply: random (but legal) choice of move ! (1) player will win (2) player will avoid losing (3) player will set up a win by forcing the opponent to avoid losing 1 ply: X ’s move

13 2-ply scores for O col 0col 1col 2col 3col 4col 5col 6 1-ply scores for X col 0col 1col 2col 3col 4col 5col 6

14 3-ply scores for X col 0col 1col 2col 3col 4col 5col 6 2-ply scores for O col 0col 1col 2col 3col 4col 5col 6

15 ‘X’ ‘O’ new‘X’ Col 6 Col 5 Col 4 Col 3 Col 2 Col 1 Col 0 b Choosing the best move (1) For each possible move (2) Create new boards (3) Evaluate and return the seven scores received 100.0 50.0 100.0 50.0 100.0

16 class Player { public double[] ply1(Board b) { double[] s = new double[7]; for (int c=0 ; c<7 ; ++c) { if (b.allowsMove(c)) { s[c] = evaluate(b); } else s[c] = -1.0; } return s; } ply1 This is copied directly from the ply0 code!

17 evaluate class Player { // returns the appropriate score for b // remember: all of Player’s methods are available public double evaluate(Board b) { 100.0 for a win -1.0 for an invalid move 0.0 for a loss 50.0 for a “tie” Improvements? Write tournamentEvaluate for Ex. Cr.! if ( ) return 100.0; else if ( ) return 0.0; else return 50.0; not possible in evaluate !

18 class Player { public double[] ply2(Board b) { double[] s = new double[7]; for (int c=0 ; c<7 ; ++c) { if (b.allowsMove(c)) { s[c] = evaluate(b); } else s[c] = -1.0; } return s; } ply2 This is copied directly from the ply0 code!

19 breaktie class Player { // returns a column to move // public int breaktie(double[] s) { 050 0 s 0c123456

20 removeMove class Board { // removes a move from col c // public void removeMove(c) { XOOX X this.data 0 column 123456 row XOOX X XOOX X XOOX X XOOX X XOOX X 0 1 2 3 4 5

21 Good luck on this!

22 double[] s = player.findScores(b); player.printScores(s); int c = player.breaktie(s);


Download ppt "Homework 11 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/14 Mon., 11/15 Problems"

Similar presentations


Ads by Google