Homework 11 Due Sun 1114 Mon 1115 MT

  • Slides: 22
Download presentation
Homework 11 • Due Sun. , 11/14 Mon. , 11/15 ( MT sections )

Homework 11 • Due Sun. , 11/14 Mon. , 11/15 ( MT sections ) at midnight ( WTh sections ) • Problems http: //www. cs. hmc. edu/courses/2004/fall/cs 5/week_11/homework. html • Tutors available Fri. , Sat. 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

 • Tutors available -- contact information

• Tutors available -- contact information

Player Picture of a Player object Player player. For. X char checker int lookahead

Player Picture of a Player object Player player. For. X char checker int lookahead int tiebreak. Type Player(char ch, int lk, int tbk) char get. Checker() char me() void print. Scores() char opp() int go(Board b) double evaluate(Board b) double[] ply 0, 1, 2, 3, 4, N(Board b) double[] ply. Human(Board b) … int breaktie(double[] s)

Problem 1: Methods to write New Methods class Board public void remove. Move(int c)

Problem 1: Methods to write New Methods class Board public void remove. Move(int c) public void clear() public boolean is. Over() Methods class Player small methods: constructor, me(), you(), print. Scores public double[] find. Scores(Board b) public double[] ply 0(Board b) (ply 1, ply 2, ply 3, ply 4, (ply. N) ) extra credit public int ply. Human(Board b) public int breaktie(double[] s) public double evaluate(Board b) public double tournament. Evaluate(Board b) extra credit no “go”

Problem 1: Outline create an array of 7 doubles s ply find the score

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

Hw 10 Hw 11 class CS 5 App { public static void main(String[] args)

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

Details ‘X’ ply tiebreaker Details ‘O’ ply tiebreaker Player px Player po Player px

Details ‘X’ ply tiebreaker Details ‘O’ ply tiebreaker Player px Player po 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;

Choosing a move 1) Find scores at appropriate lookahead… ply 0: 0 ply of

Choosing a move 1) Find scores at appropriate lookahead… ply 0: 0 ply of lookahead ply 1: 1 ply of lookahead ply 2, 3, 4: 2, 3, 4 ply of lookahead ply. Human: ask the user find. Scores chooses one of these methods to run 2) Print the scores. print. Scores: prints the scores to each column 3) Break ties to determine the next move. breaktie: chooses ONE maximum score

find. Scores returns the appropriate set of seven scores char checker int lookahead class

find. Scores returns the appropriate set of seven scores char checker int lookahead class Player { public double[] find. Scores(Board b) { int tiebreak. Type

class Player { // returns a set of scores! // with the user choosing

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

class Player { public double[] ply 0(Board b) { double[] s = new double[7];

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

Looking ahead … 0 ply: random (but legal) choice of move ! 1 ply:

Looking ahead … 0 ply: random (but legal) choice of move ! 1 ply: X’s move 2 ply: X’s move 3 ply: 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

2 -ply scores for O col 0 col 1 col 2 col 3 col

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

3 -ply scores for X col 0 col 1 col 2 col 3 col

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

‘X’ ‘O’ new‘X’ Choosing the best move b (1) For each possible move (2)

‘X’ ‘O’ new‘X’ Choosing the best move 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 100. 0

class Player { public double[] ply 1(Board b) { double[] s = new double[7];

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

evaluate 100. 0 for a win 50. 0 for a “tie” 0. 0 for

evaluate 100. 0 for a win 50. 0 for a “tie” 0. 0 for a loss -1. 0 for an invalid move class Player not possible in evaluate! { // 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 tournament. Evaluate for Ex. Cr. !

class Player { public double[] ply 2(Board b) { double[] s = new double[7];

class Player { public double[] ply 2(Board b) { double[] s = new double[7]; for (int c=0 ; c<7 ; ++c) { if (b. allows. Move(c)) { This is copied directly from the ply 0 code! s[c] = evaluate(b); } else s[c] = -1. 0; } return s; } ply 2

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

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

class Board { // removes a move from col c // public void remove.

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

Good luck on this!

Good luck on this!

double[] s = player. find. Scores(b); player. print. Scores(s); int c = player. breaktie(s);

double[] s = player. find. Scores(b); player. print. Scores(s); int c = player. breaktie(s);