Fall 2011 Computer Programming I

= Course Description = An introduction to programming and problem solving. Topics include data types, variables, assignment, control structures (selection and iteration), structured data types, functions, and good program design.

Notes and lectures are based on Python Programming: An introduction to Programming by John Zelle

= Facebook Group = http://www.facebook.com/groups/151448744941448/

= Labs =
 * [[Media:Lab_01.pdf|Lab 01]]
 * [[Media:Lab_02.pdf|Lab 02]]
 * [[Media:Lab_03.pdf|Lab 03]]
 * [[Media:Lab_04.pdf|Lab 04]]
 * [[Media:Lab_05.pdf|Lab 05]]
 * [[Media:Lab_06.pdf|Lab 06]]
 * [[Media:Lab_07.pdf|Lab 07]]
 * [[Media:Lab_08.pdf|Lab 08]]
 * [[Media:Lab_09.pdf|Lab 09]]
 * [[Media:Lab_10.pdf|Lab 10]]
 * [[Media:Lab_11.pdf|Lab 11]]

= Assignment Policy = [[Media:Policy.pdf|Policy]]

= Tentative Schedule =

Week 1
What is Python?

[http://prezi.com/npyiiiuzacqe/csci-220-welcome-to-programming-i/?auth_key=453e1f085953958bafa67798bdc877f99b0a57a6 Chapter 1. Computers and Programs]

Dates: Aug 24, 26

Assignment: [[Media:Fall_2011_CSCI_220_Assignment_1.pdf|Programming Assignment #1]]

our_first_program.py

 * 1) Created a function to say hello to someone

def hello(name): print("Hello",name)
 * 1) def - syntax to define a function
 * 2) hello is the name of the function
 * 3) name is the parameter that I'm passing the function. It will have a value when I call the function.

Week 2
Your first programs and IDE

Chapter 2. Writing Simple Programs

Solution to in class for loop problems:
 * 1) Solutions to in class for loop hand out

print("Number 1.") for i in [1, 4, 9, 16]: print (i)
 * 1.

print("Number 2.") for i in range(5): print (i)
 * 2.

print("Number 3.") sum = 0 for i in range(3): sum = sum + i print(sum)
 * 3.

print("Number 4.") product = 1 for i in range(3): product = product * i   print (product)
 * 4.

print("Number 5.") product = 1 for i in range(3): product = product * (i+1) print(product)
 * 5.

print("Number 6.") print("Solution 1") for i in range(4): square = (i+1) ** 2 print(square) print("Solution 2") for i in range(4): square = (i+1) * (i+1) print(square)
 * 6.

loop = eval(input("How many values would you like to average? ")) sum = 0 for i in range(loop): num = eval(input("Enter a number: ")) sum = sum + num average = sum / loop print(average)
 * 7.

Dates: Aug 29, 31, Sept 2

