-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_screen.py
More file actions
1500 lines (1267 loc) · 74.8 KB
/
Copy pathgame_screen.py
File metadata and controls
1500 lines (1267 loc) · 74.8 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# game_screen.py
import os
import random
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.spinner import Spinner
from kivy.uix.slider import Slider
from kivy.uix.switch import Switch # Added for fullscreen toggle
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.image import Image
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget # Added Widget
from kivy.properties import StringProperty, NumericProperty
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle, Ellipse # Added Ellipse
from kivy.clock import Clock
from kivy.animation import Animation
from kivy.app import App # Added import
from custom_widgets import ImageButton
from game_logic import GameState
from profile_manager import ProfileManager, UserProfile
# Custom Widget for 'O' Markers for optimized animations
class OMarkerWidget(Widget):
ellipse_current_scale = NumericProperty(0.8)
def __init__(self, **kwargs):
super().__init__(**kwargs)
with self.canvas:
self.ellipse_color_instruction = Color(0, 0.4, 0.6, 1) # Initial Blue-Teal color
self.ellipse_instruction = Ellipse(
# Initial pos/size will be updated by _update_ellipse_visuals
# Set to small initial values to avoid visual glitch if not updated immediately
pos=(self.center_x - 5, self.center_y - 5),
size=(10, 10)
)
# Bind properties to the update method
self.bind(pos=self._update_ellipse_visuals,
size=self._update_ellipse_visuals,
ellipse_current_scale=self._update_ellipse_visuals)
# Call once to set initial visuals based on actual size and scale
# This might be better called via Clock.schedule_once to ensure layout has occurred
# For now, direct call assuming initial properties are somewhat valid or will be updated.
self._update_ellipse_visuals()
def _update_ellipse_visuals(self, instance=None, value=None):
# This method updates the ellipse based on the widget's properties
scale = self.ellipse_current_scale
# Calculate position and size for the ellipse to be centered
ellipse_width = self.width * scale
ellipse_height = self.height * scale
self.ellipse_instruction.pos = (self.center_x - ellipse_width / 2,
self.center_y - ellipse_height / 2)
self.ellipse_instruction.size = (ellipse_width, ellipse_height)
def start_animations(self):
# Color Animation (shifting RGBA for color and opacity)
color_anim = (Animation(rgba=(0.1, 0.5, 0.7, 0.7), duration=2.0, t='in_out_sine') +
Animation(rgba=(0, 0.4, 0.6, 1.0), duration=2.0, t='in_out_sine'))
color_anim.repeat = True
color_anim.start(self.ellipse_color_instruction)
# Size Animation for ellipse_current_scale
# Kivy's property system will automatically trigger _update_ellipse_visuals
size_pulse_anim = (Animation(ellipse_current_scale=0.9, duration=1.5, t='in_out_sine') +
Animation(ellipse_current_scale=0.7, duration=1.5, t='in_out_sine'))
size_pulse_anim.repeat = True
size_pulse_anim.start(self) # Start on self (the OMarkerWidget instance)
class GameScreen(Screen):
def update_game_board_layout(self, instance, value):
# instance is the widget whose size change triggered this, e.g., self.grid_plus_labels_container
# value is its new size (width, height)
available_width = value[0]
available_height = value[1]
if not hasattr(self, 'grid_size') or not self.grid_size or not hasattr(self, 'grid_layout'):
print("Warning: Game board components not ready for layout update.")
return
num_rows = self.grid_size[0]
num_cols = self.grid_size[1]
if num_rows == 0 or num_cols == 0:
return
# Define proportions for labels vs grid (these are initial size_hints)
# These might need to be fixed values if size_hints are removed from children
row_labels_width_proportion = 0.05
col_labels_height_proportion = 0.05
# grid_layout_width_proportion = 0.95 (of remaining after row_labels)
# grid_layout_height_proportion = 0.95 (of remaining after col_labels)
# Calculate space available for the main grid (self.grid_layout)
# This needs to account for the space the labels will take.
# This is tricky because label sizes depend on grid size and vice-versa if we want alignment.
# Let's determine cell_edge based on the total available space for the grid AND its labels.
# The grid itself is in grid_and_row_labels_row (takes X% of width, 95% of height of grid_plus_labels_container)
# And col_labels_and_spacer_row (takes X% of width, 5% of height of grid_plus_labels_container)
# Effective space for the interactive grid area (grid_layout + row_labels + col_labels + corner_spacer)
# This is essentially the full space of self.grid_plus_labels_container
spacing_x, spacing_y = self.grid_layout.spacing if isinstance(self.grid_layout.spacing, (list, tuple)) else (self.grid_layout.spacing, self.grid_layout.spacing)
# Calculate potential cell_edge if row/col labels had zero size:
# This is the available space for the *grid cells area* within grid_plus_labels_container
# grid_plus_labels_container contains:
# col_labels_and_spacer_row (height: 5% of available_height)
# grid_and_row_labels_row (height: 95% of available_height)
# Space for (grid + row_labels):
# Use the original size_hint_y proportion (0.95 for grid_and_row_labels_row)
space_for_grid_and_row_labels_h = available_height * 0.95
# Space for (col_labels + corner_spacer):
# Use the original size_hint_y proportion (0.05 for col_labels_and_spacer_row)
space_for_col_labels_and_spacer_h = available_height * 0.05
# Within grid_and_row_labels_row (width: 100% of available_width):
# row_labels_layout (width: 5% of its parent's width)
# grid_layout (width: 95% of its parent's width)
# Let's simplify: assume fixed pixel sizes for labels for a moment, or calculate cell_edge first.
# The most constrained dimension will determine cell_edge.
# Width available for num_cols cells and (num_cols-1) spacings, AND row_labels_width:
# available_width = (num_cols * cell_edge + (num_cols-1)*spacing_x) + row_label_width
# Height available for num_rows cells and (num_rows-1) spacings, AND col_labels_height:
# available_height = (num_rows * cell_edge + (num_rows-1)*spacing_y) + col_label_height
# Estimate typical label sizes (e.g., based on font size or a fixed value)
# For now, let's use their original size_hint proportions to estimate their impact.
estimated_row_labels_width = available_width * row_labels_width_proportion
estimated_col_labels_height = available_height * col_labels_height_proportion
width_for_grid_cells = available_width - estimated_row_labels_width
height_for_grid_cells = available_height - estimated_col_labels_height
cell_edge_w = (width_for_grid_cells - (num_cols - 1) * spacing_x) / num_cols
cell_edge_h = (height_for_grid_cells - (num_rows - 1) * spacing_y) / num_rows
cell_edge = min(cell_edge_w, cell_edge_h)
if cell_edge <= 1: cell_edge = 1 # Ensure positive
# --- Now, set sizes based on this cell_edge ---
# 1. Size the grid cells (children of self.grid_layout)
for child in self.grid_layout.children:
child.size_hint = (None, None)
child.size = (cell_edge, cell_edge)
# 2. Calculate actual grid_layout size and set it
actual_grid_width = num_cols * cell_edge + (num_cols - 1) * spacing_x
actual_grid_height = num_rows * cell_edge + (num_rows - 1) * spacing_y
self.grid_layout.size_hint = (None, None)
self.grid_layout.size = (actual_grid_width, actual_grid_height)
# 3. Size the label layouts
# Use a fixed reasonable size for labels for now, or make them adapt to font.
# Let's make them fit the grid.
# self.col_labels_layout was size_hint=(0.95, 1) relative to col_labels_and_spacer_row
# self.row_labels_layout was size_hint=(0.05, 1) relative to grid_and_row_labels_row
# Set size_hints to None to manually control size
self.col_labels_layout.size_hint = (None, None)
self.row_labels_layout.size_hint = (None, None)
self.corner_spacer.size_hint = (None, None)
# Define a practical minimum/default size for labels if grid is too small
min_label_width = 30
min_label_height = 20
# Column labels (numbers 1 to N for columns)
# Their height should be consistent. Width should match grid.
col_label_height = max(min_label_height, estimated_col_labels_height) # Or a fixed value like 30
self.col_labels_layout.size = (actual_grid_width, col_label_height)
for label in self.col_labels_layout.children: # These are labels for col numbers
label.size_hint_x = None
label.width = cell_edge # Make each col number label take cell_edge width
label.text_size = (label.width, None) # For text alignment
# Row labels (numbers 1 to N for rows)
# Their width should be consistent. Height should match grid.
row_label_width = max(min_label_width, estimated_row_labels_width) # Or a fixed value like 30
self.row_labels_layout.size = (row_label_width, actual_grid_height)
for label in self.row_labels_layout.children: # These are labels for row numbers
label.size_hint_y = None
label.height = cell_edge # Make each row number label take cell_edge height
label.text_size = (label.width, None) # For text alignment (use label.width for wrapping if text were long)
# 4. Size the corner spacer
self.corner_spacer.size = (row_label_width, col_label_height)
# 5. Adjust parent container sizes if necessary.
# self.col_labels_and_spacer_row contains corner_spacer and col_labels_layout
self.col_labels_and_spacer_row.size_hint = (None, None)
self.col_labels_and_spacer_row.size = (row_label_width + actual_grid_width, col_label_height)
# self.grid_and_row_labels_row contains row_labels_layout and grid_layout
self.grid_and_row_labels_row.size_hint = (None, None)
self.grid_and_row_labels_row.size = (row_label_width + actual_grid_width, actual_grid_height)
# self.grid_plus_labels_container contains col_labels_and_spacer_row and grid_and_row_labels_row
# This is the container whose size change should trigger this whole method.
# We can either let it keep its size_hint and center its children, or make it wrap them.
# For now, let it keep its size_hint and the content will be top-left aligned by default.
# To center, we'd need to add a wrapper BoxLayout or adjust padding/positioning.
# This might be an overreach for the current problem, but good to note.
# Center the content within self.grid_plus_labels_container
# self.grid_plus_labels_container is a BoxLayout with orientation='vertical'.
# Its children are self.col_labels_and_spacer_row and self.grid_and_row_labels_row.
# Width of the content. Both children rows should have the same width.
# This was calculated as: self.col_labels_and_spacer_row.size = (row_label_width + actual_grid_width, col_label_height)
content_width = self.col_labels_and_spacer_row.width
# Height of the content
content_height = self.col_labels_and_spacer_row.height + self.grid_and_row_labels_row.height
container_width = self.grid_plus_labels_container.width
container_height = self.grid_plus_labels_container.height
padding_x = (container_width - content_width) / 2
padding_y = (container_height - content_height) / 2
# Ensure padding is not negative (can happen if content somehow exceeds container, though unlikely with current logic)
padding_x = max(0, padding_x)
padding_y = max(0, padding_y)
self.grid_plus_labels_container.padding = [padding_x, padding_y, padding_x, padding_y]
print(f"update_game_board_layout: available=({available_width},{available_height}), grid=({actual_grid_width},{actual_grid_height}), cell_edge={cell_edge}")
print(f"Row Labels: {self.row_labels_layout.size}, Col Labels: {self.col_labels_layout.size}, Corner: {self.corner_spacer.size}")
# print(f"grid_plus_labels_container padding: {[padding_x, padding_y, padding_x, padding_y]}")
def __init__(self, **kwargs):
super(GameScreen, self).__init__(**kwargs)
self.main_layout = BoxLayout(orientation='horizontal')
self.add_widget(self.main_layout)
# Initialize sidebar visibility and original width
self.sidebar_visible = False
self.sidebar_original_width_hint = 0.3
# Initialize game over flag
self.game_over_flag = False
# Initialize properties for blinking
self.blink_event = None
self.blinking_buttons = []
self.blinking_animations = []
def initialize_game(self, player_configurations, grid_size, game_turn_length, marker_percentage=0.1): # player_names -> player_configurations
self.main_layout.clear_widgets()
self.o_marker_buttons = []
self.game_over_flag = False
# Initialize ProfileManager and player profile objects
self.profile_manager = ProfileManager()
# player_profile_objects will be keyed by player_game_name (display name)
self.player_profile_objects = {}
for p_config in player_configurations:
profile_username = p_config['profile_username'] # This can be None for AI
player_game_name = p_config['name'] # This is the display name, e.g., "HumanPlayer1" or "AI 1 (Easy)"
if profile_username is not None: # Human player
is_new = p_config.get('is_new_profile', False)
profile = None
if is_new:
try:
profile = self.profile_manager.create_profile(profile_username)
print(f"GameScreen: Created new profile for {profile_username}")
except ValueError as e:
print(f"GameScreen Error creating new profile '{profile_username}': {e}. Trying to get existing.")
profile = self.profile_manager.get_profile(profile_username)
if not profile: # Fallback to temp UserProfile
profile = UserProfile(profile_username)
else: # Existing human profile
profile = self.profile_manager.get_profile(profile_username)
if not profile:
print(f"GameScreen Error: Existing profile '{profile_username}' not found. Creating fallback.")
try: # Attempt to create it if it was supposed to exist but doesn't
profile = self.profile_manager.create_profile(profile_username)
except ValueError: # If it somehow exists after all (e.g. race condition)
profile = self.profile_manager.get_profile(profile_username)
if not profile: # Absolute fallback
profile = UserProfile(profile_username)
# Store the UserProfile object (or a temporary one) against the player's game name
self.player_profile_objects[player_game_name] = profile if profile else UserProfile(profile_username) # Ensure a profile object is stored
else: # AI Player (profile_username is None)
self.player_profile_objects[player_game_name] = None # Explicitly store None for AI players
print(f"GameScreen: Setting up AI player {player_game_name} with no profile object.")
# Initialize GameState
script_dir = os.path.dirname(os.path.abspath(__file__))
# GameState __init__ will now use 'name' for its primary player list.
self.game_state = GameState(player_configurations, grid_size, script_dir)
self.game_turn_length = game_turn_length # Game turn length set by player
# **Register the callback to handle GameState updates**
self.game_state.register_callback(self.handle_game_state_update)
# Cache valid image paths to avoid repeated disk checks
self.valid_company_logos = {}
for company, path in self.game_state.company_logos.items():
if os.path.exists(path):
self.valid_company_logos[company] = path
self.valid_diamond_path = None
if os.path.exists(self.game_state.diamond_image_path):
self.valid_diamond_path = self.game_state.diamond_image_path
# Sidebar for player information
self.sidebar_layout = BoxLayout(
orientation='vertical',
size_hint=(0, 1),
padding=[Window.width * 0.01, Window.height * 0.01],
spacing=Window.height * 0.01
)
self.sidebar_layout.opacity = 0
with self.sidebar_layout.canvas.before:
Color(0, 0, 0, 1) # Black background
self.sidebar_rect = Rectangle(pos=self.sidebar_layout.pos, size=self.sidebar_layout.size)
self.sidebar_layout.bind(
pos=lambda instance, value: setattr(self.sidebar_rect, 'pos', value),
size=lambda instance, value: setattr(self.sidebar_rect, 'size', value)
)
self.current_player_label = Label(
text=f"[b]Current Player:[/b] {self.game_state.players[0]}", # Use game_state.players
size_hint=(1, 0.1),
markup=True,
font_size=Window.height * 0.02,
color=(1, 1, 1, 1)
)
self.player_money_label = Label(
text=f"Cash: £6000",
size_hint=(1, 0.1),
font_size=Window.height * 0.018,
color=(1, 1, 1, 1)
)
# New Holdings Display Structure
self.holdings_title_label = Label(
text="[b]Holdings:[/b]",
markup=True,
size_hint=(1, 0.05),
font_size=Window.height * 0.018,
color=(1,1,1,1)
)
self.holdings_display_container = BoxLayout(
orientation='vertical',
size_hint=(1, 0.3), # Takes full width of sidebar_layout
spacing=5 # spacing between each holding row
)
self.total_wealth_label = Label( # Repurposed from old player_holdings_label concept
text="Total Wealth: £0",
size_hint=(1, 0.05),
font_size=Window.height * 0.018,
color=(1,1,1,1)
)
# self.company_info_label is removed as per instructions
self.sidebar_spacer = Label(size_hint=(1, 0.4)) # Corrected size_hint_y to make sum 1.0
self.settings_button = Button(
text="Settings",
size_hint=(1, 0.1),
font_size=Window.height * 0.018 # Match other sidebar elements
)
self.settings_button.bind(on_press=self.open_settings_popup)
# Clear existing widgets from sidebar_layout before adding new ones in correct order
self.sidebar_layout.clear_widgets()
# Add widgets in the specified order
self.sidebar_layout.add_widget(self.current_player_label) # size_hint_y: 0.1
self.sidebar_layout.add_widget(self.player_money_label) # size_hint_y: 0.1
self.sidebar_layout.add_widget(self.holdings_title_label) # size_hint_y: 0.05
self.sidebar_layout.add_widget(self.holdings_display_container) # size_hint_y: 0.3
self.sidebar_layout.add_widget(self.total_wealth_label) # size_hint_y: 0.05
# self.company_info_label is removed from layout
self.sidebar_layout.add_widget(self.sidebar_spacer) # size_hint_y: 0.4 (corrected)
self.sidebar_layout.add_widget(self.settings_button) # size_hint_y: 0.1
# New Total: 0.1+0.1+0.05+0.3+0.05+0.4+0.1 = 1.0
self.main_layout.add_widget(self.sidebar_layout)
# Game board layout
self.game_layout = BoxLayout(
orientation='vertical', size_hint=(1.0, 1), padding=10, spacing=10
)
# Info label to display player actions
self.info_label = Label(
text=f"Welcome to Space Monopoly! {self.game_state.players[0]}'s Turn", # Use game_state.players
size_hint=(1, 0.05),
font_size=16,
color=(1, 1, 1, 1)
)
self.game_layout.add_widget(self.info_label)
# Container for grid + labels
self.grid_plus_labels_container = BoxLayout(orientation='vertical', size_hint=(1, 0.85))
# Row for column labels and top-left spacer
self.col_labels_and_spacer_row = BoxLayout(orientation='horizontal', size_hint=(1, 0.05))
# Top-left corner spacer
self.corner_spacer = Widget(size_hint=(0.05, 1))
self.col_labels_and_spacer_row.add_widget(self.corner_spacer) # Correction already applied in previous step, this is fine.
# Column labels layout
self.col_labels_layout = BoxLayout(orientation='horizontal', size_hint=(0.95, 1))
self.col_labels_and_spacer_row.add_widget(self.col_labels_layout) # Correction already applied in previous step, this is fine.
self.grid_plus_labels_container.add_widget(self.col_labels_and_spacer_row) # Correction already applied in previous step, this is fine.
# Row for the main grid and row labels
self.grid_and_row_labels_row = BoxLayout(orientation='horizontal', size_hint=(1, 0.95))
# Row labels layout
self.row_labels_layout = BoxLayout(orientation='vertical', size_hint=(0.05, 1))
self.grid_and_row_labels_row.add_widget(self.row_labels_layout) # This line was already correct.
# Grid layout for the game board
self.grid_size = grid_size # Ensure grid_size is assigned before using it for labels
# Populate Column Labels
# Assuming grid_size = (rows, columns), so grid_size[1] is number of columns
for i in range(1, self.grid_size[1] + 1):
label = Label(
text=str(i),
size_hint=(1, 1), # Even distribution
halign='center',
valign='middle',
font_size=Window.height * 0.015
)
self.col_labels_layout.add_widget(label)
# Populate Row Labels
# Assuming grid_size = (rows, columns), so grid_size[0] is number of rows
for i in range(1, self.grid_size[0] + 1):
label = Label(
text=str(i),
size_hint=(1, 1), # Even distribution
halign='center',
valign='middle',
font_size=Window.height * 0.015
)
self.row_labels_layout.add_widget(label)
self.grid_layout = GridLayout(
cols=self.grid_size[1], rows=self.grid_size[0], spacing=1, size_hint=(0.95, 1) # Adjusted size_hint
)
with self.grid_layout.canvas.before:
Color(0.2, 0.2, 0.2, 1) # Darker grey background
self.grid_rect = Rectangle(pos=self.grid_layout.pos, size=self.grid_layout.size)
self.grid_layout.bind(
pos=lambda instance, value: setattr(self.grid_rect, 'pos', value),
size=lambda instance, value: setattr(self.grid_rect, 'size', value)
)
# Initialize grid buttons
self.grid_buttons = []
total_cells = self.grid_size[0] * self.grid_size[1]
# Use the marker_percentage from StartScreen, default to 0.1 if not provided
max_circles = int(total_cells * marker_percentage)
# Create a list of all possible (row, col) coordinates
# grid_size = (rows, columns)
all_coordinates = []
for r in range(self.grid_size[0]): # Iterate through rows
for c in range(self.grid_size[1]): # Iterate through columns
all_coordinates.append((r, c))
# Randomly shuffle the list of all possible coordinates
random.shuffle(all_coordinates)
# Select the first `max_circles` coordinates for "O" markers
o_marker_locations_list = all_coordinates[:max_circles]
o_marker_locations_set = set(o_marker_locations_list)
# grid_size = (rows, columns)
for row in range(self.grid_size[0]): # Iterate through rows
button_row = []
for col in range(self.grid_size[1]): # Iterate through columns
if (row, col) in o_marker_locations_set:
# Instantiate the custom OMarkerWidget
btn = OMarkerWidget()
# Add the new widget to o_marker_buttons list
self.o_marker_buttons.append(btn)
# Start its animations
# It's better to schedule this to ensure the widget has been added to layout
# and has its initial size determined, so animations are smooth.
Clock.schedule_once(lambda dt, widget=btn: widget.start_animations(), 0)
else:
btn = ImageButton(
source='', # Initially no image
allow_stretch=True,
keep_ratio=True,
size_hint=(1, 1),
coords=(row, col),
text=''
)
btn.bind(on_press=self.on_grid_button_press)
# btn.bind(on_release=self.show_company_info) # Removed as per instruction
btn.disabled = True # Initially, all non-circle buttons are disabled
button_row.append(btn)
self.grid_layout.add_widget(btn)
self.grid_buttons.append(button_row)
# Pass the collected 'O' marker locations to GameState
self.game_state.set_initial_o_marker_locations(o_marker_locations_set)
# Removed animation for 'O' marker buttons as per subtask instructions
# The new 'O' markers are Widgets with Ellipses, not Buttons with text/color animations.
self.grid_and_row_labels_row.add_widget(self.grid_layout) # Add grid_layout to its new parent
self.grid_plus_labels_container.add_widget(self.grid_and_row_labels_row) # Add the row containing grid and row labels
self.game_layout.add_widget(self.grid_plus_labels_container) # Add the main container to game_layout
# Button layout for additional actions
button_layout = BoxLayout(
orientation='horizontal', size_hint=(1, 0.1), spacing=10
)
self.end_turn_button = Button(
text="End Turn", on_press=self.process_human_end_turn, font_size=18, # Changed on_press
disabled=True # Initially disabled
)
self.share_management_button = Button(
text="Share Management", on_press=self.show_share_management_popup, font_size=18
)
self.toggle_sidebar_button = Button(
text="Toggle Sidebar", on_press=self.toggle_sidebar, font_size=18
)
button_layout.add_widget(self.end_turn_button)
button_layout.add_widget(self.share_management_button)
button_layout.add_widget(self.toggle_sidebar_button)
self.game_layout.add_widget(button_layout)
self.grid_plus_labels_container.bind(size=self.update_game_board_layout)
self.main_layout.add_widget(self.game_layout)
self.update_player_info()
# Start the first turn
self.next_turn()
Clock.schedule_once(self._finalize_initial_layout, 0.5) # 0.5s delay
def _finalize_initial_layout(self, dt):
# Add this line at the beginning of the method:
if hasattr(self, 'grid_plus_labels_container'): # Check if it exists
self.update_game_board_layout(self.grid_plus_labels_container, self.grid_plus_labels_container.size)
print("Executing _finalize_initial_layout: opening sidebar first.")
# Ensure sidebar_visible is False before opening, so animate_sidebar_open runs correctly.
# This also means that animate_sidebar_open will set self.sidebar_visible = True
self.sidebar_visible = False
self.animate_sidebar_open()
# Schedule the close animation to happen after the open animation has likely completed.
# The open animation is 0.3s. We'll schedule close for 0.5s after this method starts.
Clock.schedule_once(lambda edt: self._actually_close_initial_sidebar(), 0.5)
def _actually_close_initial_sidebar(self):
print("Executing _actually_close_initial_sidebar.")
# At this point, sidebar_visible should be True due to animate_sidebar_open()
# having been called and completed.
if self.sidebar_visible:
self.animate_sidebar_close()
else:
# This case might indicate a timing issue or unexpected state change.
print("Warning: Sidebar was not visible as expected before attempting to close in initial sequence.")
# As a fallback, still try to run animate_sidebar_close as it sets the final layout properties
# and ensures sidebar_visible is False.
self.sidebar_visible = True # Temporarily set to true so animate_sidebar_close runs fully
self.animate_sidebar_close()
def handle_game_state_update(self, updated_entries):
"""
Callback function to handle updates from GameState.
It receives a list of tuples: (coords, company_name)
"""
print(f"Handling game state update with {len(updated_entries)} entries.")
for coords, company_name in updated_entries:
row, col = coords
# grid_size = (rows, columns)
if 0 <= row < self.grid_size[0] and 0 <= col < self.grid_size[1]:
button = self.grid_buttons[row][col]
self.update_grid_button(button, company_name)
print(f"Updated button at ({row}, {col}) to company '{company_name}'.")
else:
print(f"Warning: Coordinates {coords} are out of bounds for grid_size {self.grid_size}.")
def verify_images(self):
for name, path in self.game_state.company_logos.items():
if not os.path.exists(path):
print(f"Error: {path} does not exist.")
else:
print(f"Loaded {name} logo from {path}.")
if not os.path.exists(self.game_state.diamond_image_path):
print(f"Warning: Diamond image not found at {self.game_state.diamond_image_path}. Falling back to text.")
def on_grid_button_press(self, instance):
"""
Handle button press logic when a grid square is clicked.
"""
# Enable the end turn button after a valid selection
self.end_turn_button.disabled = False
if instance.disabled:
return # Ignore clicks on disabled buttons
current_coords = instance.coords
current_player = self.game_state.players[self.game_state.current_player_index]
# Check for adjacent companies using company_map
adjacent_companies = self.game_state.get_adjacent_companies(current_coords)
if adjacent_companies:
if len(adjacent_companies) == 1:
# Expand the existing company
company_name = adjacent_companies.pop()
self.game_state.expand_company(current_coords, company_name, current_player)
# The UI will be updated via the callback
self.info_label.text = f"{current_player} expanded {company_name}!"
# Perform flip animation upon expansion
self.perform_flip_animation(instance)
else:
# Merge companies
self.game_state.merge_companies(current_coords, adjacent_companies, current_player)
merged_company_name = self.game_state.company_map[current_coords]["company_name"]
# The UI will be updated via the callback
self.info_label.text = f"{current_player} merged companies into {merged_company_name}!"
# Perform flip animation upon merging
self.perform_flip_animation(instance)
else: # No adjacent companies
# current_player is already defined in this scope
if self.game_state.available_company_names and self.game_state._can_found_company_at(current_coords):
# Create a new company
company_name, message = self.game_state.create_new_company(current_coords, current_player)
if company_name:
# The UI will be updated via the callback from create_new_company
self.info_label.text = message
self.perform_flip_animation(instance) # instance is the button
else:
# create_new_company failed (e.g. trying to create on 'O' marker itself)
self.info_label.text = message
# Potentially place diamond if creation failed?
# For now, let's assume if _can_found_company_at was true, but create_new_company failed,
# it's a specific rule interaction (like on 'O' marker) and not a diamond placement.
# The original human logic didn't have a fallback to diamond here if is_adjacent_to was true.
# However, the AI logic *does* fallback. For consistency, maybe human should too.
# Let's make it consistent: if create_new_company fails, human also tries diamond.
if "Cannot create company on an 'O' marker tile" in message: # Or similar check
self.info_label.text = message + " Try placing a diamond." # Guide user
# No automatic diamond placement here, user must click again if they want diamond.
# Button will be disabled after this interaction by disable_grid_buttons().
# This makes it less like AI, but gives human more control after failure.
pass
# If create_new_company failed for other reasons (e.g. no names), message is already set.
else: # No available company names or cannot found company at current_coords
# Place a diamond
self.place_diamond(instance) # instance is the button
# self.info_label.text is set by place_diamond or its callers if needed.
# For direct call here, we might need:
# self.info_label.text = f"{current_player} placed a diamond."
# However, self.place_diamond itself calls game_state.place_diamond which returns a message.
# The current self.place_diamond(self, instance) in GameScreen:
# success, message = self.game_state.place_diamond(current_coords, current_player_name)
# if success: # updates visuals
# else: self.info_label.text = message
# This seems fine. If place_diamond fails, it sets info_label. If it succeeds, a message is not explicitly set here,
# but game_state.place_diamond does return one. Let's ensure a generic success message if place_diamond itself doesn't set one.
# Checking place_diamond: it *does not* set info_label on success.
# So we should set it here.
if instance.source == self.game_state.diamond_image_path or instance.text == "◆": # Check if diamond was actually placed
self.info_label.text = f"{current_player} placed a diamond at {current_coords}."
# If place_diamond failed, it will have set its own error message.
# After the move, expand companies into adjacent diamonds
self.expand_companies_into_adjacent_diamonds()
self.disable_grid_buttons()
self.update_player_info()
self.end_turn_button.disabled = False # Enable end turn button after human move
def perform_flip_animation(self, instance):
"""
Performs a single flip animation on the given instance.
This is used when a diamond is created or converted into a company.
"""
instance.scale_x = 1 # Reset scale
flip_animation = Animation(scale_x=-1, duration=0.25) + Animation(scale_x=1, duration=0.25)
flip_animation.start(instance)
def place_diamond(self, instance):
"""
Place a diamond on the selected square and handle potential mergers.
"""
current_coords = instance.coords
current_player_name = self.game_state.players[self.game_state.current_player_index]
success, message = self.game_state.place_diamond(current_coords, current_player_name)
if success:
if self.valid_diamond_path:
# Set the image source to diamond.png
instance.source = self.valid_diamond_path
instance.reload()
instance.color = [1, 1, 1, 1]
# Start single flip animation
instance.scale_x = 1
animation = Animation(scale_x=-1, duration=0.5) + Animation(scale_x=1, duration=0.5)
# Removed animation.repeat = True to prevent continuous flipping
animation.start(instance)
else:
# Fallback to using Unicode diamond character
instance.text = u"◆"
instance.color = (1, 1, 1, 1)
instance.font_size = 24 # Increased font size for visibility
else:
self.info_label.text = message
def expand_companies_into_adjacent_diamonds(self):
"""
Check all diamonds on the board to see if they are adjacent to any companies.
If so, handle expansions or mergers accordingly.
"""
diamonds_to_remove = set()
for diamond_coords in list(self.game_state.diamond_positions):
adjacent_companies = self.game_state.get_adjacent_companies(diamond_coords)
if adjacent_companies:
button = self.grid_buttons[diamond_coords[0]][diamond_coords[1]]
if len(adjacent_companies) == 1:
# Expand the company into the diamond
company_name = adjacent_companies.pop()
self.game_state.expand_company(diamond_coords, company_name, self.game_state.players[self.game_state.current_player_index])
# The UI will be updated via the callback
self.info_label.text += f" {company_name} expanded into a diamond!"
# Perform flip animation upon expansion
self.perform_flip_animation(button)
else:
# Merge companies via the diamond
self.game_state.merge_companies(diamond_coords, adjacent_companies, self.game_state.players[self.game_state.current_player_index])
merged_company_name = self.game_state.company_map[diamond_coords]["company_name"]
# The UI will be updated via the callback
self.info_label.text += f" Companies merged into {merged_company_name} via a diamond!"
# Perform flip animation upon merging
self.perform_flip_animation(button)
# Mark the diamond for removal
diamonds_to_remove.add(diamond_coords)
# Remove the diamonds that have been absorbed
self.game_state.diamond_positions -= diamonds_to_remove
def update_grid_button(self, button, company_name):
"""
Update the grid button to reflect the company.
This includes setting the correct logo and color.
"""
if company_name in self.valid_company_logos:
logo_path = self.valid_company_logos[company_name]
button.source = '' # Clear current image
button.reload() # Process clearing
def _set_final_logo(dt): # Inner callback
button.source = logo_path
button.reload()
button.color = [1, 1, 1, 1] # Reset color with logo
print(f"Set button source (scheduled) to '{logo_path}' for company '{company_name}'.")
Clock.schedule_once(_set_final_logo, 0) # Schedule for next frame
else: # Fallback if logo doesn't exist
button.source = '' # Ensure no image is set
button.reload() # Process clearing
# Fallback color logic from previous implementation (tinting the non-existent image)
default_color = [0.5, 0.5, 0.5, 1] # A neutral default color
if hasattr(self.game_state, 'company_colors'):
button.color = self.game_state.company_colors.get(company_name, default_color)
# This print might be confusing as it says "Set button color" but it's tinting an empty source.
# For consistency with previous code, it's kept. A better approach might be button.background_color.
print(f"Logo for '{company_name}' not found. Set button color (tint) to '{button.color}'.")
else:
button.color = default_color
print(f"Logo for '{company_name}' not found and no company_colors. Set button color (tint) to default '{button.color}'.")
# These should apply immediately:
button.text = ""
button.angle = 0
button.scale_x = 1
if hasattr(button, 'anim') and button.anim:
button.anim.cancel(button)
# Using delattr as suggested in the problem description for safety
if hasattr(button, 'anim'): # Check again in case cancel somehow removed it
delattr(button, 'anim')
button.disabled = True
print(f"Button at ({button.coords[0]}, {button.coords[1]}) properties reset and disabled.")
def next_turn(self, instance=None):
"""
Proceed to the next player's turn.
"""
# Disable the end turn button until a new action is taken
self.end_turn_button.disabled = True
# Removed logic for incrementing player index and turn counter (handled by GameState.end_turn())
self.update_player_info()
current_player_name = self.game_state.players[self.game_state.current_player_index]
player_type = self.game_state.get_player_type(current_player_name)
self.info_label.text = f"{current_player_name}'s Turn ({player_type})"
if player_type == "AI (Easy)":
self.info_label.text += " - Thinking..."
self.disable_grid_buttons() # Stops blinking, disables all grid buttons
self.end_turn_button.disabled = True
Clock.schedule_once(self.run_ai_turn, 1.0) # 1 second delay
else: # Human player
self.enable_grid_buttons()
self.end_turn_button.disabled = True # Will be enabled once human makes a move
# End game check after turn length reached
if self.game_state.turn_counter >= self.game_turn_length:
self.end_game()
def run_ai_turn(self, dt):
"""
Executes an AI player's turn.
"""
current_player_name = self.game_state.players[self.game_state.current_player_index]
selected_cell, action_message = self.game_state.ai_take_turn(current_player_name)
self.info_label.text = action_message # Display AI action
# Visual Update for AI's move AND Animation
if selected_cell is not None and \
0 <= selected_cell[0] < self.grid_size[0] and \
0 <= selected_cell[1] < self.grid_size[1]:
button_to_animate = self.grid_buttons[selected_cell[0]][selected_cell[1]]
# If it's not a company tile after AI's move, then it might be a diamond.
# Company tiles visuals are updated by the callback handle_game_state_update.
# Diamond visuals are handled here.
if selected_cell not in self.game_state.company_map:
# This is the existing diamond visual update logic
if isinstance(button_to_animate, ImageButton): # Check if it's an ImageButton
if self.valid_diamond_path:
button_to_animate.source = self.valid_diamond_path
button_to_animate.reload()
else:
button_to_animate.text = "◆"
button_to_animate.font_size = 24
button_to_animate.color = [1, 1, 1, 1]
print(f"AI Debug: Updated button {selected_cell} for diamond after AI turn.")
# If it's an OMarkerWidget, it cannot become a diamond. No visual update needed here for it.
# Perform flip animation for the AI's chosen cell if it's an ImageButton
# This will animate the button whether it became a company (updated by callback) or a diamond (updated above).
if isinstance(button_to_animate, ImageButton):
self.perform_flip_animation(button_to_animate)
# OMarkerWidget instances do not have perform_flip_animation and should not be selected by AI.
elif selected_cell is not None: # selected_cell was not None, but was out of bounds
print(f"AI Error: selected_cell {selected_cell} is out of bounds for the grid. Rows: {self.grid_size[0]}, Cols: {self.grid_size[1]}")
# info_label is already set by action_message.
# If selected_cell is None, info_label already has a message like "no available moves".
# Nothing specific to do for visuals if selected_cell is None.
self.update_player_info()
self.expand_companies_into_adjacent_diamonds() # Important after AI move
self.disable_grid_buttons() # Ensure grid is disabled after AI move
success, end_turn_message = self.game_state.end_turn()
if not success:
self.info_label.text = f"AI Error: {end_turn_message}" # Update info label with error
# Potentially show a popup or handle error more gracefully
# For now, allowing human to see the issue and maybe End Turn button becomes active
self.end_turn_button.disabled = False
return # Do not proceed to next turn if AI failed its turn obligation
# self.game_state.end_turn() has updated player_index and turn_counter
self.next_turn() # Sets up UI for the new current player (Human or AI)
def process_human_end_turn(self, instance):
"""
Processes a human player's attempt to end their turn.
"""
current_player_name = self.game_state.players[self.game_state.current_player_index]
success, message = self.game_state.end_turn()
if success:
self.info_label.text = message
self.next_turn()
else: # Player hasn't made a move or other end_turn condition not met
self.info_label.text = message
Popup(title='Move Required',
content=Label(text=message),
size_hint=(0.5, 0.3)).open()
def buy_shares(self, company_name, player, amount):
"""
Allow players to buy shares in a company.
"""
success, message = self.game_state.buy_shares(company_name, player, amount)
self.info_label.text = message
self.update_player_info()
def sell_shares(self, company_name, player, amount):
"""
Allow players to sell shares in a company.
"""
success, message = self.game_state.sell_shares(company_name, player, amount)
self.info_label.text = message
self.update_player_info()
def show_share_management_popup(self, instance):
"""
Show a popup to manage shares (buy or sell).
"""
current_player = self.game_state.players[self.game_state.current_player_index]
cash = self.game_state.player_wealth[current_player]
holdings_value = 0
for company, num_shares in self.game_state.player_shares[current_player].items():
if company in self.game_state.company_info:
share_value = self.game_state.company_info[company]["value"]
total_share_value = share_value * num_shares
holdings_value += total_share_value
total_wealth = cash + holdings_value
content = BoxLayout(orientation='vertical', spacing=10, padding=10)
# Player's cash and holdings at the top
player_info_layout = BoxLayout(orientation='horizontal', size_hint=(1, 0.1))
player_cash_label = Label(text=f"Cash: £{cash}", size_hint=(0.5, 1))
player_holdings_label = Label(text=f"Holdings Value: £{holdings_value}", size_hint=(0.5, 1))
player_info_layout.add_widget(player_cash_label)
player_info_layout.add_widget(player_holdings_label)
content.add_widget(player_info_layout)
# Company information header
header_layout = BoxLayout(orientation='horizontal', size_hint=(1, 0.1))
header_layout.add_widget(Label(text="[b]Logo[/b]", size_hint=(0.2, 1), markup=True))
header_layout.add_widget(Label(text="[b]Company[/b]", size_hint=(0.2, 1), markup=True))
header_layout.add_widget(Label(text="[b]Price[/b]", size_hint=(0.2, 1), markup=True))
header_layout.add_widget(Label(text="[b]Your Shares[/b]", size_hint=(0.2, 1), markup=True))
header_layout.add_widget(Label(text="[b]Total Value[/b]", size_hint=(0.2, 1), markup=True))
content.add_widget(header_layout)
# List of companies with their prices and player's holdings
companies_layout = GridLayout(cols=1, size_hint=(1, 0.4))
for company in self.game_state.company_info.keys():
company_layout = BoxLayout(orientation='horizontal', size_hint=(1, None), height=60)
# Add Company Logo
if company in self.valid_company_logos:
logo_path = self.valid_company_logos[company]
logo = Image(source=logo_path, size_hint=(0.2, 1), allow_stretch=True, keep_ratio=True)
else:
# Placeholder if logo not found
logo = Label(text="No Logo", size_hint=(0.2, 1))
company_layout.add_widget(logo)
# Add Company Name
company_label = Label(text=company, size_hint=(0.2, 1), halign='left', valign='middle')
company_label.bind(size=company_label.setter('text_size'))
company_layout.add_widget(company_label)
# Add Company Price
price = self.game_state.company_info[company]["value"]
price_label = Label(text=f"£{price}", size_hint=(0.2, 1))
company_layout.add_widget(price_label)
# Add Player's Shares
num_shares = self.game_state.player_shares[current_player].get(company, 0)
shares_label = Label(text=str(num_shares), size_hint=(0.2, 1))
company_layout.add_widget(shares_label)