Skip to content

Commit

Permalink
Add encapsulation refactoring example with multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
ariannedee committed May 12, 2020
1 parent 937cf26 commit 8be5279
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 30 deletions.
60 changes: 30 additions & 30 deletions Refactoring/refactoring_3_abstraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
There are 2 players.
Each player takes turn rolling a die and moving that number of spaces.
The first person to space 100 wins.
Object-oriented with a Player object
"""
import random


class Player(object):
def __init__(self, player_num):
class Player:
def __init__(self, player_number):
self.score = 0
self.player_num = player_num
self.player_number = player_number

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

def make_move(self):
dice = random.randint(1, 6)
self.score += dice
print(f'{self} roll a {dice} (score: {self.score})')
self.score += self.roll_die()
print(f'{self}: {self.score}')

def met_goal(self):
@property
def has_won(self):
return self.score >= 100

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


players = []
for i in range(1, 3):
players.append(Player(i))

print('Start game!')
while True:
for player in players:
player.make_move() # Care about "what", not "how"
if player.met_goal():
print(f'{player} won!')
break
else:
continue
break

print()
for player in players:
print(f'{player} score: {player.score}')
return f'Player {self.player_number}'


def play_game(num_players=2):
players = []
for i in range(num_players):
players.append(Player(i + 1))

while True:
for player in players:
player.make_move()
if player.has_won:
print(f'{player} wins!')
return


if __name__ == '__main__':
play_game(num_players=3)
18 changes: 18 additions & 0 deletions Refactoring/refactoring_4_encapsulation/game.py
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
12 changes: 12 additions & 0 deletions Refactoring/refactoring_4_encapsulation/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

if __name__ == '__main__':
game = Game(num_players=4, max_die_number=20)
winner = game.play_game()
print(f'Congratulations {winner}')
29 changes: 29 additions & 0 deletions Refactoring/refactoring_4_encapsulation/player.py
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

0 comments on commit 8be5279

Please sign in to comment.