96
CS 5 Today: two I's AI UI hw11pr2.p y an arbitrarily- good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.htm l I wish there were three i's! Hw 11 is due Tuesday, 11/24 M/T 11/23-24 Introduction to final projects After Thanksgiving TheoComp! T 11/24 10pm Ex.Cr. circuits lab… W 11/25 1:15pm Ex.Cr. circuits lab… Schedule:

CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Embed Size (px)

Citation preview

Page 1: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

CS 5 Today: two I's

AI UI

hw11pr2.py

an arbitrarily-good Connect 4 player!

Player class

Extra:

scoreBoard4Tourney

an intuitive Connect 4 player:

hw11pr1.html

I wish there were three i's!

Hw 11 is due Tuesday, 11/24

M/T 11/23-24 Introduction to final projects

After Thanksgiving TheoComp!

T 11/24 10pm Ex.Cr. circuits lab…

W 11/25 1:15pm Ex.Cr. circuits lab…

Schedule:

Page 2: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Epic winsFor?

Python had no Connect-four datatype…

| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|X| |O|--------------- 0 1 2 3 4 5 6

Looking further ahead?

… but you've already corrected that!

Page 3: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Two-player games have been a key focus of AI as long as computers have been around…

Strategic thinking == intelligence?

In 1945, Alan Turing predicted that computers

would be better chess players than people in

~ 50 years…

and thus would have achieved intelligence.

Alan Turing memorial Manchester, England

Page 4: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

How humans play games…

- experts could reconstruct these perfectly - novice players did far worse…

An experiment (by A. deGroot) was performed in which chess positions were shown to novice and expert players…

Page 5: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

How humans play games…

- experts could reconstruct these perfectly - novice players did far worse…

Random chess positions (not legal ones) were then shown to the two groups

- experts and novices did equally well (badly) at reconstructing them!

An experiment (by A. deGroot) was performed in which chess positions were shown to novice and expert players…

Page 6: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Two-player games have been a key focus of AI as long as computers have been around…

Strategic thinking == intelligence?

humanscomputers

good at evaluating the strength of a board for a player

good at looking ahead in the game to

find winning combinations of

moves

… humans and computers have different relative strengths in these games.

hw11 pr2

building an AI chess playeremulating a human by

evaluating a board position

ex. credit

Page 7: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Computer Chess…

Page 8: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2 solutionsProblem 1

def zerospan( L, hi, low ):

for i in range(len(L)):

if low <= L[i] <= hi:

L[i] = 0.0

This does not change L!

last time:

Page 9: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2 solutionsProblem 2

Hmmm Problem 3

Page 10: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Player data

PlayerpForX

What data does a computer AI player need?

Let's see a demo!

stringox

stringtbt

'X' 'LEFT'intply

0

DATA MEMBERS

tiebreakTypechecker, O or X moves to look ahead

surprisingly little!

How about knowledge about its opponent?

Page 11: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Player

Player methods

__init__(self, ox, tbt, ply)

__repr__(self)

scoreBoard(self, b)

scoresFor(self, b)

tiebreakMove(self, scores)

nextMove(self, b)

oppCh(self)

Board

__init__( self, width, height )

allowsMove( self, col )

__repr__( self )

addMove( self, col, ox )

isFull( self )

winsFor( self, ox )

hostGame( self )

delMove( self, col )

Page 12: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

scoreBoard(self,b) ‘X’‘O’

Assigns a score to any board, b

100.0 50.0 0.0A simple system:for a win for a lossfor anything else

Score for Score for

Score for Score for

Page 13: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

scoreBoard

Assigns a score to any board, b

100.0 50.0 0.0A simple system:for a win for a lossfor anything else

scoreBoard(self, b)

Implementation ideas…

What methods that already exist will come in handy?

This doesn't seem to be looking very

far ahead !

How can there be no 'X' or 'O' input?

What class is this method in?

