Решение на От ливадите до Лас Вегас (и назад) от Симеон Йорданов

Обратно към всички решения

Към профила на Симеон Йорданов

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 11 успешни тест(а)
  • 4 неуспешни тест(а)

Код

import random
class Card:
def __init__(self,suit,face):
self.suit=suit
self.face=face
def get_suit(self):
return self.suit
def get_face(self):
return self.face
class Deck:
suits=['clubs', 'diamonds', 'hearts', 'spades']
faces=['2','3','4','5','6','7','8','9','10','J','Q','K','A']
def __init__(self,face_filter=faces):
self.deck:list[Card]=[]
for suit in self.suits:
for face in face_filter:
self.deck.append(Card(suit,face))
def deal_card(self):
card=self.deck[0]
self.deck.pop(0)
return card
def cut(self):
cards_cut=random.randint(1,len(self.deck)-1)
for add_card in range (cards_cut):
self.deck.append(self.deck[add_card])
for remove_card in range (cards_cut):
self.deck.pop(0)
def shuffle(self):
random.shuffle(self.deck)
def get_cards(self):
return self.deck
class Player:
def __init__(self):
self.player_hand:list[Card]=[]
def get_cards(self):
return self.player_hand
def add_card(self,card):
self.player_hand.append(card)
def remove_hand(self):
self.player_hand=[]
class Game:
players:list[Player]=[]
def __init__(self,num_players,dealing_direction,dealing_instructions:tuple[int]):
for player in range (num_players):
self.players.append(Player())
self.full_deck=Deck()
self.cur_deck=Deck()
self.direction=dealing_direction
self.dealing_instructions=dealing_instructions
def get_players(self):
return self.players
def prepare_deck(self):
for player in self.players:
player.remove_hand()
self.cur_deck=self.full_deck
self.cur_deck.shuffle()
self.cur_deck.cut()
def deal(self,player):
for index_player in range (len(self.players)):
if player==self.players[index_player]:
fs_player_index=index_player
break
players_in_deal_order:list[Player]=[]
if self.dealing_instructions=='ltr':
for index in range(fs_player_index,len(self.players)):
players_in_deal_order.append(self.players[index])
for index in range(fs_player_index):
players_in_deal_order.append(self.players[index])
else:
players_in_deal_order.append(self.players[fs_player_index])
for index in range(fs_player_index):
players_in_deal_order.append(self.players[index])
for index in range(fs_player_index+1,len(self.players)):
players_in_deal_order.append(self.players[index])
for instruct in self.dealing_instructions:
for player in players_in_deal_order:
for insctruct_size in range(instruct):
player.add_card(self.cur_deck.deal_card())
def get_deck(self):
return self.cur_deck
class Belot(Game):
def __init__(self):
for player in range (4):
self.players.append(Player())
self.full_deck=Deck(['7','8','9','10','J','Q','K','A'])
self.cur_deck=self.full_deck
self.direction='ltr'
self.dealing_instructions=(2,3,3)
def prepare_deck(self):
for player in self.players:
player.remove_hand()
self.cur_deck=Deck(['7','8','9','10','J','Q','K','A'])
self.cur_deck.shuffle()
self.cur_deck.cut()
class Poker(Game):
def __init__(self):
for player in range (9):
self.players.append(Player())
self.full_deck=Deck()
self.cur_deck=self.full_deck
self.direction='rtl'
self.dealing_instructions=(1,1,1,1,1)
def prepare_deck(self):
for player in self.players:
player.remove_hand()
self.cur_deck=Deck()
self.cur_deck.shuffle()
self.cur_deck.cut()

Лог от изпълнението

........F.FF..F
======================================================================
FAIL: test_collecting_cards_before_dealing (test.TestGame)
Test collecting the cards before a new deal.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 40 != 52

======================================================================
FAIL: test_dealing_rtl (test.TestGame)
Test dealing the cards right to left.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: {('clubs', '8'), ('clubs', '9'), ('clubs', 'K')} not found in ({('clubs', 'Q'), ('clubs', '7'), ('clubs', '6')}, {('spades', '10'), ('spades', '9'), ('spades', '4')})

======================================================================
FAIL: test_players_creation (test.TestGame)
Test creation and retrieval of players.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: 7 != 2

======================================================================
FAIL: test_correct_direction_and_players_deal (test.TestPoker)
Test dealing with correct direction and players.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: {('hearts', '6'), ('clubs', '5'), ('spades', '2'), ('diamonds', '10'), ('clubs', 'A')} not found in ({('hearts', '4'), ('hearts', 'K'), ('diamonds', '8'), ('clubs', '3'), ('clubs', 'Q')}, {('spades', '4'), ('spades', 'K'), ('diamonds', '3'), ('diamonds', 'Q'), ('hearts', '8')})

