-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
270 lines (223 loc) · 12.1 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import random
import copy
from card import Card, generate_all_cards
from location import Location, generate_all_locations
from ai import AIPlayer
from data import update_deck_data, save_deck_data, load_deck_data, get_average_power
import json
class Game:
def __init__(self):
self.current_turn = 1
self.all_cards = generate_all_cards()
self.all_locations = generate_all_locations()
self.players = [AIPlayer(self, 0, copy.deepcopy(self.all_cards)), AIPlayer(self, 1, copy.deepcopy(self.all_cards))]
self.locations = [Location("Location 1", []), Location("Location 2", []), Location("Location 3", [])]
self.current_turn = 0
self.current_location = 0
self.prepare_game()
self.winner=2
def play_card(self, card, player_number, location_number):
player = self.players[player_number - 1]
location = self.locations[location_number]
card_copy = copy.deepcopy(card)
card_copy.owner = player_number - 1
card_copy.turn_played = self.current_turn
card_copy.location = location_number
# Ensuring the location does not already have 4 cards from the player.
if sum(1 for card in location.cards if card.owner == player_number - 1) >= 4:
#print(f"Player {player_number} cannot play {card.name} at this location. It already has 4 cards from the player.")
return
if card.energy_cost <= player.energy:
card_copy.owner = player_number - 1
card_copy.location = location_number
location.cards.append(card_copy)
if player_number == 1:
location.player1_played_card = True
else:
location.player2_played_card = True
player.hand.remove(card)
player.played_cards.append(card_copy)
player.energy -= card.energy_cost
# Update the player's turn_energy_spent
player.turn_energy_spent += card.energy_cost
# Check for Hawkeye cards and apply the effect if necessary
hawkeye_cards = [c for c in location.cards if c.name == "Hawkeye" and c.turn_played == self.current_turn - 1 and not c.hawkeye_effect_applied and c.location == location_number]
if hawkeye_cards:
for hawkeye_card in hawkeye_cards:
last_played_card = location.cards[-1]
if last_played_card.owner == hawkeye_card.owner and last_played_card.turn_played == self.current_turn:
hawkeye_card.power += 2 # Apply the effect
hawkeye_card.hawkeye_effect_applied = True # Set the flag after applying the effect
else:
#print(f"Player {player_number} cannot play {card.name} yet. It costs more energy than the current turn.")
return
def reveal_location(self):
if self.current_location >= len(self.locations):
return
location = self.locations[self.current_location]
location.revealed = True
self.current_location += 1
location.apply_location_effect(self)
def generate_locations(self):
return random.sample(self.all_locations, 3)
def prepare_game(self):
self.locations = self.generate_locations()
self.reveal_location()
def play_turn(self):
for player_number, player in enumerate(self.players, 1):
player.played_cards = []
player.played_card_locations = []
chosen_card_indices, location_indices = player.choose_card_and_location()
if chosen_card_indices is not None and location_indices is not None:
for card_index, location_index in zip(chosen_card_indices, location_indices):
card = player.hand[card_index]
if card.energy_cost <= player.energy:
self.play_card(card, player_number, location_index)
player.played_cards.append(card)
player.played_card_locations.append(location_index)
else:
#print(f"Player {player_number} cannot play {card.name} yet. It costs more energy than the current turn.")
break
else:
break
# Determine the order in which players reveal cards
player1_wins = 0
player2_wins = 0
for location in self.locations:
if location.calculate_total_power(0) > location.calculate_total_power(1):
player1_wins += 1
elif location.calculate_total_power(0) < location.calculate_total_power(1):
player2_wins += 1
if player1_wins > player2_wins:
reveal_order = [1, 2]
elif player1_wins < player2_wins:
reveal_order = [2, 1]
else:
player1_total_power = sum(location.calculate_total_power(0) for location in self.locations)
player2_total_power = sum(location.calculate_total_power(1) for location in self.locations)
if player1_total_power >= player2_total_power:
reveal_order = [1, 2]
else:
reveal_order = [2, 1]
# Reveal cards and apply card and location effects in the determined order
for player_number in reveal_order:
self.reveal_cards(player_number)
def reveal_cards(self, player_number):
player = self.players[player_number - 1]
if player.played_cards:
for card in player.played_cards:
if card.location is not None:
location = self.locations[card.location]
location_card = next((c for c in location.cards if c.owner == card.owner and c.name == card.name and c.location == card.location), None)
if card.ability is not None and card.ability.ability_type == "On Reveal":
power_bonus = card.ability.effect(card, self, player_number - 1, location)
if power_bonus is not None and power_bonus > 0:
card.power += power_bonus
# Update the location card's power value as well
if location_card is not None:
location_card.power = card.power # Update the power of the card in location.cards
#print("Card: ", card.name, "Has increased from ", card.base_power, "to ", card.power)
def apply_ongoing_abilities(self):
for player_number in range(1, 3):
player = self.players[player_number - 1]
for location in self.locations:
for card in location.cards:
if card.owner == player_number - 1 and card.ability is not None and card.ability.ability_type == "Ongoing":
card.ability.effect(card, self, player_number - 1, location)
self.apply_location_effects(player_number)
def apply_location_effects(self, player_number):
player = self.players[player_number - 1]
for location_index, location in enumerate(self.locations):
if location.effect and location.revealed:
for card in location.cards:
if card.owner == player_number and not card.location_effect_applied:
location.effect(card, player, location_index) # Pass location_index instead of location
card.location_effect_applied = True # Set the flag after applying the effect
def end_of_turn(self):
# Reset energy for each player according to the current turn
for player in self.players:
player.energy = self.current_turn + 1
player.turn_energy_spent = 0
# Reset the cards_this_turn list for each location
for location in self.locations:
location.player1_played_card = False
location.player2_played_card = False
for player in self.players:
player.draw_card()
# Apply end of turn effects for each location
for i, location in enumerate(self.locations):
if location.end_of_turn_effect:
location.end_of_turn_effect(i, self, self.current_turn)
def display_game_state(self):
print("\nCards at each location:")
print("Player 1 (above) / Player 2 (below)")
player1_cards_by_location = []
player2_cards_by_location = []
for location in self.locations:
player1_power = location.calculate_total_power(0)
player2_power = location.calculate_total_power(1)
location_index = self.locations.index(location)
player1_cards = [f"{card.name} ({card.power})" for card in location.cards if card.owner == 0]
player2_cards = [f"{card.name} ({card.power})" for card in location.cards if card.owner == 1]
max_cards = max(len(player1_cards), len(player2_cards))
player1_cards += [''] * (max_cards - len(player1_cards))
player2_cards += [''] * (max_cards - len(player2_cards))
player1_cards_by_location.append(player1_cards + [f"Player 1 Power: {player1_power}"])
player2_cards_by_location.append(player2_cards + [f"Player 2 Power: {player2_power}"])
max_rows = max(len(cards) for cards in player1_cards_by_location + player2_cards_by_location)
for row in range(max_rows):
for player1_cards in player1_cards_by_location:
print("{:^25}".format(player1_cards[row] if row < len(player1_cards) else ""), end=" ")
print()
for location in self.locations:
if location.revealed:
print("{:^25}".format(location.name), end=" ")
else:
print("{:^25}".format("???"), end=" ")
print()
for row in range(max_rows):
for player2_cards in player2_cards_by_location:
print("{:^25}".format(player2_cards[row] if row < len(player2_cards) else ""), end=" ")
print()
# Display decks and hands of both players
print("\nPlayer 1 deck and hand:")
print("Deck:", [f"{card.name} ({card.power})" for card in self.players[0].deck])
print("Hand:", [f"{card.name} ({card.power})" for card in self.players[0].hand])
print("\n")
print("Player 2 deck and hand:")
print("Deck:", [f"{card.name} ({card.power})" for card in self.players[1].deck])
print("Hand:", [f"{card.name} ({card.power})" for card in self.players[1].hand])
print("\n")
def determine_winner(self):
player1_score = sum(location.calculate_total_power(0) for location in self.locations)
player2_score = sum(location.calculate_total_power(1) for location in self.locations)
#print("Player 1 had a total power of:", player1_score)
#print("Player 2 had a total power of:", player2_score)
player1_wins = 0
player2_wins = 0
for location in self.locations:
if location.calculate_total_power(0) > location.calculate_total_power(1):
player1_wins += 1
elif location.calculate_total_power(0) < location.calculate_total_power(1):
player2_wins += 1
decks_data = load_deck_data('decks_data.json')
if player1_wins > player2_wins:
#print("Player 1 Wins!")
update_deck_data(self.players[0].starting_deck, "win", player1_score, decks_data)
update_deck_data(self.players[1].starting_deck, "loss", player2_score, decks_data)
elif player1_wins < player2_wins:
#print("Player 2 Wins!")
update_deck_data(self.players[0].starting_deck, "loss", player1_score, decks_data)
update_deck_data(self.players[1].starting_deck, "win", player2_score, decks_data)
save_deck_data(decks_data, 'decks_data.json')
def play_game(self):
for turn in range(6): # Loop through the 6 turns
self.current_turn = turn + 1
#print(f"Turn {self.current_turn}")
if 4 > self.current_turn > 1:
self.reveal_location()
self.play_turn()
self.apply_ongoing_abilities()
self.end_of_turn()
#self.display_game_state()
self.determine_winner()