0% found this document useful (0 votes)
15 views8 pages

Tic Tac Minimax Algorithm

The document describes a GitHub repository for a Tic-Tac-Toe implementation using the Minimax AI algorithm. It explains the structure of a game tree, the workings of the Minimax algorithm, and provides pseudocode along with a Python implementation. The repository is open-source and licensed under GPL-3.0, with contributions from multiple developers.

Uploaded by

emmadanielo683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Tic Tac Minimax Algorithm

The document describes a GitHub repository for a Tic-Tac-Toe implementation using the Minimax AI algorithm. It explains the structure of a game tree, the workings of the Minimax algorithm, and provides pseudocode along with a Python implementation. The repository is open-source and licensed under GPL-3.0, with contributions from multiple developers.

Uploaded by

emmadanielo683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
3729125, 243. AM [Github - Cledersonbeltc-tac-oe-minimax: Minimax is @ Al algoritm, @ Cledersonbc / tic-tac-toe-minimax | Public Minimax is a Al algorithm. 8B GPL-3.0 license YY 440 stars 253 forks FF Branches © Tags Activity WW star D Notifications, <> Code © Issues 3 [4 Pullrequests 2 © Actions fH] Projects @ Security Lx Insights FP 3Branches QOTagss FP OD Q Gotofile Go to file Code oo © ledersonbe Merge pull request #3 from sahi-python/master 10168a7 5 yearsago & preview Last part of explanation on R.. 8 years ago © pyversion refactored some variable na. 7 years ago WS web_version Last part of explanation on R... 8 years ago 1 itignore Playable version of Tic Tac Toe 8 years ago © ucense Initial commit 8 years ago © READMEmd Update README.md 5 years ago © indexhtmi Adjusted mobile viewport T years ago a tic-tac-toe-minimax An implementation of Minimax Al Algorithm on Tic-Tac-Toe (or Noughts and Crosses) game. Try it: Tic-tac-toe - Minimax ntps:igihub.com/Cledersonbctictac-toe-minimax?tab=readine-ov-le 18 3729125, 2:43AM [Githus - Cledersonbeltc-tac-oe-minimax: Minimax is @ Al algorithm, Minimax Algorithm O X O (CO README 418 GPL-30 license O XX Introduction To solve games using Al, we will introduce the concept of a game tree followed by minimax algorithm. The different states of the game are represented by nodes in the game tree, very similar to the above planning problems. The idea is just slightly different. In the game tree, the nodes are arranged in levels that correspond to each player's turns in the game so that the “root” node of the tree (usually depicted at the top of the diagram) is the beginning position in the game. In tic-tac-toe, this would be the empty grid with no Xs or Os played yet. Under root, on the second level, there are the possible states that can result from the first player's moves, be it X or O. We call these nodes the “children” of the root node. Each node on the second level, would further have as its children nodes the states that can be reached from it by the opposing player's moves. This is continued, level by level, until reaching states where the game is over. In tic-tac-toe, this means that either one of the players gets a line of three and wins, or the board is full and the game ends in a tie. ntps:igihub.com/Cledersonbctic-tac-tov-minimax?tab=readine-ov-le » 3729125, 2:43AM [Github - Cledersonbeltc-tac-oe-minimax: Minimax is @ Al algoritm, What is Minimax? Minimax is a artificial intelligence applied in two player games, such as tic-tac-toe, checkers, chess and go. This games are known as zero-sum games, because in a mathematical representation: one player wins (+1) and other player loses (-1) or both of anyone not to win ©. How does it works? The algorithm search, recursively, the best move that leads the Max player to win or not lose (draw). it consider the current state of the game and the available moves at that state, then for each valid move it plays (alternating min and max) untilit finds a terminal state (win, draw or lose). Understanding the Algorithm The algorithm was studied by the book Algorithms in a Nutshell (George Heineman; Gary Pollice; Stanley Selkow, 2009). Pseudocode (adapted): minimax(state, depth, player) o if (player = max) then best = [null, ~infinity] else best [null, +infinity] if (depth = © or gameover) then score = evaluate this state for player return [null, score] for each valid move m for player in state s do execute move mons [move, score] = minimax(s, depth - 1, -player) undo move mon s if (player = max) then Af score > best.score then best [move, score] else if score < best.score then best = [move, score] return best end ntps:igihub.com/Cledersonbctictac-tov-minimax?tab=readine-ov-le » ‘unanas,243 aM CGttuo -Cledersonboticacte-minimax: Minka eA algo Now welll see each part of this pseudocode with Python implementation, The Python implementation is available at this repository, First of all, consider it: board = [ (0, 0, 0), (0, 0, 0), [0, 0, 0} MAX = +1 MIN = -1 The MAX may be X or O and the MIN may be O or X, whatever. The board is 3x3. def minimax(state, depth, player. oe ‘state: the current board in tic-tac-toe (node) ‘* depth: index of the node in the game tree * player: may be a MAX player or MIN player if player == MAX: oe return [-1, -1, -infinity] else: return [-1, -1, +infinity] Both players start with your worst score. If player is MAX, its score is -infinity. Else if player is MIN, its score is +infinity. Note: infinity is an alias for inf (from math module, in Python). The best move on the board is [-1, -1] (row and column) for all, if depth @ or gane_over(state): score = evaluate(state) return score If the depth is equal zero, then the board hasn't new empty cells to play. Or, if a player wins, then the game ended for MAX or MIN. So the score for that state will be returned. © IF MAX won: return +1 © If MIN won: return -1 © Else: return 0 (draw) Now we'll see the main part of this code that contains recursion. for cell in enpty_celis(state): e x, y = cell[@], cell[2] state[x][y] = player ‘tps othub. com Cledersonbetic-tacsoe-minimaxabsreade-ov-fle » 129125, 243 AM [Github - Cledersonbeltc-tac-oe-minimax: Minimax is @ Al algoritm, score = minimax(state, depth - 1, -player) state[x][y] = @ score[@], score[1] = x, y For each valid moves (empty cells): * x receives cell row index *y: receives cell column index state[x][y|: it's like board|available_row][available_col] receives MAX or MIN player ‘* score = minimax(state, depth - 1, -player) © state: is the current board in recursion; © depth -1 © -player if a player is MAX (+1) will be MIN (-1) and vice versa. \dex of the next state; The move (+1 or -1) on the board is undo and the row, column are collected. The next step is compare the score with best. if player == MAX: oe if score[2] > best[2]: best = score else: if score[2] < best[2]: best = score For MAX player, a bigger score will be received. For a MIN player, a lower score will be received. And in the end, the best move is returned, Final algorithm: def minimax(state, depth, player): oa if player == MAX: best = [-1, » infinity] else: best = [-1, » +infinity] if depth == @ or game_over(state): score = evaluate(state) return [-1, -1, score] for cell in empty_cells(state): x, y = cell[0], cell[2] state[x][y] = player score = minimax(state, depth - 1, -player) state[x][y] = @ score[@], score[1] = x, y ntps:igihub.com/Cledersonbctictac-tov-minimax?tab=readine-ov-le » 129125, 243 AM [Github - Cledersonbeltc-tac-oe-minimax: Minimax is @ Al algoritm, if player == MAX: Af score[2] > best[2]: best = score else: if score[2] < best[2]: best = score return best Game Tree Below, the best move is on the middle because the max value is on 2nd node on left. MAX OooxXx Maximizing 0 Best move: [1, 1] (center) x Depth = 3 O|x IT _—— 1 ——-, oO x oo oO x x x x @ °8 x Q x ox 0|0|x Depth = 2 eae aoe MAX o|o|x| fojo|x) ofo|x| jojo a x 0o@xxo xx @x ez 0|x| [ofo|x}|4 jojo v v v yall 0|o|x| | folo|x| lolo|x Depth = 0 @* 0@xxo xxo@ xX} Ofo|x) ofo|x Take a look that the depth is equal the valid moves on the board. The complete code is available in py_version/. Simplified game tree: ntps:igihub.com/Cledersonbctictac-tov-minimax?tab=readine-ov-le » 3729125, 243. AM [Github - Cledersonbelte-tac-oe-minimax: Minimax is @ Al algorttm, MAX MAX MAX | OE MIN MIN MIN el Oo 0 That tree has 11 nodes. The full game tree has 549.946 nodes! You can test it putting a static global variable in your program and incrementing it for each minimax function call per turn. Releases No releases published Packages No packages published Contributors 2 ‘ ® sahil-python Sahil ntps:igihub.com/Cledersonbctictac-toe-minimax?tab=readine-ov-le MAX » 118 3729125, 243. AM [Githus - Cledersonbeltc-tac-oe-minimax: Minimax is @ Al algorithm, Languages © Python 47.8% © JavaScript 326% @ CSS 111% @ HTMLSS% ntps:igihub.com/Cledersonbctictac-toe-minimax?tab=readine-ov-le

You might also like