forked from tehblasian/double-card-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.py
208 lines (160 loc) · 7.91 KB
/
Card.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
from enum import Enum
class Card:
CardColor = Enum('CARD_COLOR', 'WHITE RED')
CardSymbol = Enum('CARD SYMBOL', 'WHITE_DOT BLACK_DOT')
_card_id = 1
def __init__(self, state, locationArr):
self._state = state
self._segments = self._createCardSegments(state, locationArr)
Card._card_id += 1
def getLocation(self):
return self._location
def getState(self):
return self._state
def getSegments(self):
return self._segments
def setState(self, state):
self._state = state
def _getXLocationIndexFromLetter(self, rowLetter):
indices = {
'A': 1,
'B': 2,
'C': 3,
'D': 4,
'E': 5,
'F': 6,
'G': 7,
'H': 8,
}
return indices[rowLetter]
def _getAdjacentXLocation(self, rowIndex):
return int(rowIndex) + 1
def _getYLocationIndexBelow(self, colIndex):
return int(colIndex) - 1
def _getYLocationIndexAbove(self, colIndex):
return int(colIndex) + 1
def _createCardSegments(self, state, locationArr):
col = locationArr[0].upper()
row = locationArr[1]
first_segment = None
second_segment = None
firstSegmentXLocation = self._getXLocationIndexFromLetter(col)
if state == 1:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.BLACK_DOT, firstSegmentXLocation, row)
secondSegmentXLocation = self._getAdjacentXLocation(self._getXLocationIndexFromLetter(col))
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.WHITE_DOT, secondSegmentXLocation, row)
if state == 2:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.WHITE_DOT, firstSegmentXLocation, row)
secondSegmentYLocation = self._getYLocationIndexAbove(row)
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.BLACK_DOT, firstSegmentXLocation, secondSegmentYLocation)
if state == 3:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.WHITE_DOT, firstSegmentXLocation, row)
secondSegmentXLocation = self._getAdjacentXLocation(self._getXLocationIndexFromLetter(col))
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.BLACK_DOT, secondSegmentXLocation, row)
if state == 4:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.BLACK_DOT, firstSegmentXLocation, row)
secondSegmentYLocation = self._getYLocationIndexAbove(row)
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.WHITE_DOT, firstSegmentXLocation, secondSegmentYLocation)
if state == 5:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.WHITE_DOT, firstSegmentXLocation, row)
secondSegmentXLocation = self._getAdjacentXLocation(self._getXLocationIndexFromLetter(col))
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.BLACK_DOT, secondSegmentXLocation, row)
if state == 6:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.BLACK_DOT, firstSegmentXLocation, row)
secondSegmentYLocation = self._getYLocationIndexAbove(row)
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.WHITE_DOT, firstSegmentXLocation, secondSegmentYLocation)
if state == 7:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.BLACK_DOT, firstSegmentXLocation, row)
secondSegmentXLocation = self._getAdjacentXLocation(self._getXLocationIndexFromLetter(col))
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.WHITE_DOT, secondSegmentXLocation, row)
if state == 8:
first_segment = self.CardSegment(Card._card_id, state, Card.CardColor.RED, Card.CardSymbol.WHITE_DOT, firstSegmentXLocation, row)
secondSegmentYLocation = self._getYLocationIndexAbove(row)
second_segment = self.CardSegment(Card._card_id, state, Card.CardColor.WHITE, Card.CardSymbol.BLACK_DOT, firstSegmentXLocation, secondSegmentYLocation)
first_segment.setSibling(second_segment)
second_segment.setSibling(first_segment)
return [first_segment, second_segment]
def __str__(self):
return '''
State: {}
ID: {}
Segments: \n{}\n{}
'''.format(self._state, self._card_id, str(self._segments[0]), str(self._segments[1]))
def __eq__(self, otherCard):
if otherCard is None:
return False
return (self._card_id == otherCard._card_id
and self._state == otherCard._state
and self._segments[0] == otherCard._segments[0]
and self._segments[1] == otherCard._segments[1])
def __hash__(self):
return hash((self._segments[0], self._segments[1]))
# def _getAdjacentLocation(self, state, col, row):
# right = {
# 'A': 'B',
# 'B': 'C',
# 'C': 'D',
# 'D': 'E',
# 'E': 'F',
# 'F': 'G',
# 'H': None,
# }
# if state in [1, 3, 5, 7]:
# return '{}{}'.format(right[col.upper()], row)
# elif state in [2, 4, 6, 8]:
# return '{}{}'.format(row, row - 1)
# else:
# raise ValueError()
class CardSegment:
def __init__(self, parent, state, color, symbol, locationX, locationY):
self._parent = parent
self._state = int(state)
self._color = color
self._symbol = symbol
self._locationX = locationX
self._locationY = 12 - int(locationY)
self._sibling = None
def getParent(self):
return self._parent
def getState(self):
return self._state
def getColor(self):
if self._color == Card.CardColor.RED:
return 'RED'
return 'WHITE'
def getSymbol(self):
if self._symbol == Card.CardSymbol.WHITE_DOT:
return 'WDOT'
return 'BDOT'
def getLocationX(self):
return self._locationX
def getLocationY(self):
return self._locationY
def getSibling(self):
return self._sibling
def setLocation(self, location):
self._location = location
def setSibling(self, sibling):
self._sibling = sibling
def __str__(self):
return '''
Parent ID: {}
State: {}
Color: {}
Symbol: {}
X Location: {}
Y Location: {}
Sibling: \n\t\t{}
'''.format(self._parent, self._state, self._color, self._symbol, self._locationX, self._locationY, (self._sibling._locationX, self._sibling._locationY))
def __eq__(self, otherSegment):
if otherSegment is None:
return False
return (self._parent == otherSegment._parent
and self._state == otherSegment._state
and self._color == otherSegment._color
and self._symbol == otherSegment._symbol
and self._locationX == otherSegment._locationX
and self._locationY == otherSegment._locationY
and self._sibling == otherSegment._sibling)
def __hash__(self):
return hash((self._state, self._color, self._symbol, self._locationX, self._locationY))