Page 14: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Looking further ahead…

scoreBoard looks ahead 0 movesThe "Zen" approach --

we are excellent at this!

If you look one move ahead, how many possibilities are there to consider?

0-ply

1-ply

A 1-ply lookahead player will "see" an impending victory.

turn

A score for each

column…?

scores

p42.scoresFor( b42 )

special case

Page 15: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Two moves ahead… ?

A 2-ply lookahead player will also "see" an opponent's

impending victory.

's move…

What about 3-ply?

2-ply

1-ply score

score

p43.scoresFor( b42 ) and p44

Page 16: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

scoresFor

scoreBoard looks ahead 0 movesThe "Zen" approach --

we are excellent at this!

If you look one move ahead, how many possibilities are there to consider?

0-ply

1-ply

scoresFor( self, b ) returns a LIST of scores, one for each column you might choose to move next…

2-ply

Page 17: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

b

0-ply scores for O:

col 0 col 1 col 2 col 3 col 4 col 5 col 6

1-ply scores for O:

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

3-ply scores for O:

col 0 col 1 col 2 col 3 col 4 col 5 col 6

"Quiz" 'X'

'O'you - or self - is

playing 'O'

Name(s):

Fill in the N-ply score for a move to each column. The same move is evaluated at each ply! It's just evaluated farther into the future.

Looks 0 moves into the future

Looks 1 move into the future

Looks 2 moves into the future

Looks 3 moves into the future

Page 18: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

|O| | | | | | ||X| | | |O| |X||O| | | |X|O|X||X| | | |O|O|X||X| |X| |X|O|O||X| |O|O|O|X|X|--------------- 0 1 2 3 4 5 6

| | | | | | |O|| | | | | | |O|| | | | | | |X||X| |X|O| | |O||X|O|O|X| |X|X||X|O|O|O| |O|X|--------------- 0 1 2 3 4 5 6

It is O’s move. What scores does a 1-ply lookahead for O assign to each

move? col 0 col 1 col 2 col 3 col 4 col 5 col 6

It is X’s move. What scores does a 2-ply lookahead for X assign to each

move?col 0 col 1 col 2 col 3 col 4 col 5 col 6

Which change at 3-ply?

Which change at 2-ply?

Example 1-ply and 2-ply lookahead scores

Page 19: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

|O| | | | | | ||X| | | |O| |X||O| | | |X|O|X||X| | | |O|O|X||X| |X| |X|O|O||X| |O|O|O|X|X|--------------- 0 1 2 3 4 5 6

| | | | | | |O|| | | | | | |O|| | | | | | |X||X| |X|O| | |O||X|O|O|X| |X|X||X|O|O|O| |O|X|--------------- 0 1 2 3 4 5 6

It is O’s move. What scores does a 1-ply lookahead for O assign to each

move? col 0 col 1 col 2 col 3 col 4 col 5 col 6

It is X’s move. What scores does a 2-ply lookahead for X assign to each

move?

col 0 col 1 col 2 col 3 col 4 col 5 col 6

be careful!

-1 100 50 100 50 100 50

100 0 0 0 50 0 -1

Which change at 2-ply? 0 0

Which change at 3-ply?0

Answers to example lookahead scores

Page 20: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

‘X’‘O’new‘X’

Col 6

Col 5

Col 4Col 3Col 2

Col 1

Col 0

b

scoresFor each column

(1) For each possible move

(2) Add it to the board

Page 21: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

(self) 'X'

'O'

new 'X'

Col 6

Col 5

Col 4Col 3Col 2

Col 1

Col 0

b(1) For each possible move

(2) Add it to the board

(3) Ask OPPONENT its scoresFor each board!

At what ply?

[50,50,50,50,50,100,50]

[ 0, 0, 0, 0, 0, 0, 0 ]

scoresForneeds to return a list of 7 numbers for self

these are all of the opponent's evaluations of its next move…

[ 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0 ]

