-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
145 lines (117 loc) · 3.6 KB
/
game.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
from card import card
from player import player
import numpy as np
import serial
import sys
import time
sys.path.insert(1, 'OpenCV-Card-Detector')
from CardDetector import identify_card
arduino = serial.Serial(port='COM4', baudrate=115200, timeout=.1)
class game:
def __init__(self):
self.deck = []
self.initialize_deck()
self.p1 = player()
self.p2 = player()
self.topCard = self.select_card()
self.currentTurn = 1
for _ in range(0,7):
self.p1.add_card(self.select_card())
self.p2.add_card(self.select_card())
#self.topcard= current_top_card
# self.turn
def initialize_deck(self):
for c in ['R' , 'G' ,'B' ,'Y']:
for n in range (-2, 10):
# if n == 7:
# continue
self.deck.append(card(c,n))
self.deck.append(card('W',-4))
self.deck.append(card('W',-4))
self.deck.append(card('W',0))
self.deck.append(card('W',0))
def select_card(self):
c = np.random.choice(self.deck)
self.deck.remove(c)
return c
def turn(self, turnCard):
if (self.currentTurn == 1):
self.p1.remove_card(turnCard)
self.topCard = turnCard
self.currentTurn = 2
elif (self.currentTurn == 2):
self.reset()
while True:
self.move_a_card()
col, num = identify_card()
print(turnCard)
if (turnCard == card(col, num)):
self.p2.remove_card(turnCard)
break
self.topCard = turnCard
self.currentTurn = 1
# def play_card(self, topCard, card):
# pass
# def uno(self):
# pass
def cpu_choose_card(self):
t = self.p2.hand
for c in t:
print(c.number)
if (self.topCard.number == c.number) or (self.topCard.color == c.color) :
self.turn(c)
return
for c in t:
if (c.color == 'W') :
self.turn(c)
return
drawCard = self.select_card()
self.p2.add_card(drawCard)
self.currentTurn = 1
def move_a_card(self):
a = arduino.write(bytes("1", 'utf-8'))
time.sleep(0.05)
def reset(self):
a = arduino.write(bytes("2", 'utf-8'))
time.sleep(0.5)
g = game()
while True:
print("Player 1 Hand")
print(g.p1)
print("Player 2 Hand")
print(g.p2)
print("Top Card")
print(g.topCard)
a = arduino.readline()
#wait for user to either press play or skip button
while not ((a == b'played\r\n') or (a == b'skipped\r\n')) :#
a = arduino.readline()
#print(a)
if a == b'played\r\n':
#try:
col, num = identify_card()
ca = card(col, int(num))
g.turn(ca)
#print("test")
#except ValueError:
# col, num = identify_card()
# g.turn(card(col, num))
else:
#some function to draw a card
g.reset()
newCard = g.select_card()
#keep moving through until found is card
while True:
g.move_a_card()
col, num = identify_card()
if (newCard == card(col, num)):
g.p1.add_card(newCard)
g.currentTurn = 2
break
print("Player 1 Hand")
print(g.p1)
print("Player 2 Hand")
print(g.p2)
print("Top Card")
print(g.topCard)
g.cpu_choose_card()