Git Product home page Git Product logo

negamax's Introduction

Introduction to negamax

ver 0.0.3

nimble

Negamax is a nim library for executing the Negamax AI algorithm on a turn-based game. The library uses the turn_based_game nimble library as the framework for the game itself.

The negamax algorithm searches and weighs possible future moves. It is a varation of the minimax algorithm that is optimized for games where the "value" of a game's state for one player is directly inverse of the value to the oppossing player. This is known as a [zero-sum game](https://en.wikipedia.org/wiki/Zero-sum_game).

This algorithm is desgined to do alpha/beta pruning, which shortens the search tree.

This algorithem is currently recursive. The author is currently working on a non-recursive one as well.

Negamax has the following restrictions:

  1. It only works for two-player games.
  2. It does not work with games that involve any randomness during game play. (Initial randomness for "board setup" etc. before game play begins is just fine.)
  3. It requires that the value of the board be zero-sum in nature.

Algorithm details:

Usage

The bulk of the work is in making the game itself. See the turn_based_game library for details.

Once made, simply import the negamax library and use a NegamaxPlayer instead of a normal Player. Include the depth of the search as an object parameter. The depth is measured in plies. One ply is a single play. So, one full round of play between two players is two plies.

The Negamax AI specifically requires that the

  • scoring,
  • get_state, and
  • restore_state

methods be defined. Again, see the turn_based_game docs for details.

Simple Example

import turn_based_game
import negamax

import knights

#
#  Game of Knights
#
# Knights is played on a 5 row by 5 column chessboard with standard Knight pieces. Just like
# in chess, the Knight move by jumping in an L pattern: moving one space in any direction followed by
# moving two spaces at a right angle to the first move. When a knight makes a jump, the place that it
# formerly occupied is marked with an X and it can no longer be landed on by either player. As the
# game progresses, there are fewer and fewer places to land. There are no captures in this game.
#
# To start, each player has one Knight placed in an opposite corner. The players then take turns jumping.
# The last player to still have a place to move is the winner.
#

var game = Knights()

game.setup(@[
  Player(name: "Black Knight"),
  NegamaxPlayer(name: "White Knight", depth: 7)
])

var history: seq[string] = @[]

history = game.play()

echo "history: " & $history

For the content pulled by "import knights", see https://github.com/JohnAD/negamax/blob/master/examples/knights.nim

Videos

The following two videos (to be watched in sequence), demonstrate how to use this library and the turn_based_game library:

Credit

The code for this engine mimics that written in Python at the EasyAI library located at <https://github.com/Zulko/easyAI>. That library contains both the game rule engine (called TwoPlayerGame) as well as a variety of AI algorithms to play as game players, such as Negamax.

Table Of Contents

  1. Introduction to negamax
  2. Appendices

    1. negamax Reference

negamax's People

Contributors

johnad avatar mnlphlp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

terrisgo

negamax's Issues

Speed [Question]

Hi @JohnAD,

did you ever get around to finishing the non-recursive version? I'd love to try out the optimization

thinking and moves

Hi John, I see from your youtube video that you indicate on the terminal when negamax is "thinking". Not sure how you did this. Also, can negamax indicate on the terminal the move it decided on.

BTW I'm working on a chess variant: https://github.com/freevryheid/longbow that uses your nim modules. These motivated me to code the thing after thinking about it for months. I'm still trying to optimize the scoring :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.