项目作者: lantunes

项目描述 :
A Dots and Boxes game engine for Python
高级语言: Python
项目地址: git://github.com/lantunes/dnbpy.git
创建时间: 2018-04-23T22:59:09Z
项目社区:https://github.com/lantunes/dnbpy

开源协议:Apache License 2.0

下载


Dots ‘n Boxes Game Engine for Python

DnBPy is a light-weight Dots ‘n Boxes game engine for Python. It is
particularly useful for AI projects, and can be used as an environment
for Reinforcement Learning projects.

Installation

  1. pip install dnbpy

Usage example

  1. import dnbpy
  2. # (2, 2) means: create a 2x2 board
  3. # player identifiers are not limited to strings
  4. game = dnbpy.Game((2, 2), ['player1', 'player2'])
  5. game.select_edge(0, 'player1')
  6. game.select_edge(2, 'player2')
  7. game.select_edge(3, 'player1')
  8. game.select_edge(5, 'player2')
  9. print(game.get_score('player1'))
  10. # 0
  11. print(game.get_score('player2'))
  12. # 1
  13. print(game.is_finished())
  14. # False

The Board State

The board state is represented as a bit vector. The bit vector’s length
is equal to the number of edges. The index of a bit in the vector
corresponds to an edge on the board. If the bit has value 1, the edge
is selected, and if the bit has value 0, the edge is unselected.

The board edges are indexed as follows (for the 2x2 case):

  1. * 0 * 1 *
  2. 2 3 4
  3. * 5 * 6 *
  4. 7 8 9
  5. * 10 * 11 *

In the example above, if edges 1 and 5 are selected, the board state
will be 01000100000. For other board dimensions, the same
convention is followed for edge indexing. The horizontal edges at the
very top of the board are indexed first, left-to-right, followed by the
vertical edges of the first row, etc.

A board is described using the convention n x m, where n is the
number of rows, and m is the number of columns. The 2x3 case is indexed
as follows:

  1. * 0 * 1 * 2 *
  2. 3 4 5 6
  3. * 7 * 8 * 9 *
  4. 10 11 12 13
  5. * 14 * 15 * 16 *

Boxes

A board consists of a number of boxes. The boxes on a board can be
accessed:

  1. game = dnbpy.Game((2, 2), ['player1', 'player2'])
  2. boxes = game.get_all_boxes()
  3. print(len(boxes))
  4. # 4
  5. print(str(boxes[0]))
  6. # 0-2-3-5
  7. print(str(boxes[1]))
  8. # 1-3-4-6
  9. print(str(boxes[2]))
  10. # 5-7-8-10
  11. print(str(boxes[3]))
  12. # 6-8-9-11

During a game, the game engine keeps track of the boxes that players
capture:

  1. game = dnbpy.Game((2, 2), ['player1', 'player2'])
  2. game.select_edge(0, 'player1')
  3. game.select_edge(2, 'player2')
  4. game.select_edge(3, 'player1')
  5. game.select_edge(5, 'player2')
  6. boxes = game.get_boxes('player1')
  7. print(len(boxes))
  8. # 0
  9. boxes = game.get_boxes('player2')
  10. print(len(boxes))
  11. # 1
  12. print(str(boxes[0]))
  13. # 0-2-3-5

Built-in Players

DnBPy includes several built-in players, ranging from simple to more complex levels of sophistication and gameplay.
These players can be used as follows:

  1. move = built_in_player.select_edge(game.get_board_state())

Assuming we already have a game of type dnbpy.Game initialized, and a built_in_player of type dnbpy.Policy
initialized, the select_edge function can be called to get the player’s next move. The following built-in players are
available:

Randomly-acting Player

  • selects an edge randomly
  • initialization: built_in_player = dnbpy.RandomPolicy()

Level 1 Heuristic Player

  • selects an edge randomly that completes a box, if possible; otherwise, it selects an edge at random
  • initialization: built_in_player = dnbpy.Level1HeuristicPolicy((rows, cols))

Level 2 Heuristic Player

  • selects an edge randomly that completes a box, if possible; otherwise, it selects an edge randomly that does not allow
    the opponent to complete a box; otherwise, it selects an edge at random
  • initialization: built_in_player = dnbpy.Level2HeuristicPolicy((rows, cols))

Level 3 Minimax Player

  • uses depth-limited minimax search with alpha-beta pruning to estimate the value of a board position
  • initialization: built_in_player = dnbpy.Level3MinimaxPolicy((rows, cols), 3)

Command-line demo

DnBPy includes a simple command-line demo program. Start the program with:

  1. python play.py

To play against the randomly-acting player, or the level 1, 2, or 3 heuristic player, enter one of $random, $L1,
$L2, or $L3 as a player name, respectively.