-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
378 lines (325 loc) · 20 KB
/
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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from rules_config import *
from player import *
from trick import Trick
import random
import pygame
from image import *
from model import draw_model
#-----------------------------------------------------------------------------------------------------------------------
#Main, plays through one iteration of a game of klaverjassen
#-----------------------------------------------------------------------------------------------------------------------
def main():
pygame.init()
pygame.font.init()
name_font = pygame.font.SysFont('arial', 20)
message_font = pygame.font.SysFont('arial', 16)
clock = pygame.time.Clock()
screen_size = (900, 600) # place_holder for board size
diagram_width = 600
message_screen_height = 150
game_display = pygame.display.set_mode((screen_size[0] + diagram_width, screen_size[1] + message_screen_height))
game_display.fill((205, 205, 255), pygame.Rect(0, 0, screen_size[0], screen_size[1]))
game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 0, diagram_width, screen_size[1]))
game_display.fill((255, 255, 255), pygame.Rect(0, screen_size[1] + 2, screen_size[0] + diagram_width, message_screen_height))
pygame.display.update()
#while not game_over:
pygame.time.wait(1000)
#initialize randomized cards and trump
if fixed_hands:
trump = 'Diamonds'
else:
trump = SUITS[random.randint(0, 3)]
print(str(trump) + ' is trump')
ord_cards = []
cards = []
for suit in SUITS:
for face in FACES:
rank = NORMAL_POINTS[face] if suit != trump else TRUMP_POINTS[face]
ord_cards.append((suit, face, rank))
if fixed_hands:
#SUITS = ['Hearts', 'Spades', 'Diamonds', 'Clubs']
#FACES = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven']
fixed_cards = {}
# the hands are fixed, because we know the order of ord_cards, from the order of SUITS and FACES, we can use this to pick cards
# for the hands, first number determines suit, second number determines rank
#fixed_cards['South'] = [ord_cards[8 + 7], ord_cards[0 + 4], ord_cards[16 + 4], ord_cards[0 + 1], ord_cards[0 + 6], ord_cards[16 + 5], ord_cards[16 + 6], ord_cards[16 + 3]]
#fixed_cards['West'] = [ord_cards[8 + 3], ord_cards[8 + 0], ord_cards[24 + 0], ord_cards[24 + 2], ord_cards[24 + 5], ord_cards[16 + 1], ord_cards[0 + 7], ord_cards[0 + 5]]
#fixed_cards['North'] = [ord_cards[8 + 5], ord_cards[8 + 2], ord_cards[8 + 6], ord_cards[24 + 1], ord_cards[24 + 7], ord_cards[16 + 0], ord_cards[16 + 2], ord_cards[0 + 2]]
#fixed_cards['East'] = [ord_cards[8 + 4], ord_cards[8 + 1], ord_cards[0 + 3], ord_cards[24 + 4], ord_cards[24 + 6], ord_cards[24 + 3], ord_cards[16 + 7], ord_cards[0 + 0]]
fixed_cards['South'] = [ord_cards[0 + 1], ord_cards[0 + 2], ord_cards[0 + 3], ord_cards[0 + 7],
ord_cards[16 + 0], ord_cards[16 + 4], ord_cards[16 + 5], ord_cards[16 + 6]]
fixed_cards['West'] = [ord_cards[0 + 0], ord_cards[0 + 5], ord_cards[0 + 6], ord_cards[0 + 4],
ord_cards[16 + 3], ord_cards[16 + 1], ord_cards[16 + 2], ord_cards[16 + 7]]
fixed_cards['North'] = [ord_cards[8 + 7], ord_cards[8 + 4], ord_cards[8 + 2], ord_cards[8 + 1],
ord_cards[24 + 6], ord_cards[24 + 5], ord_cards[24 + 7], ord_cards[24 + 0]]
fixed_cards['East'] = [ord_cards[8 + 3], ord_cards[8 + 0], ord_cards[24 + 1], ord_cards[24 + 2],
ord_cards[8 + 6], ord_cards[8 + 5], ord_cards[24 + 3], ord_cards[24 + 4]]
cards = list(ord_cards)
random.shuffle(cards)
# print(cards)
# initialize players
assert NUM_PLAYERS == 4
player_1 = Player(Team('South', 'North', '1'), 0, 'South', cards)
player_2 = Player(Team('East', 'West', '2'), 1, 'West', cards)
player_3 = Player(Team('South', 'North', '1'), 2, 'North', cards)
player_4 = Player(Team('East', 'West', '2'), 3, 'East', cards)
players = [player_1, player_2, player_3, player_4]
common_knowledge = [('GAME_RULE_TRUMP', trump, True)]
idx = 0
idx2 = CLOSED_CARDS
#assert CLOSED_CARDS == OPEN_CARDS
for player in players: # Card division between players
if not fixed_hands:
player.closed_cards = cards[idx:idx2]
idx = idx2
idx2 += OPEN_CARDS
player.open_cards = cards[idx:idx2]
idx = idx2
idx2 += CLOSED_CARDS
else:
idx = 0
idx2 = CLOSED_CARDS
player.closed_cards = fixed_cards[player.name][idx:idx2]
idx = idx2
idx2 += OPEN_CARDS
player.open_cards = fixed_cards[player.name][idx:idx2]
player.own_cards = list(player.closed_cards + player.open_cards)
for open_card in player.open_cards: # Add open cards to common knowledge
common_knowledge.append((player.name, open_card, False))
print(str(player.name) + ' has ' + str(player.closed_cards) + ' and ' + str(player.open_cards))
#print(str(player.name) + ' has ' + str(len(player.closed_cards)) + ' closed cards and ' + str(len(player.open_cards)) + ' open cards')
for player in players: # Knowledge init
player.knowledge.extend(common_knowledge)
for closed_card in player.closed_cards:
player.knowledge.append((player.name, closed_card, False))
player.create_possibles()
if debug:
print(player.name + " knows (initial) " + str(player.knowledge))
score = {'1' : 0, '2' : 0}
# First rounds HE RE THE GAME BEGINS!!!!!!!!!!!!!!!!!!
game_pause = True
for game_round in range(NUM_ROUNDS):
game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 0, diagram_width, screen_size[1]))
model_title = 'Current card knowledge of players:'
model_title_display = name_font.render(model_title, 1, (0, 0, 0))
game_display.blit(model_title_display, (screen_size[0] + 10, 10))
dropdown_width = 140
dropdown = pygame.Rect(screen_size[0] + 10, 35, dropdown_width, 20)
pygame.draw.rect(game_display, (0, 0, 0), dropdown, 1)
dropdown_display = message_font.render('choose a card', 1, (0, 0, 0))
game_display.blit(dropdown_display, ((dropdown.left + 5), dropdown.top))
for player in players: # Each following player picks card to play and plays
pygame.draw.line(game_display, (0, 0, 0), (screen_size[0], screen_size[1]),
(screen_size[0] + diagram_width, screen_size[1]), 2)
pygame.display.update()
message_counter = 0
turn = True
menu_open = False
select_available = False
while turn:
if player == players[0] and game_round == 0:
run_one_frame = True
else:
run_one_frame = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
game_pause = not game_pause
print('PAUSED ' + str(game_pause))
if event.key == pygame.K_RIGHT:
run_one_frame = True
x, y = pygame.mouse.get_pos()
#print(x, y)
m_pressed = pygame.mouse.get_pressed()
x_hover_dropdown = dropdown.left < x < dropdown.left + dropdown.width
y_hover_dropdown = dropdown.top < y < dropdown.top + dropdown.height
if x_hover_dropdown and y_hover_dropdown and m_pressed[0]:
for i in range(1, len(ord_cards) + 1):
dropdown_opt = pygame.Rect(screen_size[0] + 10, 35 + 20*i, dropdown_width, 20)
pygame.draw.rect(game_display, (255, 255, 255), dropdown_opt, 0)
pygame.draw.rect(game_display, (0, 0, 0), dropdown_opt, 1)
card = ord_cards[i - 1]
card_display = message_font.render(card[1] + ' of ' + card[0], 1, (0, 0, 0))
game_display.blit(card_display, ((dropdown_opt.left + 5), dropdown_opt.top))
pygame.display.update()
menu_open = True
if menu_open and not m_pressed[0]:
select_available = True
if select_available and m_pressed[0]:
if x_hover_dropdown and dropdown.top + dropdown.height < y < dropdown.top + dropdown.height*(len(ord_cards) + 1):
game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + dropdown_width + 10, 55, diagram_width,
screen_size[1] - 55))
i = int((y - 35) / 20 - 1)
card = ord_cards[i]
picked_card = message_font.render('card is: ' + card[1] + ' of ' + card[0], 1, (0, 0, 0))
game_display.blit(picked_card, (screen_size[0] + 200, 60))
draw_model(game_display, players, message_font, card, screen_size[0], 55, diagram_width, screen_size[1] - 55)
#else:
# game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 55, diagram_width, screen_size[1] + message_screen_height - 55))
# pygame.draw.line(game_display, (0, 0, 0), (screen_size[0], screen_size[1]), (screen_size[0] + diagram_width, screen_size[1]), 2)
pygame.display.update()
select_available = False
if not game_pause or run_one_frame:
if player == players[0]:
game_display.fill((205, 205, 255), pygame.Rect(0, 0, screen_size[0], screen_size[1]))
for i in range(len(PLAYERS)):
name_display = name_font.render(PLAYERS[i], 1, (0, 0, 0))
game_display.blit(name_display, name_pos(PLAYERS[i], 8, screen_size))
trump_display = name_font.render(str(trump) + ' is trump', 1, (0, 0, 0))
open_close_info = 'Game with ' + str(OPEN_CARDS) + ' open cards and ' + str(
CLOSED_CARDS) + ' closed cards.'
open_close_display = message_font.render(open_close_info, 1, (0, 0, 0))
continue_info1 = 'Press right arrow to continue'
continue_info2 = 'Press space bar to skip to end and close'
continue_display1 = message_font.render(continue_info1, 1, (0, 0, 0))
continue_display2 = message_font.render(continue_info2, 1, (0, 0, 0))
score_update1 = 'South and North: ' + str(score['1'])
score_update2 = 'East and West: ' + str(score['2'])
score_update_display1 = message_font.render(score_update1, 1, (0, 0, 0))
score_update_display2 = message_font.render(score_update2, 1, (0, 0, 0))
game_display.blit(score_update_display1, (20, screen_size[1] - 50))
game_display.blit(score_update_display2, (20, screen_size[1] - 35))
game_display.blit(trump_display, (10, 10))
game_display.blit(open_close_display, (10, 35))
game_display.blit(continue_display1, (600, 10))
game_display.blit(continue_display2, (600, 25))
else:
clear_hands(game_display, (205, 205, 255), len(player.closed_cards)+len(player.open_cards), screen_size)
game_display.fill((255, 255, 255), pygame.Rect(screen_size[0] + 2, 55, diagram_width, screen_size[1] - 55))
for pla in players:
for i in range(len(pla.closed_cards)): # Draw hands closed cards
card = pla.closed_cards[i]
if pla == player:
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
else:
file_name = 'b.gif'
image, im_rect = load_image(file_name)
game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i))
for i in range(len(pla.open_cards)):
card = pla.open_cards[i]
if pla == player:
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
else:
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
image, im_rect = load_image(file_name)
game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i+len(pla.closed_cards)))
pygame.display.update()
if game_pause:
if fast:
pygame.time.delay(100)
else:
pygame.time.delay(1500)
clear_hands(game_display, (205, 205, 255), len(player.closed_cards) + len(player.open_cards),
screen_size)
if player == players[0]:
print('\nnew round') # First player plays a card here
card, thoughts = players[0].play_card(trump)
trick = Trick(trump, players[0], card)
else:
card, thoughts = player.play_card(trump, trick)
trick.add_card(player, card)
#
game_display.fill((255, 255, 255), pygame.Rect(0, screen_size[1] + 2, screen_size[0] + diagram_width, message_screen_height))
for i in range(len(thoughts)):
message = message_font.render(thoughts[i], 1, (0, 0, 0))
game_display.blit(message, (10, screen_size[1] + 10 + (message_counter * 20)))
message_counter += 1
# Draw trick
card = trick.cards[-1]
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
image, im_rect = load_image(file_name)
game_display.blit(image, (screen_size[0]/2 + CENTER_POS[player.name][0], screen_size[1]/2 + CENTER_POS[player.name][1]))
public_ann_played_card = 'public announcement: ' + player.name + ' plays ' + trick.cards[-1][1] + ' of ' + trick.cards[-1][0]
played_card_display = message_font.render(public_ann_played_card, 1, (0, 0, 0))
game_display.blit(played_card_display, (10, screen_size[1] + 10 + (message_counter * 20)))
message_counter += 1
pa_updated = False
for pla in players:
for i in range(len(pla.closed_cards)): # Draw hands closed cards
card = pla.closed_cards[i]
if pla == player:
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
else:
file_name = 'b.gif'
image, im_rect = load_image(file_name)
game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i))
for i in range(len(pla.open_cards)):
card = pla.open_cards[i]
if pla == player:
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
else:
file_name = IMAGE_DICT[card[1]] + IMAGE_DICT[card[0]] + '.gif'
image, im_rect = load_image(file_name)
game_display.blit(image, calc_offset(pla.name, len(pla.closed_cards)+len(pla.open_cards), screen_size, i+len(pla.closed_cards)))
pygame.display.update()
try: # Knowledge update for all players
pla.knowledge.remove((trick.players[-1].name, trick.cards[-1], False))
except ValueError:
pass
pla.knowledge.append((trick.players[-1].name, trick.cards[-1], True))
notices = pla.update_possibles(trick)
if not pa_updated:
for i in range(len(notices)):
message = message_font.render(notices[i], 1, (0, 0, 0))
game_display.blit(message, (10, screen_size[1] + 10 + (message_counter * 20)))
message_counter += 1
pa_updated = True
if debug:
print(pla.name + ' Knows (update): ' + str(pla.knowledge))
if game_pause:
if fast:
pygame.time.delay(100)
else:
pygame.time.delay(1500)
if player.turn == 3:
trick.check_bonus()
if game_round == (NUM_ROUNDS - 1):
score[trick.winner.team.nr] += int(trick.score + 10) # Final round is worth 10 points
end = trick.winner.name + ' wins the final trick with highest card ' + str(
trick.high_card) + ', trick score ' + str(int(trick.score + 10))
print(end)
end_display = message_font.render(end, 1, (0, 0, 0))
game_display.blit(end_display, (10, screen_size[1] + message_screen_height - 25))
else:
score[trick.winner.team.nr] += int(trick.score) # Score is added to winning team
end = trick.winner.name + ' wins the trick with highest card ' + str(
trick.high_card) + ', trick score ' + str(int(trick.score))
print(end)
end_display = message_font.render(end, 1, (0, 0, 0))
game_display.blit(end_display, (10, screen_size[1] + message_screen_height - 25))
pygame.display.update()
if not game_pause or run_one_frame:
turn = False
players = players[trick.winner.turn:] + players[:trick.winner.turn] # Winning player is new starter
for i in range(NUM_PLAYERS):
players[i].turn = i
print(score)
pit = False
total = score['1']+score['2']
if score['1'] == 0:
score['2'] = total + 100
print('team 2: PIT!')
pit = True
elif score['2'] == 0:
score['1'] = total + 100
print('team 1: PIT!')
pit = True
if score['1'] <= (total/2) and not pit:
print('team 1: NAT!')
score['1'] = 0
score['2'] = total
print(score)
game_display.fill((205, 205, 255), pygame.Rect(20, screen_size[1] - 50, 150, 30))
pygame.display.update()
score_update1 = 'South and North: ' + str(score['1'])
score_update2 = 'East and West: ' + str(score['2'])
score_update_display1 = message_font.render(score_update1, 1, (0, 0, 0))
score_update_display2 = message_font.render(score_update2, 1, (0, 0, 0))
game_display.blit(score_update_display1, (20, screen_size[1] - 50))
game_display.blit(score_update_display2, (20, screen_size[1] - 35))
pygame.display.update()
pygame.time.delay(2500)
if __name__ == "__main__": # game loop
main()