0% found this document useful (0 votes)
28 views18 pages

Lec 9

cs101

Uploaded by

정재웅
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views18 pages

Lec 9

cs101

Uploaded by

정재웅
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Spring 2024

CS101 – Objects: Creation and Attributes


Lecture 9

School of Computing, KAIST


Roadmap
Last week we learned
• Files
- Reading from a file
- Writing to a file
• break and continue

This week we will learn


• Objects
- Object creation
- Object attributes

CS101 Copyright (c) School of Computing, KAIST 2


Blackjack
There are 52 cards.
Each card has a suit and a face.
The suits
• Spades
• Hearts
• Diamonds
• Clubs

The faces
• 2
• 3
• ...
• 10
• Jack
• Queen
• King
• Ace
https://commons.wikimedia.org/wiki/File:English_pattern_playing_cards_deck.svg

CS101 Copyright (c) School of Computing, KAIST 3


Blackjack

• The value of a card is the number for a number card, 11


for an Ace, and 10 for Jack, Queen, and King.

• We can represent cards as a tuple (face, suit, value).

• If card is a card, then card[0] is the face, card[1] is the suit, and
card[2] is the value.

• Blackjack rules
• The player receives 2 cards and decides to get one more card or not.
• The player wins, if the sum of the values of his/her cards is closer to 21
than the dealer.
• The player loses, if the sum is larger than 21.

CS101 Copyright (c) School of Computing, KAIST 4


Blackjack game interface

Hand of a Score of a
player player

Hand of a Score of a
dealer dealer

Message Waiting message for


Y/N decision of a player

CS101 Copyright (c) School of Computing, KAIST 14 / 17


Blackjack game example

CS101 Copyright (c) School of Computing, KAIST 6


Cards as tuples
Computing the value of a hand:
def hand_value(hand):
total = 0
for card in hand:
total += card[2]
return total
Printing a card nicely:
def card_string(card):
article = "a "
if card[0] in [8, "Ace"]:
article = "an "
return article + str(card[0]) + " of " + card[1]
Easy to make mistakes
What does card[2] mean?
What if somebody creates a card ("Ace", "Spades", 5) ?
CS101 Copyright (c) School of Computing, KAIST 7
Classes
Making cards as objects

Let us define a new object type with attributes for face, suit, and value:
class Card(object):
"""A Blackjack card."""
pass

card = Card() Create a Card object


card.face = "Ace"
card.suit = "Spades"
Set attributes of the card
card.value = 11

Card has a user-defined type:


>>> type(card)
<class ' main .Card'>

CS101 Copyright (c) School of Computing, KAIST 8


Cards as objects
Computing the value of a hand:
def hand_value(hand):
total = 0
for card in hand:
total += card.value
return total
Printing a card nicely:
def card_string(card):
article = "a "
if card.face in [8, "Ace"]:
article = "an "
return article+str(card.face)+" of "+card.suit
Getting rid of mistakes
What does card[2] mean?
What if somebody creates a card ("Ace", "Spades", 5)?
CS101 Copyright (c) School of Computing, KAIST 9
Two or more cards

We can create many cards via Card class


card1 = Card()
card1.face = "Ace"
card1.suit = "Spades"
card1.value = 11
card2 = Card()
card2.face = 2
card2.suit = "Clubs"
card2.value = 2
. . .
>>> print(card_string(card1))
an Ace of Spades
>>> print(card_string(card2))
a 2 of Clubs

CS101 Copyright (c) School of Computing, KAIST 10


Objects are mutable

There is one big difference between tuples and Card objects:


Card objects are mutable:
card = Card()
card.face = "Ace"
card.suit = "Spades"
card.value = 11
# ... AND LATER ...
card.suit = "Hearts"

CS101 Copyright (c) School of Computing, KAIST 11


Functions are objects
A function is an object:
>>> def f(x):
... return math.sin(x / 3.0 + math.pi/4.0)
>>> print(f)
<function f at 0xb7539a3c>
>>> print(type(f))
<class 'function'>

We can use a function as an argument:


def print_table(func, x0, x1, step):
x = x0
while x <= x1:
print(x, func(x))
x += step
print_table(f, -math.pi, 3 * math.pi, math.pi/8)

CS101 Copyright (c) School of Computing, KAIST 12


Journey of Chicken

An animation by Jeong-eun Yu and Geum-hyeon Song (2010 Freshmen).

Three Layer objects: hen, chick1, chick2 (all chickens).


Each chicken has body, wing, eye, and beak.
The hen also has two red dots on the head.

The hen and the chicks are exactly the same. The hen is larger and white.

CS101 Copyright (c) School of Computing, KAIST 13


Copy & Paste

The simplest method to make similar objects is to write the code once, and
copy & paste it (with the necessary modifications).

Disadvantage: When you find a bug, you have to debug all copies of the code.
It is not easy to change the appearance of all the chickens at once.

Let’s try to implement the chicken as an object:


class Chicken(object):
"""Graphic representation of a chicken."""
pass

Our chicken will have attributes layer, body, wing, eye, and beak.

CS101 Copyright (c) School of Computing, KAIST 14


Chicken objects

The function make_chicken creates a chicken object, positioned at (0, 0).


def make_chicken(hen = False):
layer = Layer()
if hen:
body = Ellipse(70,80)
body.setFillColor("white")
else:
body = Ellipse(40,50)
body.setFillColor("yellow")
body.move(0, 10)
body.setBorderColor("yellow")
body.setDepth(20)
layer.add(body)
# similar for wing, eye, beak, dots

CS101 Copyright (c) School of Computing, KAIST 15


Chicken objects

Finally we create and return the Chicken object:


def make_chicken(hen = False):
# ... see previous page

ch = Chicken()
ch.layer = layer
ch.body = body
ch.wing = wing
ch.eye = eye

# return the Chicken object


return ch

CS101 Copyright (c) School of Computing, KAIST 16


Using chickens

We use Chicken objects by accessing their attributes:

hen = make_chicken(True)
chick1 = make_chicken()
chick1.layer.move(120, 0)

herd = Layer()
herd.add(hen.layer)
herd.add(chick1.layer)
herd.move(600, 200)

chick2 = make_chicken()
chick2.layer.move(800, 200)

CS101 Copyright (c) School of Computing, KAIST 17


Questions?

CS101 Copyright (c) School of Computing, KAIST 18

You might also like