Assignment: [[Media:Prog02_CDTime.pdf|Programming Assignment #2]]

Week 3
Data types

Chapter 3. Computing with Numbers

Chapter 5. Sequences: Strings, Lists, and Files

Dates: Sept 5, 7, 9

Assignment: [[Media:Prog03.pdf|Assignment #3]]

Videos
Lecture 1 (download)

Lecture 2 (download)

Lecture 3 (download)

Week 4
Strings and Lists

Chapter 5. Sequences: Strings, Lists, and Files

Dates: Sept 12, 14, 16

Assignment: TBA

Videos
Lecture 1 (download)

Lecture 2 (download)

Lecture 3 (download)

Practice Exam I
[[Media:Practice_Exam.pdf|Practice Exam I]]

[[Media:Practice_Exam_Solution.PDF|Practice Exam I with Solutions]]

Week 5
Introduction to graphics and objects

Chapter 4. Objects and Graphics

graphics.py

Dates: Sept 19, 21, 23

Assignment: TBA

Videos
Lecture 1 (download)

Lecture 3 (download)

Week 6
Introduction to graphics and objects

Chapter 4. Objects and Graphics

Dates: Sept 26, 28, 30

Exam #1 will be on Monday September 26th

Assignment: [[Media:Assignment4.pdf|Assignment 4]] (Due Friday, Oct. 7)

Videos
Lecture 1 (download)

Week 7
Threading example using the graphics package

File I/O

File I/O from Chapter 5. Sequences: Strings, Lists, and Files

Dates: Oct 3, 5, 7

Assignment: [[Media:Assignment_5.pdf|Assignment 5]] [[Media:example.txt|example.txt]] (Due Friday, Oct 14 by 5 PM) [[Media:test.txt|test.txt]]

Videos
Lecture 1 (download)

Lecture 2 (download)

Week 8
Functions

Chapter 6. Defining Functions

Dates: Oct 10, 12, 14

Assignment: None

Videos
Lecture 1 (download)

Week 9
How do computers make decisions?

Chapter 7. Decision Structures

Dates: Holiday, Oct 19, 21

Assignment: [[Media:Prog06.pdf|Assignment 6]] - Due Friday Oct. 28 by 5 PM.

Videos
Lecture 1 (download)

Lecture 2 (download)

Week 10
[[Media:Fall_2011_CSCI_220_Function_Conditionals.pdf|Function and Conditionals Worksheet]] [ [[Media:Fall_2011_CSCI_220_Function_Conditionals_Solutions.pdf|Solutions]] ]

[[Media:Fall_2011_CSCI_220_Function_Conditionals_Continued.pdf|Function and Conditionals Worksheet Continued]] [ Solutions ]

Loops

Chapter 8. Loop Structures and Booleans

Dates: Oct 24, 26, 28

Assignment: [[Media:Fall_2011_CSCI_220_HW_7.pdf|HW_7.pdf]] - Due on Friday, November 4th by 5 PM.

Videos
Lecture 1 (download)

Lecture 2 (download)

Week 11
[[Media:Fall_2011_CSCI_220_Practice_Exam_II.pdf|Practice Exam II]]

[[Media:Fall_2011_CSCI_220_Practice_Exam_II_Solutions.pdf|Practice Exam II Solutions]]

More work on indefinite loops and conditionals.

[[Media:Fall_2011_CSCI_220_Indefinite_Worksheet.pdf|Indefinite Worksheet]]

[[Media:Fall_2011_CSCI_220_Indefinite_Worksheet_Solutions.pdf|Indefinite Worksheet Solutions]]

checkMouse example: from graphics import *

win = GraphWin("Jump Jack",300,300) win.setCoords(0,0,100,100)

text = Text(Point(25,90),"Start") text.draw(win) text = Text(Point(75,90),"Stop") text.draw(win) rect = Rectangle(Point(15,95),Point(35,85)) rect.draw(win) rect = Rectangle(Point(65,95),Point(85,85)) rect.draw(win)

while True: p = win.getMouse x = p.getX y = p.getY if x >= 15 and x <= 35 and y >= 85 and y <= 95: # Check to see if start clicked break
 * 1) Check for start

i = 0 text = Text(Point(50,50),str(i)) text.draw(win) while True: p = win.checkMouse if p != None: x = p.getX y = p.getY if x >= 65 and x <= 85 and y >= 85 and y <= 95: # Check to see if start clicked break i = i + 1 text.setText(str(i)) win.getMouse win.close
 * 1) Check for stop will updating something on screen

Tic Tac Toe: from graphics import *

def draw_board(w): line = Line(Point(0,30),Point(90,30)) line.draw(w) line = Line(Point(0,60),Point(90,60)) line.draw(w) line = Line(Point(30,0),Point(30,90)) line.draw(w) line = Line(Point(60,0),Point(60,90)) line.draw(w)

def check_for_winner(board): if board[0][0] == board[0][1] and board[0][1] == board[0][2]: return board[0][0] if board[1][0] == board[1][1] and board[1][1] == board[1][2]: return board[1][0] if board[2][0] == board[2][1] and board[2][1] == board[2][2]: return board[2][0] if board[0][0] == board[1][0] and board[1][0] == board[2][0]: return board[0][0] if board[0][1] == board[1][1] and board[1][1] == board[2][1]: return board[0][1] if board[0][2] == board[1][2] and board[1][2] == board[2][2]: return board[0][2] if board[0][0] == board[1][1] and board[1][1] == board[2][2]: return board[0][0] if board[2][0] == board[1][1] and board[1][1] == board[0][2]: return board[2][0] for row in board: for square in row: if square == None: return None return "no one"

def execute_move(board,turn,win): p = win.getMouse row = int(p.getY//30) col = int(p.getX//30) if board[row][col] == None: board[row][col] = turn text = Text(Point(col*30+15,row*30+15),turn) text.draw(win) return True return False

win = GraphWin("Tic Tac Toe",300,300)

win.setCoords(0,0,90,90)

draw_board(win) board = None,None,None],[None,None,None],[None,None,None

turn = "X" while check_for_winner(board) == None: # Execute a turn of the game if execute_move(board,turn,win): if turn == "X": turn = "O" else: turn = "X"
 * 1) Play the game

print("The winner is",check_for_winner(board))

win.getMouse win.close

Dates: Oct 31, Nov 2, 4

Videos
Lecture 1 (download)

Week 12
Exam #2 Monday November 7th

Classes

Chapter 10. Classes

Dates: Nov 7, 9, 11

Assignment: [[Media:Fall_2011_CSCI_220_Assignment_8.pdf|Assignment 8]] - Due Monday, November 14 by 5 PM

Videos
Lecture 1 (download)

Lecture 2 (download)

Week 13
Chapter 10. Classes

Searching algorithms

Chapter 13. Algorithm Design and Recursion

Dates: Nov 14, 16, 18

Assignment: [[Media:Fall_2011_CSCI_220_Assignment9.pdf|Assignment 9 - Due Monday, November 21 by 5 PM]]

Videos
Lecture 1 (download)

Lecture 2 (download)

Lecture 3 (download)

Week 14
Data collections

Chapter 11. Data Collections

Searching and Sorting algorithms

Chapter 13. Algorithm Design and Recursion

Dates: Nov 21, Holiday

Assignment: [[Media:Fall_2011_CSCI_220_Assignment_10.pdf|Assignment 10.pdf]] - Due November 28th by 5 PM

uno_graphics.py from random import randint from graphics import * from string import ascii_letters

CARD_WIDTH = 10 CARD_HEIGHT = 15

USERHOME = str(os.getenv('HOME')) if USERHOME == "None": USERHOME = os.getenv('USERPROFILE') UNO_GAMES_DIR = USERHOME+"/Dropbox/UNO_Games/"

def decode(input, shift=3): return modify_input(input, -shift)

def encode(input, shift=3): return modify_input(input, shift)

def modify_input(input, shift): trans = str.maketrans(ascii_letters, ascii_letters[shift:] + ascii_letters[:shift]) return input.translate(trans)

def create_card(card_str): if card_str.find("wild") != -1: if card_str == "wild": card = WildCard elif card_str == "wild draw 4": card = WildCard card.is_draw_4 = True elif card_str.find("reverse") != -1: color = card_str.split(" ")[0] card = ReverseCard(color) elif card_str.find("skip") != -1: color = card_str.split(" ")[0] card = SkipCard(color) elif card_str.find("draw 2") != -1: color = card_str.split(" ")[0] card = Draw2Card(color) else: color = card_str.split(" ")[0] number = int(card_str.split(" ")[1]) card = StandardCard(color,number) return card
 * 1) Creates a card from a string (e.g., green 1)

class StandardCard: def __init__(self,c,n): self.color = c       self.number = n        self.drawn = False

def draw(self,win): if self.drawn: return self.rect = Rectangle(Point(0,0),Point(CARD_WIDTH,CARD_HEIGHT)) self.rect.draw(win) self.rect.setFill(self.color) self.text = Text(Point(CARD_WIDTH/2,CARD_HEIGHT/2),self.number) self.text.draw(win) self.drawn = True

def undraw(self): self.rect.undraw self.text.undraw

def move_to(self,x,y): center = self.rect.getCenter dx = x - center.getX dy = y - center.getY self.move(dx,dy)

def move(self,dx,dy): self.text.move(dx,dy) self.rect.move(dx,dy)

def is_clicked(self,p): center = self.rect.getCenter x1 = center.getX - CARD_WIDTH/2 x2 = center.getX + CARD_WIDTH/2 y1 = center.getY - CARD_HEIGHT/2 y2 = center.getY + CARD_HEIGHT/2 x = p.getX y = p.getY if x >= x1 and x <= x2 and y >= y1 and y <= y2: return True else: return False

def as_string(self): return self.color + " " + str(self.number)

class WildCard: def __init__(self): self.is_draw_4 = False self.color = "black" self.number = -1 self.drawn = False

def draw(self,win): if self.drawn: return self.rect = Rectangle(Point(0,0),Point(CARD_WIDTH,CARD_HEIGHT)) self.rect.draw(win) self.rect.setFill(self.color) self.text = Text(Point(CARD_WIDTH/2,CARD_HEIGHT/2),self.number) self.text.draw(win) self.drawn = True

def undraw(self): self.rect.undraw self.text.undraw

def move_to(self,x,y): center = self.rect.getCenter dx = x - center.getX dy = y - center.getY self.move(dx,dy)

def move(self,dx,dy): self.text.move(dx,dy) self.rect.move(dx,dy)

def is_clicked(self,p): center = self.rect.getCenter x1 = center.getX - CARD_WIDTH/2 x2 = center.getX + CARD_WIDTH/2 y1 = center.getY - CARD_HEIGHT/2 y2 = center.getY + CARD_HEIGHT/2 x = p.getX y = p.getY if x >= x1 and x <= x2 and y >= y1 and y <= y2: return True else: return False

def as_string(self): if self.is_draw_4 == False: return "wild" else: return "wild draw 4"

class ReverseCard: def __init__(self,c): self.color = c       self.number = -1 self.drawn = False

def draw(self,win): if self.drawn: return self.rect = Rectangle(Point(0,0),Point(CARD_WIDTH,CARD_HEIGHT)) self.rect.draw(win) self.rect.setFill(self.color) self.text = Text(Point(CARD_WIDTH/2,CARD_HEIGHT/2),self.number) self.text.draw(win) self.drawn = True

def undraw(self): self.rect.undraw self.text.undraw

def move_to(self,x,y): center = self.rect.getCenter dx = x - center.getX dy = y - center.getY self.move(dx,dy)

def move(self,dx,dy): self.text.move(dx,dy) self.rect.move(dx,dy)

def is_clicked(self,p): center = self.rect.getCenter x1 = center.getX - CARD_WIDTH/2 x2 = center.getX + CARD_WIDTH/2 y1 = center.getY - CARD_HEIGHT/2 y2 = center.getY + CARD_HEIGHT/2 x = p.getX y = p.getY if x >= x1 and x <= x2 and y >= y1 and y <= y2: return True else: return False

def as_string(self): return self.color + " reverse" class SkipCard: def __init__(self,c): self.color = c       self.number = -1 self.drawn = False

def draw(self,win): if self.drawn: return self.rect = Rectangle(Point(0,0),Point(CARD_WIDTH,CARD_HEIGHT)) self.rect.draw(win) self.rect.setFill(self.color) self.text = Text(Point(CARD_WIDTH/2,CARD_HEIGHT/2),self.number) self.text.draw(win) self.drawn = True

def undraw(self): self.rect.undraw self.text.undraw

def move_to(self,x,y): center = self.rect.getCenter dx = x - center.getX dy = y - center.getY self.move(dx,dy)

def move(self,dx,dy): self.text.move(dx,dy) self.rect.move(dx,dy)

def is_clicked(self,p): center = self.rect.getCenter x1 = center.getX - CARD_WIDTH/2 x2 = center.getX + CARD_WIDTH/2 y1 = center.getY - CARD_HEIGHT/2 y2 = center.getY + CARD_HEIGHT/2 x = p.getX y = p.getY if x >= x1 and x <= x2 and y >= y1 and y <= y2: return True else: return False

def as_string(self): return self.color + " skip"

class Draw2Card: def __init__(self,c): self.color = c       self.number = -1 self.drawn = False

def draw(self,win): if self.drawn: return self.rect = Rectangle(Point(0,0),Point(CARD_WIDTH,CARD_HEIGHT)) self.rect.draw(win) self.rect.setFill(self.color) self.text = Text(Point(CARD_WIDTH/2,CARD_HEIGHT/2),self.number) self.text.draw(win) self.drawn = True

def undraw(self): self.rect.undraw self.text.undraw

def move_to(self,x,y): center = self.rect.getCenter dx = x - center.getX dy = y - center.getY self.move(dx,dy)

def move(self,dx,dy): self.text.move(dx,dy) self.rect.move(dx,dy)

def is_clicked(self,p): center = self.rect.getCenter x1 = center.getX - CARD_WIDTH/2 x2 = center.getX + CARD_WIDTH/2 y1 = center.getY - CARD_HEIGHT/2 y2 = center.getY + CARD_HEIGHT/2 x = p.getX y = p.getY if x >= x1 and x <= x2 and y >= y1 and y <= y2: return True else: return False

def as_string(self): return self.color + " draw 2" class Player: def __init__(self,name): self.name = name self.cards = []

def play_a_card(self,color,number): # Exhaustive search through the cards for i in range(len(self.cards)): if (self.cards[i].as_string.find(color) >= 0 and               self.cards[i].as_string.find(str(number)) >= 0 and                self.cards[i].as_string.find("Draw") == -1):

card = self.cards[i] self.cards.remove return card return None

def can_play_card(self,discard_pile): discard_color = discard_pile[-1].color discard_number = discard_pile[-1].number for card in self.cards: if card.color == discard_color or card.number == discard_number: return True return False

def as_string(self): card_strs = [] for card in self.cards: card_strs.append(card.as_string) return self.name + ":" + "\t".join(card_strs)

class Game: # Starts a new game with player 'name' # Creates three computer players # Creates a board object that has a random game id   # Create a blank board # Initialize a new board # Deal the cards def create_new_game(self,name): self.p1 = Player(name) self.p2 = Player("Computer 1") self.p3 = Player("Computer 2") self.p4 = Player("Computer 3")

self.board = Board(randint(1,100000)) self.board.create_blank_board self.board.initialize_new_board self.deal

def start(self,player): if player == self.p1: self.board.set_p1(self.p1) self.board.set_p2(self.p2) self.board.set_p3(self.p3) self.board.set_p4(self.p4) elif player == self.p2: self.board.set_p1(self.p2) self.board.set_p2(self.p3) self.board.set_p3(self.p4) self.board.set_p4(self.p1) elif player == self.p3: self.board.set_p1(self.p3) self.board.set_p2(self.p4) self.board.set_p3(self.p1) self.board.set_p4(self.p2) elif player == self.p4: self.board.set_p1(self.p4) self.board.set_p2(self.p1) self.board.set_p3(self.p2) self.board.set_p4(self.p3)

self.save_game game_over = "" while game_over != None: game_over = self.board.wait_for_click if game_over == "Card Played": self.save_game elif game_over == "Reload Button Clicked": return game_over return None

def save_game(self): outfile = open(UNO_GAMES_DIR+str(self.board.game_id),"w") print(encode(self.as_string),file=outfile,end="") outfile.close def load_game(self,game_id): infile = open(UNO_GAMES_DIR+str(game_id),"r") # Load the players lines = decode(infile.read).split("\n") infile.close fields = lines[-1].split(":") self.p4 = Player(fields[0]) for card_str in fields[1].split("\t"): self.p4.cards.append(create_card(card_str)) fields = lines[-2].split(":") self.p3 = Player(fields[0]) for card_str in fields[1].split("\t"): self.p3.cards.append(create_card(card_str))

fields = lines[-3].split(":") self.p2 = Player(fields[0]) for card_str in fields[1].split("\t"): self.p2.cards.append(create_card(card_str))

fields = lines[-4].split(":") self.p1 = Player(fields[0]) for card_str in fields[1].split("\t"): self.p1.cards.append(create_card(card_str))

self.board = Board(game_id) self.board.load_old_board

def deal_one_card(self,p): if len(self.board.deck) == 0: return None i = randint(0,len(self.board.deck)-1) card = self.board.deck[i] p.cards.append(card) self.board.deck.remove(self.board.deck[i]) return card

def deal(self): for i in range(7): self.deal_one_card(self.p1) self.deal_one_card(self.p2) self.deal_one_card(self.p3) self.deal_one_card(self.p4)

def check_card(self,card_to_play): discard_card = self.board.discard_piles[-1] if discard_card.color == card_to_play.color or discard_card.number == card_to_play.number: return True return False

def as_string(self): results = self.board.as_string results += "\n" + self.p1.as_string results += "\n" + self.p2.as_string results += "\n" + self.p3.as_string results += "\n" + self.p4.as_string return results

class MsgBox: def __init__(self,msg): win = GraphWin("",600,100) win.setCoords(0,0,100,100) Text(Point(50,80),msg).draw(win) button = Button(50,40,"OK",win,10,20) while True: p = win.getMouse if button.is_clicked(p): break win.close

class Button: def __init__(self,x,y,string,win,width,height): self.width = width self.height = height self.rect = Rectangle(Point(x-self.width/2,y-height/2),Point(x+self.width/2,y+height/2)) self.rect.draw(win) self.text = Text(Point(x,y),string) self.text.draw(win) self.rect.setFill('gray') def is_clicked(self,p): center = self.rect.getCenter x1 = center.getX - self.width/2 x2 = center.getX + self.width/2 y1 = center.getY - self.height/2 y2 = center.getY + self.height/2 x = p.getX y = p.getY if x >= x1 and x <= x2 and y >= y1 and y <= y2: return True else: return False

class Board: def __init__(self,game_id): self.game_id = game_id
 * 1) Board class