[50,50,50,50,50,50,50]

[50,50,50,50,50,100,50]

Page 22: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

(self) 'X'

'O'

new 'X'

Col 6

Col 5

Col 4Col 3Col 2

Col 1

Col 0

b(1) For each possible move

(2) Add it to the board

(3) Ask OPPONENT its scoresFor each board!

At what ply?

[50,50,50,50,50,100,50]

[ 0, 0, 0, 0, 0, 0, 0 ]

scoresForneeds to return a list of 7 numbers for self

these are all of the opponent's evaluations of its next move…

[ 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0 ]

[50,50,50,50,50,50,50]

[50,50,50,50,50,100,50]

What score does the opponent give

each?

Page 23: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

(self) 'X'

'O'

new 'X'

Col 6

Col 5

Col 4Col 3Col 2

Col 1

Col 0

b(1) For each possible move

(2) Add it to the board

(3) Ask OPPONENT its scoresFor each board!

At what ply?

[50,50,50,50,50,100,50]

[ 0, 0, 0, 0, 0, 0, 0 ]

scoresForneeds to return a list of 7 numbers for self

these are all of the opponent's evaluations of its next move…

[ 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0 ] [ 0, 0, 0, 0, 0, 0, 0 ]

[50,50,50,50,50,50,50]

[50,50,50,50,50,100,50]

What score does the opponent give

each?

max(S) = 0

max(S) = 100

max(S) = 0max(S) = 0

max(S) = 0max(S) = 50

max(S) = 100

What score does self give each?

100

0100

100 10050

0

Page 24: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Difficulty == Branching Factor

Branching Factor Estimatesfor different two-player games

Tic-tac-toe 4

Connect Four 7

Checkers 10

Othello 30

Chess 40

Go 300

• On average, there are fewer than 40 possible moves that a chess player can make from any board configuration…

0 Ply

1 Ply

2 Ply

Hydra at home in the United Arab Emirates…

Hydra looks ahead 18 ply !What do you think, Noam?

Page 25: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

500

1200

2000

2800

Computer Chess

early programs ~ 1960’s

Computers cut their teeth playing chess…

Ranking

beginner

amateur

world ranked

world champion

MacHack (1100) ~ 1967 MIT

Deep Thought ~ 1989 Carnegie Mellon

Slate (2070) ~ 1970’s Northwestern

Deep Blue ~ 1996 IBM

Deep Blue rematch ~ 1997 IBM

100’s of moves/sec

10,000’s of moves/sec

1,000,000’s moves/sec

3,500,000 moves/secDeep Fritz: 2002X3D Fritz: 2003 Hydra: 2006

200,000,000 moves/sec

first paper: 1950

What is Hydra's chess rating?

Page 26: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Games’ Branching Factors

Branching Factor Estimatesfor different two-player games

Tic-tac-toe 4

Connect Four 7

Checkers 10

Othello 30

Chess 40

Go 300

1 Ply

2 Ply

Boundaries for qualitatively different games…

0 Ply

Page 27: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Games’ Branching Factors

Branching Factor Estimatesfor different two-player games

Tic-tac-toe 4

Connect Four 7

Checkers 10

Othello 30

Chess 40

Go 300

“solved” games

computer-dominated

human-dominated

1 Ply

2 Ply

0 Ply

Progress

Page 28: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html
Page 29: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

def scoresFor(self, b):(1) For each possible move

(2) Add it to the board b

(3) Ask OPPONENT its scoresFor each b at ply-1

(4) self's score is 100-max!

Page 30: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Write tiebreakMove to return the leftmost best score

inside the list scores

def tiebreakMove(self, scores):

if self.tbt == 'LEFT':

How would 'RANDOM' and 'RIGHT' work differently?

Page 31: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

hw11 this week

• Extra: scoreBoard4Tourney and a CS 5 C4 round-robin

http://www.stanford.edu/~ccecka/research/C4.html