----------------------------------------------------------------------
Ran 15 tests in 0.166s

FAILED (failures=4)

История (1 версия и 7 коментара)

Симеон обнови решението на 15.11.2022 17:50 (преди около 2 години)

+import random
+
+class Card:
+ def __init__(self,suit,face):
+ self.suit=suit
+ self.face=face
+
+ def get_suit(self):
+ return self.suit
+
+ def get_face(self):
+ return self.face
+
+class Deck:
+
+ suits=['clubs', 'diamonds', 'hearts', 'spades']
+ faces=['2','3','4','5','6','7','8','9','10','J','Q','K','A']
+
+ def __init__(self,face_filter=faces):
+ self.deck:list[Card]=[]
+ for suit in self.suits:
+ for face in face_filter:
+ self.deck.append(Card(suit,face))
+
+ def deal_card(self):
+ card=self.deck[0]
+ self.deck.pop(0)
+ return card
+
+ def cut(self):
+
+ cards_cut=random.randint(1,len(self.deck)-1)
+
+ for add_card in range (cards_cut):
+ self.deck.append(self.deck[add_card])
+
+ for remove_card in range (cards_cut):
+ self.deck.pop(0)
+
+ def shuffle(self):
+ random.shuffle(self.deck)
+
+ def get_cards(self):
+ return self.deck
+
+class Player:
+ def __init__(self):
+ self.player_hand:list[Card]=[]
+
+ def get_cards(self):
+ return self.player_hand
+
+ def add_card(self,card):
+ self.player_hand.append(card)
+
+ def remove_hand(self):
+ self.player_hand=[]
+
+class Game:
+ players:list[Player]=[]
+
+ def __init__(self,num_players,dealing_direction,dealing_instructions:tuple[int]):
+ for player in range (num_players):
+ self.players.append(Player())
+
+ self.full_deck=Deck()
+ self.cur_deck=Deck()
+ self.direction=dealing_direction
+ self.dealing_instructions=dealing_instructions
+
+ def get_players(self):
+ return self.players
+
+ def prepare_deck(self):
+ for player in self.players:
+ player.remove_hand()
+
+ self.cur_deck=self.full_deck
+ self.cur_deck.shuffle()
+ self.cur_deck.cut()
+
+ def deal(self,player):
+
+ for index_player in range (len(self.players)):
+ if player==self.players[index_player]:
+ fs_player_index=index_player
+ break
+
+ players_in_deal_order:list[Player]=[]
+
+ if self.dealing_instructions=='ltr':
+ for index in range(fs_player_index,len(self.players)):
+ players_in_deal_order.append(self.players[index])
+
+ for index in range(fs_player_index):
+ players_in_deal_order.append(self.players[index])
+
+ else:
+ players_in_deal_order.append(self.players[fs_player_index])
+ for index in range(fs_player_index):
+ players_in_deal_order.append(self.players[index])
+
+ for index in range(fs_player_index+1,len(self.players)):
+ players_in_deal_order.append(self.players[index])
+
+ for instruct in self.dealing_instructions:
+ for player in players_in_deal_order:
+ for insctruct_size in range(instruct):
+ player.add_card(self.cur_deck.deal_card())
+
+ def get_deck(self):
+ return self.cur_deck
+
+
+class Belot(Game):
+ def __init__(self):
+ for player in range (4):
+ self.players.append(Player())
+
+ self.full_deck=Deck(['7','8','9','10','J','Q','K','A'])
+ self.cur_deck=self.full_deck
+ self.direction='ltr'
+ self.dealing_instructions=(2,3,3)
+
+ def prepare_deck(self):
+ for player in self.players:
+ player.remove_hand()
+
+ self.cur_deck=Deck(['7','8','9','10','J','Q','K','A'])
+ self.cur_deck.shuffle()
+ self.cur_deck.cut()
+
+
+class Poker(Game):
+ def __init__(self):
+ for player in range (9):
+ self.players.append(Player())
+
+ self.full_deck=Deck()
+ self.cur_deck=self.full_deck
+ self.direction='rtl'
+ self.dealing_instructions=(1,1,1,1,1)
+
+ def prepare_deck(self):
+ for player in self.players:
+ player.remove_hand()
+
+ self.cur_deck=Deck()
+ self.cur_deck.shuffle()
+ self.cur_deck.cut()
+