# Creates the graphics objects of a blank board def create_blank_board(self): self.win = GraphWin("UNO",600,600) self.win.setCoords(0,0,100,100) self.win.setBackground(color_rgb(100, 255, 100)) self.text_p1 = Text(Point(50,10),'Player 1: 0') self.text_p1.draw(self.win) self.text_p2 = Text(Point(10,50),'Player 2: 0') self.text_p2.draw(self.win) self.text_p3 = Text(Point(50,90),'Player 3: 0') self.text_p3.draw(self.win) self.text_p4 = Text(Point(90,50),'Player 4: 0') self.text_p4.draw(self.win)

self.rect_discard_pile = Rectangle(Point(80,80),Point(80+CARD_WIDTH,80+CARD_HEIGHT)) self.rect_discard_pile.draw(self.win) self.rect_discard_pile.setFill('gray') self.text_discard_pile = Text(Point(80+CARD_WIDTH/2,80+CARD_HEIGHT/2),"Discard") self.text_discard_pile.draw(self.win)

self.quit_button = Button(10,90,"Quit",self.win,8,4) self.reload_button = Button(10,80,"Reload",self.win,12,4)

self.text_game_id = Text(Point(20,90),str(self.game_id)) self.text_game_id.draw(self.win)

def initialize_new_board(self): self.create_deck