Using more scores than 0, 50, and 100 !

• Problem 2: A Connect Four Player…

don't give this board a 50.0 !

• Problem 1: (Lab) Trying out a command-line user interface

Page 32: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

User Inferfaces

No undo!

% ls

file1.txt file2.txt

file3.txt !

% rm *!

(long pause…)

Page 33: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Design for software and beyond

0. Conceptual

models

1. Mapping

2. Visibility

3. Feedback

4. Affordances

Don Norman's key principles:

Page 34: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Conceptual Models

Users always bring something "to the table"

these don't work!

Images from The Design of Everyday Things

Page 35: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Mapping

is matching expected (spatial) relationships

Where to plug in the keyboard and mouse?

?

Page 36: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Visibility

is making functionality apparent

Shower?Slide projector…

From: www.baddesigns.com

"I used to have that awful shower controller where you pull down on the nozzle to turn it on. I had to tell every guest how to do it, and when we sold our house, we got a call from the new owners about 5 days later asking how to turn on the shower. They had been taking baths for 5 days! Unbelievable." - BL

Page 37: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Feedback

providing information back to the user

from the UI Hall of Shame

Microsoft Access

Microsoft Outlook

http://homepage.mac.com/bradster/iarchitect/shame.htmBut some of us graphics aren't so lazy!

Page 38: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Affordances

are the functions that form suggests…

Opening the XO? Door handles

built-in user's manual

Page 39: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Where do these go wrong?Mapping

How to open this gas cap?

Visibility Feedback Affordances

This handle unfastens the seat from the

floor.

How to turn on this stove?

Set to 5 minutes?

Win NT Dialog

Page 40: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

The designers aren't the users…

Page 41: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

See you on Monday!

Remember: CS Hw #11 is due next Tuesday…

Page 42: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Thinking about User Interfaces

Command Line Tablet / Touch

Page 43: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Ambient Information

Page 44: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Display Walls

Page 45: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Thinking about User Interfaces

What other types of human/computer interfaces can you think of?

#1 WI

MP

indowsconsenusointer

GUI

Page 46: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Affordances~ physical and cultural expectations

0. Conceptual models 1. Mapping

Matching user expectations e.g., Directory structures

Visibility and Feedback

Banks of glass doorsSlide projector

Affordances The functions that form suggests! Phone settings

Page 47: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Why is this a better design?

Page 48: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Keypad numbers layout

• A case of external inconsistency

1 2 34 5 6

7 8 9

7 8 9

1 2 3

4 5 6

0 0

(a) phones, remote controls(b) calculators, computer keypads

Page 49: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Affordances: to give a clue• Affordances: The perceived and actual properties of an object that signal of the object can be used (from The Design of Everyday Things)

Page 50: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Physical Affordances

Page 51: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Physical Affordances

What do the Zune wheel and the door handle have in common?

Page 52: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Virtual AffordancesClick Me Click Me

Page 53: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

"Quiz"

Name(s):

Note the perceivedaffordances inthis interface.Are there anythat are missing?Misleading?

Page 54: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

0-ply scores for O:

col 0 col 1 col 2 col 3 col 4 col 5 col 6

1-ply scores for O:

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

3-ply scores for O:

col 0 col 1 col 2 col 3 col 4 col 5 col 6

Solutions

-1

-1

-1

-1

50 50 50 50 50 50

50 50 100 50 50 50

0

0

100

100

0

0

0 0 50

100

00

b ‘X’‘O’

Page 55: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

|O| | | | | | ||X| | | |O| |X||O| | | |X|O|X||X| | | |O|O|X||X| |X| |X|O|O||X| |O|O|O|X|X|--------------- 0 1 2 3 4 5 6

It is O’s move. What scores does a 1-ply lookahead for O assign to each

move? col 0 col 1 col 2 col 3 col 4 col 5 col 6

Which change at 2-ply?

Page 56: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Looking further ahead …

