on Vic help sign in
ikoHSE/main/projectpref.py
324 lines 11 kB history raw
1# -*- coding: utf-8 -*-
3import numpy as np
5class Modulo3Incrementer:
6 x = 0
8 def __init__(self, x):
9 self.x = x
11 def __add__(self, a):
12 return int((self.x + a) % 3)
14 def __sub__(self, a):
15 return int((self.x + 333 - a) % 3)
17 def __iadd__(self, other):
18 self.x = (self.x + other) % 3
19 return self.x
21 def __isub__(self, other):
22 self.x = (self.x + 333 - other) % 3
24 def __int__(self):
25 return self.x
27class Card:
28 @classmethod
29 def CardsAmount(arg):
30 return 32
32 def __init__(self, id, istrump=False):
33 self.suit = id % 4
34 self.rating = id / 4
36 def __lt__(self, other):
37 return (self.suit + 1) * 100 + self.rating < (other.suit + 1) * 100 + other.rating
39 def __str__(self):
40 val = ''
41 if self.rating == 0:
42 val = '7'
43 elif self.rating == 1:
44 val = '8'
45 elif self.rating == 2:
46 val = '9'
47 elif self.rating == 3:
48 val = '10'
49 elif self.rating == 4:
50 val = 'J'
51 elif self.rating == 5:
52 val = 'Q'
53 elif self.rating == 6:
54 val = 'K'
55 elif self.rating == 7:
56 val = 'A'
57 suit = 0
58 if self.suit == 0:
59 suit = '♠'
60 elif self.suit == 1:
61 suit = '♣'
62 elif self.suit == 2:
63 suit = '♦'
64 elif self.suit == 3:
65 suit = '♥'
66 return val + suit
68class Hand:
69 def __init__(self, hand):
70 self.deck = list(map(lambda x: Card(x), hand))
72 def __str__(self):
73 return ' '.join(map(str, sorted(self.deck)))
75 def __getitem__(self, key):
76 return self.deck[key]
79class Player:
80 tricks_amount = 0
81 deck = []
82 name = ''
83 id = 0
85 def __init__(self, name, id):
86 self.name = name
87 self.id = id
88 self.bullet = 0
89 self.mountain = 0
90 self.whist = 0
92 def IncTricksAmount(self):
93 self.tricks_amount += 1
95 def updateDeck(self, deck):
96 self.deck = deck
98 def __str__(self):
99 return 'name=' + str(self.name) + ' id=' + str(self.id) + ' deck:' + str(self.deck)
101 def GetBarg(self, shedule, leftDeccision, rightDeccision):
102 xx = shedule.GetFreeList()
103 # print(''.join(xx[0]))
104 if any(xx):
105 return ("Game", xx[0])
106 else:
107 return "Pass"
109 def Get(self, id):
110 card = self.deck[id]
111 self.deck = self.deck[:id] + self.deck[id+1:]
112 return card
114 # self.table, playerleft.get_hand(), playerright.get_hand()
115 def get_card(self, table, lefthand, righthand):
116 return self.Get(0)
118 def get_hand(self):
119 return self.deck
124class AnswerType: # данный класс - это ответ игрока при торгах
125 isPass = False
126 isWhist = False
127 isPlay = False
128 isMiser = False
129 PlayChoose = (-1, -1)
131 class Action:
132 @staticmethod
133 def Mizer():
134 return "Mizer"
136 @staticmethod
137 def Pass():
138 return "Pass"
140 @staticmethod
141 def Vist():
142 return "Vist"
144 @staticmethod
145 def Game():
146 return "Game"
148 def __init__(self, typeOfAction): #define ПАС/ ВИСТ
149 if typeOfAction == AnswerType.Action.Mizer():
150 self.isMizer = True
151 elif type(typeOfAction) is tuple:
152 self.isPlay = True
153 self.PlayChoose = typeOfAction[1]
154 elif typeOfAction == AnswerType.Action.Vist():
155 self.isVist = True
156 elif typeOfAction == AnswerType.Action.Pass():
157 self.isPass = True
159 def Tostr(self, y):
160 x = AnswerType(y)
161 if x.isPlay:
162 return y[1][0] + y[1][1]
163 else:
164 return y
166# class Rate(Card):
169class Shedule:
170 def __init__(self):
171 self.masks = ['6', '7', '8', '9', '10']
172 self.masks1 = ['♠', '♣', '♦', '♥', 'БК']
173 self.alls = [(i, j) for i in self.masks for j in self.masks1]
174 self.used = {i: False for i in self.alls}
177 def GetIdMask1(self, c):
178 for i in enumerate(self.masks1):
179 if i[1] == c:
180 return i[0]
181 return False
183 @staticmethod
184 def cmp(a, b):
185 x1 = Shedule.GetIdMask1(Shedule(), a[1])
186 x2 = Shedule.GetIdMask1(Shedule(), b[1])
187 return int(a[0]) * 100 + x1 - int(b[0]) * 100 - x2
189 def SetUsed(self, cnt, typeX):
190 card = (cnt, typeX)
191 ans = list(filter(lambda x: self.used[x] == False, self.used.keys()))
192 ans = list(sorted(ans, cmp=self.cmp))
193 for i in range(len(ans)):
194 if ans[i] == card:
195 for j in range(i + 1):
196 self.used[ans[j]] = True
198 def IsUsed(self, cnt, typeX):
199 return self.used[(cnt, typeX)]
201 def GetFreeList(self):
202 ans = list(filter(lambda x: self.used[x] == False, self.used.keys()))
203 ans = list(sorted(ans, cmp=self.cmp))
204 # print(' '.join(map(lambda x: str(x[0])+str(x[1]), ans)))
205 return ans
207 def __str__(self):
208 return "Locked: " + ' '.join(map(str, filter(lambda x: self.used[x] == True, self.used.keys())))
212class Round:
213 typeOfRound = -1
214 table = []
215 players = -1
216 manager_id = -1
217 allCards = np.arange(32) # crea
219 def MakeOneCircle(self, manager_id, players, trump='♠', amount_cnt=6):
220 tripleset = []
221 for manager in range(3):
222 id_man = (manager + manager_id) % 3
223 player = players[id_man]
224 playerleft = players[(id_man + 3 - 1) % 3]
225 playerright = players[(id_man + 3 + 1) % 3]
226 card = player.get_card(self.table, playerleft.get_hand(), playerright.get_hand()) # самое главное - сделать ход
227 tripleset.append((card, id_man))
228 self.table = tripleset
229 tripleset = list(sorted(tripleset, key=lambda x: x[0], reverse=True))
230 winner_id = tripleset[0][1]
231 players[winner_id].IncTricksAmount()
232 return winner_id
234 def __init__(self, players, manager_id):
235 self.table = []
236 self.players = players
237 self.manager_id = manager_id
238 self.allCards = np.arange(32) # creating a deck
239 np.random.shuffle(self.allCards)
240 xDecks = (Hand(self.allCards[:10]), Hand(self.allCards[10:20]), Hand(self.allCards[20:30]))
241 self.talon = Hand(self.allCards[30:])
242 for P, D in zip(self.players, xDecks):
243 P.updateDeck(D)
245 def MakeBargs(self, players, manager_id):
246 manager_id = Modulo3Incrementer(manager_id)
247 shedule = Shedule()
248 freeList = shedule.GetFreeList()
249 answers = [-1, -1, -1]
250 # первый круг голосов - возможны распасы
251 for i in range(3):
252 ans = players[(manager_id + 0) % 3].GetBarg(shedule, answers[(manager_id + 3 - 1) % 3], answers[(manager_id + 1) % 3])
253 answers[(manager_id + 0) % 3] = ans
254 if AnswerType(ans).isPlay:
255 shedule.SetUsed(ans[1][0], ans[1][1])
256 elif AnswerType(ans).isMiser:
257 shedule.SetUsed('8', 'БК')
258 manager_id += 1
259 # print(' '.join(map(lambda x: str(x[0])+str(x[1]), answers)))
260 # первый круг голосов пройден
261 while True: # пока есть свободные ячейки в таблице
262 if len(list(filter(lambda x: AnswerType(x).isPass is True, answers))) == 2: # если хотя бы два не играют
263 break
265 ans = players[(manager_id + 0) % 3].GetBarg(shedule, answers[(manager_id + 3 - 1) % 3],
266 answers[(manager_id + 1) % 3])
267 answers[(manager_id + 0) % 3] = ans
268 print(ans)
269 if AnswerType(ans).isPlay:
270 shedule.SetUsed(ans[1][0], ans[1][1])
271 elif AnswerType(ans).isMiser:
272 shedule.SetUsed('8', 'БК')
273 manager_id += 1 # modulo 3
275 return answers
278 def __str__(self):
279 return 'Player {0}: {1}, \nPlayer {2}: {3}, \nPlayer {4}: {5}, TALON: {6}\n'.format(self.players[0].name,
280 ' '.join(map(str, self.players[0].deck)),
281 self.players[1].name,
282 ' '.join(map(str,
283 self.players[
284 1].deck)),
285 self.players[2].name,
286 ' '.join(map(str,
287 self.players[
288 2].deck)),
289 self.talon)
292class Game:
293 players = []
294 maxBullet = 20
296 def __init__(self, name1="Trus", name2="Balbes", name3="Bivaliy"):
297 self.players.append(Player(name1, 0))
298 self.players.append(Player(name2, 1))
299 self.players.append(Player(name3, 2))
300 self.manager = 0
301 r = Round(self.players, self.manager)
302 self.answers = r.MakeBargs(self.players, self.manager)
303 print(self.answers)
304 print(' '.join(map(lambda x: AnswerType(x).Tostr(x), self.answers)))
305 notPass = ()
306 for i in self.answers:
307 if not AnswerType(i).isPass:
308 notPass = i
309 if notPass == '':
310 print("RASPASI!!!)!)!)!)!)!)!))!)!)!)!)!)!)!)!)")
311 elif AnswerType(notPass).isMiser:
312 print("MIZER TIMEEEOJOJOSNDVOJDNVIOJSNFOEJFNEOJN")
313 else:
314 while any(self.players[0].deck):
315 self.manager = r.MakeOneCircle(self.manager, self.players, notPass[1][1], int(notPass[1][0]))
316 print(r)
318 #while any(filter(lambda x: x.bullet < self.maxBullet, self.players)):
319 # r = Round(self.players, self.manager)
320 # print(r)
321 # print
324g = Game()