Skip to content

Commit

Permalink
Update code for class on May 12, 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
ariannedee committed May 13, 2020
1 parent 8be5279 commit 19440c4
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 158 deletions.
11 changes: 7 additions & 4 deletions Examples/example_2_using_classes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from Examples.example_5_methods import Bike, Condition

bike = Bike('Univega Alpina, orange', Condition.OKAY, sale_price=500, cost=100)
my_bike = Bike(description='Univega Alpina, orange',
condition=Condition.OKAY,
sale_price=500,
cost=100)

bike.service(spent=30, sale_price=600) # cost=$130, sale_price=$600
my_bike.service(spent=30, sale_price=600) # cost=$130, sale_price=$600

print(bike.sale_price) # 600
print(my_bike.sale_price) # 600

print(bike.sell()) # sold=True
print(my_bike.sell()) # sold=True
42 changes: 16 additions & 26 deletions Refactoring/refactoring_1_procedural.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,24 @@
There are 2 players.
Each player takes turn rolling a die and moving that number of spaces.
The first person to space 100 wins.
First pass using procedural/imperative programming
"""
import random

player_1_score = 0
player_2_score = 0

print('Start game!')

while True:
dice = random.randint(1, 6)
player_1_score += dice
print(f'Player 1 rolls a {dice} (score: {player_1_score})')

if player_1_score >= 100:
print('Player 1 wins!')
break

dice = random.randint(1, 6)
player_2_score += dice
print(f'Player 2 rolls a {dice} (score: {player_2_score})')
if __name__ == '__main__':
player_1_score = 0
player_2_score = 0

if player_2_score >= 100:
print('Player 2 wins!')
break
while True:
player_1_die = random.randint(1, 6)
player_1_score += player_1_die
print(f'Player 1 score: {player_1_score}')
if player_1_score >= 100:
print('Player 1 wins!')
break

print()
print('Final score:')
print(f'Player 1: {player_1_score}')
print(f'Player 2: {player_2_score}')
player_2_die = random.randint(1, 6)
player_2_score += player_2_die
print(f'Player 2 score: {player_2_score}')
if player_2_score >= 100:
print('Player 2 wins!')
break
59 changes: 0 additions & 59 deletions Refactoring/refactoring_4_encapsulation.py

This file was deleted.

Empty file.
4 changes: 3 additions & 1 deletion Refactoring/refactoring_4_encapsulation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
Each player takes turn rolling a die and moving that number of spaces.
The first person to space 100 wins.
"""
from game import Game
from __future__ import absolute_import

from game import Game # In PyCharm, mark parent directory as Sources Root for imports to work

if __name__ == '__main__':
game = Game(num_players=4, max_die_number=20)
Expand Down
68 changes: 0 additions & 68 deletions Refactoring/refactoring_5_inheritance.py

This file was deleted.

Empty file.
21 changes: 21 additions & 0 deletions Refactoring/refactoring_5_inheritance/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from player import LuckyPlayer, Player


class Game:
def __init__(self, num_players=2, max_die_number=6):
self.target_score = 50
self.dice_type = max_die_number
self.players = []
for i in range(num_players):
if i == 1:
self.players.append(LuckyPlayer())
else:
self.players.append(Player())

def play_game(self):
while True:
for player in self.players:
player.make_move(with_die=self.dice_type)
if player.score >= self.target_score:
print(f'{player} wins!')
return player
12 changes: 12 additions & 0 deletions Refactoring/refactoring_5_inheritance/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Simulate a simple board game.
There are 2 players.
Each player takes turn rolling a die and moving that number of spaces.
The first person to space 100 wins.
"""
from game import Game # In PyCharm, mark parent directory as Sources Root for imports to work

if __name__ == '__main__':
game = Game(num_players=4, max_die_number=20)
winner = game.play_game()
print(f'Congratulations {winner}')
33 changes: 33 additions & 0 deletions Refactoring/refactoring_5_inheritance/player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import random


class Player:
num_players = 0

def __init__(self):
Player.num_players += 1
self.score = 0
self.player_number = Player.num_players

def _roll_die(self, dice_number):
die = random.randint(1, dice_number)
print(f'{self} rolled a {die}')
return die

def make_move(self, with_die):
self.score += self._roll_die(with_die)
print(f'{self}: {self.score}')

def __str__(self):
return f'Player {self.player_number}'


class LuckyPlayer(Player):
def _roll_die(self, dice_number):
die = random.randint(dice_number//2, dice_number)
print(f'{self} rolled a {die}')
return die

def __str__(self):
player_string = super().__str__()
return f'{player_string} (lucky)'

0 comments on commit 19440c4

Please sign in to comment.