0 ply:

2 ply: 3 ply:

Zen choice of move: here and now

| | | | | | | || | | | | | | ||O| | | | | | ||X| | | | | | ||X|O|O| | |X| ||O|X|X|O|X|O| |--------------- 0 1 2 3 4 5 6

| | | | | | | || | | | | | | || | | | |X| | || | | | |O|O| || |X|X| |X|O| ||O|X|O| |O|X| |--------------- 0 1 2 3 4 5 6

(1) Player will win

(2) Player will avoid losing

(3) Player will set up a win by forcing the opponent to avoid losing

X’s move

X‘s move

1 ply:

| | | | | | | || | | | | | | || | | | | | | || | | | | | | || |O|X| | | | ||O|X|X|X| |O|O|--------------- 0 1 2 3 4 5 6

X’s move

Page 57: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

‘X’‘O’new‘X’

Col 6

Col 5

Col 4Col 3Col 2

Col 1

Col 0

b(1) For each possible move

(2) Add it to the board

(3) Ask OPPONENT to score each board

(4) Which score will the opponent choose?

0.0

50.0

0.00.0

50.0

0.0

What, then, should assign for your score? (self's score)

scoresFor each column

50.0

Page 58: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

‘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) Add it to the board

(3) Ask OPPONENT to score each board - ply?

(4) Reverse the scores

100.0

50.0

50.0

100.0100.0

50.0

100.0

Page 59: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

‘X’‘O’new‘X’

Col 6

Col 5

Col 4Col 3Col 2

Col 1

Col 0

b

Choosing the best move

100.0

50.0

50.0

100.0100.0

50.0

100.0

(1) For each possible move

(2) Add it to the board

(3) Ask OPPONENT to score each board - ply?

(4) Reverse the scores

(5) Find one max - that's it!

Page 60: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four

| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|X| |O|--------------- 0 1 2 3 4 5 6

Suppose our Board class's 2d list of lists is named self.data. What is the

name of this single spot?

For your convenience, the creators of Python’s library have included a Board class that can represent any size of Connect Four board... !

Page 61: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: the object b

This is true for sufficiently broad definitions of “the creators of Python’s library” ...

Boardb

def addMove(self, col, player)

intNROWS

intNCOLS

def allowsMove(self, col)

char char char

char char char

char char char

datalist char

char

char

def winsFor(self, player)

data members

methods

What is player ?

Page 62: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: the object b

This is true for sufficiently broad definitions of “the creators of Python’s library” ...

Boardb

def addMove(self, col, player)

intNROWS

intNCOLS

def allowsMove(self, col)

char char char

char char char

char char char

datalist char

char

char

def winsFor(self, player)

data members

methods

Which methods will alter b? Which leave it alone?

Page 63: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: Board

Starting code for the Board class

class Board: def __init__( self, numRows, numCols ): """ our Board's constructor """ self.NROWS = numRows self.NCOLS = numCols self.data = [] for r in range(self.NROWS): onerow = [' ']*self.NCOLS self.data += [onerow]

def __repr__(self): """ thoughts? """

look familiar?

Page 64: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: Boardclass Board: def __init__( self, numRows, numCols ): """ our Board's constructor """ self.NROWS = numRows self.NCOLS = numCols self.data = [] for r in range(self.NR): onerow = [' ']*self.NC self.data += [onerow]

def __repr__(self): """ thoughts? """ s = '\n' for r in range(self.NROWS): s += '|' for c in range(self.NCOLS): s += self.data[r][c] + '|'

return s

look familiar?

a bit more to go !

Page 65: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Problem 2

class Board

__init__

allowsMove

__repr__

addMove

isFull

winsFor

the “constructor”

checks if allowed

places a checker

outputs to screen

checks if space left

checks if a player has won

Hw11 Pr2: Connect Four Board

hostGame play!

What's trickiest here?

Page 66: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Problem 2

class Board

