Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | flora-hancock |
View: | 216 times |
Download: | 0 times |
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
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)
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
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
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
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
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
class Player{ public double[] findScores(Board b) {
returns the appropriate set of
seven scores
charchecker
intlookahead
inttiebreakType
findScores
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”
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
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
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
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
‘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
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!
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!
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!
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
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