-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
250 lines (188 loc) · 7.07 KB
/
classes.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
'''
Sequence Online
classes.py - stores all classes and uses
Author: Pratiksha Jain
'''
# classes for pygame
from itertools import product
#from find_sequence import places
import random
# player class
class Player():
def __init__(self, name, color) -> None:
self.id = id # id of player
self.name = name # name of player - string
self.hand = [] # assigned cards in hand
self.color = color # assigned team
def addCard(self, deck):
self.hand.append(deck.drawCard())
def removeCard(self, card_id):
for index, card in enumerate(self.hand):
if card.id == card_id:
self.hand.pop(index)
def validPlay(self, card_id):
for card in self.hand:
if card.id == card_id:
return True
else:
return False
def showHand(self, returnValue=False):
hands = []
for card in self.hand:
hands.append(card.id)
if returnValue:
return hands
print(hands)
# board class
class Board():
def __init__(self, grid, players=[0,1]) -> None:
self.grid = grid
self.places = {}
self.graphs = [Graph(i, grid) for i in players]
places = [
['0-0', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8', 'S-9', '0-0'],
['C-6', 'C-5', 'C-4', 'C-3', 'C-2', 'H-1', 'H-13', 'H-12', 'H-10', 'S-10'],
['C-7', 'S-1', 'D-2', 'D-3', 'D-4', 'D-5', 'D-6', 'D-7', 'H-9', 'S-12'],
['C-8', 'S-13', 'C-6', 'C-5', 'C-4', 'C-3', 'C-2', 'D-8', 'H-8', 'S-13'],
['C-9', 'S-12', 'C-7', 'H-6', 'H-5', 'H-4', 'H-1', 'D-9', 'H-7', 'S-1'],
['C-10', 'S-10', 'C-8', 'H-7', 'H-2', 'H-3', 'H-13', 'D-10', 'H-6', 'D-2'],
['C-12', 'S-9', 'C-9', 'H-8', 'H-9', 'H-10', 'H-12', 'D-12', 'H-5', 'D-3'],
['C-13', 'S-8', 'C-10', 'C-12', 'C-13', 'C-1', 'D-1', 'D-13', 'H-4', 'D-4'],
['C-1', 'S-7', 'S-6', 'S-5', 'S-4', 'S-3', 'S-2', 'H-2', 'H-3', 'D-5'],
['0-0', 'D-1', 'D-13', 'D-12', 'D-10', 'D-9', 'D-8', 'D-7', 'D-6', '0-0']
]
# generating places
for i in range(10):
for j in range(10):
suit,val = places[i][j].split('-')
if (i,j) in [(0,0), (0,9), (9,0), (9,9)]:
self.places[(i,j)] = Place(suit, val, i,j, 4,1)
# filled=-1,0,1,2,3,4 fixed=1
self.places[(i,j)] = Place(suit, val, i,j, -1,0)
def validPlayOnBoard(self, x,y, card_id):
if self.places[(x,y)].card.id == card_id:
return True
else:
return False
def spaceEmpty(self, x,y):
if self.places[(x,y)].filled == -1:
return True
else:
return False
def updateBoard(self, player, x,y,card_id):
wildcard = int(card_id.split("-")[2])
if wildcard == 0: # not wild card
self.places[(x,y)].filled = player
self.grid[x][y] = player
elif wildcard == 1: # one eyed - removes
self.places[(x,y)].filled = -1
self.grid[x][y] = -1
elif wildcard == 2: # two-eyed - adds
self.places[(x,y)].filled = player
self.grid[x][y] = player
def showBoard(self):
print("Grid:")
for row in self.grid:
print(row)
class Place():
def __init__(self, suit, val, x, y, filled, fixed) -> None:
self.card = Card(suit, val)
self.suit = suit
self.val = val
self.filled = filled
self.fixed = fixed
self.x = x
self.y = y
def fixPlace(self):
self.fixed = 1
# graph class for doing sequences
class Graph():
def __init__(self, color, grid):
self.grid = grid
self.gdict = {}
self.color = color
for id in range(100):
row = id//10
col = id%10
if self.grid[row][col] in (self.color, 4):
self.addColoredEdge(row,col)
self.sequences = []
def addVertex(self, v):
if v not in self.gdict:
self.gdict[v] = []
def addEdge(self, edge):
(v1, v2) = tuple(edge)
if not v1 in self.gdict:
self.addVertex(v1)
self.gdict[v1].append(v2)
if not v2 in self.gdict:
self.addVertex(v2)
self.gdict[v2].append(v1)
self.gdict[v1] = list(set(self.gdict[v1]))
self.gdict[v2] = list(set(self.gdict[v2]))
def addColoredEdge(self, row, col):
neigbours = self.generateNeighbours(row,col)
for r,c in neigbours:
if self.grid[r][c] in (self.color, 4):
self.addEdge((row*10 +col,r*10+c))
def generateNeighbours(self,row, col):
neighbours= []
for c in product(*(range(n-1, n+2) for n in (row,col))):
if c != (row,col) and all(0 <= n < 10 for n in c):
neighbours.append(c)
return neighbours
def findSequences(self):
for id1 in self.gdict.keys():
for id2 in self.gdict[id1]:
stack = [id1]
if id2>id1:
diff = id2-id1
self.dfs(stack, id2, diff,5)
def dfs(self, visited, node, diff, n): #function for dfs
if node not in visited:
visited.append(node)
if len(visited) == n:
self.sequences.append(visited)
return True
found = False
for neighbour in self.gdict[node]:
if neighbour-node == diff:
self.dfs(visited, neighbour, diff,n)
return found
def findSequences_n(self, n): # n>=3
for id1 in self.gdict.keys():
for id2 in self.gdict[id1]:
stack = [id1]
if id2>id1:
diff = id2-id1
self.dfs(stack, id2, diff, n)
# card class
class Card():
def __init__(self, suit, val, wild=0) -> None:
# id is a string which is of the form "S-14-0" {suit, value, wild}
self.suit = suit
self.val = val
self.wild = wild
self.id = str(suit)+"-"+str(val)+"-"+str(wild)
class Deck():
def __init__(self) -> None:
self.cards = []
def initialBuild(self):
for s in ['S',"C","D","H"]:
for v in range(1,14):
if v == 11:
self.cards.append(Card(s,v,wild=1))
self.cards.append(Card(s,v,wild=2))
else:
self.cards.append(Card(s,v))
self.cards.append(Card(s,v))
def shuffle(self):
random.Random(4).shuffle(self.cards)
def drawCard(self):
return self.cards.pop(0)
def serveHands(self, numberofplayers=2, numberofcards=7):
mat = [[0 for i in range(numberofcards)] for j in range(numberofplayers)]
for j in range(numberofcards):
for i in range(numberofplayers):
mat[i][j] = self.drawCard()
return mat