self.create_discard_pile self.discard_pile[-1].draw(self.win) self.discard_pile[-1].move_to(50,50) self.card_selected = None

def load_old_board(self): self.create_blank_board

infile = open(UNO_GAMES_DIR+str(self.game_id),"r") lines = decode(infile.read).split("\n") infile.close deck_strs = lines[2].split(":")[1].split("\t") self.deck = [] for deck_str in deck_strs: self.deck.append(create_card(deck_str))

discard_pile_strs = lines[1].split(":")[1].split("\t") self.discard_pile = [] for discard_pile_str in discard_pile_strs: self.discard_pile.append(create_card(discard_pile_str)) self.discard_pile[-1].draw(self.win) self.discard_pile[-1].move_to(50,50)

self.card_selected = None

def wait_for_click(self): p = self.win.getMouse # Check to see if card was clicked for card in self.p1.cards: if card.is_clicked(p): # Reset the card positions for i in range(len(self.p1.cards)): self.p1.cards[i].move_to(self.x_positions[i],20) card.move(0,5) self.card_selected = card return "Card Clicked"

elif self.discard_pile[-1].is_clicked(p): if self.card_selected != None: self.discard_pile[-1].undraw self.card_selected.move_to(50,50) self.p1.cards.remove(self.card_selected) self.discard_pile.append(self.card_selected) self.card_selected = None self.draw_cards return "Card Played"

