homework 11

22
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

Upload: gili

Post on 05-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Homework 11

Homework 11

• Due ( MT sections )

( WTh sections )at midnight

Sun., 11/14

Mon., 11/15

• Problemshttp://www.cs.hmc.edu/courses/2004/fall/cs5/week_11/homework.html

• Tutors availableFri., 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

Page 2: Homework 11

• Tutors available -- contact information

Page 3: Homework 11

charchecker

Picture of a Player object

Player

intlookaheadPlayer

playerForXPlayer(char ch, int lk, int tbk)

inttiebreakType

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)

Page 4: Homework 11

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

Page 5: Homework 11

Problem 1: Outline

main while

find the score for each column

find the maximum score

break ties

return a single move

create an array of 7 doubles

s

s5050 0 100 0 100 -1

max = 100

(right, left, random)

3 (column 3)

ply

tiebkr

Page 6: Homework 11

Hw10class 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()

67

Page 7: Homework 11

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;

Playerpx

Playerpo

Details‘X’ ply tiebreaker

Details‘O’ ply tiebreaker

Playerplayer

Page 8: Homework 11

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

findScoreschooses one of these

methods to run

Page 9: Homework 11

class Player{ public double[] findScores(Board b) {

returns the appropriate set of

seven scores

charchecker

intlookahead

inttiebreakType

findScores

Page 10: Homework 11

plyHumanclass Player{ // returns a set of scores! // with the user choosing a col public double[] plyHuman(Board b) {

? ? ? ? ? ? ?s0c 1 2 3 4 5 6

• need to prompt for input• need a valid score• need to support “hints”

Page 11: Homework 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

Page 12: Homework 11

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 X’s move X’s move

Page 13: Homework 11

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

Page 14: Homework 11

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

Page 15: Homework 11

‘X’‘O’new‘X’

Col 6

Col 5

Col 4Col 3Col 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

50.0

100.0100.0

50.0

100.0

Page 16: Homework 11

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!

Page 17: Homework 11

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!

Page 18: Homework 11

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!

Page 19: Homework 11

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

0 50 50 0 50 -1 -1s0c 1 2 3 4 5 6

Page 20: Homework 11

removeMoveclass Board{ // removes a move from col c // public void removeMove(c) {

X O O X X

this.data

0

column

1 2 3 4 5 6

row

X O O X X

X O O X X

X O O X X

X O O X X

X O O X X0

1

2

3

4

5

Page 21: Homework 11

Good luck on this!

Page 22: Homework 11

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