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 _convertBoard(self, board): outBoard = [] for r in range(3): for c in range(3): i = 3 * r + c if board[i] == game.Empty: t = '.' else: t = board[i].lower() outBoard.append(t) return Board.fromstring(''.join(outBoard))
def test_when_layout_is_empty(self): self.assertEqual(str(Board.fromstring()), '.........')
def setUp(self): self.board = Board.fromstring('x.xo..')
def setUp(self): self.board = Board.fromstring('x.o.o.x.x')
def test_when_layout_is_longer_than_9_characters(self): self.assertEqual(str(Board.fromstring('x oxoxo')), 'x.......o')
def test_when_layout_is_non_empty_but_shorter_than_9_characters(self): self.assertEqual(str(Board.fromstring('x')), 'x........')
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 setUp(self): self.board = Board.fromstring('x...o')
def test_when_layout_contains_non_pieces(self): self.assertEqual(str(Board.fromstring('x.o-*x1o^')), 'x.o..x.o.')
def test_eeeexeeee(self): self.assertEqual( ai.evaluate(Board.fromstring('....x'), 'o').positions, [(1, 1), (1, 3), (3, 1), (3, 3)])