-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update code for class on May 12, 2020
- Loading branch information
1 parent
8be5279
commit 19440c4
Showing
10 changed files
with
92 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)' |