__init__

allowsMove

__repr__

addMove

isFull

winsFor

the “constructor”

checks if allowed

places a checker

outputs to screen

checks if space left

checks if a player has won

Hw11 Pr2: Connect Four Board

hostGame play!

What's trickiest here?

Page 67: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

What's wrong here?

| | | | | | | || | | | | | | || | | | | | | || | | |O|O| | ||X|X| |O|X|X|X||X|O|O|O|O|X|X|--------------- 0 1 2 3 4 5 6

def winsForHoriz(self, player): inarow = 0

for r in range(self.NROWS): for c in range(self.NCOLS):

if self.data[r][c] == player: inarow += 1 else: inarow = 0

if inarow == 4: return True

return False

Page 68: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Strategies?

horizontals

verticals

diagonals ??| | | | | | | || | | | | | | || | | | | | | || | | |O|O| | ||X|X| |O|X|X|X||X|O|O|O|O|X|X|--------------- 0 1 2 3 4 5 6

Page 69: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

“Quiz”

class Board{ # __init__ and __repr__ methods here… # 3 data members: # self.NR == number of rows # self.NC == number of cols # self.data == the 2d list of lists of chars

def mysteryMethod(self, col, ox): r = 0 while r < self.NR and self.data[r][col] == ' ': r += 1 self.data[r-1][col] = ox

def allowsMove(self, col):

}

Briefly, what is each line of the

mysteryMethod doing? Which method is it?

Write allowsMove to return whether the input col is a valid column to move.

(True or False)

1

2

3

Could it go wrong?

Page 70: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Problem 2

class Board

__init__

allowsMove

__repr__

addMove

isFull

winsFor

the “constructor”

checks if allowed

places a checker

outputs to screen

checks if space left

checks if a player has won

Hw11 Pr2: Connect Four Board

hostGame play!

What's trickiest here?

Page 71: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

def scoresFor(self, b):(1) For each possible move

(2) Add it to the board

(3) Ask OPPONENT to score each board at ply-1

(4) self's score is 100-max

Page 72: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Problem 2

class Board

__init__

allowsMove

__repr__

addMove

isFull

winsFor

the “constructor”

checks if allowed

places a checker

outputs to screen

checks if space left

checks if a player has won

Hw11 Pr2: Connect Four Board

hostGame play!

What's trickiest here?

Page 73: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Strategies?

horizontals

verticals

diagonals ??| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|O| |O|--------------- 0 1 2 3 4 5 6

Page 74: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

|O| | | | | | ||X| | | |O| |X||O| | | |X|O|X||X| | | |O|O|X||X| |X| |X|O|O||X| |O|O|O|X|X|--------------- 0 1 2 3 4 5 6

It is O’s move. What scores does a 1-ply lookahead for O assign to each

move? col 0 col 1 col 2 col 3 col 4 col 5 col 6

Which change at 2-ply?

Example 1-ply and 2-ply lookahead scores

Page 75: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

0-ply scores for O:

col 0 col 1 col 2 col 3 col 4 col 5 col 6

1-ply scores for O:

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

3-ply scores for O:

col 0 col 1 col 2 col 3 col 4 col 5 col 6

Solutions

-1

-1

-1

-1

50 50 50 50 50 50

50 50 100 50 50 50

0

0

100

100

0

0

0 0 50

100

00

b ‘X’‘O’

Page 76: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: the object b

Boardb

intwidthstr str str

str str str

str str str

datalist str

str

str

data

intheight

What is the name of the method that will construct this data?

Page 77: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: constructor

class Board: """ a datatype representing a C4 board with an arbitrary number of rows and cols """ def __init__( self, width, height ): """ the constructor for objects of type Board """ self.width = width self.height = height self.data = [] # this will be the board for row in range( 6 ): boardRow = [] for col in range( 7 ): boardRow += [' '] # add a space to this row self.data += [boardRow]

Bad magic?

