EECS 110: Lec 15:
Classes and Objects (2)
Aleksandar Kuzmanovic
Northwestern University
http://networks.cs.northwestern.edu/EECS110-s17/
EECS 110 important dates
Projects
Fri., 5/19: online
Mon., 5/22: class
Projects
Fri., 5/26: recitation
Tue., 5/30: recitation, W. lab
Final!
Wed., 5/24: rev. for final
Wed., 5/31, final
Classes & Objects
An object-oriented programming language allows you
to build your own customized types of variables.
(1) A class is a type of variable.
(2) An object is one such variable.
There will typically
be MANY objects
of a single class.
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
Objects
An object is a data structure (like a list), except
(1) Its data elements have names chosen by the programmer.
(2) Data elements are chosen & organized by the programmer
(3) An object can have behaviors built-in by the programmer.
usually called "methods"
instead of functions
class Date:
""" a blueprint (class) for objects
that represent calendar days
"""
def __init__( self, mo, dy, yr ):
""" the Date constructor """
self.month = mo
self.day = dy
self.year = yr
The Date
class
def __repr__( self ):
""" used for printing Dates """
s = "%02d/%02d/%04d" % (self.month, self.day, self.year)
return s
EECS 110 Today
Connect Four
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
Aargh!
Python has no Connect-four datatype…
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X| | | |
| |X| |X|O| | |
|X|O|O|O|X| |O|
--------------0 1 2 3 4 5 6
… but we can correct that!
Designing classes
1) What data? (Data Members)
Not limited to 7x6!
2) What are objects' crucial capabilities? (Methods)
Designing classes
1) What data? (Data Members)
- height, width
- Where the chips are
- Whose turn it is
- Winning condition
- End condition (number of chips played)
2) What are objects' crucial capabilities? (Methods)
Not limited to 7x6!
Designing classes
1) What data? (Data Members)
- height, width
- Where the chips are
- Whose turn it is
- Winning condition
- End condition (number of chips played)
2) What are objects' crucial capabilities? (Methods)
- creates a new object
- check for win
- print
- get the next move/switch turn
-check for full board
Not limited to 7x6!
Connect Four:
Board
b
list
data
the object b
str
str
str
str
str
str
str
str
str
str
str
str
int
width
int
height
data
What is the name of the method
that will construct this data?
Connect Four:
Board
b
list
data
the object b
str
str
str
str
str
str
str
str
str
str
str
str
int
width
int
height
data
What is the name of the method
that will construct this data?
__init__(…)
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( height ): # 6
boardRow = []
for col in range( width ): # 7
boardRow += [' ']
# add a space to this row
self.data += [boardRow]
Connect Four:
Board
b
list
data
str
str
str
str
str
str
str
str
str
What is the name of the method
that will print this data?
the object b
str
str
str
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | |X| | | |
| |X| |X|O| | |
|X|O|O|O|X| |O|
--------------0 1 2 3 4 5 6
int
width
int
height
Connect Four:
__repr__
def __repr__(self):
""" this method returns a string representation
for an object of type Board
"""
s = ''
which row is row 0,
for row in range( self.height ):
row 1, and so on?
s += '|'
for col in range( self.width ):
s += self.data[row][col] + '|'
s += '\n'
To remove?
To add?
return s
Connect Four:
__repr__
def __repr__(self):
""" this method returns a string representation
for an object of type Board
"""
s = ''
which row is row 0,
for row in range( self.height ):
row 1, and so on?
s += '|'
for col in range( self.width ):
s += self.data[row][col] + '|'
s += '\n‘
s += '--'*self.width + '-\n‘
for col in range( self.width ):
s += ' ' + str(col%10)
s += '\n'
return s
class Board
{
a C4
board
col #
'X' or 'O'
def mystery(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
"Quiz"
Step through this
mystery method.
What is each line doing?
What's going wrong?
def allowsMove(self, col):
Write allowsMove to return
True if col is a valid move;
False otherwise.
}
class Board
{
a C4
board
col #
'X' or 'O'
def mystery(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
"Quiz"
Step through this
mystery method.
What is each line doing?
What's going wrong?
# Adds ox at the top of a column
# But at the same time overwrites the existing
# elements in the column…
# Does not check if it is possible to write in a column…
def allowsMove(self, col):
Write allowsMove to return
True if col is a valid move;
False otherwise.
}
class Board
{
a C4
board
col #
'X' or 'O'
def mystery1(self, col, ox):
if allowsMove(col):
for row in range( self.height ):
if self.data[row][col] != ' ':
self.data[row-1][col] = ox
return
self.data[self.height-1][col] = ox
"Quiz"
Step through this
mystery method.
What is each line doing?
What's going wrong?
def allowsMove(self, col):
Write allowsMove to return
True if col is a valid move;
False otherwise.
}
class Board
{
a C4
board
col #
'X' or 'O'
def mystery1(self, col, ox):
if allowsMove(col):
for row in range( self.height ):
if self.data[row][col] != ' ':
self.data[row-1][col] = ox
return
self.data[self.height-1][col] = ox
"Quiz"
Step through this
mystery method.
What is each line doing?
What's going wrong?
def allowsMove(self, col):
if 0 <= col < self.width:
return self.data[0][col] == ' '
Write allowsMove to return
True if col is a valid move;
False otherwise.
}
C4 Board class:
methods
the “constructor”
__init__( self, width, height )
checks if allowed
allowsMove( self, col )
places a checker
removes a checker
outputs a string
checks if any space is left
checks if a player has won
play!
addMove( self, col, ox )
delMove( self, col )
__repr__( self )
isFull( self )
winsFor( self, ox )
hostGame( self )
Which of these will require
the most thought?
winsFor( self, ox )
b.winsFor( 'X' )
b
or 'O'
X
O
Thoughts?
winsFor( self, ox )
b
def winsFor(self, ox):
# check for horizontal wins
for row in range(0,self.height):
for col in range(0,self.width-3):
if self.data[row][col] == ox and \
self.data[row][col+1] == ox and \
self.data[row][col+2] == ox and \
self.data[row][col+3] == ox:
return True
X
O
# check for vertical wins
Thoughts?
The Player class (Extra credit)
Details
Player
pForX
(data and methods)
What data and methods are needed to
construct and implement a Player object?
Player
Picture of a Player object
DATA
Player
pForX
3
'LEFT'
'X'
string
ox
string
tbt
checker, O or X
tiebreakType
int
ply
__init__(self, ox, tbt, ply)
__repr__(self)
oppCh(self)
scoreBoard(self, b)
scoresFor(self, b)
tiebreakMove(self, scores)
nextMove(self, b)
METHODS
scoreBoard
‘X’
‘O’
Assigns a score to any board, b
A simple system:
Score for
Score for
100.0
50.0
0.0
for a win
for anything else
for a loss
Score for
Score for
scoreBoard
‘X’
‘O’
Assigns a score to any board, b
A simple system:
Score for
Score for
100.0
0.0
100.0
50.0
0.0
for a win
for anything else
for a loss
Score for
Score for
scoreBoard
‘X’
‘O’
Assigns a score to any board, b
A simple system:
Score for
Score for
100.0
0.0
100.0
50.0
0.0
for a win
for anything else
for a loss
Score for
Score for
50.0
50.0
scoreBoard
Assigns a score to any board, b
A simple system:
100.0
50.0
0.0
for a win
for anything else
for a loss
Implementation ideas…
scoreBoard(self, b)
How can there be no
'X' or 'O' input?
What class is
this method in?
What methods that
already exist will
come in handy?
This doesn't seem
to be looking very
far ahead !
Looking further ahead…
scoreBoard looks ahead 0 moves
0-ply
If you look one move ahead, how many
possibilities are there to consider?
A 1-ply lookahead
player will "see" an
impending victory.
to move…
1-ply
score
Looking further ahead…
scoreBoard looks ahead 0 moves
0-ply
If you look one move ahead, how many
possibilities are there to consider?
A 1-ply lookahead
player will "see" an
impending victory.
to move…
1-ply
-1 50 50 50 100 50 50
score
Looking further ahead…
scoreBoard looks ahead 0 moves
0-ply
If you look one move ahead, how many
possibilities are there to consider?
A 2-ply lookahead
player will also "see"
an opponent's
impending victory.
to move…
2-ply
-1 0 0 0 50 0 0
score
Looking further ahead…
scoreBoard looks ahead 0 moves
0-ply
If you look one move ahead, how many
possibilities are there to consider?
1-ply
2-ply
scoresFor( self, b ) returns a LIST of scores,
one for each column you can choose to move next…
Example 1-ply and 2-ply lookahead scores
|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
|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
-1
100
50
100
50
100
50
Which change at 2-ply?
Example 1-ply and 2-ply lookahead scores
|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
-1
100
50
100
50
100
50
col 0
col 1
col 2
col 3
col 4
col 5
col 6
-1
100
0
100
0
100
50
1-ply
2-ply
Example 1-ply and 2-ply lookahead scores
| | | | | | |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 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
Example 1-ply and 2-ply lookahead scores
| | | | | | |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 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
100
0
0
0
50
0
-1
Practice
b
‘X’
‘O’
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
col 0
col 1
col 2
col 3
col 4
col 5
col 6
0-ply scores for O:
1-ply scores for O:
2-ply scores for O:
3-ply scores for O:
Solutions
0-ply scores for O:
1-ply scores for O:
2-ply scores for O:
3-ply scores for O:
b
‘X’
‘O’
col 0
col 1
col 2
col 3
col 4
col 5
col 6
-1
50
50
50
50
50
50
col 0
col 1
col 2
col 3
col 4
col 5
col 6
-1
50
50
100
50
50
50
col 0
col 1
col 2
col 3
col 4
col 5
col 6
-1
0
0
100
0
0
50
col 0
col 1
col 2
col 3
col 4
col 5
col 6
-1
0
0
100
0
0
100
© Copyright 2026 Paperzz