def test_when_no_moves_available(self): with self.assertRaisesRegex(ValueError, 'no available moves: xxxoo....'): ai.evaluate(Board.fromstring('xxxoo'), 'x') with self.assertRaisesRegex(ValueError, 'no available moves: xxxoo....'): ai.evaluate(Board.fromstring('xxxoo'), 'o')
def test_xeeeoeeee(self): self.assertEqual( ai.evaluate(Board.fromstring('x...o'), 'x').positions, [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (3, 3)])
def move(self, ttt): (r, c) = self._rand.choice( ai.evaluate(self._convertBoard(ttt.board()), ttt.whoseTurn().lower()).positions) iSpace = 3 * (r - 1) + c - 1 for m in ttt.validMoves(): if m.iSpace == iSpace: return m assert (False), "iSpace = %d [%s]" % ( iSpace, [str(m) for m in ttt.validMoves()])
def result(self, state, input): player, b = self.boardify(state) b[input[0], input[1]] = player next_player = token.other_token(player) try: minmax = ai.evaluate(b, next_player) except ValueError: result = [next_player + str(b)] return {'optimal': result, 'suboptimal': result} results = {'optimal': [], 'suboptimal': []} optimal_moves = set(tuple(minmax.positions)) for (r, c, p) in b: if token.isempty(p): b[r, c] = next_player if (r, c) in optimal_moves: results['optimal'].append(player + str(b)) else: results['suboptimal'].append(player + str(b)) b[r, c] = '.' if not results['suboptimal']: results['suboptimal'] = results['optimal'] return results
async def _tictactoe(self, ctx, *, opponent: discord.Member = None): ''' Play tic tac toe ''' if opponent == ctx.author: return await ctx.send(embed=discord.Embed( title="Error!", description="You can't play against yourself!", colour=discord.Colour.red())) if opponent: def check(reaction, user): return (user == opponent) and (str(reaction.emoji) in ["✅", "❎"]) accept = discord.Embed( title="Tictactoe", description= f"{opponent.mention}, {ctx.author.mention} wants to play tictactoe with you!\nDo you accept?", colour=discord.Colour.green() ).set_footer( text= "React with ✅ if you want to play, and ❎ if you don't! | Request expires in 60 seconds.", icon_url=opponent.avatar_url) msg = await ctx.send(f"{opponent.mention}", embed=accept) await msg.add_reaction("✅") await msg.add_reaction("❎") try: reaction, _user = await self.bot.wait_for('reaction_add', timeout=60, check=check) except asyncio.TimeoutError: error = discord.Embed( title="Uh oh!", description="Looks like the request expired!", colour=discord.Colour.red()) await msg.edit(content=None, embed=error) try: await msg.clear_reactions() except discord.errors.HTTPException: pass else: if str(reaction.emoji) == "✅": await msg.delete() game = Game() game.start("x") board = await toEmoji(game.board.cells) player = opponent xOrO = "⭕" if player == ctx.author else "❌" board_embed = discord.Embed( title="Tic Tac Toe", description=board, colour=discord.Colour.green() if player == opponent else discord.Colour.red()).add_field( name="Player", value=f"{player.mention}: {xOrO}") board_message = await ctx.send(embed=board_embed) for reaction in expected: await board_message.add_reaction(expected[reaction]) def numcheck(reaction, user): return (user == player) and (str(reaction.emoji) in [ expected[reaction] for reaction in expected ]) and (reaction.message == board_message) while True: while True: try: number, user = await self.bot.wait_for( 'reaction_add', check=numcheck, timeout=60.0) except asyncio.TimeoutError: error = discord.Embed( title="Uh oh!", description= f"{player.mention} didn't play in time!", colour=discord.Colour.red()) await board_message.edit(content=None, embed=error) try: return await board_message.clear_reactions( ) except discord.errors.HTTPException: return else: for key, time in enumerate(expected, start=1): if str(number.emoji) == expected[key]: place = time break x, y = coords[place] result = game.moveto(x, y) try: await number.message.clear_reaction( number.emoji) except discord.errors.HTTPException: pass else: pass if result['name'] == 'next-turn': player = opponent if player == ctx.author else ctx.author xOrO = "⭕" if player == ctx.author else "❌" board_embed = discord.Embed( title="Tic Tac Toe", description=await toEmoji(game.board.cells), colour=discord.Colour.green() if player == opponent else discord.Colour.red()).add_field( name="Player", value=f"{player.mention}: {xOrO}") await board_message.edit(embed=board_embed) elif result['name'] == 'gameover': if result['reason'] == 'winner': win_embed = discord.Embed( title="🎉 Winner! 🎉", description=await toEmoji(game.board.cells), colour=discord.Colour.gold() ).add_field( name="Winner", value=f"Congrats! {player.mention} won!" ) await board_message.edit(embed=win_embed) try: return await board_message.clear_reactions( ) except discord.errors.HTTPException: return elif result['reason'] == 'squashed': draw_embed = discord.Embed( title="Draw!", description=f"It's a draw!", colour=discord.Colour.blurple()) await board_message.edit(embed=draw_embed) try: return await board_message.clear_reactions( ) except discord.errors.HTTPException: return elif str(reaction.emoji) == "❎": error = discord.Embed( title="Uh oh!", description= f"Looks like {opponent.mention} didn't want to play!", colour=discord.Colour.red()) await msg.edit(content=None, embed=error) try: await msg.clear_reactions() except discord.errors.HTTPException: pass else: game = Game() game.start("x") board = await toEmoji(game.board.cells) player = self.bot.user xOrO = "⭕" if player == ctx.author else "❌" board_embed = discord.Embed( title="Tic Tac Toe", description=board, colour=discord.Colour.green() if player == opponent else discord.Colour.red()).add_field( name="Player", value=f"{player.mention}: {xOrO}") board_message = await ctx.send(embed=board_embed) for reaction in expected: await board_message.add_reaction(expected[reaction]) messageID = board_message.id while True: aiMove = ai.evaluate(game.board, "x") x, y = random.choice(aiMove[2]) result = game.moveto(x, y) number = await getItem(coords, (x, y)) board_message = await board_message.channel.fetch_message( messageID) for i in board_message.reactions: if str(i.emoji) == expected[int(number)]: try: await board_message.clear_reaction(i) except discord.errors.HTTPException: pass if result['name'] == 'next-turn': player = opponent if player == ctx.author else ctx.author xOrO = "⭕" if player == ctx.author else "❌" board_embed = discord.Embed( title="Tic Tac Toe", description=await toEmoji(game.board.cells), colour=discord.Colour.green() if player == opponent else discord.Colour.red()).add_field( name="Player", value=f"{player.mention}: {xOrO}") await board_message.edit(embed=board_embed) elif result['name'] == 'gameover': if result['reason'] == 'winner': win_embed = discord.Embed( title="🎉 Winner! 🎉", description=await toEmoji(game.board.cells), colour=discord.Colour.gold()).add_field( name="Winner", value=f"Congrats! {player.mention} won!") await board_message.edit(embed=win_embed) try: return await board_message.clear_reactions() except discord.errors.HTTPException: return elif result['reason'] == 'squashed': draw_embed = discord.Embed( title="Draw!", description=f"It's a draw!", colour=discord.Colour.blurple()) await board_message.edit(embed=draw_embed) try: return await board_message.clear_reactions() except discord.errors.HTTPException: return def numcheck(reaction, user): return (user == player) and (str(reaction.emoji) in [ expected[reaction] for reaction in expected ]) and (reaction.message == board_message) try: number, user = await self.bot.wait_for('reaction_add', check=numcheck, timeout=60.0) except asyncio.TimeoutError: error = discord.Embed( title="Uh oh!", description=f"{player.mention} didn't play in time!", colour=discord.Colour.red()) await board_message.edit(content=None, embed=error) try: return await board_message.clear_reactions() except discord.errors.HTTPException: return else: for key, time in enumerate(expected, start=1): if str(number.emoji) == expected[key]: place = time break x, y = coords[place] result = game.moveto(x, y) try: await number.message.clear_reaction(number.emoji) except discord.errors.HTTPException: pass else: pass if result['name'] == 'next-turn': player = self.bot.user if player == ctx.author else ctx.author xOrO = "⭕" if player == ctx.author else "❌" board_embed = discord.Embed( title="Tic Tac Toe", description=await toEmoji(game.board.cells), colour=discord.Colour.green() if player == opponent else discord.Colour.red()).add_field( name="Player", value=f"{player.mention}: {xOrO}") await board_message.edit(embed=board_embed) elif result['name'] == 'gameover': if result['reason'] == 'winner': win_embed = discord.Embed( title="🎉 Winner! 🎉", description=f"Congrats! {player.mention} won!", colour=discord.Colour.gold()) await board_message.edit(embed=win_embed) try: return await board_message.clear_reactions() except discord.errors.HTTPException: return elif result['reason'] == 'squashed': draw_embed = discord.Embed( title="Draw!", description=f"It's a draw!", colour=discord.Colour.blurple()) await board_message.edit(embed=draw_embed) try: return await board_message.clear_reactions() except discord.errors.HTTPException: return
async def on_message(message): if message.content == "z.info": tmpembed = discord.Embed(title = "XenoBot", description = "a0.0.1") tmpembed.set_thumbnail(url = app.user.avatar_url) await message.channel.send(embed = tmpembed) return elif message.content == "z.help": tmpembed = discord.Embed(title = "Minigames", description = f"There're {len(minigames)} minigames!\n\ `z.play <Minigame Number>` to play minigame!") for x in range(len(minigames)): tmpembed.add_field(name = f"{x + 1}. {minigames[x][0]}", value = f"`{minigames[x][1]}`") await message.channel.send(embed = tmpembed) return elif message.content == "z.ping": tmpembed = discord.Embed(title = "🏓 Pong!", description = f"{str(app.latency * 1000)[:6]}ms") await message.channel.send(embed = tmpembed) elif message.content.startswith("z.play"): gamenumber = message.content[7:] if gamenumber == "1" or gamenumber == "가위바위보": tmpembed = discord.Embed(title = "🖐️ 가위바위보", description = "각자 가위 또는 바위 또는 보를 내서 승부를 결정하는 게임입니다. 가위는 보를, 바위는 가위를, 보는 묵을 이길 수 있습니다. 두 명이 같은 손을 낼 시 비깁니다.") await message.channel.send(embed = tmpembed) tmpembed = discord.Embed(title = "가위, 바위, 보 중 하나를 선택하세요") main = await message.channel.send(embed = tmpembed) await main.add_reaction("✌️") await main.add_reaction("✊") await main.add_reaction("🖐️") hands = ["✌️", "✊", "🖐️"] def check(reaction, user): return user == message.author and str(reaction.emoji) in hands try: reaction, user = await app.wait_for('reaction_add', timeout = 5, check = check) except asyncio.TimeoutError: tmpembed = discord.Embed(title = "당신은 패배하였습니다!", description = "선택하는 데 시간이 너무 오래 걸렸습니다!") await message.channel.send(embed = tmpembed) return else: choiceOfCpu = random.choice(hands) if choiceOfCpu == str(reaction.emoji): tmpembed = discord.Embed(title = "비겼습니다!", description = f"봇도 {choiceOfCpu}를 냈습니다!") await message.channel.send(embed = tmpembed) return winlist = ["✊✌️", "✌️🖐️", "🖐️✊"] if str(reaction.emoji) + choiceOfCpu in winlist: tmpembed = discord.Embed(title = "당신이 이겼습니다!", description = f"봇은 {choiceOfCpu}를 냈습니다!") await message.channel.send(embed = tmpembed) return else: tmpembed = discord.Embed(title = "봇이 이겼습니다!", description = f"봇은 {choiceOfCpu}를 냈습니다!") await message.channel.send(embed = tmpembed) return elif gamenumber == "2" or gamenumber == "업다운": tmpembed = discord.Embed(title = "↕️ 업다운", description = "1~100까지 숫자중 하나를 골랐을 때 그것을 맞추는 게임입니다. 기회는 총 5번 있습니다. 숫자를 추측하면 봇이 생각한 숫자보다 큰지 작은지 알려줍니다.") await message.channel.send(embed = tmpembed) choiceOfCpu = random.randint(1, 100) before = "" for x in range(5): tmpembed = discord.Embed(title = f"{before}{5 - x}번의 기회가 남았습니다.", description = "1~100까지 숫자중 하나를 고르세요.") await message.channel.send(embed = tmpembed) def check(msg): return msg.author == message.author try: msg = await app.wait_for('message', timeout = 10, check = check) except asyncio.TimeoutError: tmpembed = discord.Embed(title = "봇이 승리하였습니다!", description = "선택하는 데 시간이 너무 오래 걸렸습니다!") await message.channel.send(embed = tmpembed) return else: try: a = int(msg.content) except: before = "숫자를 입력해 주세요. " else: if choiceOfCpu == a: tmpembed = discord.Embed(title = "당신이 이겼습니다!", description = f"봇이 생각한 숫자는 {choiceOfCpu}였습니다.") await message.channel.send(embed = tmpembed) return elif choiceOfCpu > a: before = f"{a}보다 큽니다! " else: before = f"{a}보다 작습니다! " tmpembed = discord.Embed(title = "봇이 이겼습니다!", description = f"봇이 생각한 숫자는 {choiceOfCpu}였습니다.") await message.channel.send(embed = tmpembed) elif gamenumber == "3" or gamenumber == "슬라이딩 퍼즐": tmpembed = discord.Embed(title = "🔠 16 슬라이딩 퍼즐", description = "각각의 타일을 밀어서 A부터 O까지 숫자를 순서대로 정렬하는 퍼즐입니다.") await message.channel.send(embed = tmpembed) mixingCount = 0 tmpembed = discord.Embed(title = "난이도를 선택하세요!", description = "🟩 : 쉬움\n🟧 : 보통\n🟥 : 어려움") difficulty = await message.channel.send(embed = tmpembed) await difficulty.add_reaction("🟩") await difficulty.add_reaction("🟧") await difficulty.add_reaction("🟥") difficulties = ["🟩", "🟧", "🟥"] def check(reaction, user): return user == message.author and str(reaction.emoji) in difficulties try: reaction, user = await app.wait_for('reaction_add', timeout = 40, check = check) except asyncio.TimeoutError: tmpembed = discord.Embed(title = "시간 초과!", description = "난이도를 선택하는 데 시간이 너무 오래 걸렸습니다!") await message.channel.send(embed = tmpembed) return else: if str(reaction.emoji) == difficulties[0]: mixingCount = random.randint(5, 10) if str(reaction.emoji) == difficulties[1]: mixingCount = random.randint(15, 25) if str(reaction.emoji) == difficulties[2]: mixingCount = random.randint(30, 50) xpt, ypt = 3, 3 doneBoard = [ [1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16] ] board = [ [1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16] ] def moveTo(mx, my, xpt, ypt): if not(0 <= (xpt + mx) < 4): return False if not(0 <= (ypt + my) < 4): return False tmp = board[ypt + my][xpt + mx] board[ypt + my][xpt + mx] = board[ypt][xpt] board[ypt][xpt] = tmp return True def boardStr(): strs = "" for y in range(4): for x in range(4): if board[y][x] == 16: strs += "🟪" else: strs += ":regional_indicator_" + ("abcdefghijklmno")[board[y][x] - 1] + ":" strs += "\n" return strs tmpembed = discord.Embed(title = "타일을 섞는 중입니다.") main = await message.channel.send(embed = tmpembed) await main.add_reaction("⬆") await main.add_reaction("⬇") await main.add_reaction("⬅") await main.add_reaction("➡") await main.add_reaction("🚫") cntMix, cntSolv = 0, 0 moving = "" while cntMix < mixingCount: canmove = [[1, 0], [-1, 0], [0, 1], [0, -1]] movedirc = ["⬅", "➡", "⬆", "⬇"] wantmove = random.randint(0, 3) if moveTo(canmove[wantmove][0], canmove[wantmove][1], xpt, ypt) == True: if len(moving) >= 1: if moving[len(moving) - 1] == movedirc[0] and wantmove == 1: moving = moving[0: len(moving) - 1] xpt += canmove[wantmove][0] ypt += canmove[wantmove][1] cntMix -= 1 continue if moving[len(moving) - 1] == movedirc[1] and wantmove == 0: moving = moving[0: len(moving) - 1] xpt += canmove[wantmove][0] ypt += canmove[wantmove][1] cntMix -= 1 continue if moving[len(moving) - 1] == movedirc[2] and wantmove == 3: moving = moving[0: len(moving) - 1] xpt += canmove[wantmove][0] ypt += canmove[wantmove][1] cntMix -= 1 continue if moving[len(moving) - 1] == movedirc[3] and wantmove == 2: moving = moving[0: len(moving) - 1] xpt += canmove[wantmove][0] ypt += canmove[wantmove][1] cntMix -= 1 continue xpt += canmove[wantmove][0] ypt += canmove[wantmove][1] moving += movedirc[wantmove] cntMix += 1 while 1: tmpembed = discord.Embed(title = "타일을 밀 방향을 선택하세요. [포기: 🚫]", description = boardStr()) await main.edit(embed = tmpembed) arrows = ["⬆", "⬇", "⬅", "➡", "🚫"] def check(reaction, user): return user == message.author and str(reaction.emoji) in arrows try: reaction, user = await app.wait_for('reaction_add', timeout = 40, check = check) except asyncio.TimeoutError: tmpembed = discord.Embed(title = "실패하였습니다!", description = "생각하는 데 시간이 너무 오래 걸렸습니다!") await message.channel.send(embed = tmpembed) return else: if str(reaction.emoji) == arrows[4]: tmpembed = discord.Embed(title = "봇이 이겼습니다!", description = f"봇이 타일을 섞은 과정은 {moving}이었습니다.") await message.channel.send(embed = tmpembed) return if str(reaction.emoji) == arrows[0]: if moveTo(0, 1, xpt, ypt) == True: ypt += 1 cntSolv += 1 if str(reaction.emoji) == arrows[1]: if moveTo(0, -1, xpt, ypt) == True: ypt += -1 cntSolv += 1 if str(reaction.emoji) == arrows[2]: if moveTo(1, 0, xpt, ypt) == True: xpt += 1 cntSolv += 1 if str(reaction.emoji) == arrows[3]: if moveTo(-1, 0, xpt, ypt) == True: xpt += -1 cntSolv += 1 try: await reaction.remove(message.author) except: time.sleep(0) if board == doneBoard: break tmpembed = discord.Embed(title = "타일을 밀 방향을 선택하세요. [포기: 🚫]", description = boardStr()) await main.edit(embed = tmpembed) tmpembed = discord.Embed(title = "당신이 이겼습니다!", description = f"섞으면서 타일을 움직인 횟수는 {cntMix}이고 풀면서 타일을 움직인 횟수는 {cntSolv}입니다. 봇이 타일을 섞은 과정은 {moving}이었습니다.") await message.channel.send(embed = tmpembed) elif gamenumber == "4" or gamenumber == "틱택토": tmpembed = discord.Embed(title = "⏺️ 틱택토", description = "번갈아 가며 칸에 표시를 하고 한 줄을 먼저 완성하면 승리합니다.") await message.channel.send(embed = tmpembed) board = Board.fromstring('.') def getboard(board, selX, selY): if selY == 1: strs = ":blue_square::arrow_down::two::three:\n" if selY == 2: strs = ":blue_square::one::arrow_down::three:\n" if selY == 3: strs = ":blue_square::one::two::arrow_down:\n" a = 0 for x in range(3): if x == 0: if selX - 1 == x: strs += ":arrow_right:" else: strs += ":regional_indicator_a:" if x == 1: if selX - 1 == x: strs += ":arrow_right:" else: strs += ":regional_indicator_b:" if x == 2: if selX - 1 == x: strs += ":arrow_right:" else: strs += ":regional_indicator_c:" for y in range(3): if board[a] == '.': strs += "🟪" if board[a] == 'o': strs += ":regional_indicator_o:" if board[a] == 'x': strs += ":regional_indicator_x:" a += 1 strs += "\n" return strs tmpembed = discord.Embed(title = "로딩 중입니다.") main = await message.channel.send(embed = tmpembed) await main.add_reaction("🇦") await main.add_reaction("🇧") await main.add_reaction("🇨") await main.add_reaction("1️⃣") await main.add_reaction("2️⃣") await main.add_reaction("3️⃣") await main.add_reaction("☑️") selectedX, selectedY = 1, 1 while 1: tmpembed = discord.Embed(title = "알파벳과 숫자를 선택하고 ☑️를 눌러주세요.", description = getboard(str(board), selectedX, selectedY)) await main.edit(embed = tmpembed) choices = ["🇦", "🇧", "🇨", "1️⃣", "2️⃣", "3️⃣", "☑️"] def check(reaction, user): return user == message.author and str(reaction.emoji) in choices try: reaction, user = await app.wait_for('reaction_add', timeout = 30, check = check) except asyncio.TimeoutError: tmpembed = discord.Embed(title = "당신은 패배하였습니다!", description = "칸을 고르는 데 시간이 너무 오래 걸렸습니다!") await message.channel.send(embed = tmpembed) return else: if str(reaction.emoji) == "☑️": if board[selectedX, selectedY] == " ": board[selectedX, selectedY] = "o" ismewin = False if board[1, 1] == board[1, 2] == board[1, 3] == "o": ismewin = True if board[2, 1] == board[2, 2] == board[2, 3] == "o": ismewin = True if board[3, 1] == board[3, 2] == board[3, 3] == "o": ismewin = True if board[1, 1] == board[2, 1] == board[3, 1] == "o": ismewin = True if board[1, 2] == board[2, 2] == board[3, 2] == "o": ismewin = True if board[1, 3] == board[2, 3] == board[3, 3] == "o": ismewin = True if board[1, 1] == board[2, 2] == board[3, 3] == "o": ismewin = True if board[1, 3] == board[2, 2] == board[1, 3] == "o": ismewin = True if ismewin: tmpembed = discord.Embed(title = "알파벳과 숫자를 선택하고 ☑️를 눌러주세요.", description = getboard(str(board), selectedX, selectedY)) await main.edit(embed = tmpembed) tmpembed = discord.Embed(title = "당신이 이겼습니다!") await message.channel.send(embed = tmpembed) return try: airesult = ai.evaluate(board, "x") aido = random.choice(airesult.positions) board[aido] = "x" ismewin = False if board[1, 1] == board[1, 2] == board[1, 3] == "x": ismewin = True if board[2, 1] == board[2, 2] == board[2, 3] == "x": ismewin = True if board[3, 1] == board[3, 2] == board[3, 3] == "x": ismewin = True if board[1, 1] == board[2, 1] == board[3, 1] == "x": ismewin = True if board[1, 2] == board[2, 2] == board[3, 2] == "x": ismewin = True if board[1, 3] == board[2, 3] == board[3, 3] == "x": ismewin = True if board[1, 1] == board[2, 2] == board[3, 3] == "x": ismewin = True if board[1, 3] == board[2, 2] == board[1, 3] == "x": ismewin = True if ismewin: tmpembed = discord.Embed(title = "알파벳과 숫자를 선택하고 ☑️를 눌러주세요.", description = getboard(str(board), selectedX, selectedY)) await main.edit(embed = tmpembed) tmpembed = discord.Embed(title = "봇이 이겼습니다!") await message.channel.send(embed = tmpembed) return except: tmpembed = discord.Embed(title = "알파벳과 숫자를 선택하고 ☑️를 눌러주세요.", description = getboard(str(board), selectedX, selectedY)) await main.edit(embed = tmpembed) tmpembed = discord.Embed(title = "비겼습니다!") await message.channel.send(embed = tmpembed) return elif str(reaction.emoji) == "🇦": selectedX = 1 elif str(reaction.emoji) == "🇧": selectedX = 2 elif str(reaction.emoji) == "🇨": selectedX = 3 elif str(reaction.emoji) == "1️⃣": selectedY = 1 elif str(reaction.emoji) == "2️⃣": selectedY = 2 elif str(reaction.emoji) == "3️⃣": selectedY = 3 try: await reaction.remove(message.author) except: time.sleep(0) else: tmpembed = discord.Embed(title = "Unknown Minigame Number", description = "Send `z.help`to show minigames list") await message.channel.send(embed = tmpembed) if message.content.startswith("z.eval") and isModer(message.author): await message.channel.send(eval(message.content[7:])) elif message.content.startswith("z.exec") and isModer(message.author): exec(message.content[7:])
def test_when_not_token_turn(self): with self.assertRaisesRegex(ValueError, "not x's turn to play: xxo......"): ai.evaluate(Board.fromstring('xxo'), 'x')
def test_when_board_is_invalid(self): with self.assertRaisesRegex(ValueError, 'invalid board: xxx......'): ai.evaluate(Board.fromstring('xxx'), 'x') with self.assertRaisesRegex(ValueError, 'invalid board: xxx......'): ai.evaluate(Board.fromstring('xxx'), 'o')
def test_when_not_a_token(self): with self.assertRaisesRegex(ValueError, 'must be a token: .'): ai.evaluate(Board.fromstring(), '.')
def test_xeooeexex(self): self.assertEqual( ai.evaluate(Board.fromstring('x.oo..x.x'), 'o').positions, [(1, 2), (2, 2), (2, 3), (3, 2)])
def test_xeeeeeeee(self): self.assertEqual( ai.evaluate(Board.fromstring('x'), 'o').positions, [(2, 2)])
def test_xoexoeeee(self): self.assertEqual( ai.evaluate(Board.fromstring('xo.xo.'), 'o').positions, [(3, 2)])
def test_eeeexeeee(self): self.assertEqual( ai.evaluate(Board.fromstring('....x'), 'o').positions, [(1, 1), (1, 3), (3, 1), (3, 3)])
class RandomAgent(object): def __init__(self, game): self.game = game def next_move(self): free_locations = get_free_locations(self.game) return free_locations[random.randint(0, len(free_locations))] # This is one round, ending with a reward game = Game() agent = RandomAgent(game) game.start('o') while True: ai_move = ai.evaluate(game.board, 'o').positions[0] status = game.moveto(*ai_move) print(game.board.toascii()) if status['name'] == 'gameover': break agent_move = agent.next_move() status = game.moveto(*agent_move) print(game.board.toascii()) if status['name'] == 'gameover': break if game.statistics['xwins'] == 1: agent_reward = 1 elif game.statistics['squashed'] == 1: agent_reward = 0 else: agent_reward = -1