-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopa_2players.py
More file actions
299 lines (264 loc) · 12.5 KB
/
Scopa_2players.py
File metadata and controls
299 lines (264 loc) · 12.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# -*- coding: utf-8 -*-
"""AlgProj.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1AgkIKf5qWSfJxywMWh0nW-5ZGaNfCSVW
# 2 Players Game
"""
import random
import itertools
# Create an Italian 40-card deck
suits = ['Quadri', 'Picche', 'Cuori', 'Fiori']
values = ['Asso', '2', '3', '4', '5', '6', '7', 'Jack', 'Donna', 'Re']
def initialize_game(mode):
# Shuffle the deck
deck = [{'value': value, 'suit': suit} for value in values for suit in suits]
random.shuffle(deck)
players_hands=[[],[]]
player1_name = input("Enter the name for Player 1: ")
if mode==1:
player2_name = input("Enter the name for Player 2: ")
else:
player2_name = ""
# Draw four cards and place them face-up on the table
table_cards = [deck.pop() for _ in range(4)]
# Initialize scores and collected cards for each player
total_scores = [0, 0]
collected_cards = [[],[]]
return deck, total_scores, collected_cards, table_cards, players_hands, player1_name, player2_name
# Deal three cards to each player
def deal_cards(deck, num_players, num_cards):
hands = [[] for _ in range(num_players)]
for _ in range(num_cards):
for player in range(num_players):
card = deck.pop()
hands[player].append(card)
return hands
def beggin_of_turn(deck, table_cards, players_hands):
# Check if the player's hand is empty before dealing new cards
for i in range(len(players_hands)):
if not players_hands[i] and deck:
# Deal three cards to the player if the hand is empty and the deck is not empty
players_hands[i] = [deck.pop() for _ in range(3)]
return deck, players_hands, table_cards
def card_value(card):
# Helper function to get the numerical value of a card
if card['value'] == 'Jack':
return 8
elif card['value'] == 'Donna':
return 9
elif card['value'] == 'Re':
return 10
elif card['value'] == 'Asso':
return 1
else:
return int(card['value'])
def check_settebello(player_collection, total_score0, total_score1):
# Check if the player has the Settebello card (7 of coins)
settebello_card = {'value': '7', 'suit': 'Fiori'}
if settebello_card in player_collection:
total_score0+=1
#print("7b player1")
else:
total_score1+=1
#print("7b player2")
return total_score0, total_score1
def primiera_value (card):
if card['value'] == 'Jack':
return 10
elif card['value'] == 'Donna':
return 10
elif card['value'] == 'Re':
return 10
elif card['value'] == 'Asso':
return 16
elif card['value'] == '7':
return 21
elif card['value'] == '6':
return 18
else:
return 0
def calculate_primiera_score(player_collection0, player_collection1, total_score0, total_score1):
primiera_values = ['7', '6', 'Asso', 'Jack', 'Donna', 'Re']
primiera_score0=primiera_valuee(player_collection0)
primiera_score1=primiera_valuee(player_collection1)
#print("primiera: ", primiera_score0, primiera_score1)
if primiera_score0 > primiera_score1:
total_score0 += 1
elif primiera_score1 > primiera_score0:
total_score1 += 1
return total_score0, total_score1
def primiera_valuee (collection):
primiera_values = ['7', '6', 'Asso', 'Jack', 'Donna', 'Re']
suits = ['Quadri', 'Picche', 'Cuori', 'Fiori']
sum=0
for value in primiera_values:
for suit in suits:
card = {'value': value, 'suit': suit}
if card in collection:
if value == 'Donna' or value == 'Re' or value == 'Jack':
sum+= 10
elif value == 'Asso':
sum+= 16
elif value == '7':
sum+= 21
elif value == '6':
sum+= 18
return sum
def calculate_ori_score(player_collection0, player_collection1, total_score0, total_score1):
ori_score0=ori_valuee(player_collection0)
ori_score1=ori_valuee(player_collection1)
#print("ori: ", ori_score0, ori_score1)
if ori_score0 > ori_score1:
total_score0 += 1
elif ori_score1 > ori_score0:
total_score1 += 1
return total_score0, total_score1
def ori_valuee (collection):
ori_values = ['2', '3', '4', '5','7', '6', 'Asso', 'Jack', 'Donna', 'Re']
sum=0
for value in ori_values:
#for suit in suits:
card = {'value': value, 'suit': 'Quadri'}
if card in collection:
sum+= 1
return sum
def determine_winner(total_scores):
# Determine the winner based on total scores
if total_scores[0] > total_scores[1]:
print("Player 1 wins!")
elif total_scores[1] > total_scores[0]:
print("Player 2 wins!")
else:
print("It's a tie!")
def calculate_points(total_scores0, total_scores1,collected_cards0, collected_cards1, player1_name, player2_name):
print("Scopa:",total_scores0, total_scores1)
# Calculate Settebello and Primiera scores at the end of the game
total_scores0, total_scores1=check_settebello(collected_cards0, total_scores0, total_scores1)
print("Settebello:", total_scores0, total_scores1)
total_scores0, total_scores1= calculate_primiera_score(collected_cards0, collected_cards1, total_scores0, total_scores1)
print("Primiera:", total_scores0, total_scores1)
# Determine Carte
if len(collected_cards0)> len(collected_cards1):
total_scores0+=1
if len(collected_cards0)< len(collected_cards1):
total_scores1+=1
print("Carte:", total_scores0, total_scores1)
#print("carte",len(collected_cards0),len(collected_cards1) )
# Determine Ori
total_scores0, total_scores1= calculate_ori_score(collected_cards0, collected_cards1, total_scores0, total_scores1)
print("Ori:", total_scores0, total_scores1)
# Display the final scores
print("Final Scores", player1_name,":", total_scores0)
print("Final Scores", player2_name,":", total_scores1)
return
import itertools
def play_turn(player_hand, table_cards, player_collection, total_scores):
while True:
# Display the current state of the game
print("Table cards:", [f"{card['value']}, {card['suit']}" for card in table_cards])
print("Your hand:", [f"{card['value']}, {card['suit']}" for card in player_hand])
try:
# Allow the player to play a card from their hand
selected_card_index = int(input("Select the index of the card you want to play (0 to {}): ".format(len(player_hand) - 1)))
# Get the selected card
played_card = player_hand.pop(selected_card_index)
# Determine if the played card captures other cards on the table based on value
captured_cards = []
# First, try to capture cards with the same value
matching_value_cards = [card for card in table_cards if card_value(played_card) == card_value(card)]
if matching_value_cards:
if len(matching_value_cards) > 1:
# Case 1: Multiple cards with the same value, prompt the player to choose
print("Multiple cards with the same value. Choose which card to capture:")
for i, card_on_table in enumerate(matching_value_cards):
print(f"{i+1}: {card_on_table['value']}, {card_on_table['suit']}")
choice = int(input("Enter the number of the card you want to pick: ")) - 1
captured_cards.extend(matching_value_cards[choice])
else:
# Case 2: Only one card with the same value
captured_cards.extend(matching_value_cards)
# If no cards with the same value, try combinations
if not captured_cards:
valid_combinations = []
for combination_size in range(1, len(table_cards) + 1):
# Try all combinations of the cards on the table
for combination in itertools.combinations(table_cards, combination_size):
if card_value(played_card) == sum(card_value(captured_card) for captured_card in combination):
# The played card can capture the combination of cards on the table
valid_combinations.append(list(combination))
if valid_combinations:
if len(valid_combinations) > 1:
# Case 3: Multiple valid combinations, prompt the player to choose
print("Multiple valid combinations. Choose which cards to capture:")
for i, combination in enumerate(valid_combinations):
formatted_combination = [f"{card['value']}, {card['suit']}" for card in combination]
print(f"{i+1}: {formatted_combination}")
choice = int(input("Enter the number of the combination you want to pick: ")) - 1
chosen_combination = valid_combinations[choice]
# Remove selected cards from the table
for card_on_table in chosen_combination:
table_cards.remove(card_on_table)
# Add selected cards to the player's collection
captured_cards.extend(chosen_combination)
else:
chosen_combination = valid_combinations[0]
# Remove selected cards from the table
for card_on_table in chosen_combination:
table_cards.remove(card_on_table)
# Add selected cards to the player's collection
captured_cards.extend(chosen_combination)
else:
# Case 4: No valid combinations or only one valid combination
print("No matching value on the table. The card has been added to the table.")
table_cards.append(played_card)
if len(captured_cards) > 0:
# Case 5: The played card captures cards with the same value
print("You captured the following cards:", [f"{card['value']}, {card['suit']}" for card in captured_cards])
captured_indices = []
for captured_card in captured_cards:
# Find the index of the captured card in the table
indices = [i for i, card in enumerate(table_cards) if card == captured_card]
captured_indices.extend(indices)
# Remove captured cards from the table in reverse order to avoid index issues
for index in sorted(captured_indices, reverse=True):
table_cards.pop(index)
for captured_card in captured_cards:
player_collection.append(captured_card)
player_collection.append(played_card)
if not table_cards:
print("Scopa! You get 1 point.")
total_scores += 1
return captured_cards, total_scores, player_collection, table_cards
except (ValueError, IndexError):
print("Invalid input. Please enter a valid index.")
continue
def play_game():
# Initialize the game
deck, total_scores, collected_cards, table_cards, players_hands, player1_name, player2_name = initialize_game(1)
last=0
# Play multiple rounds until the deck is empty
while deck or any(players_hands):
# Begin Player 1's turn
deck, players_hands, table_cards = beggin_of_turn(deck, table_cards, players_hands)
# Player 1 plays their turn
print("\n",player1_name, "'s turn:")
captured_cards, total_scores[0], collected_cards[0], table_cards = play_turn(players_hands[0], table_cards, collected_cards[0], total_scores[0])
# Save the captured cards for Player 1
if len(captured_cards)>0:
last=1
# Begin Player 2's turn
print("\n",player2_name,"'s turn:")
deck, players_hands, table_cards = beggin_of_turn(deck, table_cards, players_hands)
# Player 2 plays their turn
captured_cards, total_scores[1], collected_cards[1], table_cards = play_turn(players_hands[1], table_cards, collected_cards[1], total_scores[1])
# Save the captured cards for Player 2
if len(captured_cards)>0:
last=2
for table_card in table_cards:
collected_cards[last-1].append(table_card)
print(collected_cards[0])
print(collected_cards[1])
calculate_points(total_scores[0], total_scores[1],collected_cards[0], collected_cards[1], player1_name, player2_name)
# Play the game§
play_game()