{"id":36499,"date":"2024-11-01T09:48:58","date_gmt":"2024-11-01T09:48:58","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36499"},"modified":"2024-11-01T11:52:57","modified_gmt":"2024-11-01T11:52:57","slug":"deep-learning-pytorch-course-implementation-of-tic-tac-toe-game-using-monte-carlo-tree-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36499\/","title":{"rendered":"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search"},"content":{"rendered":"<p><body><\/p>\n<article>\n<p>This article explains the process of implementing a Tic-Tac-Toe game using the Monte Carlo Tree Search (MCTS) algorithm with deep learning and PyTorch. We will primarily understand how MCTS works and how AI can play the Tic-Tac-Toe game using it.<\/p>\n<h2>Tic-Tac-Toe Game Overview<\/h2>\n<p>Tic-Tac-Toe is a game played on a 3&#215;3 square grid where two players take turns placing X or O. The player who manages to place three of their marks in a row, column, or diagonal wins the game.<\/p>\n<h2>Step 1: Environment Setup<\/h2>\n<p>To follow this tutorial, you need to install the necessary packages. Here are the main libraries required.<\/p>\n<pre><code>pip install torch numpy matplotlib<\/code><\/pre>\n<h2>Step 2: Game Environment Implementation<\/h2>\n<p>First, we implement the Tic-Tac-Toe game environment. We must define the game&#8217;s rules and create a class to represent the state.<\/p>\n<pre><code>\nimport numpy as np\n\nclass TicTacToe:\n    def __init__(self):\n        self.board = np.zeros((3, 3), dtype=int)  # 0: empty, 1: X, -1: O\n        self.current_player = 1  # 1: X's turn, -1: O's turn\n\n    def reset(self):\n        self.board = np.zeros((3, 3), dtype=int)\n        self.current_player = 1\n\n    def make_move(self, row, col):\n        if self.board[row, col] == 0:\n            self.board[row, col] = self.current_player\n            self.current_player *= -1\n\n    def check_winner(self):\n        for player in [1, -1]:\n            for row in range(3):\n                if np.all(self.board[row, :] == player):  # Check rows\n                    return player\n            for col in range(3):\n                if np.all(self.board[:, col] == player):  # Check columns\n                    return player\n            if np.all(np.diag(self.board) == player) or np.all(np.diag(np.fliplr(self.board)) == player):\n                return player\n        return None if np.any(self.board == 0) else 0  # Game is ongoing\n        \n    def display(self):\n        symbols = {1: 'X', -1: 'O', 0: ' '}\n        for row in self.board:\n            print(\"|\".join(symbols[x] for x in row))\n            print(\"-\" * 5)\n        print(\"\\n\")\n\n# Game Test\ngame = TicTacToe()\ngame.make_move(0, 0)\ngame.display()\ngame.make_move(1, 1)\ngame.display()\n        <\/code><\/pre>\n<h2>Step 3: Monte Carlo Tree Search (MCTS) Algorithm<\/h2>\n<p>MCTS is a method to solve decision-making problems in uncertain situations. Essentially, this algorithm consists of the following four steps:<\/p>\n<ol>\n<li><strong>Selection<\/strong>: Select a node from the current tree.<\/li>\n<li><strong>Expansion<\/strong>: Expand possible actions from the selected node.<\/li>\n<li><strong>Simulation<\/strong>: Play the game from the expanded node to obtain the result.<\/li>\n<li><strong>Backpropagation<\/strong>: Update the information for the parent node with the result.<\/li>\n<\/ol>\n<h3>MCTS Class Implementation<\/h3>\n<pre><code>\nimport random\nfrom collections import defaultdict\n\nclass MCTSNode:\n    def __init__(self, state, parent=None):\n        self.state = state  # Current game state\n        self.parent = parent\n        self.children = []  # Child nodes\n        self.wins = 0  # Number of wins\n        self.visits = 0  # Number of visits\n\n    def ucb1(self):\n        if self.visits == 0:\n            return float('inf')  # Select nodes that have not been visited before\n        return self.wins \/ self.visits + np.sqrt(2 * np.log(self.parent.visits) \/ self.visits)\n\nclass MCTS:\n    def __init__(self, iterations):\n        self.iterations = iterations\n\n    def search(self, game):\n        root = MCTSNode(state=game)\n\n        for _ in range(self.iterations):\n            node = self.select(root)\n            winner = self.simulate(node.state)\n            self.backpropagate(node, winner)\n\n        return max(root.children, key=lambda child: child.visits).state\n\n    def select(self, node):\n        while node.children:\n            node = max(node.children, key=lambda child: child.ucb1())\n        if node.visits > 0:\n            for action in self.get_valid_moves(node.state):\n                child_state = node.state.copy()\n                child_state.make_move(action[0], action[1])\n                child_node = MCTSNode(state=child_state, parent=node)\n                node.children.append(child_node)\n        return random.choice(node.children) if node.children else node\n\n    def simulate(self, state):\n        current_player = state.current_player\n        while True:\n            winner = state.check_winner()\n            if winner is not None:\n                return winner\n            valid_moves = self.get_valid_moves(state)\n            move = random.choice(valid_moves)\n            state.make_move(move[0], move[1])\n\n    def backpropagate(self, node, winner):\n        while node is not None:\n            node.visits += 1\n            if winner == 1:  # X wins\n                node.wins += 1\n            node = node.parent\n\n    def get_valid_moves(self, state):\n        return [(row, col) for row in range(3) for col in range(3) if state.board[row, col] == 0]\n\n# MCTS Usage Example\nmcts = MCTS(iterations=1000)\nmove = mcts.search(game)\nprint(\"AI's choice:\", move)\n        <\/code><\/pre>\n<h2>Step 4: Implementing the Game Between AI and User<\/h2>\n<p>Now, let&#8217;s implement a game between the user and the AI using the completed MCTS.<\/p>\n<pre><code>\ndef play_game():\n    game = TicTacToe()\n    while True:\n        game.display()\n        if game.current_player == 1:  # User's turn\n            row, col = map(int, input(\"Enter the row and column numbers (0, 1, or 2): \").split())\n            game.make_move(row, col)\n        else:  # AI's turn\n            print(\"AI is choosing...\")\n            move = mcts.search(game)\n            game.make_move(move[0], move[1])\n            print(f\"AI chose the position: {move}\")\n\n        winner = game.check_winner()\n        if winner is not None:\n            game.display()\n            if winner == 1:\n                print(\"Congratulations! You won!\")\n            elif winner == -1:\n                print(\"AI won!\")\n            else:\n                print(\"It's a draw!\")\n            break\n\nplay_game()\n        <\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we explored the basic guidelines of deep learning and PyTorch. The process of implementing a simple Tic-Tac-Toe AI using Monte Carlo Tree Search can be technically challenging, but it was an extremely interesting experience in the end. We hope to move forward and develop a more complete AI using various algorithms and techniques.<\/p>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article explains the process of implementing a Tic-Tac-Toe game using the Monte Carlo Tree Search (MCTS) algorithm with deep learning and PyTorch. We will primarily understand how MCTS works and how AI can play the Tic-Tac-Toe game using it. Tic-Tac-Toe Game Overview Tic-Tac-Toe is a game played on a 3&#215;3 square grid where two &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36499\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[149],"tags":[],"class_list":["post-36499","post","type-post","status-publish","format-standard","hentry","category-pytorch-study"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36499\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"This article explains the process of implementing a Tic-Tac-Toe game using the Monte Carlo Tree Search (MCTS) algorithm with deep learning and PyTorch. We will primarily understand how MCTS works and how AI can play the Tic-Tac-Toe game using it. Tic-Tac-Toe Game Overview Tic-Tac-Toe is a game played on a 3&#215;3 square grid where two &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36499\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:48:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:57+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36499\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36499\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search\",\"datePublished\":\"2024-11-01T09:48:58+00:00\",\"dateModified\":\"2024-11-01T11:52:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36499\/\"},\"wordCount\":290,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36499\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36499\/\",\"name\":\"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:48:58+00:00\",\"dateModified\":\"2024-11-01T11:52:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36499\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36499\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36499\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36499\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"This article explains the process of implementing a Tic-Tac-Toe game using the Monte Carlo Tree Search (MCTS) algorithm with deep learning and PyTorch. We will primarily understand how MCTS works and how AI can play the Tic-Tac-Toe game using it. Tic-Tac-Toe Game Overview Tic-Tac-Toe is a game played on a 3&#215;3 square grid where two &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search\"","og_url":"https:\/\/atmokpo.com\/w\/36499\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:48:58+00:00","article_modified_time":"2024-11-01T11:52:57+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36499\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36499\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search","datePublished":"2024-11-01T09:48:58+00:00","dateModified":"2024-11-01T11:52:57+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36499\/"},"wordCount":290,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36499\/","url":"https:\/\/atmokpo.com\/w\/36499\/","name":"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:48:58+00:00","dateModified":"2024-11-01T11:52:57+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36499\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36499\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36499\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Implementation of Tic-Tac-Toe Game using Monte Carlo Tree Search"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36499"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36499\/revisions"}],"predecessor-version":[{"id":36500,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36499\/revisions\/36500"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}