def __init__(self): os.environ['SDL_VIDEO_CENTERED'] = '1' # to center window pygame.init() self.window = Window("Game", config.HEIGHT, config.WIDTH, config.SCALE, flags=0) # [300, 226] self.screen = self.window.get() self.clock = pygame.time.Clock() self.isRunning = False self.texts = text.Texts(self.screen) self.textures = sprites.load( os.path.join('res', 'graphics', 'textures.png')) self.spriteBank = sprites.loadSpriteBank(self.textures) self.mark = Mark(0, 0) self.sounds = sound.Sounds() self.allSprites = sprites.GameSpriteGroup() self.player = Player(self.window.get(), self.textures, 5 * config.TILESIZE, 5 * config.TILESIZE, self.allSprites, self.spriteBank, self.mark, self.sounds, self) self.board = Board(self.window, self.textures, self.spriteBank, self.mark, self.texts, self.player) self.board.initBoard(10, 10, 16) self.renderingText = False
def step(self, m): """ Performs a move on the state """ if self.outcome != None: raise Exception('Game outcome has already been determined') import move from mark import Mark if not self.valid_move(m): raise Exception('Invalid move "%s" in game "%s". Valid moves: %s'% (m.dumps(), self.dumps(), ', '.join(self.get_valid_moves()))) if m.type == move.CLASSIC: # Put a single classic mark in a square sq = self.square(m.sq1) sq._spookies = [] for mark in sq.marks: sq._spookies.append(mark) sq.marks = [ Mark(sq, player=m.player, weight=m.weight) ] elif m.type == move.SPOOKY: # Put two spooky marks in the squares a = Mark(self.square(m.sq1), type=move.SPOOKY, player=m.player, weight=m.weight) b = Mark(self.square(m.sq2), type=move.SPOOKY, player=m.player, weight=m.weight, link=a) a.link = b self.square(m.sq1).marks.append(a) self.square(m.sq2).marks.append(b) elif m.type == move.COLLAPSE: # Expand a square self.square(m.sq2).collapse() self.moves.append(m) self._find_cycle() # Find cyclic marks self.outcome = self.get_outcome()
def AImove(self, position): availableMoves = self.getAvailableMoves() for i in range(len(availableMoves)): if position[0] == availableMoves[i][0] and position[ 1] == availableMoves[i][1]: self.board[position[0]][position[1]] = 1 if position[0] == 0 and position[1] == 0: self.objects.append( Mark(300, 300, 0, 0, self.player, 30, 1)) if position[0] == 0 and position[1] == 1: self.objects.append( Mark(380, 300, 0, 0, self.player, 30, 1)) if position[0] == 0 and position[1] == 2: self.objects.append( Mark(460, 300, 0, 0, self.player, 30, 1)) if position[0] == 1 and position[1] == 0: self.objects.append( Mark(300, 380, 0, 0, self.player, 30, 1)) if position[0] == 1 and position[1] == 1: self.objects.append( Mark(380, 380, 0, 0, self.player, 30, 1)) if position[0] == 1 and position[1] == 2: self.objects.append( Mark(460, 380, 0, 0, self.player, 30, 1)) if position[0] == 2 and position[1] == 0: self.objects.append( Mark(300, 460, 0, 0, self.player, 30, 1)) if position[0] == 2 and position[1] == 1: self.objects.append( Mark(380, 460, 0, 0, self.player, 30, 1)) if position[0] == 2 and position[1] == 2: self.objects.append( Mark(460, 460, 0, 0, self.player, 30, 1))
def is_end(self): ''' 盤面の状態がゲーム修了かどうか return: True:修了 False:修了していない(勝負がついていない) ''' if self.is_win(Mark(Maru())) or self.is_win(Mark(Batsu())) or len( self.get_valid_actions()) == 0: return True return False
def is_draw(self): ''' 盤面の状態が引き分けかどうか return: True:引き分け False:引き分けではない(勝負がついていない) ''' if len(self.get_valid_actions()) == 0 and not self.is_win(Mark( Maru())) and not self.is_win(Mark(Batsu())): return True return False
def startMarkPosition(self): """ Method to mark the start position of a new block. Starts by seeing if it is in edit mode. If it is in edit mode, it gets the index of the current mark and compares the proposed new start against all the other marks to see if it overlaps with any of them. If not, it updates the start position of the block. If it is not in edit move, it checks to see if the proposed position overlaps with any of the other blocks. If it does not, it creates a new block and sets the start position at the current position """ try: if self.is_editing: self.state.marks[self.markItr].start = self.song.get_position() self.print_to_screen('Block {} start edited'.format( (self.markItr + 1))) self.write_state_information() else: if self.current_mark: self.current_mark = None self.current_mark = Mark() self.current_mark.start = self.song.get_position() self.print_to_screen( 'Starting block {}'.format(len(self.state.marks) + 1)) except Exception as ex: sounds.error_sound(self.volume) self.log(ex)
def __init__(self,board, mark): super(QPlayer, self).__init__() self.mark = mark self.mark2 = Mark.getOpposite(mark) self.lastAction = Position() self.lastReward = 0 self.lastState = [] self.qMap = QMapArray(board.getSize()) self.board = board
def __add_mark_and_select_it(self): m = Mark(self.__IMG_SZ[0] / 2, self.__IMG_SZ[1] / 2, 10, 20, 0) if self.__marks: SH_C = 5 lm = self.__marks[-1] m.cx += SH_C if lm.cx < self.__IMG_SZ[0] / 2 else -SH_C m.cy += SH_C if lm.cy < self.__IMG_SZ[1] / 2 else -SH_C self.__marks.append(m) self.__chosen_mark_idx = len(self.__marks) - 1 self.__redraw()
def step(self, m): """ Performs a move on the state """ if self.outcome != None: raise Exception('Game outcome has already been determined') import move from mark import Mark if not self.valid_move(m): raise Exception( 'Invalid move "%s" in game "%s". Valid moves: %s' % (m.dumps(), self.dumps(), ', '.join(self.get_valid_moves()))) if m.type == move.CLASSIC: # Put a single classic mark in a square sq = self.square(m.sq1) sq._spookies = [] for mark in sq.marks: sq._spookies.append(mark) sq.marks = [Mark(sq, player=m.player, weight=m.weight)] elif m.type == move.SPOOKY: # Put two spooky marks in the squares a = Mark(self.square(m.sq1), type=move.SPOOKY, player=m.player, weight=m.weight) b = Mark(self.square(m.sq2), type=move.SPOOKY, player=m.player, weight=m.weight, link=a) a.link = b self.square(m.sq1).marks.append(a) self.square(m.sq2).marks.append(b) elif m.type == move.COLLAPSE: # Expand a square self.square(m.sq2).collapse() self.moves.append(m) self._find_cycle() # Find cyclic marks self.outcome = self.get_outcome()
def begining_ending_block(self, start): """ Method to make a block starting from the begining of the file to the current position or from the current position to the end of the file Arguments: start: Boolean - if True, then the block is from the begining to the current position, if False - from the current position to the end of the file """ try: if self.current_mark: self.print_to_screen('There is unfinished block') else: mark = Mark(position=self.song.get_position()) if start: mark.start = 0 else: mark.end = 1 self.overwriteOverlaps(mark) self.state.marks = sorted(self.state.marks, key=itemgetter('start')) self.markItr += 1 self.print_to_screen('saved') self.write_state_information() except Exception as ex: self.log(ex)
def start(self, verbose=False): ''' 対戦の開始 ''' state = State() current_player_mark = 1 result = None while (True): #print("="*30) current_player = self.players[current_player_mark] if verbose: print("%s" % (state.to_array())) print state.output() print("-" * 5) # プレイヤーの行動の選択 index = current_player.select_index(state) #print("%s selected %i" % (self.players[current_player_mark].mark.to_string(), index)) state = state.set(index, self.players[current_player_mark].mark) # この時点のstateで報酬が発生する場合はここでrewardを判定して学習できる # tic_tac_toeでは勝負が決まるまで報酬は0 current_player.learn(0) if state.is_win(self.players[current_player_mark].mark): result = self.players[current_player_mark].mark # 勝者の報酬 current_player.learn(1, True) # 敗者の報酬 self.players[result.opponent().to_int()].learn(-1, True) if verbose: print("%s" % (state.to_array())) print("-" * 5) state.output() print("-" * 5) print("%s win!!!" % (self.players[current_player_mark].mark.to_string())) break elif state.is_draw(): result = Mark(Empty()) for player in self.players.itervalues(): player.learn(0, True) if verbose: state.output() print("draw.") break current_player_mark = self.players[ current_player_mark].mark.opponent().to_int()
def hasWon(self,mark=-1): if mark==-1: mark=Mark.getOpposite(self.nextMove()) for arr in self.board: if(arr==[mark for i in range(0,self.n)]): return True for i in range(0,self.n): arr=[row[i] for row in self.board] if(arr==[mark for i in range(0,self.n)]): return True arr=[self.board[i][i] for i in range(0,self.n)] if(arr==[mark for i in range(0,self.n)]): return True arr=[self.board[i][self.n-i-1] for i in range(0,self.n)] if(arr==[mark for i in range(0,self.n)]): return True return False
def createNewMark(self): """ Method to create a new block and set it to current. defunct """ try: if self.current_mark: sounds.error_sound(self.volume) self.log( 'tried to create a new mark when one existed - createNewMark()' ) else: count = len(self.state.marks) self.current_mark = Mark(position=self.song.get_position()) self.state.marks.append(self.current_mark) self.write_state_information() self.print_to_screen('block {}.'.format(count + 1)) except Exception as ex: self.log(ex)
def get_marks(access_token: str, number_of_days: int = 7): response = requests.get( join_url(website, "api/3/marks"), headers={"Authorization": f"Bearer {access_token}"}, ) data = json.loads(response.text) current = datetime.now() current = datetime.timestamp(current) return [ Mark( subject=subject["Subject"]["Name"], value=mark["MarkText"], weight=mark["Weight"], type_=mark["TypeNote"], caption=mark["Caption"], time_added=parse_datetime(mark["EditDate"]).timestamp(), ) for subject in data["Subjects"] for mark in subject["Marks"] if _days_difference_ok( current, parse_datetime(mark["EditDate"]).timestamp(), number_of_days) ]
def begining_ending_block_old(self, start): """ Method to make a block starting from the begining of the file to the current position or from the current position to the end of the file Arguments: start: Boolean - if True, then the block is from the begining to the current position, if False - from the current position to the end of the file """ try: if self.current_mark: sounds.error_sound(self.volume) self.log( 'tried to use B or E while an existing block was current - beginning_ending_block()' ) self.print_to_screen('Overlap with an existing block') else: mark = Mark(position=self.song.get_position()) if not self.check_for_overlap(self.song.get_position()): if start: mark.start = 0 mark.end = self.song.get_position() else: mark.start = self.song.get_position() mark.end = 1 self.state.marks.append(mark) self.state.marks = sorted(self.state.marks, key=itemgetter('start')) self.markItr += 1 self.print_to_screen('saved') self.write_state_information() else: self.log( 'Tried to use B or E and found an overlap with exisitng block' ) self.print_to_screen('Overlap with an existing block') except Exception as ex: self.log(ex)
# 勝者の報酬 current_player.learn(1, True) # 敗者の報酬 self.players[result.opponent().to_int()].learn(-1, True) if verbose: print("%s" % (state.to_array())) print("-" * 5) state.output() print("-" * 5) print("%s win!!!" % (self.players[current_player_mark].mark.to_string())) break elif state.is_draw(): result = Mark(Empty()) for player in self.players.itervalues(): player.learn(0, True) if verbose: state.output() print("draw.") break current_player_mark = self.players[ current_player_mark].mark.opponent().to_int() #print("="*30) if __name__ == '__main__': player1 = Player(Mark(Maru())) player2 = Player(Mark(Batsu())) game = Game(player1, player2) game.start(verbose=True)
# モデルのロード with open('tic_tac_toe_com_1_sarsa_r.pkl', 'rb') as f: com_1 = dill.load(f) with open('tic_tac_toe_com_2_sarsa_r.pkl', 'rb') as f: com_2 = dill.load(f) while (True): print("Select a type of fight [1, 2, 3, q]") print("1: human vs com2") print("2: com1 vs human") print("3: com1 vs com2") print("q: quit") type_of_fight = 1 input_line = raw_input() if input_line.isdigit(): type_of_fight = int(input_line) else: if input_line == 'q': break continue if type_of_fight == 1: game = Game(Player(Mark(Maru())), com_2) elif type_of_fight == 2: game = Game(com_1, Player(Mark(Batsu()))) elif type_of_fight == 3: game = Game(com_1, com_2) game.start(True)
def __init__(self): # 盤面を配列で保持 self.state = [Mark(Empty())] * 9
class Game: def __init__(self): os.environ['SDL_VIDEO_CENTERED'] = '1' # to center window pygame.init() self.window = Window("Game", config.HEIGHT, config.WIDTH, config.SCALE, flags=0) # [300, 226] self.screen = self.window.get() self.clock = pygame.time.Clock() self.isRunning = False self.texts = text.Texts(self.screen) self.textures = sprites.load( os.path.join('res', 'graphics', 'textures.png')) self.spriteBank = sprites.loadSpriteBank(self.textures) self.mark = Mark(0, 0) self.sounds = sound.Sounds() self.allSprites = sprites.GameSpriteGroup() self.player = Player(self.window.get(), self.textures, 5 * config.TILESIZE, 5 * config.TILESIZE, self.allSprites, self.spriteBank, self.mark, self.sounds, self) self.board = Board(self.window, self.textures, self.spriteBank, self.mark, self.texts, self.player) self.board.initBoard(10, 10, 16) self.renderingText = False def gameLoop(self): self.isRunning = True while self.isRunning: self.clock.tick(config.FPS) self.update() if not self.isRunning: break self.render() pygame.quit() def setRenderingText(self, bool): self.renderingText = bool def update(self): self.board.update() if not self.checkCollide(self.player) and not self.renderingText: self.player.handleInput() else: self.player.y += -self.player.dy self.player.x += -self.player.dx if self.player.x < 0: self.player.x = 0 if self.player.y < 0: self.player.y = 0 if self.player.x + self.player.rect.width > 10 * 16 * 16: self.player.x = 10 * 16 * 16 - 16 if self.player.y + self.player.rect.height > 10 * 16 * 16: self.player.y = 10 * 16 * 16 - 16 self.cleanMobs() self.cleanItems() keys = pygame.key.get_pressed() events = pygame.event.get() mobsItemsDico = self.getScreenMobsItems() self.player.update(mobsItemsDico) self.updateMark() keys = pygame.key.get_pressed() events = pygame.event.get() if self.player.userEnded: self.isRunning = False def getScreenMobsItems(self): mobsItems = {} for line in self.board.boardGrid: for col in line: if config.CANVASWIDTH + config.CANVASWIDTH/2 > col.xStart - self.mark.x > - config.CANVASWIDTH - config.CANVASWIDTH/2 and \ config.CANVASHEIGHT + config.CANVASHEIGHT/2 > col.yStart - self.mark.y > - config.CANVASHEIGHT - config.CANVASHEIGHT/2: mobsItems[col] = { "mobsGen": col.enemiesGenerated, "itemsGen": col.itemsGenerated } return mobsItems def cleanMobs(self): for line in self.board.boardGrid: for col in line: if config.CANVASWIDTH + config.CANVASWIDTH/2 > col.xStart - self.mark.x > - config.CANVASWIDTH - config.CANVASWIDTH/2 and \ config.CANVASHEIGHT + config.CANVASHEIGHT/2 > col.yStart - self.mark.y > - config.CANVASHEIGHT - config.CANVASHEIGHT/2: for mob in col.enemiesToDestroy: if mob in col.enemiesGenerated: self.sounds.playHitSound() col.enemiesGenerated.remove(mob) col.enemies.remove(mob) # should be in list col.enemiesToDestroy.clear() def cleanItems(self): for line in self.board.boardGrid: for col in line: if config.CANVASWIDTH + config.CANVASWIDTH/2 > col.xStart - self.mark.x > - config.CANVASWIDTH - config.CANVASWIDTH/2 and \ config.CANVASHEIGHT + config.CANVASHEIGHT/2 > col.yStart - self.mark.y > - config.CANVASHEIGHT - config.CANVASHEIGHT/2: for item in col.itemsToDestroy: if item in col.itemsGenerated: col.itemsGenerated.remove(item) col.items.remove(item) # should be in list col.itemsToDestroy.clear() def checkCollide(self, player): for line in self.board.boardGrid: for col in line: if config.CANVASWIDTH + config.CANVASWIDTH/2 > col.xStart - self.mark.x > - config.CANVASWIDTH - config.CANVASWIDTH/2 and \ config.CANVASHEIGHT + config.CANVASHEIGHT/2 > col.yStart - self.mark.y > - config.CANVASHEIGHT - config.CANVASHEIGHT/2: for mob in col.enemiesGenerated: if pygame.sprite.collide_rect(mob, player): player.collideMob(mob, col.generatedWall) for wall in col.generatedWall: for mob in col.enemiesGenerated: if pygame.sprite.collide_rect(mob, wall): mob.collideWall() if pygame.sprite.collide_rect(player, wall): return True return False def updateMark(self): if self.player.x - self.mark.getX() > self.player.screen.get_width( ) * 0.70 and not self.player.x > 17 * len( self.board.boardGrid ) * config.TILESIZE - config.CANVASWIDTH - 64: offset = self.player.x - self.mark.getX( ) - self.player.screen.get_width() * 0.70 self.mark.x += offset elif self.player.x - self.mark.getX() < self.player.screen.get_width( ) * 0.30 and not self.player.x <= config.CANVASWIDTH * 0.30 + 17: offset = self.player.x - self.mark.getX( ) - self.player.screen.get_width() * 0.30 self.mark.x += offset if self.player.y - self.mark.getY() < self.player.screen.get_height( ) * 0.30 and not self.player.y <= config.CANVASHEIGHT * 0.30 - 17: offset = self.player.y - self.mark.getY( ) - self.player.screen.get_height() * 0.30 self.player.mark.y += offset elif self.player.y - self.mark.getY() > self.player.screen.get_height( ) * 0.7 and not self.player.y > 17 * len( self.board.boardGrid ) * config.TILESIZE - config.CANVASHEIGHT + 16: offset = self.player.y - self.mark.getY( ) - self.player.screen.get_height() * 0.70 self.mark.y += offset self.mark.y = int(self.mark.y) self.mark.x = int(self.mark.x) def render(self): self.screen.fill((255, 255, 255)) self.board.render() self.player.render() # self.allSprites.draw(self.screen) self.window.render() def compute_penetration(self, block, old_rect, new_rect): """Calcul la distance de pénétration du `new_rect` dans le `block` donné. `block`, `old_rect` et `new_rect` sont des pygame.Rect. Retourne les distances `dx_correction` et `dy_correction`. """ dx_correction = dy_correction = 0.0 if old_rect.bottom <= block.top < new_rect.bottom: dy_correction = block.top - new_rect.bottom elif old_rect.top >= block.bottom > new_rect.top: dy_correction = block.bottom - new_rect.top if old_rect.right <= block.left < new_rect.right: dx_correction = block.left - new_rect.right elif old_rect.left >= block.right > new_rect.left: dx_correction = block.right - new_rect.left return dx_correction, dy_correction
if self.is_win(Mark(Maru())) or self.is_win(Mark(Batsu())) or len( self.get_valid_actions()) == 0: return True return False def output(self): for x, y, z in zip(*[iter(self.state)] * 3): print("%s %s %s" % (x.to_string(), y.to_string(), z.to_string())) if __name__ == '__main__': state1 = State() print state1.state print state1.get_valid_actions() print state1.get(0).to_int() new_state = state1.set(0, Mark(Maru())) print new_state print new_state.get(0).to_int() print state1.to_array() print new_state.to_array() print state1.win_state print state1.is_win(Mark(Maru())) maru1 = Mark(Maru()) maru2 = Mark(Maru()) print maru1.to_int() == maru2.to_int() new_state2 = new_state.set(1, Mark(Maru())) new_state3 = new_state2.set(2, Mark(Maru())) print new_state3.to_array() print new_state3.is_win(Mark(Maru())) print new_state2.is_draw() print new_state2.is_end()
def applyEdits(self, local_marks): """ Method to create the final command for editing the original file. """ self.song.stop() self.check_for_null_blocks() # filename, file_extension = os.path.splitext(self.original_file) # edited_file = filename + "-edited" + file_extension # edited_file = self.old_file_name command = ['ffmpeg', '-i', self.original_file] select = "select='" aselect = "aselect='" # this reorganizes the marks to represent the blocks between the 'removed' # blocks last = 0 for each in local_marks: self.log(each) temp = each.end each.end = each.start each.start = last last = temp n = Mark() n.start = last n.end = 1 local_marks.append(n) # filter all the ones where start and end are equal local_marks = list( filter(lambda item: item.start != item.end, local_marks)) for i, each in enumerate(local_marks): if i == 0: select += """between(t,{},{})""".format( (self.mark_to_milliseconds(each.start) / 1000), (self.mark_to_milliseconds(each.end) / 1000), ) aselect += """between(t,{},{})""".format( (self.mark_to_milliseconds(each.start) / 1000), (self.mark_to_milliseconds(each.end) / 1000), ) else: select += """+between(t,{},{})""".format( (self.mark_to_milliseconds(each.start) / 1000), (self.mark_to_milliseconds(each.end) / 1000), ) aselect += """+between(t,{},{})""".format( (self.mark_to_milliseconds(each.start) / 1000), (self.mark_to_milliseconds(each.end) / 1000), ) select += """',setpts=N/FRAME_RATE/TB """ aselect += """',asetpts=N/SR/TB""" command.append('-vf') command.append(select) command.append('-af') command.append(aselect) command.append(self.output_file_name) self.log(command) return command
def end(self): """Return a mark pointing at the end of the buffer""" return Mark(len(self.text), self)
from mark import Mark from maru_mark import Maru from batsu_mark import Batsu from empty_mark import Empty from tic_tac_toe_state import State from tic_tac_toe_game import Game # 共通行動価値テーブル value = Value() print("Sarsa method:") print("Training com1 and com2.") com_1 = SarsaCom(Mark(Maru()), value) com_2 = SarsaCom(Mark(Batsu()), value) print("Input the number of iterations:") iterations = 10000 while(True): input_line = raw_input() if input_line.isdigit(): iterations = int(input_line) break else: print("Input number:") for i in xrange(iterations): game = Game(com_1, com_2)
#encoding: utf-8 ''' tic tac toe のSarsa λ実行クラス ''' from tic_tac_toe_sarsa_r_com import SarsaRCom from tic_tac_toe_nn_com import SarsaNNCom from mark import Mark from maru_mark import Maru from batsu_mark import Batsu from tic_tac_toe_game import Game import dill com_1 = SarsaNNCom(Mark(Maru()), 0.1, 0.1, 0.6) com_2 = SarsaRCom(Mark(Batsu()), 0.1, 0.1, 0.6) iterations = 100000 print("Input the number of iterations (%d):" % (iterations)) while(True): input_line = raw_input() if input_line.isdigit(): iterations = int(input_line) break elif input_line == '': break else: print("Input number:") # 学習 for i in xrange(iterations):
def handle_events(self): if self.winner == 0 and self.player == -1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_1: if self.board[0][0] == 0: self.objects.append( Mark(300, 300, 0, 0, self.player, 30, 1)) self.board[0][0] = -1 self.setPlayer() elif event.key == pygame.K_2: if self.board[0][1] == 0: self.objects.append( Mark(380, 300, 0, 0, self.player, 30, 1)) self.board[0][1] = -1 self.setPlayer() elif event.key == pygame.K_3: if self.board[0][2] == 0: self.objects.append( Mark(460, 300, 0, 0, self.player, 30, 1)) self.board[0][2] = -1 self.setPlayer() elif event.key == pygame.K_4: if self.board[1][0] == 0: self.objects.append( Mark(300, 380, 0, 0, self.player, 30, 1)) self.board[1][0] = -1 self.setPlayer() elif event.key == pygame.K_5: if self.board[1][1] == 0: self.objects.append( Mark(380, 380, 0, 0, self.player, 30, 1)) self.board[1][1] = -1 self.setPlayer() elif event.key == pygame.K_6: if self.board[1][2] == 0: self.objects.append( Mark(460, 380, 0, 0, self.player, 30, 1)) self.board[1][2] = -1 self.setPlayer() elif event.key == pygame.K_7: if self.board[2][0] == 0: self.objects.append( Mark(300, 460, 0, 0, self.player, 30, 1)) self.board[2][0] = -1 self.setPlayer() elif event.key == pygame.K_8: if self.board[2][1] == 0: self.objects.append( Mark(380, 460, 0, 0, self.player, 30, 1)) self.board[2][1] = -1 self.setPlayer() elif event.key == pygame.K_9: if self.board[2][2] == 0: self.objects.append( Mark(460, 460, 0, 0, self.player, 30, 1)) self.board[2][2] = -1 self.setPlayer() if self.winner == 1 or self.winner == -1: self.player = -1 if self.winner == -1: self.objects.append( TextObject(50, 650, "Player 1 wins. Press any button to close", (255, 255, 255), None, 50)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: pygame.quit() sys.exit() elif self.winner == 1: self.objects.append( TextObject(50, 650, "Player 2 wins. Press any button to close", (255, 255, 255), None, 50)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: pygame.quit() sys.exit() if self.winner == 2: self.objects.append( TextObject(50, 650, "Draw. Press any button to close", (255, 255, 255), None, 50)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: pygame.quit() sys.exit() elif self.winner == 0 and self.player == 1: self.checkWinner() if self.winner == 0 and self.player == 1 and len( self.getAvailableMoves()) > 0: availableMoves = self.getAvailableMoves() maxValue = 0 bestMove = availableMoves[0] for move in availableMoves: boardCopy = copy.deepcopy(self.board) boardCopy[move[0]][move[1]] = 1 value = self.TicTacToeModel.predict(boardCopy, 2) if value > maxValue: maxValue = value bestMove = move selectedMove = bestMove self.AImove(selectedMove) self.setPlayer()
状態 報酬 事後状態から行動を行った後の状態 ''' if next_state is None: next_state_value = 0.0 else: #状態価値テーブルから価値を取得 next_state_value = self.value[next_state] # 行動価値の更新式 self.value[state] += self.step_size * (reward + next_state_value - self.value[state]) #print("value[state] %f", self.value[state]) if __name__ == '__main__': value = Value() state = State() #print value.get_value(state) #print value.get_max_action(state, Mark(Maru())) new_state = state.set(3, Mark(Maru())) new_state1 = state.set(4, Mark(Maru())) new_state2 = new_state.set(2, Mark(Batsu())) value.update(state, 10, new_state) value.update(new_state, 10, new_state2) value.update(new_state, 10, new_state2) value.update(new_state1, 10, new_state2) value.update(new_state1, 100, new_state2) #value.update(state, -3, new_state2) print value.get_max_action(state, Mark(Maru()))
def start(self): """Return a mark pointing at the begining of the buffer""" return Mark(0, self)
for x, y, z in zip(*[iter(self.state)]*3): print("%s %s %s" % (x.to_string(), y.to_string(), z.to_string())) if __name__ == '__main__': state1 = State() print state1.state print state1.get_valid_actions() print state1.get(0).to_int() new_state = state1.set(0, Mark(Maru())) print new_state print new_state.get(0).to_int() print state1.to_array() print new_state.to_array() print state1.win_state print state1.is_win(Mark(Maru())) maru1 = Mark(Maru()) maru2 = Mark(Maru()) print maru1.to_int() == maru2.to_int() new_state2 = new_state.set(1, Mark(Maru())) new_state3 = new_state2.set(2, Mark(Maru())) print new_state3.to_array() print new_state3.is_win(Mark(Maru())) print new_state2.is_draw() print new_state2.is_end() print new_state3.is_end() new_state = state1.set(0, Mark(Batsu())) new_state2 = new_state.set(1, Mark(Maru())) new_state4 = new_state2.set(2, Mark(Batsu())) new_state5 = new_state4.set(3, Mark(Maru())) new_state6 = new_state5.set(4, Mark(Batsu())) new_state7 = new_state6.set(5, Mark(Batsu()))