-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
957 lines (872 loc) · 34.8 KB
/
Copy pathclasses.py
File metadata and controls
957 lines (872 loc) · 34.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
import random
from enum import Enum
from tabulate import tabulate
from enum import Enum
import os
from text_pictures import trophy
import numpy as np
class InvalidPlayerData(Exception):
pass
class FieldNotOnBoardError(Exception):
def __init__(self):
super().__init__('Given row and column must be beetwen 0 snd 5')
class WrongFieldDataError(Exception):
def __init__(self):
super().__init__('Given row and column must be numbers')
class OccupiedFieldEror(Exception):
def __init__(self):
super().__init__('You cannot place a sign on occupied field')
class WrongSignError(Exception):
def __init__(self):
super().__init__('Sign must be either cross or circle')
class WrongFieldError(Exception):
pass
class Direction(Enum):
diagonally_long_up = 'diagonally_long_up'
diagonally_long_down = 'diagonally_long_down'
diagonally_short_up = 'diagonally_short_up'
diagonally_short_down = 'diagonally_short_down'
horizontally = 'horizontally'
vertically = 'vertically'
class BoardCombinations():
"""
Klasa reprezentująca planszę 6x6 wykorzystywaną w grze
w celach analizy zawartości planszy w trakcie rozgrywki.
Puste pola są w niej reprezentowane znakiem '-'
Domyślnie tworzona jako pusta plansza.
Atrybuty klasy:
X_combinations:
Kombinacje złożone z X na planszy
O_combinations:
Kombinacje złożone z O na planszy
X_counter:
Licznik długości kombinacji z X
O_counter:
Licznik długości kombinacji z O
state:
Stan (zawartość) planszy reprezentowany przez macierz (listę)
Pola reprezentowane są wierszem i kolumną stanu planszy
"""
def __init__(self, board_state=None):
self._X_combinations = []
self._O_combinations = []
self._X_counter = 0
self._O_counter = 0
self._state = board_state if board_state else [
['-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-'],
['-', '-', '-', '-', '-', '-']
]
def state(self):
return self._state
def set_board_state(self, board_state):
self._state = board_state
def X_counter(self):
return self._X_counter
def O_counter(self):
return self._O_counter
def is_full(self):
"""Sprawdza czy plansza jest pełna"""
for i in range(6):
if '-' in self.state()[i]:
return False
return True
def is_field_empty_and_on_board(self, field):
"""
Przyjmuje współżędne pola na planszy (wiersz i kolumnę)
Sprawdza czy pole jest puste i czy jest na planszy
"""
row = field[0]
column = field[1]
if 0 <= row <= 5 and 0 <= column <= 5:
if self._state[row][column] == '-':
return True
return False
def is_win_order(self):
"""
Sprawdza czy porządek zwycięrzył
(Czy jest na planszy kombinacja o długości 5)
"""
combinations = self.find_combinations()
for O_X_combination in combinations:
for combination in O_X_combination:
if combination[0] == 5:
return True
return False
def O_combination_update(
self,
row_number,
column_number,
direction,
previous_O_count
):
"""
Funkcja dodaje kombinacje znaków O do O_combinations,
jeśli dana kombinacja skończyła się poprzez wystąpienie
pustego pola lub przeciwnego znaku na następnym polu.
Kombinacja znaków jest reprezentowana przez
długość kombinacji, wiersz i kolumnę zakończenia kombinacji
oraz jej kierunek.
"""
if self._O_counter == 0 and previous_O_count >= 1:
if direction == Direction.horizontally.value:
self._O_combinations.append((
previous_O_count,
row_number,
column_number - 1,
direction
))
if direction == Direction.vertically.value:
self._O_combinations.append((
previous_O_count,
row_number - 1,
column_number,
direction
))
if direction == Direction.diagonally_short_up.value:
self._O_combinations.append((
previous_O_count,
row_number + 1,
column_number - 1,
direction
))
if direction == Direction.diagonally_long_up.value:
self._O_combinations.append((
previous_O_count,
row_number + 1,
column_number - 1,
direction
))
if direction == Direction.diagonally_short_down.value:
self._O_combinations.append((
previous_O_count,
row_number - 1,
column_number - 1,
direction
))
if direction == Direction.diagonally_long_down.value:
self._O_combinations.append((
previous_O_count,
row_number - 1,
column_number - 1,
direction
))
def O_combination_update_edge(
self,
row_number,
column_number,
direction,
):
"""
Funkcja dodaje kombinacje znaków O do O_combinations,
jeśli dana kombinacja dotarła do krawędzi planszy
(następne pole byłoby poza planszą).
Kombinacja znaków jest reprezentowana przez
długość kombinacji, wiersz i kolumnę zakończenia kombinacji
oraz jej kierunek.
"""
if self._O_counter != 0:
if direction == Direction.horizontally.value:
if column_number == 5:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if direction == Direction.vertically.value:
if row_number == 5:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_short_up.value:
if row_number == 0 and column_number == 4:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if row_number == 1 and column_number == 5:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_long_up.value:
if row_number == 0 and column_number == 5:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_short_down.value:
if row_number == 5 and column_number == 4:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if row_number == 4 and column_number == 5:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_long_down.value:
if row_number == 5 and column_number == 5:
self._O_combinations.append((
self._O_counter,
row_number,
column_number,
direction
))
def X_combination_update(
self,
row_number,
column_number,
direction,
previous_X_count
):
"""
Funkcja dodaje kombinacje znaków X do X_combinations,
jeśli dana kombinacja skończyła się poprzez wystąpienie
pustego pola lub przeciwnego znaku na następnym polu.
Kombinacja znaków jest reprezentowana przez
długość kombinacji, wiersz i kolumnę zakończenia kombinacji
oraz jej kierunek.
"""
if self._X_counter == 0 and previous_X_count >= 1:
if direction == Direction.horizontally.value:
self._X_combinations.append((
previous_X_count,
row_number,
column_number - 1,
direction
))
if direction == Direction.vertically.value:
self._X_combinations.append((
previous_X_count,
row_number - 1,
column_number,
direction
))
if direction == Direction.diagonally_short_up.value:
self._X_combinations.append((
previous_X_count,
row_number + 1,
column_number - 1,
direction
))
if direction == Direction.diagonally_long_up.value:
self._X_combinations.append((
previous_X_count,
row_number + 1,
column_number - 1,
direction
))
if direction == Direction.diagonally_short_down.value:
self._X_combinations.append((
previous_X_count,
row_number - 1,
column_number - 1,
direction
))
if direction == Direction.diagonally_long_down.value:
self._X_combinations.append((
previous_X_count,
row_number - 1,
column_number - 1,
direction
))
def X_combination_update_edge(
self,
row_number,
column_number,
direction,
):
"""
Funkcja dodaje kombinacje znaków X do X_combinations,
jeśli dana kombinacja dotarła do krawędzi planszy
(następne pole byłoby poza planszą).
Kombinacja znaków jest reprezentowana przez
długość kombinacji, wiersz i kolumnę zakończenia kombinacji
oraz jej kierunek.
"""
if self._X_counter != 0:
if direction == Direction.horizontally.value:
if column_number == 5:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if direction == Direction.vertically.value:
if row_number == 5:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_short_up.value:
if row_number == 0 and column_number == 4:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if row_number == 1 and column_number == 5:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_long_up.value:
if row_number == 0 and column_number == 5:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_short_down.value:
if row_number == 5 and column_number == 4:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if row_number == 4 and column_number == 5:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
if direction == Direction.diagonally_long_down.value:
if row_number == 5 and column_number == 5:
self._X_combinations.append((
self._X_counter,
row_number,
column_number,
direction
))
def combinations_counter_update(
self,
sign,
row_number,
column_number,
direction
):
"""
Funkcja aktualizuje wartość liczników długości kombinacji
O_counter i X_counter.
Jeśli znak na polu to X licznik X_counter ziększa się o 1
(wydłużenie kombuinacji X o 1),
a licznik O_counter jest wyzerowany (zakończenie
lub podtrzymanie braku kombinacji O), analogicznie dla pola o znaku O.
Gdy pole jest puste oba liczniki są wyzerowane ponieważ,
oznacza to zakończenie lub brak kombinacji.
Wartości liczników przekazane są do funkcji aktualizującej kombinacje.
"""
previous_X_count = self._X_counter
previous_O_count = self._O_counter
if sign == 'X':
self._X_counter += 1
self._O_counter = 0
if sign == 'O':
self._O_counter += 1
self._X_counter = 0
if sign == '-':
self._O_counter = 0
self._X_counter = 0
self.O_combination_update(
row_number,
column_number,
direction,
previous_O_count
)
self.O_combination_update_edge(
row_number,
column_number,
direction,
)
self.X_combination_update(
row_number,
column_number,
direction,
previous_X_count
)
self.X_combination_update_edge(
row_number,
column_number,
direction,
)
def find_combinations(self):
"""
Funkcja dokonuje skanowania po kolejnych wierszach,
kolumnach i ukosach planszy wywołując aktualizacje liczników
kombinacji dla każdego napotkanego pola.
Skanowanie odbywa się od lewej do prawej
Skanowanie odbywa się kolejno po:
Wierszach
Kolumnach
Krótkich przekątnych skierowanych do góry (diagonally_short_up):
Są to dwie przekątne o długości 4 pola,
pierwsza z początkiem na polu (4,0) i końcem (0,4),
a druga z początkiem na (5,1) i końcem (1,5)
Krótkich przekątnych skierowanych do dołu (diagonally_short_down):
Są to dwie przekątne o długości 4 pola,
pierwsza z początkiem na polu (0,1) i końcem (4,5),
a druga z początkiem na (1,0) i końcem (5,4)
Długiej przekątnej skierowanej do góry (diagonally_long_up):
Przekątna o długości 5 pól
z początkiem na polu (5,0) i końcem (0,5)
Długiej przekątnej skierowanej do dołu (diagonally_long_down):
Przekątna o długości 5 pól
z początkiem na polu (0,0) i końcem (5,5)
"""
self._X_combinations = []
self._O_combinations = []
row_number = 0
for row in range(6):
row = self._state[row]
column_number = 0
self._X_counter = 0
self._O_counter = 0
for sign in row:
self.combinations_counter_update(
sign,
row_number,
column_number,
Direction.horizontally.value
)
column_number += 1
row_number += 1
for column in range(6):
row_number = 0
self._X_counter = 0
self._O_counter = 0
for row in range(6):
sign = self._state[row][column]
self.combinations_counter_update(
sign,
row_number,
column,
Direction.vertically.value
)
row_number += 1
for i in range(2):
self._X_counter = 0
self._O_counter = 0
for row in range(5):
row_number = 4 + i - row
column = row + i
sign = self._state[row_number][column]
self.combinations_counter_update(
sign,
row_number,
column,
Direction.diagonally_short_up.value
)
for i in range(2):
self._X_counter = 0
self._O_counter = 0
for row in range(5):
row_number = row - i + 1
column = i + row
sign = self._state[row_number][column]
self.combinations_counter_update(
sign,
row_number,
column,
Direction.diagonally_short_down.value
)
self._X_counter = 0
self._O_counter = 0
for row in range(6):
sign = self._state[row][row]
self.combinations_counter_update(
sign,
row,
row,
Direction.diagonally_long_down.value
)
self._X_counter = 0
self._O_counter = 0
for row in range(6):
row_number = 5 - row
column = row
sign = self._state[row_number][column]
self.combinations_counter_update(
sign, row_number, column, Direction.diagonally_long_up.value)
return self._X_combinations, self._O_combinations
def is_5_combination_possible(self, chosen_comb, field):
field_row, field_column = field[0:2]
comb_sign = chosen_comb[4]
comb_direction = chosen_comb[3]
X_combinations, O_combinations = self.find_combinations()
if comb_direction in (
Direction.diagonally_short_down.value,
Direction.diagonally_short_up.value):
return True
if comb_sign == 'X':
if comb_direction == Direction.horizontally.value:
for combination in O_combinations:
if (
combination[3] == Direction.horizontally.value
and combination[1] == field_row
):
if combination[0] > 1:
return False
if combination[2] not in (0, 5):
return False
return True
if comb_direction == Direction.vertically.value:
for combination in O_combinations:
if (
combination[3] == Direction.vertically.value
and combination[2] == field_column
):
if combination[0] > 1:
return False
if combination[1] not in (0, 5):
return False
return True
if comb_direction == Direction.diagonally_long_up.value:
for combination in O_combinations:
if combination[3] == Direction.diagonally_long_up.value:
if combination[0] > 1:
return False
if combination[1] != 5 and combination[2] != 0:
if combination[1] != 0 and combination[2] != 5:
return False
return True
if comb_direction == Direction.diagonally_long_down.value:
for combination in O_combinations:
if combination[3] == Direction.diagonally_long_down.value:
if combination[0] > 1:
return False
if combination[1] != 5 and combination[2] != 5:
if combination[1] != 0 and combination[2] != 0:
return False
return True
if comb_sign == 'O':
if comb_direction == Direction.horizontally.value:
for combination in X_combinations:
if (
combination[3] == Direction.horizontally.value
and combination[1] == field_row
):
if combination[0] > 1:
return False
if combination[2] not in (0, 5):
return False
return True
if comb_direction == Direction.vertically.value:
for combination in X_combinations:
if (
combination[3] == Direction.vertically.value
and combination[2] == field_column
):
if combination[0] > 1:
return False
if combination[1] not in (0, 5):
return False
return True
if comb_direction == Direction.diagonally_long_up.value:
for combination in X_combinations:
if combination[3] == Direction.diagonally_long_up.value:
if combination[0] > 1:
return False
if combination[1] != 5 and combination[2] != 0:
if combination[1] != 0 and combination[2] != 5:
return False
return True
if comb_direction == Direction.diagonally_long_down.value:
for combination in X_combinations:
if combination[3] == Direction.diagonally_long_down.value:
if combination[0] > 1:
return False
if combination[1] != 5 and combination[2] != 5:
if combination[1] != 0 and combination[2] != 0:
return False
return True
def move_with_5_comb(
self,
chosen_comb,
first_field,
second_field,
order_factor):
"""
Funkcja sprawdza czy podane pola są na planszy i czy w lini
określonej przez wybraną kombinację (chosen_comb)
możliwe jest stworzenie kombinacji
o długości 5 znaków (określonych przez znak kombinacji)
Dodatkowo warunkiem sprawdzenia pierwszego pola
jest bool(order_factor) == True.
"""
if self.is_field_empty_and_on_board(first_field) and order_factor:
if self.is_5_combination_possible(chosen_comb, first_field):
return first_field
if self.is_field_empty_and_on_board(second_field):
if self.is_5_combination_possible(chosen_comb, second_field):
return second_field
class Board(BoardCombinations):
"""Klasa Board dziedziczy po BoardCombinations.
reprezentuje ona polanszę 6x6 gry i wykorzystana jest w wizualnym
interfejsie urzytkownika.
"""
def put(self, sign, row, column):
"""
Funkcja przyjmuje współżędne pola oraz jego znak,
a następnie wstawia ten znak na podane pole.
Może ona wznośić następujące wyjątki:
WrongFieldData gdy któraś współżędna pola nie jest liczbą
FieldNotOnBoard gdy podane pole nie znajduje się na planszy
OccupiedFieldError gdy podane pole nie jest pustego
Wrong sign error gdy podany znak nie jest X lub O
"""
if isinstance(row, str) and isinstance(column, str):
if not row.isdigit() or not column.isdigit():
raise WrongFieldDataError
row = int(row)
column = int(column)
if row not in range(6) or column not in range(6):
raise FieldNotOnBoardError
if self._state[row][column] != '-':
raise OccupiedFieldEror
if sign not in ('X', 'x', 'o', 'O'):
raise WrongSignError
if sign in ('X', 'x'):
self._state[row][column] = 'X'
else:
self._state[row][column] = 'O'
def board_print(self):
"""
Funkcja wypisuje stan planszy na ekran w formie tabeli.
Tabela tworzona jest przy pomocy biblioteki tabulate
w formacie fancy_grid.
Nagłówek tabeli to kolejne liczby od 0 do 5,
a indeksowanie wierszy jest włączone (od 0 do 5)
"""
header = ['0', '1', '2', '3', '4', '5']
print(
tabulate(
self._state,
headers=header,
tablefmt='fancy_grid',
showindex=True))
class Player:
"""
Klasa Player reprezentuje gracza w grze.
Ruchy gracza klasy Player sterowane są przez urzytkownika.
Atrybuty:
Nazwa
Cel:
Określa on czy gracz jest porządkiem czy chaosem,
(czyli czy jego cel to stworzenie kombinacji
czy powstrzymanie jej otworzenia)
"""
def __init__(self, name, player_goal):
"""
Tworząc instancję klasy Player niezbędne jest podanie
jego imienia oraz celu gry,
a cel gry musi być to chaos lub order (chaos lub porządek),
w przeciwnym wypadku zostaje wzniesiony wyjątek InvalidPlayerData.
"""
if not name:
raise InvalidPlayerData('Player needs to have name')
if player_goal not in ('chaos', 'order'):
raise InvalidPlayerData('Player has to be either chaos or order')
self._name = name
self._goal = player_goal
def name(self):
return self._name
def goal(self):
return self._goal
def generate_move(self, board_combinations=None):
"""
Funkcja zwraca dane niezbędne do wykonania ruchu
(współżędne pola i jego znak) na podstawie danych
wpisanych przez użytkownika w terminalu.
"""
row = input('Row: ')
column = input('Column: ')
sign = input('Sign: ')
return row, column, sign
class PcRandomPlayer(Player):
"""
Klasa PcRandomPlayer dziedziczy po Player.
Jest to gracz którego ruchy generowane są w sposób losowy
"""
def generate_random_move(self, board_combinations):
"""
Funkcja zwraca współżędnie losowo wybranego pola
spośród pustych pól na planszy losowy znak ( O lub X)
"""
row = random.randint(0, 5)
column = random.randint(0, 5)
while board_combinations.state()[row][column] != '-':
row = random.randint(0, 5)
column = random.randint(0, 5)
sign_list = ['X', 'O']
sign = random.choice(sign_list)
return row, column, sign
def generate_move(self, board_combinations):
"""
Funkcja generuje ruch gracza klasy PcRandomPlayer.
Jest to losowy ruch.
"""
return self.generate_random_move(board_combinations)
class PcPlayer(PcRandomPlayer):
"""
Klasa PcPlayer dziedziczy po PcRandomPlayer.
Ruchy gracza sterowane są przez algorytm tak by były możliwe
najbardziej skuteczne.
"""
def choose_sign(self, comb_sign):
"""
Funkcja wybiera znak jaki zostanie postawiony w danym ruchu
w zależności czy gracz PcPlayer buduje kombinacje (jest porządkiem)
czy blokuje powstawanie kombuinacji (jest chaosem)
"""
block_signs = ('X', 'O')
build_signs = ('O', 'X')
signs = build_signs if self._goal == 'order' else block_signs
return signs[0] if comb_sign == 'O' else signs[1]
def choose_move(self, chosen_comb, board_combinations):
"""
Funkcja wybiera ruch (współżędne pola i znak)
opierający się na dostawieniu znaku przy którymś z dwóch końców
przekazanej do funkcji kombinacji.
W celu zmniejszenia przewidywalności wybranego ruchu i
uniknięcia sytuacji w której do kombinacji która ma wolne pola
przy obu końcach zawsze dostawiany jest znak z tej samej strony,
wykorzystany jest czynnik losowej kolejności który orzyjmuje losową
wartość 0 lub 1 i determinuje kolejność sprawdzenia możliwości
dostawienia (przykładowo gdy do poziomej kombinacji
można dołożyć zarówno z prawej i z lewej to wybór ten będzie losowy,
dzięki temu nie występuje sytuacja w której zawsze dostawiony
jest znak z lewej lub zawsze z prawej).
Kombinacja zostaje wjybrana edynie w sytuacji
w której któreś z pól (lub oba) przy końcach kombinacji jest wolne
oraz jeśli w lini kominacji da się utworzyć kombinację o długości 5
ze znaku tej kombinacji. Dzięki temu chaos blokuje jedynie
te linie w których istnieje zagrożenie stworzenia kombinacji 5,
a porządek dokłada jedynie w tej sytuacji
"""
(
comb_len,
comb_end_row,
comb_end_column,
comb_direction,
comb_sign
) = chosen_comb
sign = self.choose_sign(comb_sign)
random_order_factor = random.randint(0, 1)
while random_order_factor < 2:
if comb_direction == Direction.horizontally.value:
first_field = comb_end_row, comb_end_column + 1, sign
second_field = comb_end_row, comb_end_column - comb_len, sign
chosen_field = board_combinations.move_with_5_comb(
chosen_comb,
first_field,
second_field,
random_order_factor
)
if chosen_field:
return chosen_field
if comb_direction == Direction.vertically.value:
first_field = comb_end_row + 1, comb_end_column, sign
second_field = comb_end_row - comb_len, comb_end_column, sign
chosen_field = board_combinations.move_with_5_comb(
chosen_comb,
first_field,
second_field,
random_order_factor
)
if chosen_field:
return chosen_field
if comb_direction in (
Direction.diagonally_short_up.value,
Direction.diagonally_long_up.value):
first_field = comb_end_row - 1, comb_end_column + 1, sign
second_field = comb_end_row + comb_len, comb_end_column - comb_len, sign
chosen_field = board_combinations.move_with_5_comb(
chosen_comb,
first_field,
second_field,
random_order_factor
)
if chosen_field:
return chosen_field
if comb_direction in (
Direction.diagonally_short_down.value,
Direction.diagonally_long_down.value):
first_field = comb_end_row + 1, comb_end_column + 1, sign
second_field = comb_end_row - comb_len, comb_end_column - comb_len, sign
chosen_field = board_combinations.move_with_5_comb(
chosen_comb,
first_field,
second_field,
random_order_factor
)
if chosen_field:
return chosen_field
random_order_factor += 1
def generate_move(self, board_combinations):
"""
Funkcja generuje ruch gracza komputerowego.
Spośród najdłuższych kombinacji wybierana jest najdłóższa kombinacja
do której da się dołożyć znak, a w jej lini da się zbudować kombinację
o długości 5 ze znaku który ma ta kombinacja. Jeśli kilka kombinacji ma
taką samą długość to zostaje wybrana losowa.
Jeśli wedle tego algorytmu nie zostanie wybrana żadna z kombinacji
zostaje wykonany ruch losowy.
"""
X_combinations, O_combinations = board_combinations.find_combinations()
X_combinations_random_order = random.sample(
X_combinations, k=len(X_combinations))
O_combinations_random_order = random.sample(
O_combinations, k=len(O_combinations))
is_move_generated = False
def first_value(value):
return value[0]
while is_move_generated is False:
if not X_combinations_random_order:
X_chosen_comb = (0,)
if X_combinations_random_order:
X_chosen_comb = max(
X_combinations_random_order, key=first_value)
X_combinations_random_order.remove(X_chosen_comb)
X_chosen_comb += ('X',)
if not O_combinations_random_order:
O_chosen_comb = (0,)
if O_combinations_random_order:
O_chosen_comb = max(
O_combinations_random_order, key=first_value)
O_combinations_random_order.remove(O_chosen_comb)
O_chosen_comb += ('O',)
chosen_comb = max(X_chosen_comb, O_chosen_comb, key=first_value)
if chosen_comb != (0,):
chosen_move = self.choose_move(chosen_comb, board_combinations)
if chosen_move:
return chosen_move
if chosen_comb == (0,):
return self.generate_random_move(board_combinations)