Page 78: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Connect Four: the object b

Boardb

intwidthstr str str

str str str

str str str

datalist str

str

str

intheight

| | | | | | | || | | | | | | || | | | | | | || | | |X| | | || |X| |X|O| | ||X|O|O|O|X| |O|--------------- 0 1 2 3 4 5 6

What is the name of the method that will print this data?

Page 79: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

def __repr__(self): """ this method returns a string representation for an object of type Board """ s = '' for row in range( 6 ): s += '|' for col in range( 7 ): s += self.data[row][col] + '|' s += '\n'

return s

Connect Four: __repr__

To remove?

To add?

which row is row 0, row 1, and so on?

Page 80: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Examples

def addMove(self, col, ox):

row = self.height-1 while True: if self.data[row][col] == ' ': self.data[row][col] = ox

row -= 1

def allowsMove(self, col):

Step through this addMove method.

How does it work?

What's wrong?

a C4 board

col #'X' or 'O'

allowsMove should return True if col is a valid move;

False otherwise.

Page 81: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

C4 Board class: methods

__init__( self, width, height )

allowsMove( self, col )

__repr__( self )

addMove( self, col, ox )

isFull( self )

winsFor( self, ox )

the “constructor”

checks if allowed

places a checker

outputs a string

checks if any space is left

checks if a player has won

hostGame( self )play!

delMove( self, col )removes a checker

Which is trickiest… ?

Page 82: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Checking wins… ?

Thought

s?

X O

b

corner cases?

Page 83: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

A whole new class of programming

CS 5 today

Mon., 11/26Wed., 11/28

Mon., 12/3

Software Engineering

Final projects

TheoComp !

Exam review

Python's Date class

Wed., 12/5Mon., 12/10

Lab and HW #11

Hey! How did you get a

body?

Choose your side…

Page 84: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Page 85: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Problem 1b Problem 2

def zerospan( L, hi, low ):

for i in range(len(L)):

if low <= L[i] <= hi:

L[i] = 0.0

def zerospan( L, hi, low ):

for x in L:

if low <= x <= hi:

x = 0.0

def ROOKnotcastle( B, r, c ):

for row in range(len(B)):

for col in range(len(B[0])):

if row == r or col == c:

B[row][col] = 1

This does not change L!

Page 86: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

def zerospan( L, hi, low ):

for x in L:

if low <= x <= hi:

x = 0.0

CAUTION: This does not change L!

Sets values between low and hi to 0.0.

Page 87: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2Problem 3

00 read r1 # input to r1

01 add r2 r2 r1 # add into r2

02 jnez r1 00 # if r1 != 0: goto 00

03 write r2 # else: write the sum

using the stack -- Aargh!adding in place :-)

Page 88: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2Problem 5

ruler( 1 )

ruler( 2 )

'1'

'212'

ruler( 3 ) '3231323'

ruler( 4 ) '434243414342434'

s si

def ruler( n ):

s = ''

for i in range(n,0,-1):

s += str(i) + s

return s

Page 89: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Page 90: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Page 91: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

A whole new class of programming

CS 5 today

Mon., 11/26Wed., 11/28

Mon., 12/3

Software Engineering

Final projects

TheoComp !

Exam review

Python's Date class

Wed., 12/5Mon., 12/10

Lab and HW #11

Hey! How did you get a

body?

Choose your side…

Page 92: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Page 93: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Page 94: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Exam 2

Page 95: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html
Page 96: CS 5 Today: two I's AIUI hw11pr2.py an arbitrarily-good Connect 4 player! Player class Extra: scoreBoard4Tourney an intuitive Connect 4 player: hw11pr1.html

Two-player games have been a key focus of AI as long as computers have been around…

Strategic thinking == intelligence?

… humans and computers have different relative strengths in these games.

humanscomputers

good at evaluating the strength of a board for a player

good at looking ahead in the game to

find winning combinations of

moves