All Projects → Cledersonbc → Tic Tac Toe Minimax

Cledersonbc / Tic Tac Toe Minimax

Licence: gpl-3.0
Minimax is a AI algorithm.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Tic Tac Toe Minimax

Pygame Learning Environment
PyGame Learning Environment (PLE) -- Reinforcement Learning Environment in Python.
Stars: ✭ 828 (+311.94%)
Mutual labels:  artificial-intelligence, game
Game Datasets
🎮 A curated list of awesome game datasets, and tools to artificial intelligence in games
Stars: ✭ 261 (+29.85%)
Mutual labels:  artificial-intelligence, game
Commandcenter
Starcraft AI Bot
Stars: ✭ 456 (+126.87%)
Mutual labels:  artificial-intelligence, game
Tic Tac Toe
An unbeatable game of Tic Tac Toe.
Stars: ✭ 57 (-71.64%)
Mutual labels:  artificial-intelligence, game
Warriorjs
🏰 An exciting game of programming and Artificial Intelligence
Stars: ✭ 8,673 (+4214.93%)
Mutual labels:  artificial-intelligence, game
Owl Bt
owl-bt is editor for Behavior trees. It has been inspired by Unreal engine behavior trees in a way, that it supports special node items like decorators and services. This makes trees smaller and much more readable.
Stars: ✭ 112 (-44.28%)
Mutual labels:  artificial-intelligence, game
Snake
Artificial intelligence for the Snake game.
Stars: ✭ 1,241 (+517.41%)
Mutual labels:  artificial-intelligence, game
Infinity Square Space
Infinity Square/Space. The prototype of the game is open source. Unity Asset.
Stars: ✭ 122 (-39.3%)
Mutual labels:  artificial-intelligence, game
Acurustrack
A multi-object tracking component. Works in the conditions where identification and classical object trackers don't (e.g. shaky/unstable camera footage, occlusions, motion blur, covered faces, etc.). Works on any object despite their nature.
Stars: ✭ 196 (-2.49%)
Mutual labels:  artificial-intelligence
Bounceback
Boomerang Zelda Homage for JS13k
Stars: ✭ 200 (-0.5%)
Mutual labels:  game
Deep Learning With Python
Deep learning codes and projects using Python
Stars: ✭ 195 (-2.99%)
Mutual labels:  artificial-intelligence
Droneworld
droneWorld: a 3D world map and a three.js playground
Stars: ✭ 197 (-1.99%)
Mutual labels:  game
Lale
Library for Semi-Automated Data Science
Stars: ✭ 198 (-1.49%)
Mutual labels:  artificial-intelligence
Clashjs
Javascript AI battle game. Create your own battleship.
Stars: ✭ 196 (-2.49%)
Mutual labels:  game
Pokemon data
全ポケモンのJSONデータです。
Stars: ✭ 201 (+0%)
Mutual labels:  game
Vectorai
Vector AI — A platform for building vector based applications. Encode, query and analyse data using vectors.
Stars: ✭ 195 (-2.99%)
Mutual labels:  artificial-intelligence
Dm control
DeepMind's software stack for physics-based simulation and Reinforcement Learning environments, using MuJoCo.
Stars: ✭ 2,592 (+1189.55%)
Mutual labels:  artificial-intelligence
Supermariowar
A fan-made multiplayer Super Mario Bros. style deathmatch game
Stars: ✭ 200 (-0.5%)
Mutual labels:  game
Aimandshoot
A neuroevolution game experiment.
Stars: ✭ 201 (+0%)
Mutual labels:  game
Chunkstories
Somewhat fancy blocky game engine written in Kotlin
Stars: ✭ 199 (-1%)
Mutual labels:  game

tic-tac-toe-minimax

An implementation of Minimax AI Algorithm on Tic-Tac-Toe (or Noughts and Crosses) game. Try it: Tic-tac-toe - Minimax

Introduction

To solve games using AI, 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.

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 (0).

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) until it 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)

	if (player = max) then
		best = [null, -infinity]
	else
		best = [null, +infinity]

	if (depth = 0 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 m on s
		[move, score] = minimax(s, depth - 1, -player)
		undo move m on s

		if (player = max) then
			if score > best.score then best = [move, score]
		else
			if score < best.score then best = [move, score]

	return best
end

Now we'll 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):
  • 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:
	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 == 0 or game_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 empty_cells(state):
	x, y = cell[0], cell[1]
	state[x][y] = player
	score = minimax(state, depth - 1, -player)
	state[x][y] = 0
	score[0], 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: index of the next state;
    • -player: if a player is MAX (+1) will be MIN (-1) and vice versa.

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:
	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):
	if player == MAX:
		best = [-1, -1, -infinity]
	else:
		best = [-1, -1, +infinity]

	if depth == 0 or game_over(state):
		score = evaluate(state)
		return [-1, -1, score]

	for cell in empty_cells(state):
		x, y = cell[0], cell[1]
		state[x][y] = player
		score = minimax(state, depth - 1, -player)
		state[x][y] = 0
		score[0], score[1] = x, y

		if player == MAX:
			if 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.

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:

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.

In a more complex game, such as chess, it's hard to search whole game tree. However, Alpha–beta Pruning is an optimization method to the minimax algorithm that allows us to disregard some branches in the search tree, because he cuts irrelevant nodes (subtrees) in search. For more information, see:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].