Skip to content

Commit 9cc779a

Browse files
committed
Merge branch 'view-separation'
2 parents 1675b65 + a393c14 commit 9cc779a

32 files changed

+505
-458
lines changed

rummy/controller/__init__.py

Whitespace-only changes.

rummy/controller/ai_controller.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from random import choice
2+
3+
from rummy.player.player import Player
4+
from rummy.ui.view import View
5+
6+
7+
class AiController:
8+
9+
@staticmethod
10+
def show_start_turn(player: Player):
11+
output = ''
12+
if player.ai_only:
13+
output += View.template_turn_start(player)
14+
else:
15+
output += View.template_ai_turn_start(player)
16+
output += View.template_ai_thought(player, 'Choosing pick up')
17+
View.render(output)
18+
19+
@staticmethod
20+
def show_end_turn(player: Player):
21+
output = ''
22+
if player.ai_only:
23+
output += View.template_ai_turn_end(player)
24+
output += View.template_ai_thought(player, 'Choosing card to discard')
25+
View.render(output)
26+
27+
@staticmethod
28+
def show_knocked(player):
29+
if player.has_someone_knocked():
30+
View.render(View.prepare_template('/knocked.txt'))
31+
32+
@staticmethod
33+
def show_discard(player: Player):
34+
output = ''
35+
if player.ai_only:
36+
output += View.template_ai_hand_data(player.hand.get_score())
37+
output += View.template_player_discarded(player.round.deck.inspect_discard())
38+
View.render(output)
39+
40+
@classmethod
41+
def draw_card(cls, player):
42+
output = ''
43+
if player.round.deck.has_discard():
44+
current_score = player.hand.get_score()
45+
scores = player.melds.find_discard_scores(player.hand.get_hand(), player.round.deck.inspect_discard())
46+
if player.ai_only:
47+
output += View.template_ai_discard_data(current_score, scores)
48+
output += cls._choose_pickup(player, current_score, scores)
49+
else:
50+
player.take_from_deck()
51+
output += View.template_ai_thought(player, 'Drawing from deck')
52+
View.render(output)
53+
54+
@staticmethod
55+
def _choose_pickup(player, current_score, scores):
56+
output = ''
57+
if min(scores) < current_score - 4 or min(scores) <= 10:
58+
player.take_from_discard()
59+
output += View.template_ai_thought(player, 'Drawing from discard')
60+
else:
61+
player.take_from_deck()
62+
output += View.template_ai_thought(player, 'Drawing from deck')
63+
return output
64+
65+
@classmethod
66+
def discard_or_knock(cls, player):
67+
scores = player.melds.find_discard_scores(player.hand.get_hand())
68+
score = min(scores)
69+
if score <= 10 and not player.round.knocked:
70+
player.round.knocked = True
71+
discard = cls._choose_discard(player, score, scores)
72+
player.round.deck.discard_card(discard)
73+
74+
@staticmethod
75+
def _choose_discard(player, score, scores):
76+
if scores.count(score) > 1:
77+
choices = [(i, x) for (i, x) in enumerate(scores) if (x == score)]
78+
discard = choice(choices)[0]
79+
else:
80+
discard = scores.index(score)
81+
return player.hand.discard_card(discard)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from rummy.player.human import Human
2+
from rummy.player.player import Player
3+
from rummy.ui.player_action_dialog import PlayerActionDialog
4+
from rummy.ui.user_input import UserInput
5+
from rummy.ui.view import View
6+
7+
8+
class HumanController:
9+
10+
@staticmethod
11+
def show_start_turn(player: Player):
12+
if isinstance(player, Human):
13+
View.render(View.template_turn_start(player))
14+
15+
@staticmethod
16+
def show_end_turn(player: Player):
17+
if isinstance(player, Human):
18+
View.render(View.template_player_turn_end(player))
19+
20+
@staticmethod
21+
def show_knocked(player):
22+
if player.has_someone_knocked():
23+
View.render(View.prepare_template('/knocked.txt'))
24+
25+
@staticmethod
26+
def show_discard(player: Player):
27+
if isinstance(player, Human):
28+
View.render(View.template_player_discarded(player.round.deck.inspect_discard()))
29+
30+
@classmethod
31+
def draw_card(cls, player):
32+
if player.round.deck.has_discard():
33+
View.render(cls._choose_pick_up(player))
34+
else:
35+
player.take_from_deck()
36+
View.render('Drew from deck')
37+
38+
@staticmethod
39+
def _choose_pick_up(player):
40+
user_input = UserInput.create_input(PlayerActionDialog.pick_up_or_draw())
41+
if user_input == 'p':
42+
player.take_from_discard()
43+
return 'Drawing from discard'
44+
else:
45+
player.take_from_deck()
46+
return 'Drawing from deck'
47+
48+
@classmethod
49+
def discard_or_knock(cls, player):
50+
player.discard(cls._choose_discard(player))
51+
52+
@staticmethod
53+
def _choose_discard(player):
54+
user_input = ''
55+
scores = player.melds.find_discard_scores(player.hand.get_hand())
56+
while user_input not in [str(i) for i in range(1, 9)]:
57+
if min(scores) <= 10 and not player.round.show_knocked:
58+
user_input = UserInput.create_input(PlayerActionDialog.choose_discard_or_knock())
59+
if user_input == "k":
60+
player.knock()
61+
continue
62+
else:
63+
user_input = UserInput.create_input(PlayerActionDialog.choose_discard())
64+
return user_input
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from abc import abstractmethod, ABCMeta
2+
3+
from rummy.player.player import Player
4+
from rummy.ui.view import View
5+
6+
7+
class PlayerController(metaclass=ABCMeta):
8+
9+
@staticmethod
10+
@abstractmethod
11+
def show_start_turn(player: Player):
12+
pass
13+
14+
@staticmethod
15+
@abstractmethod
16+
def show_end_turn(player: Player):
17+
pass
18+
19+
@staticmethod
20+
@abstractmethod
21+
def show_discard(player: Player):
22+
pass
23+
24+
@classmethod
25+
@abstractmethod
26+
def draw_card(cls, player):
27+
pass
28+
29+
@staticmethod
30+
@abstractmethod
31+
def _choose_pick_up(player):
32+
pass
33+
34+
@classmethod
35+
@abstractmethod
36+
def discard_or_knock(cls, player):
37+
pass
38+
39+
@staticmethod
40+
@abstractmethod
41+
def _choose_discard(player):
42+
pass
43+
44+
@staticmethod
45+
def show_knocked(player):
46+
if player.has_someone_knocked():
47+
View.render(View.prepare_template('/knocked.txt'))

