1 / 22

Homework 11

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

gili
Télécharger la présentation

Homework 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 11 Sun., 11/14 ( MT sections ) • Due at midnight Mon., 11/15 ( WTh sections ) • Problems http://www.cs.hmc.edu/courses/2004/fall/cs5/week_11/homework.html • Tutors available names and hours linked from the CS 5 Syllabus 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)

  2. Tutors available -- contact information

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

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

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

  6. Hw10 Hw11 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 } } Changes 6 7 Player px = new Player(‘X’,lk,tb); Player po = new Player(‘O’,lk,tb); Player player = px; player.me() player.me() if (player == px) player = po; else player = px;

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

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

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

  10. plyHuman class Player { // returns a set of scores! // with the user choosing a col public double[] plyHuman(Board b) { • need to prompt for input • need a valid score • need to support “hints” s ? ? ? ? ? ? ? c 0 1 2 3 4 5 6

  11. ply0 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; }

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

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

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

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

  16. ply1 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; } This is copied directly from the ply0 code!

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

  18. ply2 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; } This is copied directly from the ply0 code!

  19. breaktie class Player { // returns a column to move // public int breaktie(double[] s) { s 0 50 50 0 50 -1 -1 c 0 1 2 3 4 5 6

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

  21. Good luck on this!

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

More Related