def update(self, board, idPlayer, move): self.board = board a, b = board[0].n1, board[-1].n2 plays = [0] * (self.nMax + 1) plays[a] = 1 plays[b] = 1 if move == 'x': idTemp = self.playerId(idPlayer) passTemp = dpc(self.passBones[idTemp]) for i in Bone.IDX[a]: passTemp[i] = 1 for i in Bone.IDX[b]: passTemp[i] = 1 self.passBones[idTemp] = dpc(passTemp) boardTemp = [0] * self.nTotal for b in self.board: boardTemp[Bone.IDX[b.n1][b.n2]] = 1 myBones = [0] * self.nTotal for b in self.bones: myBones[Bone.IDX[b.n1][b.n2]] = 1 bones1 = [ int(not (m or b or p)) for m, b, p in zip(myBones, boardTemp, self.passBones[0]) ] bones2 = [ int(not (m or b or p)) for m, b, p in zip(myBones, boardTemp, self.passBones[1]) ] bones3 = [ int(not (m or b or p)) for m, b, p in zip(myBones, boardTemp, self.passBones[2]) ] self.state = [] self.state.extend(bones1) self.state.extend(bones2) self.state.extend(bones3) self.state.extend(plays)
def find(): global maxsplitsA global maxsplits q = deque() q.append(([0, 0, nrow - 1, ncol - 1], [])) while q: sp, cursplits = q.pop() r, c, r2, c2 = sp A = getArea(r, c, r2, c2) if A < (2 * L): continue S0, valid = compScore(sp) S0new = (sum(s for _, s in cursplits) + S0) if S0new > maxsplitsA and valid: maxsplitsA = S0new maxsplits = dpc(cursplits) + [(sp,S0)] write_sol() for colsp in range(c2 - c - 1): # 0-> n-1 splits spA = [r, c, r2, c + colsp] S, valid = compScore(spA) spB = [r, c + colsp + 1, r2, c2] S2, valid2 = compScore(spB) cursplits2 = dpc(cursplits) + ([(spA, S), ] if valid else []) q.append([spB, cursplits2]) cursplits2 = dpc(cursplits) + ([(spB, S2), ] if valid2 else []) q.append([spA, cursplits2]) for rowsp in range(r2 - r - 1): spA = [r, c, r + rowsp, c2] S, valid = compScore(spA) spB = [r + rowsp + 1, c, r2, c2] S2, valid2 = compScore(spB) cursplits2 = dpc(cursplits) + ([(spA, S), ] if valid else []) q.append([spB, cursplits2]) cursplits2 = dpc(cursplits) + ([(spB, S2), ] if valid2 else []) q.append([spA, cursplits2])
def repartir(self): numeros = range(self.nMax+1) fichas = list( combinations( numeros, 2) ) for i in numeros: fichas.append( (i,i) ) jugIdx = [ i % self.nJug for i in range (len( fichas ) ) ] rnd.shuffle( jugIdx ) for ficha, jug in zip( fichas, jugIdx ): f = Ficha(ficha[0],ficha[1]) self.fichas.append( dpc( f ) ) self.jugadores[jug].agregarFicha( f )
"material": { "wire": False, "color": (200, 63, 63), "metal": 1, "map": "none", "alpha": 255 }, "physics": { "rigidBody": True, "velocity": (0, 0, 0), "friction": 0.05, "bounce": 0, "control": False }, 'animation': { 'xpos': dpc(blankanim), 'ypos': dpc(blankanim), 'zpos': dpc(blankanim), 'xrot': dpc(blankanim), 'yrot': dpc(blankanim), 'xscale': dpc(blankanim), 'yscale': dpc(blankanim), 'zscale': dpc(blankanim) } } world = {"gravity": 0.01, "background": (127, 127, 127), "projection": 1} player = {"jumpStrength": 2, "maxSpeed": 2, "accel": 0.05} scene = [
def play(): # Mute Option chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--mute-audio") # Minimize Window chrome_options.add_argument('--headless') # Open Google Chrome Browser driver = webdriver.Chrome("./chromedriver.exe", chrome_options=chrome_options) # Open Dominoes page driver.get("https://dominoes.playdrift.com/") # Select SinglePlayer driver.find_element_by_link_text('Singleplayer').click() # Select Block Type select = Select(driver.find_element_by_name('chain_dominoes')) select.select_by_value('block') # Select 4 Players select = Select(driver.find_element_by_name('size')) select.select_by_value('4') # Select 4 Players select = Select(driver.find_element_by_name('opponent')) select.select_by_value('bothard') ## Additional Options driver.find_element_by_xpath( ".//*[contains(text(),'Show additional')]").click() # Select Limit Points select = Select(driver.find_element_by_name('limit')) select.select_by_value('150') # Select Initial select = Select(driver.find_element_by_name('bones')) select.select_by_value('7') ## Start Game driver.find_element_by_link_text('Play').click() ## Read Game # Board BOARDS = [] BONES_NUMBER = [] BOARD = [] board = driver.find_element_by_class_name('board') # My Hand myHand = [] hand = driver.find_element_by_class_name('hand.local') myHandFound = False # Other players hands handsList = [] hands = driver.find_element_by_class_name('hands') # Game Played gamePlayed = False # My Turn myTurn = False initTime = datetime.datetime.now() # Ended end = False while True: try: bonesTemp = board.find_elements_by_class_name('bone') # If another bone is in the board... if len(bonesTemp) > len(BOARD): gamePlayed = True myTurnMessage = True #... add it to the board bonesTemp = board.find_elements_by_class_name('bone') BOARD = getBoneBoard(dpc(bonesTemp[-1].get_attribute('style')), BOARD) #... and count the unknown bones unknownBones = hands.find_elements_by_class_name('bone') u = getUnknownBones( [dpc(b.get_attribute('style')) for b in unknownBones]) u.insert(0, len(hand.find_elements_by_class_name('bone'))) if DEBUG: print(BOARD) # print( u ) # print() BOARDS.append(dpc(BOARD)) BONES_NUMBER.append(u) # Are the final bones? myHandTemp = hand.find_elements_by_class_name('bone') myHandTemp = [b.get_attribute('style') for b in myHandTemp] bonesTemp = hands.find_elements_by_class_name('bone') bonesTemp = [b.get_attribute('style') for b in bonesTemp] finalBones = areFinalBones(bonesTemp) time.sleep(0.0075) bonesTemp = hands.find_elements_by_class_name('bone') bonesTemp = [b.get_attribute('style') for b in bonesTemp] if finalBones: # print('Últimas Fichas') # Window Size SIZE = driver.get_window_size() W, H = SIZE['width'], SIZE['height'] final = getFinalBones(bonesTemp, W, H) myHandFinal = [getBoneHand2(b) for b in myHandTemp] final[0] = myHandFinal # for b in final : print( b ) driver.close() break # Is my turn? myHandTemp = hand.find_elements_by_class_name('bone') myTurn = isMyTurn( [dpc(b.get_attribute('style')) for b in myHandTemp]) dT = datetime.datetime.now() - initTime if myTurn and myHandFound and dT.seconds > 5 and not end: actions = ActionChains(driver) initTime = datetime.datetime.now() # Play.... randomly bone = next( (bone for bone in myHandTemp if isAnAvaliableBone(bone.get_attribute('style'))), None) actions.move_to_element(bone) actions.click_and_hold(on_element=None) actions.perform() highlight = board.find_elements_by_class_name( 'dominoesui-highlight') if len(highlight): actions.move_to_element(highlight[0]) actions.release(on_element=None) actions.perform() # Find out my hand if not myHandFound: myHandTemp = hand.find_elements_by_class_name('bone') if len(myHandTemp) == 7: myHand = [ getBoneHand(dpc(b.get_attribute('style'))) for b in myHandTemp ] myHandFound = True # print( f"My Hand: \n {myHand} \n" ) except KeyboardInterrupt: driver.close() break except: driver.close() pass raise if DEBUG: print(myHand) print() for b, n in zip(BOARDS, BONES_NUMBER): print(f'{b} | {n}') print() print(final) ## SAVE DATA = [myHand, BOARDS, BONES_NUMBER, final] with open( 'games/RAW_' + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M') + '.h5', "wb") as f: pickle.dump(DATA, f)
def play(self): self.reset() self.deal() ended = False idx = -1 for i, p in enumerate(self.players): if Bone(self.nMax, self.nMax) in p.bones: idx = i break nPass, k = 0, 1 while not ended: # board, bone, len(self.bones) == 0, bone is None self.board, bone, ended, playerPass = self.players[idx].play( self.board) move = 'x' if playerPass else bone for p in self.players: p.update(self.board, idx, move) if playerPass: nPass += 1 else: nPass = 0 if nPass == self.nJug: ended = True if DEBUG: if playerPass: print(f'Turn {k:d}: The Player {idx:d} pass') else: print( f'Turn {k:d}: The Player {idx:d} plays the bone {bone}' ) self.printPlayers() self.printBoard() k += 1 idx += 1 idx %= self.nJug idx = (idx - 1) % self.nJug locked = True if nPass < self.nJug: print(f'\tPlayer {idx:d} wins!!!!') locked = False else: s0 = np.sum([b.sum() for b in self.players[0].bones]) s1 = np.sum([b.sum() for b in self.players[1].bones]) s2 = np.sum([b.sum() for b in self.players[2].bones]) s3 = np.sum([b.sum() for b in self.players[3].bones]) idx = np.argmin([s0, s1, s2, s3]) locked = True print(f'\tGame Locked :(. Player {idx:d} wins!!!!') rates = [[self.players[0].MMR], [self.players[1].MMR], [self.players[2].MMR], [self.players[3].MMR]] ranks = [1] * 4 ranks[idx] = 0 (r1, ), (r2, ), (r3, ), (r4, ) = rate(rates, ranks=ranks) self.players[0].MMR = dpc(r1) self.players[1].MMR = dpc(r2) self.players[2].MMR = dpc(r3) self.players[3].MMR = dpc(r4) return idx, locked
def process(strFile): DATA = None with open(strFile, 'rb') as r: DATA = pickle.load(r) myHand, BOARDS, BONES_NUMBER, final = tuple(DATA) if DEBUG: for b, n in zip(BOARDS, BONES_NUMBER): print(f'{b} | {n}') if DEBUG: print() ### POST-PROCESSING ## Hands Recontructions HANDS = dpc(final) players = [] actions = [] tempCant = [7, 7, 7, 7] tempBoard = [] for b, n in zip(BOARDS, BONES_NUMBER): delta = [a - b for a, b in zip(tempCant, n)] plays = [idx for idx, val in enumerate(delta) if val != 0] idxPlay = plays[-1] if len(players) > 0 and idxPlay != (players[-1] + 1) % 4: tempPlayer = (players[-1] + 1) % 4 while tempPlayer != idxPlay: players.append(tempPlayer) actions.append('x') tempPlayer = (tempPlayer + 1) % 4 bone = list(set(b) - set(tempBoard)) bone = bone[-1] if bone == b[0]: bone = bone[::-1] actions.append(bone) HANDS[idxPlay].append(bone) players.append(idxPlay) tempCant = dpc(n) tempBoard = dpc(b) # Get Actions if DEBUG: print(myHand) print() for hand in HANDS: print(hand) print() print([]) ALL_BOARDS = [[]] idx = -1 for p, a in zip(players, actions): if DEBUG: print(f'Player {p:d} played {a}') if a != 'x': idx += 1 if DEBUG: print(BOARDS[idx]) ALL_BOARDS.append(BOARDS[idx]) # Re-play the game (Get States) if DEBUG: print() playerStates = [playerState(i, hand) for i, hand in enumerate(HANDS)] STATES = [] ACTIONS = [] ZEROS = lambda: [[0] * 7 for _ in range(7)] for p, a, b in zip(players, actions, ALL_BOARDS[1:]): # Get state of player p state = playerStates[p].state() # Get action of player p aTemp = ZEROS() if a != 'x': i, j = a aTemp[i][j] = 1 # Update all player's states for s in playerStates: s.update(p, a, b) # Save state and action if player doesn't pass if a != 'x' or p == 0: STATES.append(state) ACTIONS.append(aTemp) if DEBUG: print(f'# States: {len(STATES)} | # Actions: {len(ACTIONS)}') for s, a in zip(STATES, ACTIONS): print(f'{s} ---> {a}\n') return STATES, ACTIONS
n2[1] = 88 print(id(n1), id(n2)) print(n1, ":", n2) n2 = [4, 5, 6] print(id(n1), id(n2)) print(n1, ":", n2) n3 = n2[:] print(n2, ":", n3) n3 = [99, 99, 99] print(n2, ":", n3) n3[2] = 55 print(n2, ":", n3) no = ['4', '5', '6', ['1', '2', '3']] yo = no[:] no[3][0] = '22' print(no, ":", yo) from copy import deepcopy as dpc no = [1, 2, 3, [2, 3, 4]] yes = dpc(no) yes[3][2] = 33 print(no, ":", yes)