-
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.
Add encapsulation refactoring example with multiple files
- Loading branch information
1 parent
937cf26
commit 8be5279
Showing
4 changed files
with
89 additions
and
30 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
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,18 @@ | ||
from player import 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 _ in range(num_players): | ||
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 | ||
|
||
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,29 @@ | ||
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}' | ||
|
||
|
||
if __name__ == '__main__': | ||
dummy_player = Player() | ||
for i in range(1000): | ||
die = dummy_player._roll_die(6) | ||
assert 1 <= die <= 6 |