Skip to main content

Easy-to-use game AI algorithms (Negamax etc. )

Project description

EasyAI (full documentation here) is a pure-Python artificial intelligence framework for two-players abstract games such as Tic Tac Toe, Connect 4, Reversi, etc. It makes it easy to define the mechanisms of a game, and play against the computer or solve the game. Under the hood, the AI is a Negamax algorithm with alpha-beta pruning and transposition tables as described on Wikipedia.

Installation

If you have pip installed, type this in a terminal

sudo pip install easyAI

Otherwise, download the source code (for instance on Github), unzip everything into one folder and in this folder, in a terminal, type

sudo python setup.py install

Additionally you will need to install Numpy to be able to run some of the examples.

A quick example

Let us define the rules of a game and start a match against the AI:

from easyAI import TwoPlayerGame, Human_Player, AI_Player, Negamax

class GameOfBones( TwoPlayerGame ):
    """ In turn, the players remove one, two or three bones from a
    pile of bones. The player who removes the last bone loses. """

    def __init__(self, players=None):
        self.players = players
        self.pile = 20 # start with 20 bones in the pile
        self.current_player = 1 # player 1 starts

    def possible_moves(self): return ['1','2','3']
    def make_move(self,move): self.pile -= int(move) # remove bones.
    def win(self): return self.pile<=0 # opponent took the last bone ?
    def is_over(self): return self.win() # Game stops when someone wins.
    def show(self): print ("%d bones left in the pile" % self.pile)
    def scoring(self): return 100 if game.win() else 0 # For the AI

# Start a match (and store the history of moves when it ends)
ai = Negamax(13) # The AI will think 13 moves in advance
game = GameOfBones( [ Human_Player(), AI_Player(ai) ] )
history = game.play()

Result:

20 bones left in the pile

Player 1 what do you play ? 3

Move #1: player 1 plays 3 :
17 bones left in the pile

Move #2: player 2 plays 1 :
16 bones left in the pile

Player 1 what do you play ?

Solving the game

Let us now solve the game:

from easyAI import solve_with_iterative_deepening
r,d,m = solve_with_iterative_deepening(
    game=GameOfBones(),
    ai_depths=range(2,20),
    win_score=100
)

We obtain r=1, meaning that if both players play perfectly, the first player to play can always win (-1 would have meant always lose), d=10, which means that the wins will be in ten moves (i.e. 5 moves per player) or less, and m='3', which indicates that the first player’s first move should be '3'.

These computations can be speed up using a transposition table which will store the situations encountered and the best moves for each:

tt = TranspositionTable()
GameOfBones.ttentry = lambda game : game.pile # key for the table
r,d,m = solve_with_iterative_deepening(
    game=GameOfBones(),
    ai_depths=range(2,20),
    win_score=100,
    tt=tt
)

After these lines are run the variable tt contains a transposition table storing the possible situations (here, the possible sizes of the pile) and the optimal moves to perform. With tt you can play perfectly without thinking:

game = GameOfBones( [  AI_Player( tt ), Human_Player() ] )
game.play() # you will always lose this game :)

Contribute !

EasyAI is an open source software originally written by Zulko and released under the MIT licence. Contributions welcome! Some ideas: AI algos for incomplete information games, better game solving strategies, (efficient) use of databases to store moves, AI algorithms using parallelisation.

For troubleshooting and bug reports, the best for now is to ask on Github.

Maintainers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easyAI-2.0.11.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

easyAI-2.0.11-py3-none-any.whl (42.2 kB view details)

Uploaded Python 3

File details

Details for the file easyAI-2.0.11.tar.gz.

File metadata

  • Download URL: easyAI-2.0.11.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for easyAI-2.0.11.tar.gz
Algorithm Hash digest
SHA256 842b4c64d9ff08fc9619211ebd4deca6f30c40f635b80d97115a7a398f9e4293
MD5 f4b1c9238a8660d38816795f9cf3b1f1
BLAKE2b-256 da9edef77e0df2bb317760c1d447517390c051d0b5b219a89f74e6fad1b41edc

See more details on using hashes here.

File details

Details for the file easyAI-2.0.11-py3-none-any.whl.

File metadata

  • Download URL: easyAI-2.0.11-py3-none-any.whl
  • Upload date:
  • Size: 42.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for easyAI-2.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 583a7eca67d7a2012cc78b9087aa64a46869061cc3b641cdca14aaa18d5133da
MD5 2cc4662a75a3a68828231aaf9c19671a
BLAKE2b-256 f7d9f9b4d11b778ce7dec23c6cf658bf5a49825a10088aa95723dfbcbe0fab99

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page