-
Notifications
You must be signed in to change notification settings - Fork 0
/
playnine.py
2237 lines (2025 loc) · 98 KB
/
playnine.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
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
from random import shuffle, choice, random, randint #randint is used as randint(5,10) 5 and 10 can be chosen
import tkinter as tk
from tkinter import filedialog
import win32gui
import win32con
import win32com
import win32com.client
import re
debugging = False
print_drawn = debugging
class Card():
#A simple card, it just has a value
def __init__(self, value):
self.value = value
self.visible_value = "F"
def flip(self):
self.visible_value = self.value
def copy(self):
c = Card(self.value)
c.visible_value = self.visible_value
return c
class Deck():
#A deck, which is just a list of cards
def __init__(self):
self.cards = []
self.fill_deck()
self.shuffle()
def fill_deck(self):
for i in range(13):
for j in range(8):
self.cards.append(Card(i))
for i in range(4):
self.cards.append(Card(-5))
def shuffle(self):
shuffle(self.cards)
def print(self):
for card in self.cards:
print(card.value)
def draw(self):
self.cards[0].flip()
if print_drawn:
print("Drew " + str(self.cards[0].visible_value))
return self.cards.pop(0)
def draw_face_down(self):
return self.cards.pop(0)
def remove_card(self, val):
for c in self.cards:
if c.value == val:
self.cards.remove(c)
break
class Board():
NEITHER_FLIPPED = 0
ONE_FLIPPED = 1
BOTH_FLIPPED = 2
BOTH_MATCH = 3
def __init__(self):
self.cards = []
self.fill_blank()
def fill_blank(self):
for col in range(4):
inner = []
for row in range(2):
inner.append(Card(69))
self.cards.append(inner)
def print(self):
for row in range(2):
for col in range(4):
if isinstance(self.cards[col][row].visible_value, str):
print(" F", end = " ")
else:
print("{:2}".format(self.cards[col][row].visible_value), end = " ")
print("")
def copy(self):
new_board = Board()
for col in range(4):
for row in range(2):
new_board.cards[col][row] = self.cards[col][row]
return new_board
def flip_all(self):
for col in range(4):
for row in range(2):
self.cards[col][row].flip()
def all_flipped(self):
for col in range(4):
for row in range(2):
if self.cards[col][row].visible_value != self.cards[col][row].value:
return False
return True
def get_unflipped_locations(self):
unflipped = []
for col in range(4):
for row in range(2):
if self.cards[col][row].visible_value == "F":
unflipped.append((col, row))
return unflipped
def get_unmatched(self):
unmatched = []
for col in range(4):
if self.get_state(col) == Board.ONE_FLIPPED or self.get_state(col) == Board.BOTH_FLIPPED:
for row in range(2):
if self.cards[col][row].visible_value != "F":
unmatched.append(self.cards[col][row].visible_value)
# if debugging:
# print("get unmatched called, it returned:")
# print(unmatched)
return unmatched
def get_highest_unmatched(self):
if len(self.get_unmatched()) > 0:
return max(self.get_unmatched())
else:
return None
def get_across_from_highest(self):
highest = -6
location = None
realrow = None
valrow = None
for col in range(4):
if self.get_state(col) == Board.ONE_FLIPPED:
for row in range(2):
if self.cards[col][row].visible_value == "F":
realrow = row
else:
valrow = row #this is stupid but I think it should work
val = self.cards[col][valrow].value
if val > highest:
highest = val
location = (col, realrow)#there's a chance this doesn't work, but I think it should
if debugging and location != None:
print("Across from highest is at " + str(location[0]) + " and " + str(location[1]))
return location
def get_location(self, card_val):
for col in range(4):
for row in range(2):
if self.get_state(col) != Board.BOTH_MATCH and self.get_state(col) != Board.NEITHER_FLIPPED:
if self.cards[col][row].visible_value == card_val:
return (col, row)
def get_unflipped(self, col):
if self.get_state(col) != Board.ONE_FLIPPED:
return -1
if self.cards[col][0].visible_value == "F":
return 0
else:
return 1
def get_matches(self):
matches = []
for col in range(4):
if self.get_state(col) == Board.BOTH_MATCH:
matches.append(self.cards[col][0])
return matches
def get_state(self, col):
if self.cards[col][0].visible_value == "F" and self.cards[col][1].visible_value == "F":
return Board.NEITHER_FLIPPED
elif self.cards[col][0].visible_value == "F" or self.cards[col][1].visible_value == "F":
return Board.ONE_FLIPPED
elif self.cards[col][0].value == self.cards[col][1].value:
return Board.BOTH_MATCH
elif (self.cards[col][0].value == 0 or self.cards[col][0].value == -5) and (self.cards[col][1].value == 0 or self.cards[col][1].value == -5):
return Board.BOTH_MATCH #makes joker and 0 count as a match
else:
return Board.BOTH_FLIPPED
def get_score(self):
score = 0
matches = []
for i in range(4):
#If they match
if self.cards[i][0].value == self.cards[i][1].value: #interesting note, could probably use walrus operator here
#if they're jokers, subtract 10 instead of just adding 0
if self.cards[i][0].value == -5:
score -= 10
matches.append(self.cards[i][0].value)
else:
#otherwise they don't match, so add the value of each card
score += self.cards[i][0].value + self.cards[i][1].value
for val in matches:
if matches.count(val) > 1:
for i in range(matches.count(val)):
matches.remove(val)
score -= 5
return score
class DNA():
def __init__(self, genes = None):
"""
the traits, in order, are:
horizontal or vertical: if it prefers flipping with a flipped card or a new col
highest to take (from discard): lowest number it will take from the discard pile
highest to keep (fron deck): lowest number it will keep from the deck
lowest to mitigate: lowest amount you need to save for it to mitigate
higest card to go for -10 with
end
highest to go out with
"""
if genes == None:
self.genes = []
for i in range(7):
self.genes.append(self.random(i))
else:
self.genes = genes
def random(self, gene):
if gene == 0:
if random() < 0.5:
return True
else:
return False
elif gene == 3:
return randint(1, 12)
elif gene == 5:
return randint(1, 6)
elif gene == 6:
return randint(-9, 20)
else:
return randint(0, 12)
def print(self):
print("horizontal preference: " + str(self.genes[0]))
print("highest to take (from discard):: " + str(self.genes[1]))
print("highest to keep (fron deck): " + str(self.genes[2]))
print("lowest to mitigate: " + str(self.genes[3]))
print("higest card to go for -10 with: " + str(self.genes[4]))
print("end: " + str(self.genes[5]))
print("highest to go out with: " + str(self.genes[6]))
class Player():
#Layout for a player, by default is a computer player
def __init__(self, early = None, late = None):
#make their board and a var for the card they last discarded
self.board = Board()
self.card_discarded = None
#for debugging
if debugging:
self.code = randint(0, 100000)
#make their score variable
self.score = 0
#make a variable for if they won
self.winner = None
#set up their dna
self.earlyDNA = None
self.lateDNA = None
if early == None:
self.earlyDNA = DNA()
else:
self.earlyDNA = early
if late == None:
self.lateDNA = DNA()
else:
self.lateDNA = late
#set up dictionaries with gene info
early_iterator = iter(self.earlyDNA.genes)
self.early = {
"horizontal preference": next(early_iterator),
"highest to take": next(early_iterator),
"highest to keep": next(early_iterator),
"lowest to mitigate": next(early_iterator),
"highest for -10": next(early_iterator),
"end": next(early_iterator)
}
late_iterator = iter(self.lateDNA.genes)
self.late = {
"horizontal preference": next(late_iterator),
"highest to take": next(late_iterator),
"highest to keep": next(late_iterator),
"lowest to mitigate": next(late_iterator),
"highest for -10": next(late_iterator),
"end for -10": next(late_iterator),
"highest to go out with": next(late_iterator)
}
#variables for taking the turn
self.one_flipped_per_row = False
self.going_for_minus10 = []
def flip_two(self):
#will always flip this one
self.board.cards[0][0].flip()
#other one determined by preference
if(self.early.get("horizontal preference")):
if debugging:
print("Flipping horizontal card")
self.board.cards[1][0].flip()
else:
if debugging:
print("Flipping vertical card")
self.board.cards[0][1].flip()
def take_turn(self, deck, discarded, opponents):
#var to see if turn is done yet
turn_done = False
#figure out what stage we're in
stage = 2
#first, see if we have one flipped in every row
if self.early.get("horizontal preference") and not self.one_flipped_per_row:
self.one_flipped_per_row = True
for col in range(4):
if(self.board.get_state(col) == Board.NEITHER_FLIPPED):
self.one_flipped_per_row = False
#now, if there isn't one flipped in every row, we are in stage 1
if self.early.get("horizontal preference") and not self.one_flipped_per_row:
stage = 1
#now check if there is only one card left to be flipped
if stage != 1:
one_flipped_count = 0
neither_flipped_count = 0
for col in range(4):
if self.board.get_state(col) == Board.ONE_FLIPPED:
one_flipped_count += 1
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
neither_flipped_count += 1
if one_flipped_count == 1 and neither_flipped_count == 0:
stage = 3
#if neither of these are true, we are in stage 2
#get info we need for turn
#find how late in the game we are
lowest_unflipped_opponent = 8
for o in opponents:
if len(o.board.get_unflipped_locations()) < lowest_unflipped_opponent:
lowest_unflipped_opponent = len(o.board.get_unflipped_locations())
if debugging:
print("Lowest amount of unflipped cards of opponents is: " + str(lowest_unflipped_opponent))
part = None
if lowest_unflipped_opponent > self.early.get("end"):
part = self.early
else:
part = self.late
replace_minus10 = True
if lowest_unflipped_opponent > self.late.get("end for -10"):
replace_minus10 = False
#now do logic for each turn
if debugging:
print("Starting the turn, stage is: " + str(stage))
print("Checking discarded card")
turn_done = self.check_card(discarded, stage, part.get("highest to take"), part, replace_minus10)
if not turn_done:
if debugging:
print("Didn't use discarded card, drawing a card")
card_drawn = deck.draw()
turn_done = self.check_card(card_drawn, stage, part.get("highest to keep"), part, replace_minus10)
if not turn_done:
if not stage == 3:
if debugging:
print("Didn't use drawn card so just flipping")
self.flip_card(stage, card_drawn)
turn_done = True
else:
if debugging:
print("Didn't use drawn card, so discarding and ending turn")
self.card_discarded = card_drawn
turn_done = True
#turn is now done
if debugging:
print("Turn is ending")
#return true if we went out, false otherwise
return self.board.all_flipped()
def take_last_turn(self, deck, discarded, opponents):
turn_done = False
if debugging:
print("On last turn")
print("checking discarded card")
#really this should be a lot more complex than I'll make it initially
#it should change depending on if you still need to make matches
turn_done = self.check_card(discarded, 4, self.late.get("highest to keep"), self.late, True)
if not turn_done:
if debugging:
print("Didn't take discarded card, so drawing a card")
card_drawn = deck.draw()
turn_done = self.check_card(card_drawn, 4, self.late.get("highest to keep"), self.late, True)
if not turn_done:
if debugging:
print("Didn't use drawn card so just discarding")
self.card_discarded = card_drawn
turn_done = True
self.board.flip_all()
def check_card(self, card, stage, lowest, part, replace_minus10):
#seperate logic just for jokers (-5s)
if card.value == -5:
if debugging:
print("Card is a joker")
if stage != 4:
#check if there is another joker
if card.value in self.board.get_unmatched():
#match it, going for -10 doesn't matter here cuz you wouldn't discard a joker for -10 (technically sometimes you should tho)
col, row = self.board.get_location(card.value)
if row == 0:
row = 1
else:
row = 0
self.switch_cards(card, col, row)
if debugging:
print("Matching the joker")
return True
#if there is a 0, match the joker with the 0
elif 0 in self.board.get_unmatched():
col, row = self.board.get_location(0)
if row == 0:
row = 1
else:
row = 0
self.switch_cards(card, col, row)
if debugging:
print("Matching the joker with a 0")
return True
else:
#nothing to match it with, so either put it in new col or replace highest unflipped card
for col in range(4):
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
self.switch_cards(card, col, 0)
if debugging:
print("Putting joker in a new col")
return True
#if not done yet
#Note: highest unmatched can't be none here, since we checked for empty cols already
col, row = self.board.get_location(self.board.get_highest_unmatched())
self.switch_cards(card, col, row)
if debugging:
print("Replacing highest unmatched with a joker")
return True
else:
#if we are on the last turn, replace highest card if it it above 5, otherwise replace facedown card
if self.board.get_highest_unmatched() != None:
if self.board.get_highest_unmatched() > 5:
col, row = self.board.get_location(self.board.get_highest_unmatched())
self.switch_cards(card, col, row)
if debugging:
print("Replacing highest unmatched with a joker")
return True
else:
col, row = self.board.get_unflipped_locations()[0]
self.switch_cards(card, col, row)
if debugging:
print("Replacing unflipped card with a joker")
return True
if stage == 1:
#check if card is a match
if card.value in self.board.get_unmatched():
if debugging:
print("The discarded card matches an unmatched card, so we're matching it")
col = self.board.get_location(card.value)[0]
row = self.board.get_unflipped(col)
self.switch_cards(card, col, row)
#if there is another one of what we just matched, we can go for -10 with it (if it's low enough)
if card.value in self.board.get_unmatched() and card.value <= part.get("highest for -10"):
self.going_for_minus10.append(card.value)
return True
#also match 0s with jokers
if card.value == 0 and -5 in self.board.get_unmatched():
#put it with a joker if there is one
col, row = self.board.get_location(-5)
if row == 0:
row = 1
else:
row = 0
self.switch_cards(card, col, row)
if debugging:
print("Matching the zero with a joker")
return True
#if we aren't done, card doesn't match anything, let's see if its same as another match
if card.value in self.board.get_matches() and card.value <= part.get("highest for -10"):
#put it in a new col
col = None
row = 0
for column in range(4):
if self.board.get_state(column) == Board.NEITHER_FLIPPED:
col = column
self.switch_cards(card, col, row)
self.going_for_minus10.append(card.value)
if debugging:
print("Taking card since it is low enough to go for -10 with")
return True
#if neither of those happen, see if it is low enough to keep
if card.value <= lowest:
col = None
for column in range(4):
if self.board.get_state(column) == Board.NEITHER_FLIPPED:
col = column
self.switch_cards(card, col, 0)
if debugging:
print("Card is low enough to keep so keeping it")
return True
elif stage == 2:
#check if card matches an unmatched card
if card.value in self.board.get_unmatched():
if debugging:
print("Card matches an unmatched card")
col, row = self.board.get_location(card.value)
if row == 0:
row = 1
else:
row = 0
#check if card we're replacing is going for -10
if self.board.cards[col][row].visible_value in self.going_for_minus10:
if debugging:
print("Card we would get rid of is going for -10")
if replace_minus10:
self.switch_cards(card, col, row)
#if there is another of what we just matched and it is low enough, go for -10 with it
if card.value in self.board.get_unmatched() and card.value <= part.get("highest for -10"):
self.going_for_minus10.append(card.value)
if debugging:
print("Replacing the card anyways")
return True
else:
if debugging:
print("We won't replace it")
elif self.board.cards[col][row].visible_value != -5:
#if card we're replacing isnt going for -10, replace it as long as it's not a -5 (might change this not sure)
self.switch_cards(card, col, row)
if debugging:
print("Matching the card")
return True
#if we're here, card doesn't match an unmatched card, so check if we can match a 0 with a -5
if card.value == 0 and -5 in self.board.get_unmatched():
#put it with a joker if there is one
col, row = self.board.get_location(-5)
if row == 0:
row = 1
else:
row = 0
self.switch_cards(card, col, row)
if debugging:
print("Matching the zero with a joker")
return True
#if we're here, there is no match at all, check if card can go for -10
if card.value in self.board.get_matches() and card.value <= part.get("highest for -10"):
if debugging:
print("Card can go for -10 and is low enough")
#now figure out where to put it
#first check if there is empty col
for col in range(4):
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
if debugging:
print("There is an empty col, so putting it there")
self.going_for_minus10.append(card.value)
self.switch_cards(card, col, 0)
return True
#if not, we can replace a flipped card or an unflipped card
#we will replace a flipped card if we mitigate enough by doing so
if self.board.get_highest_unmatched() != None:
if (self.board.get_highest_unmatched() - card.value) >= part.get("lowest to mitigate"):
col, row = self.board.get_location(self.board.get_highest_unmatched())
self.switch_cards(card, col, row)
if debugging:
print("We mitigate enough by replacing our highest card, so doing that")
return True
#otherwise, replace the unflipped card across from the highest unmatched
if self.board.get_across_from_highest() != None:
if debugging:
print("Putting card across from highest card with an unflipped card across from it")
col, row = self.board.get_across_from_highest()
self.switch_cards(card, col, row)
return True
#we should never get this far within this if statement
if debugging:
print("We aren't supposed to get here in stage 2")
#if we're here, card can't go for -10 so check if it's low enough
if card.value <= lowest:
if debugging:
print("Card is low enough to keep")
#now figure out where to put it
#first check if there is empty col
for col in range(4):
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
if debugging:
print("There is an empty col, so putting it there")
self.going_for_minus10.append(card.value)
self.switch_cards(card, col, 0)
return True
#if not, we can replace a flipped card or an unflipped card
#we will replace a flipped card if we mitigate enough by doing so
if self.board.get_highest_unmatched() != None:
if (self.board.get_highest_unmatched() - card.value) >= part.get("lowest to mitigate"):
col, row = self.board.get_location(self.board.get_highest_unmatched())
self.switch_cards(card, col, row)
if debugging:
print("We mitigate enough by replacing our highest card, so doing that")
return True
#otherwise, replace the unflipped card across from the highest unmatched
if self.board.get_across_from_highest() != None:
if debugging:
print("Putting card across from highest card with an unflipped card across from it")
col, row = self.board.get_across_from_highest()
self.switch_cards(card, col, row)
return True
elif stage == 3:
#get value of card in last row with an unflipped card
last_card_col = None
for col in range(4):
if self.board.get_state(col) == Board.ONE_FLIPPED:
last_card_col = col
#check if card matches unmatched card
if card.value in self.board.get_unmatched():
if debugging:
print("Card matches an unmatched card")
col, row = self.board.get_location(card.value)
if row == 0:
row = 1
else:
row = 0
#see if card will make us go out
if col == last_card_col:
if debugging:
print("Card matches with last card, seeing if we wanna go out")
#see if going out is worth it
test_board = self.board.copy()
test_board.cards[col][row] = card
test_score = test_board.get_score()
if test_score <= self.late.get("highest to go out with"):
#then go out
self.switch_cards(card, col, row)
if debugging:
print("Will end with low enough score, so going out")
return True
#check if it replaces a card we're going for -10 with
elif self.board.cards[col][row].visible_value in self.going_for_minus10:
if debugging:
print("Card we would replace if going for -10")
if replace_minus10:
self.switch_cards(card, col, row)
#if there is another of what we just matched and it is low enough, go for -10 with it
if card.value in self.board.get_unmatched() and card.value <= part.get("highest for -10"):
self.going_for_minus10.append(card.value)
if debugging:
print("Replacing the card anyways")
return True
else:
if debugging:
print("We won't replace it")
elif self.board.cards[col][row].visible_value != -5:
#if card we're replacing isnt going for -10, replace it as long as it's not a -5 (might change this not sure)
self.switch_cards(card, col, row)
if debugging:
print("Matching the card")
return True
#check if we can match a 0 with a joker
if card.value == 0 and -5 in self.board.get_unmatched():
#put it with a joker if there is one
col, row = self.board.get_location(-5)
if row == 0:
row = 1
else:
row = 0
self.switch_cards(card, col, row)
if debugging:
print("Matching the zero with a joker")
return True
#if we are here, card doesn't match an unmatched card
#check if card matches a match we already have
#Note: get_highest_unmatched shouldn't be able to be None here since we're in stage 3
if card.value in self.board.get_matches() and card.value < self.board.get_highest_unmatched():
if debugging:
print("Card makes -10 and is lower than highest unmatched, so taking it")
self.going_for_minus10.append(card.value)
col, row = self.board.get_location(board.get_highest_unmatched())
self.switch_cards(card, col, row)
return True
#now check if we wanna just go out with some points
test_board = self.board.copy()
#get location of last card
col = None
for column in range(4):
if self.board.get_state(column) == Board.ONE_FLIPPED:
col = column
row = self.board.get_unflipped(col)
test_board.cards[col][row] = card
test_score = test_board.get_score()
if test_score <= self.late.get("highest to go out with"):
#then go out
self.switch_cards(card, col, row)
if debugging:
print("Will end with low enough score, so going out")
return True
#see if card mitigates
if (self.board.get_highest_unmatched() - card.value) >= part.get("lowest to mitigate"):
if (self.board.get_highest_unmatched() in self.going_for_minus10 and replace_minus10) or (not self.board.get_highest_unmatched() in self.going_for_minus10):
#there is likely a better way to do that if statement
col, row = self.board.get_location(self.board.get_highest_unmatched())
self.switch_cards(card, col, row)
if debugging:
print("We mitigate enough by replacing our highest card, so doing that")
return True
else:
#keep the -10 one, so mitigate next highest if there is one
highest = -5
for val in self.board.get_unmatched():
if val not in self.going_for_minus10 and val > highest:
highest = val
#found next highest, see if it is higher than drawn
if card.value < highest:
#switch these
col, row = self.board.get_location(highest)
self.switch_cards(card, col, row)
if debugging:
print("Keeping -10 one, so mitigating next highest")
return True
#if card doesn't mitigate, we just return false
elif stage == 4:
if card.value in self.board.get_unmatched():
if debugging:
print("The discarded card matches an unmatched card")
col, row = self.board.get_location(card.value)
if row == 0:
row = 1
else:
row = 0
if (card.value + self.board.cards[col][row].value) > (self.board.get_highest_unmatched() - card.value):
if debugging:
print("Matching saves more than mitigating")
self.switch_cards(card, col, row)
return True
else:
if debugging:
print("Matching saves less than mitigating")
#if no match or match saves less than mitigating, we either mitigate a flipped card (if it's above 5) or an unflipped card, or draw
#if card is from discard or is below -5
if lowest == part.get("highest to take") or card.value < 5:
#then mitigate at all if we can
if self.board.get_highest_unmatched() != None:
if card.value < self.board.get_highest_unmatched():
if self.board.get_highest_unmatched() > 5:
col, row = self.board.get_location(self.board.get_highest_unmatched())
self.switch_cards(card, col, row)
if debugging:
print("Switching out highest unmatched card")
return True
else:
col, row = self.board.get_unflipped_locations()[0]
self.switch_cards(card, col, row)
if debugging:
print("Switching out unflipped card")
return True
else:
col, row = self.board.get_unflipped_locations()[0]
self.switch_cards(card, col, row)
if debugging:
print("Switching out unflipped card")
return True
else:
if debugging:
print("I shouldn't get here, stage should only be 1, 2, or 3")
#if the turn is never done (no switch is made) return false
return False
def flip_card(self, stage, to_discard):
if stage == 1:
for col in range(4):
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
self.board.cards[col][0].flip()
self.card_discarded = to_discard
if self.board.cards[col][0].value in self.board.get_matches():
self.going_for_minus10.append(self.board.cards[col][0].value)
return 0
elif stage == 2:
if self.early.get("horizontal preference"):
for col in range(4):
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
self.board.cards[col][0].flip()
self.card_discarded = to_discard
if self.board.cards[col][0].value in self.board.get_matches():
self.going_for_minus10.append(self.board.cards[col][0].value)
return 0
#otherwise flip opposite highest col
if self.board.get_across_from_highest() != None:
if debugging:
print("Flipping card across from highest card with an unflipped card across from it")
col, row = self.board.get_across_from_highest()
self.board.cards[col][row].flip()
self.card_discarded = to_discard
if self.board.cards[col][row].value in self.board.get_matches():
self.going_for_minus10.append(self.board.cards[col][row].value)
return 0
#if there isn't one, flip new col
for col in range(4):
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
self.board.cards[col][0].flip()
self.card_discarded = to_discard
if self.board.cards[col][0].value in self.board.get_matches():
self.going_for_minus10.append(self.board.cards[col][0].value)
return 0
return -1
def switch_cards(self, card, col, row):
#remove card from going for -10 if it is in there
if self.board.cards[col][row].visible_value in self.going_for_minus10:
self.going_for_minus10.remove(self.board.cards[col][row].value)
#remove card from going for -10 if this gives us -10
if card.visible_value in self.going_for_minus10:
self.going_for_minus10.remove(card.value)
#actually switch the cards
self.card_discarded = self.board.cards[col][row]
self.card_discarded.flip()
self.board.cards[col][row] = card
#evolution functions
def calc_fitness(self, opponents): #might need to make this work for all rounds (multiply both sides by number of rounds)
#fitness should really be based on how much you beat your opponent by, maybe we actually only breed winners
#map score from 150 to -150 to be between 0 and 50
if not self.winner:
if debugging:
print("Fitness is 0")
return 0
else:
#get best opponent's score
best_opponent = 150
for o in opponents:
if o.board.get_score() < best_opponent:
best_opponent = o.board.get_score()
fitness = best_opponent - self.board.get_score()
if debugging:
print("Fitness is " + str(fitness))
return int(fitness)
def mate(self, partner):
new_early = DNA()
new_late = DNA()
for i in range(len(new_early.genes)):
if random() < 0.5:
new_early.genes[i] = self.earlyDNA.genes[i]
else:
new_early.genes[i] = partner.earlyDNA.genes[i]
for i in range(len(new_late.genes)):
if random() < 0.5:
new_late.genes[i] = self.lateDNA.genes[i]
else:
new_late.genes[i] = partner.lateDNA.genes[i]
return Player(new_early, new_late)
def mutate(self, mutation_rate):
# for i in range(len(self.earlyDNA.genes)):
# if random() < mutation_rate:
# self.earlyDNA.genes[i] = self.earlyDNA.random(i)
# for i in range(len(self.lateDNA.genes)):
# if random() < mutation_rate:
# self.lateDNA.genes[i] = self.lateDNA.random(i)
if random() < mutation_rate:
self.earlyDNA = DNA()
self.lateDNA = DNA()
if debugging:
print("Mutated")
class User(Player):
def __init__(self):
super(User, self).__init__()
def flip_two(self):
col = self.get_input("Enter col of card to flip ", [0, 1, 2, 3], True)
if col == "exit":
return "exit"
row = self.get_input("Enter row of card to flip ", [0, 1], True)
if row == "exit":
return "exit"
col2 = self.get_input("Enter col of second card to flip ", [0, 1, 2, 3], True)
if col2 == "exit":
return "exit"
row2 = self.get_input("Enter row of second card to flip ", [0, 1], True)
if row2 == "exit":
return "exit"
while(col == col2 and row == row2):
print("You need to flip 2 different cards")
col2 = self.get_input("Enter col of second card to flip ", [0, 1, 2, 3], True)
if col2 == "exit":
return "exit"
row2 = self.get_input("Enter row of second card to flip ", [0, 1], True)
if row2 == "exit":
return "exit"
self.board.cards[col][row].flip()
self.board.cards[col2][row2].flip()
def take_turn(self, deck, discarded, opponents):
draw_card = self.get_input("Enter draw to draw a card, or keep to use the discarded card ", ["draw", "keep"])
if draw_card == "exit":
return "exit"
if draw_card == "draw":
#check if we're supposed to flip
flip = True
one_flipped_count = 0
neither_flipped_count = 0
for col in range(4):
if self.board.get_state(col) == Board.ONE_FLIPPED:
one_flipped_count += 1
if self.board.get_state(col) == Board.NEITHER_FLIPPED:
neither_flipped_count += 1
if one_flipped_count == 1 and neither_flipped_count == 0:
flip = False
card_drawn = deck.draw()
print("You drew " + str(card_drawn.visible_value))
if flip:
keep = self.get_input("Do you want to keep the card you drew (enter keep), or flip a card on your board (enter flip)? ", ["keep", "flip"])
else:
keep = self.get_input("Do you want to keep the card you drew (enter keep), or end the turn (enter end)? ", ["keep", "end"])
if keep == "end":
return False
if keep == "exit":
return "exit"
if keep == "keep":
col = self.get_input("Enter col of where to put your card ", [0, 1, 2, 3], True)
if col == "exit":
return "exit"
row = self.get_input("Enter row of where to put your card ", [0, 1], True)
if row == "exit":
return "exit"
self.card_discarded = self.board.cards[col][row]
self.card_discarded.flip()
self.board.cards[col][row] = card_drawn
elif keep == "flip":
col = self.get_input("Enter col of card to flip ", [0, 1, 2, 3], True)
if col == "exit":
return "exit"
row = self.get_input("Enter row of card to flip ", [0, 1], True)
if row == "exit":
return "exit"
while self.board.cards[col][row].visible_value != "F":
print("You must flip an unflipped card")
col = self.get_input("Enter col of card to flip ", [0, 1, 2, 3], True)
if col == "exit":
return "exit"
row = self.get_input("Enter row of card to flip ", [0, 1], True)
if row == "exit":
return "exit"
self.board.cards[col][row].flip()
self.card_discarded = card_drawn
elif draw_card == "keep":
col = self.get_input("Enter col of where to put your card ", [0, 1, 2, 3], True)
if col == "exit":
return "exit"
row = self.get_input("Enter row of where to put your card ", [0, 1], True)
if row == "exit":
return "exit"
self.card_discarded = self.board.cards[col][row]
self.card_discarded.flip()
self.board.cards[col][row] = discarded
return self.board.all_flipped()
def take_last_turn(self, deck, discarded, opponents):
print("This is your last turn")
exited = self.take_turn(deck, discarded, opponents)
self.board.flip_all()
return exited
def get_input(self, message, valid_inputs, is_int = False):