1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | # º¸µå´Â 1Â÷¿ø ¸®½ºÆ®·Î ±¸ÇöÇÑ´Ù. game_board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] # ºñ¾î ÀÖ´Â ÄÀ» ã¾Æ¼ ¸®½ºÆ®·Î ¹ÝȯÇÑ´Ù. def empty_cells(board): cells = [] for x, cell in enumerate(board): if cell == ' ': cells.append(x) return cells # ºñ¾î ÀÖ´Â Ä¿¡´Â ³õÀ» ¼ö ÀÖ´Ù. def valid_move(x): return x in empty_cells(game_board) # À§Ä¡ x¿¡ ³õ´Â´Ù. def move(x, player): if valid_move(x): game_board[x] = player return True return False # ÇöÀç °ÔÀÓ º¸µå¸¦ ±×¸°´Ù. def draw(board): for i, cell in enumerate(board): if i%3 == 0: print('\n----------------') print('|', cell , '|', end='') print('\n----------------') # º¸µåÀÇ »óŸ¦ Æò°¡ÇÑ´Ù. def evaluate(board): if check_win(board, 'X'): score = 1 elif check_win(board, 'O'): score = -1 else: score = 0 return score # 1Â÷¿ø ¸®½ºÆ®¿¡¼ µ¿ÀÏÇÑ ¹®ÀÚ°¡ ¼öÁ÷¼±À̳ª ¼öÆò¼±, ´ë°¢¼±À¸·Î ³ªÅ¸³ª¸é # ½Â¸®ÇÑ °ÍÀ¸·Î ÇÑ´Ù. def check_win(board, player): win_conf = [ [board[0], board[1], board[2]], [board[3], board[4], board[5]], [board[6], board[7], board[8]], [board[0], board[3], board[6]], [board[1], board[4], board[7]], [board[2], board[5], board[8]], [board[0], board[4], board[8]], [board[2], board[4], board[6]], ] return [player, player, player] in win_conf # 1Â÷¿ø ¸®½ºÆ®¿¡¼ µ¿ÀÏÇÑ ¹®ÀÚ°¡ ¼öÁ÷¼±À̳ª ¼öÆò¼±, ´ë°¢¼±À¸·Î ³ªÅ¸³ª¸é # ½Â¸®ÇÑ °ÍÀ¸·Î ÇÑ´Ù. def game_over(board): return check_win(board, 'X') or check_win(board, 'O') # ¹Ì´Ï¸Æ½º ¾Ë°í¸®ÁòÀ» ±¸ÇöÇÑ´Ù. # ÀÌ ÇÔ¼ö´Â ¼øȯÀûÀ¸·Î È£ÃâµÈ´Ù. def minimax(board, depth, maxPlayer): pos = -1 # ´Ü¸» ³ëµåÀÌ¸é º¸µå¸¦ Æò°¡ÇÏ¿© À§Ä¡¿Í Æò°¡°ªÀ» ¹ÝȯÇÑ´Ù. if depth == 0 or len(empty_cells(board)) == 0 or game_over(board): return -1, evaluate(board) if maxPlayer: value = -10000 # À½ÀÇ ¹«ÇÑ´ë # ÀÚ½Ä ³ëµå¸¦ Çϳª¾¿ Æò°¡ÇÏ¿©¼ ÃÖ¼±ÀÇ ¼ö¸¦ ã´Â´Ù. for p in empty_cells(board): board[p] = 'X' # º¸µåÀÇ p À§Ä¡¿¡ 'X'À» ³õ´Â´Ù. # °æ±âÀÚ¸¦ ±³Ã¼ÇÏ¿©¼ minimax()¸¦ ¼øȯȣÃâÇÑ´Ù. x, score = minimax(board, depth-1, False) board[p] = ' ' # º¸µå´Â ¿ø »óÅ·Πµ¹¸°´Ù. if score > value: value = score # ÃÖ´ë°ªÀ» ÃëÇÑ´Ù. pos = p # ÃÖ´ë°ªÀÇ À§Ä¡¸¦ ±â¾ïÇÑ´Ù. else: value = +10000 # ¾çÀÇ ¹«ÇÑ´ë # ÀÚ½Ä ³ëµå¸¦ Çϳª¾¿ Æò°¡ÇÏ¿©¼ ÃÖ¼±ÀÇ ¼ö¸¦ ã´Â´Ù. for p in empty_cells(board): board[p] = 'O' # º¸µåÀÇ p À§Ä¡¿¡ 'O'À» ³õ´Â´Ù. # °æ±âÀÚ¸¦ ±³Ã¼ÇÏ¿©¼ minimax()¸¦ ¼øȯȣÃâÇÑ´Ù. x, score = minimax(board, depth-1, True) board[p] = ' ' # º¸µå´Â ¿ø »óÅ·Πµ¹¸°´Ù. if score < value: value = score # ÃÖ¼Ò°ªÀ» ÃëÇÑ´Ù. pos = p # ÃÖ¼Ò°ªÀÇ À§Ä¡¸¦ ±â¾ïÇÑ´Ù. return pos, value # À§Ä¡¿Í °ªÀ» ¹ÝȯÇÑ´Ù. player='X' # ¸ÞÀÎ ÇÁ·Î±×·¥ while True: draw(game_board) if len(empty_cells(game_board)) == 0 or game_over(game_board): break i, v = minimax(game_board, 9, player=='X') move(i, player) if player=='X': player='O' else: player='X' if check_win(game_board, 'X'): print('X ½Â¸®!') elif check_win(game_board, 'O'): print('O ½Â¸®!') else: print('ºñ°å½À´Ï´Ù!') | cs |