return "Discard Pile Clicked" elif self.reload_button.is_clicked(p): return "Reload Button Clicked"

elif self.quit_button.is_clicked(p): self.win.close return None

return "Other Spot Clicked"

def create_deck(self): deck = []

for i in range(10): for color in ['blue','green','red','yellow']: if i == 0: card = StandardCard(color,i) deck.append(card) else: card = StandardCard(color,i) deck.append(card) card = StandardCard(color,i) deck.append(card)

# Makes the standard wild card for i in range(4): card = WildCard deck.append(card)

# Makes wild card that is a draw 4 for i in range(4): card = WildCard card.is_draw_4 = True deck.append(card)

self.deck = deck

def create_discard_pile(self): discard_pile = [] i = randint(0,len(self.deck)-1) discard_pile.append(self.deck[i]) self.deck.remove(self.deck[i]) self.discard_pile = discard_pile

def set_p1(self,p1): self.p1 = p1       self.text_p1.setText(p1.name + ": " + str(len(p1.cards))) self.draw_cards

def draw_cards(self): p1 = self.p1       self.text_p1.setText(p1.name + ": " + str(len(p1.cards))) num_cards = len(p1.cards) card_width_with_buffer = CARD_WIDTH + 2 if len(p1.cards) % 2 != 0: # Odd x_positions = list(range(50 - (card_width_with_buffer)*(num_cards-1)//2, 50 + (card_width_with_buffer+2)*(num_cards-1)//2 + card_width_with_buffer, card_width_with_buffer)) else: x_positions = list(range(50 - card_width_with_buffer*num_cards//2 + card_width_with_buffer//2, 50 + card_width_with_buffer*num_cards//2 + card_width_with_buffer//2, card_width_with_buffer)) for i in range(len(p1.cards)): card = p1.cards[i] card.draw(self.win) card.move_to(x_positions[i],20)

