Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from datetime import datetime
2from random import randint
4from banco_imobiliario.config import settings
6from .card_patrimony import Patrimony
9class GameBoard:
11 def __init__(self, *args, **kwargs):
12 self._winner = None
13 self._played = 0
14 self._players = []
15 self.start_time = datetime.now()
16 self._cards = [
17 Patrimony(index, None)
18 for index in range(
19 int(settings.ENV_QUANTITY_OF_PROPERTIES)
20 )
21 ]
23 @property
24 def played(self):
25 return self._played
27 @played.setter
28 def played(self, played):
29 self._played = played
31 @property
32 def players(self):
33 return self._players
35 @players.setter
36 def players(self, players):
37 self._players = players
39 @property
40 def winner(self):
41 return self._winner
43 @winner.setter
44 def winner(self, winner):
45 self._winner = winner
47 @property
48 def play_dice(self):
49 '''
50 No começo da sua vez, o jogador joga um
51 dado equiprovável de 6 faces que determina
52 quantas espaços no tabuleiro o jogador vai
53 andar.
54 '''
55 return randint(1, 6)
57 def __getitem__(self, position):
58 return self._cards[position]
60 def __setitem__(self, position, patrimony):
61 self._cards[position] = patrimony
63 def __len__(self):
64 return len(self._cards)
66 def __str__(self):
67 return f"{self._cards}"
69 def __repr__(self):
70 return f"{self._cards}"
72 def remove(self, player):
73 for patrimony in self._cards:
74 if patrimony.type_of_strategy == player:
75 patrimony.type_of_strategy = None
76 self._players.remove(player)
78 def walk(self, player, _dice=None):
79 go_to_position = player.position + (_dice or self.play_dice)
80 if go_to_position >= int(settings.ENV_QUANTITY_OF_PROPERTIES):
81 '''
82 Ao completar uma volta no tabuleiro,
83 o jogador ganha 100 de saldo.
84 '''
85 player.money += float(settings.ENV_PLAYER_MONEY_ROUND)
86 go_to_position -= int(settings.ENV_QUANTITY_OF_PROPERTIES)
87 player.position = go_to_position
88 return go_to_position
90 def check_winner(self, player):
91 '''
92 Termina quando restar somente um jogador
93 com saldo positivo, a qualquer momento da
94 partida. Esse jogador é declarado o
95 vencedor.
96 '''
97 if len(self.players) == 1:
98 return player
99 if int(settings.ENV_TIMEOUT_ROUND) <= self.played:
100 money = 0
101 winner = None
102 for _player in self._players:
103 if _player.money > money:
104 money = _player.money
105 winner = _player
106 return winner
108 elements = [
109 _player.money
110 for _player in self._players if _player != player
111 ]
112 if sum(elements) < 0:
113 return player
115 return None
117 def play(self, player, board):
118 '''
119 Um jogador que fica com saldo negativo
120 perde o jogo, e não joga mais. Perde
121 suas propriedades e portanto podem ser
122 compradas por qualquer outro jogador.
123 '''
124 if player.money <= 0:
125 player.gameover = True
126 return
128 patrimony = self._cards[self.walk(player)]
129 player.income_or_sale(patrimony, board)
131 def finish(self):
132 return {
133 "time_it": (datetime.now() - self.start_time).total_seconds(),
134 "winner": self.winner,
135 "money": self.winner.money,
136 "played": self.played,
137 "strategy": self.winner,
138 "time_out": self.played > int(settings.ENV_TIMEOUT_ROUND),
139 }