"Intelligent" CS 5 An object is structured data that is alive, responsible, and intelligent. Sound too friendly? This week’s objects and classes will be just the opposite ... X to move. Is there a way to win? | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | |X| |X|O| | | |X|O|O|O|X|O| | --------------- 0 1 2 3 4 5 6 Hw 10 due 11/15 EXAM 2 Mon/Tue
"Intelligent" CS 5. EXAM 2. Hw 10 due 11/15. Mon/Tue. An object is structured data that is alive, responsible, and intelligent. Sound too friendly?. This week’s objects and classes will be just the opposite. | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
"Intelligent" CS 5
An object is structured data that is alive, responsible, and intelligent.
Sound too friendly?
This week’s objects and classes will be just the opposite ...
def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in [1,3,5,7,8,10] and self.day == 31: self.day = 0 self.month += 1 elif self.month in [4,6,9,11] and self.day == 30: self.day = 0 self.month += 1 elif self.month == 2: if self.isLeapYear() and self.day == 29: self.day = 0 self.month += 1 elif (self.isLeapYear() == False) and self.day == 28: self.day = 0 self.month += 1 elif self.month == 12 and self.day == 31: self.day = 0 self.month = 1 self.year += 1 self.day += 1
Coding Style
def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ DIM = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.isLeapYear() == True: DIM = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] self.day += 1 if self.day > DIM[self.month]: self.day = 1 self.month += 1 if self.month > 12: self.month = 1 self.year += 1 else: self.day += 1 if self.day > DIM[self.month]: self.day = 1 self.month += 1 if self.month > 12: self.month = 1 self.year += 1
Better Style, But...
def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ DIM = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if self.isLeapYear(): DIM[2] = 29 self.day += 1 if self.day > DIM[self.month]: self.day = 1 self.month += 1 if self.month > 12: self.month = 1 self.year += 1
An Elegant Solution
isBefore/isAfter
def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False
def isAfter(self, d2): """ Returns true if self is after d2 """ if self.year > d2.year: return True if self.month > d2.month and self.year == d2.year: return True if self.day > d2.day and d2.month == self.month and \ self.year == d2.year: return True return False
An Elegant Solution
def isBefore(self, d2): """ Returns true if self is before d2 """ if self.year < d2.year: return True if self.month < d2.month and self.year == d2.year: return True if self.day < d2.day and d2.month == self.month and \ self.year == d2.year: return True return False
def isAfter(self, d2): """ Returns true if self is after d2 """ return d2.isBefore(self)
Another Elegant Solution
def isBefore(self, d2): """ Returns true if self is before d2 """ return ([self.year, self.month, self.day] < [d2.year, d2.month, d2.day])
def isAfter(self, d2): """ Returns true if self is after d2 """ return d2.isBefore(self)
diff
def diff( self, d2 ): """ Returns the number of days between self and d2 """ dcopy = self.copy() difference = 0 if dcopy.isBefore(d2) == True: while dcopy.isBefore(d2) == True: dcopy.tomorrow() difference -= 1 else: while dcopy.isAfter(d2): dcopy.yesterday() difference += 1 return difference
An Elegant Solution
def diff( self, d2 ): """ Returns the number of days between self and d2 """ dcopy = self.copy() difference = 0 while dcopy.isBefore(d2): dcopy.tomorrow() difference -= 1 while dcopy.isAfter(d2): dcopy.yesterday() difference += 1 return difference
2) What are objects' crucial capabilities? (Methods)
Not limited to 7x6!
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?
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]
What is the name of the method that will print this data?
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 change?
To add?
which row is row 0, row 1, and so on?
"Quiz"class Board: def allowsMove(self, col):
def addMove(self, col, ox): for row in range( self.height ): if self.data[row][col] != ' ': self.data[row-1][col] = ox self.data[self.height-1][col] = ox
Step through this addMove method.
What is each line doing?
How many problems are there?
a C4 board col # 'X' or 'O'
Write allowsMove to return True if col is a valid move;
False otherwise.
NAME:
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 (person vs. person)!
delMove( self, col )removes a checker
Which of these will require the most thought?
winsFor( self, ox )
Thoughts?
X O
bb.winsFor( 'X' )or 'O'
corner cases?
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
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
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…
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…
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.
building an AI chess playeremulating a human by
evaluating a board position
The Player class
PlayerpForX
Details(data and methods)
What data and methods are needed to construct and implement a Player object?
Let's see a demo!
stringox
Picture of a Player object
Player
PlayerpForX
__init__(self, ox, tbt, ply)
stringtbt
__repr__(self)
scoreBoard(self, b)
scoresFor(self, b)
tiebreakMove(self, scores)
nextMove(self, b)
oppCh(self)
'X' 'LEFT'intply
3
DATA
METHODS
tiebreakTypechecker, O or X
scoreBoard ‘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
“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.