rummy/game/players.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from rummy.player.ai import AI
4+
from rummy.player.human import Human
5+
from rummy.ui.menu_action_dialog import MenuActionDialog
6+
from rummy.ui.user_input import UserInput
7+
8+
9+
class Players:
10+
def __init__(self):
11+
self.players = []
12+
self.number_of_players = -1
13+
self.number_of_opponents = -1
14+
15+
def choose_players(self):
16+
self.number_of_players = int(UserInput.create_input(MenuActionDialog.human_players()))
17+
for _ in range(int(self.number_of_players)):
18+
self.players.append(Human(len(self.players) + 1))
19+
20+
def choose_opponents(self):
21+
if self.number_of_players in [-1, 0, 1]:
22+
self.number_of_opponents = int(
23+
UserInput.create_input(MenuActionDialog.ai_players(self.number_of_players)))
24+
ai_only = self.is_ai_only()
25+
for _ in range(int(self.number_of_opponents)):
26+
self.players.append(AI(len(self.players) + 1, ai_only))
27+
28+
def is_ai_only(self):
29+
return True if self.number_of_players is 0 else False
30+
31+
def get_players(self):
32+
return self.players

rummy/game/score.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ansi_colours import AnsiColours as Colour
44

5-
from ui.view import View
5+
from rummy.ui.view import View
66

77

88
class Score:
@@ -28,12 +28,12 @@ def is_end_of_game(self):
2828
return True
2929
return False
3030

31-
def end_game(self):
31+
def show_winners(self):
3232
winners = self.find_lowest_scores()
3333
if len(winners) == 1:
34-
View.render(Colour.green("%s is the Winner!!" % winners[0]))
34+
return Colour.green("%s is the Winner!!" % winners[0])
3535
else:
36-
View.render(Colour.green(", ".join([str(w) for w in winners]) + " are joint winners!"))
36+
return Colour.green(", ".join([str(w) for w in winners]) + " are joint winners!")
3737

3838
def find_lowest_scores(self):
3939
lowest = []

rummy/game/setup_players.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)