self.x_positions = x_positions def set_p2(self,p2): self.text_p2.setText(p2.name + ": " + str(len(p2.cards)))

def set_p3(self,p3): self.text_p3.setText(p3.name + ": " + str(len(p3.cards)))

def set_p4(self,p4): self.text_p4.setText(p4.name + ": " + str(len(p4.cards)))

def as_string(self): discard_pile_results = [] for card in self.discard_pile: discard_pile_results.append(card.as_string) deck_results = [] for card in self.deck: deck_results.append(card.as_string) return str(self.game_id) + "\nDiscard pile:" + "\t".join(discard_pile_results) + "\n" + "Deck:" + "\t".join(deck_results)

def main: if not os.path.exists(UNO_GAMES_DIR): MsgBox("Directory " + UNO_GAMES_DIR + " does not exist") return # Log into the game, continue a game, or exit win = GraphWin("UNO",300,300) win.setCoords(0,0,100,100) start_new_game_button = Button(50,70,"Start New Game",win,40,10) continue_game_button = Button(50,30,"Continue/Join Game",win,60,10) Text(Point(30,90),"Name: ").draw(win) name_entry = Entry(Point(60,90),10) name_entry.draw(win) quit_button = Button(50,10,"Quit",win,20,10) Text(Point(30,40),"Game ID: ").draw(win) game_id_entry = Entry(Point(60,40),10) game_id_entry.draw(win) game_id = None # Loop until the user clicks on one of the buttons while True: p = win.getMouse if start_new_game_button.is_clicked(p): # The user wants to start a new game name = name_entry.getText # Get the name of the player break elif continue_game_button.is_clicked(p): # The user wants to continue/join a game name = name_entry.getText # Get name game_id = eval(game_id_entry.getText) # Get ID           break elif quit_button.is_clicked(p): win.close return
 * 1) Main loop of the program

