repl.it
@t3m2/

Jogo de cartas

Python

No description

fork
loading
Files
  • main.py
main.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
from random import shuffle
from copy import deepcopy  # deepcopy will be used to copy lists

def suit_of(card):
  """Returns the image of the suit of a card.
  Eg.:
  suit_of('As')=='♠️'
  """
  return {'s':'♠️','c':'♣️','h':'♥️','d':'♦️'}[card[1]]
  
def value_of(card):
  """Returns the value of a card."""
  return {'3':0,'4':0,'5':0,'6':0,'Q':2,'J':3,'K':4,'7':10,'A':11}[card[0]]

def points_of(sequence):
  """Returns the points of a sequence."""
  return sum([value_of(i) for i in sequence])

def f(sequence):
  """Returns a sequence with formatted suits from a given sequence.
  E.g:
  f(['3s','Qh']) == ['3♠️','Q♥️']
  """
  return [i[0]+{'s':'♠️','c':'♣️','h':'♥️','d':'♦️'}[i[1]] for i in sequence]

def strength_of(card, table_suit, initial_suit):
  """Returns the relative strength of a sequence,
  taking into account the table_suit_and the initial_suit.
  """
  if card[1]==initial_suit:
    return {'3':1,'4':2,'5':3,'6':4,'Q':5,'J':6,'K':7,'7':8,'A':9}[card[0]]
  if card[1]==table_suit:
    return {'3':10,'4':11,'5':12,'6':13,'Q':14,'J':15,'K':16,'7':17,'A':18}[card[0]]
  return 0

def winner_of(sequence, table_suit):
  """Returns the INDEX of the winner of a sequence."""
  maximum=0
  for i,j in enumerate([strength_of(k, table_suit, sequence[0][1])for k in sequence]):
    if maximum<j:
      maximum=j
      position_of_winner=i
  
  return position_of_winner

def set_the_table(deck):
  """Puts the last card of the deck in its beginng so, sets the table.
  
  Note that deck will be changed.
  """
  deck.insert(0, deck.pop())
  return deck[0]

def deal_the_cards(deck, n, inversion=0):
  """Returns a list containing n lists of 3 cards
  that each player is gonna receive in the beginnig of the game.

  The cards will be taked from deck, so deck will be changed.
  The final list will be changed according to the inversion.
  """
  given_cards=[]

  for i in range(n):
    given_cards.append(deck[-3:])
    del(deck[-3:])
  return first_inversion_of(given_cards, inversion)

def next_round(deck, n, inversion=0):
  """Returns a list containing n cards that each player is gonna
  receive in the beginning of each round except 
  the first (in this case deal_the cards() is used), 
  penultimate and last one (in the last two rounds the deck is empty).

  The cards will be taken from deck, so deck will be changed.
  The final list will be changed according to the inversion.
  """
  given_cards=[]
  for i in range(n):
    given_cards.append(deck.pop())
  
  return first_inversion_of(given_cards, inversion)

def first_inversion_of(sequence, inversion):
  """Retuns an inversion (of first type) of a sequence.

  This function will be used in the function next_round(), so
  that the player who won the last round receives the first card
  and the rest of the cards is distributed in order.

  And it'll be used in the function deal_cards(), so that the
  player who receives the first cards isn't always the same.
  """
  for i in range(inversion):
    sequence.insert(0,sequence.pop())
  return sequence

def second_inversion_of(sequence, inversion):
  """Returns an inversion (of second type) of a sequence.

  This will be used in every round except the first so that
  the first who plays first is the one who won last round.

  In the first round it will be used so that
  the player who stars will changes from game to game.
  """
  for i in range(inversion):
    sequence.append(sequence[0])
    sequence.pop(0)
  return sequence

#def players_hands_record():
  
  


# Initialization:

#deck=['3s','4s','5s','6s','Qs','Js','Ks','7s','As',
      #'3c','4c','5c','6c','Qc','Jc','Kc','7c','Ac',
      #'3h','4h','5h','6h','Qh','Jh','Kh','7h','Ah',
      #'3d','4d','5d','6d','Qd','Jd','Kd','7d','Ad']
#shuffle(deck)

rules_of_the_game="\"Bisca\" -> the rules are not avaiable yet."

n = 6  # Numbers of players.

players_names = ["Player1", "Player2", "Player3", "Player4","Player5","Player6"]
# Note that len(players_names) must be equal to n.

inversion = 0  # inversion will be used to change the order players 
# receive cards and play, according to the player who stars, and 
# the winner of each round.

# Variable used to record the games:
sequences_record = []  # Record.
received_cards_record = []  # Record.
players_hands_record = []  # Record (this variable will be removed later
# because it can be deduced from sequences_record and received_cards_record)

# The game itself:
for turn in range(1,int(36/n)+1): # The number of turn is int(36/n)
  sequence=[]
  if turn==1:
    # Initialization made in the first turn:
    deck = ['3s','4s','5s','6s','Qs','Js','Ks','7s','As',
          '3c','4c','5c','6c','Qc','Jc','Kc','7c','Ac',
          '3h','4h','5h','6h','Qh','Jh','Kh','7h','Ah',
          '3d','4d','5d','6d','Qd','Jd','Kd','7d','Ad']
    shuffle(deck)

    first_inversion = inversion  # 

    # Initialize scoring:
    if n==3:
      scoring=[0, 0, 0] # No teams in this case.
    else:
      scoring=[0, 0]  # Because if n!=3, there will only be 2 teams.

    players_hands = deal_the_cards(deck, n, inversion)  # Record.
    received_cards_record = deepcopy(players_hands)  # Record.

    table_card = set_the_table(deck)
    table_suit = table_card[1]

    print("A carta da mesa é %s%s." % (table_card[0], table_suit))
  
  elif deck:
    # If turn>1 and deck still has cards then
    # give one to each player in the beginning of the round.
    x=next_round(deck, n, inversion)
    for i in range(n):
      players_hands[i].append(x[i])
      received_cards_record[i].append(x[i])  # Record
  
  print(f(deck))
  print(players_hands)

  players_hands_record.append(deepcopy(players_hands))  # Record
  
  
  for i in second_inversion_of([j for j in range(n)], inversion):
    # Get the "move" of each player
    # and add it to the sequence.

    chosen_index = int(input("%s's move (%s):" % \
    (players_names[i], ', '.join(f(players_hands[i])))))

    sequence.append(players_hands[i].pop(chosen_index))

  sequences_record.append(sequence)  # Record.

  inversion=index_of_winner=winner_of(sequence, table_suit)  # Get index_of_winner to update socrring and
  # inversion to know who will receive the first card and start, in the the next round. 
  
  # The next lines are used to update scoring.
  if n==2 or n==3:
    scoring[index_of_winner]+=points_of(sequence)
  elif n==4:
    if index_of_winner>1:
      index_of_winner-=2
    scoring[index_of_winner]+=points_of(sequence)
  elif n==6:
    if index_of_winner>3:
      index_of_winner-=4
    if index_of_winner>1:
      index_of_winner-=2
    scoring[index_of_winner]+=points_of(sequence)

  print(scoring)

print("Ok, vamos ver se 'tá seguro... ")
input()

print(n)
print('------')

print(table_card)
print('------')

print(players_hands_record)
print('------')

print(sequences_record)
print('------')

print(received_cards_record)
Fetching token
?