win.close

# Play the game game = Game player = None if game_id == None: # Start a new game game.create_new_game(name) player = game.p1   else: # Continue/join a new game # Load a previous game game.load_game(game_id)

# Check to see if the player has already joined the game if game.p1.name == name: player = game.p1       elif game.p2.name == name: player = game.p2       elif game.p3.name == name: player = game.p3       elif game.p4.name == name: player = game.p4       else: # Check to see if there is a seat, because the player has not joined yet if game.p1.name.find("Computer") != -1: player = game.p1           elif game.p2.name.find("Computer") != -1: player = game.p2           elif game.p3.name.find("Computer") != -1: player = game.p3           elif game.p4.name.find("Computer") != -1: player = game.p4           else: # No more room in the game MsgBox("No more room in the game") player.name = name

# Start/join the game while game.start(player) != None: # A reload command was issued, so let's reload

# Save the game ID       game_id = game.board.game_id # Close the game board game.board.win.close # Load the game from the hard drive game.load_game(game_id)

# Locate the player if player.name == game.p1.name: player = game.p1       elif player.name == game.p2.name: player = game.p2       elif player.name == game.p3.name: player = game.p3       elif player.name == game.p4.name: player = game.p4 main

Week 15
Sorting algorithms

Chapter 13. Algorithm Design and Recursion

Dates: Nov 28, 30, Dec 2 - Due BEFORE class on Monday

Assignment: [[Media:Fall_2011_CSCI_220_Assignment_11.pdf|Assignment 11]]

Videos
Github (download)

Lecture 2 (download)

Week 16
Review and Final Exam

[[Media:Fall_2011_CSCI_220_Practice_Final_Exam.pdf|Practice Final Exam]]

Dates: Final is 12 - 3 PM on December 7th.