项目作者: luizfranca

项目描述 :
A competitive search with pruning and heuristics.
高级语言: Python
项目地址: git://github.com/luizfranca/dots-and-boxes.git
创建时间: 2017-02-17T11:41:19Z
项目社区:https://github.com/luizfranca/dots-and-boxes

开源协议:MIT License

下载


Dots-and-Boxes-AI

This is a artificial inteligence using competitive search and pruning for the game Dots and Boxes.

The competitive search chosen was alphabeta pruning with depth limit.

Usage

DotsAndBoxes.py

DotsAndBoxes.py is the representation of the board. It comes with a method that converts the board to string. This will be userful for testing

  1. import DotsAndBoxes as dab
  2. game = dab.DotsAndBoxes(3, 3) # creating a board 3 by 3.
  3. game.to_string()
  4. >>> '._._.|_*_*_|._._.|_*_*_|._._.'

There is also a method that receives the string of the board as input to fill the board.

  1. import DotsAndBoxes as dab
  2. game = dab.DotsAndBoxes(3, 3)
  3. game.input_board('._._.|_*_*_|._._.|_*_*_|._._.')
  4. game.to_string()
  5. >>> '._._.|_*_*_|._._.|_*_*_|._._.'

You can make a move as any player and print the board to try it visually on the UI.

  1. import DotsAndBoxes as dab
  2. game = dab.DotsAndBoxes(3, 3)
  3. game.to_string()
  4. >>> '._._.|_*_*_|._._.|_*_*_|._._.'
  5. game.move(0, 1, True)
  6. game.to_string()
  7. >>> '.X._.|_*_*_|._._.|_*_*_|._._.'

Additionally you can check if the game has finished using the is_finished method.

  1. import DotsAndBoxes as dab
  2. game = dab.DotsAndBoxes(3, 3)
  3. print game.is_finished()
  4. >>> False
  5. game.input_board('.x.x.|xWxWx|.x.x.|xWxWx|.x.x.')
  6. print game.is_finished()
  7. >>> True

DotsAndBoxesAI.py

DotsAndBoxesAI.py is the AI. The depth is the limit of how far it will search on the tree. Use player = True to play as white, or player = False to play as Black.

  1. import DotsAndBoxes as dab
  2. import DotsAndBoxes as ai
  3. game = dab.DotsAndBoxes(3, 3)
  4. print ai.alphabeta(game, depth = 8, player = True)
  5. >>> 0 1

DotsAndBoxesUI.py

DotsAndBoxesUI.py is a simple UI made to test board. It recevies the parameter of the board as a string.

```{r, engine=’bash’, codeblock_name}
$ python DotsAndBoxesUI.py “.
.x.|xWx|.x.x.|xBx|.x._.”

  1. <img src='https://cloud.githubusercontent.com/assets/4142065/23759059/6670bf20-04ca-11e7-9af1-cff390063568.png' alt='Dots and Boxes board' width='300'>
  2. Or it can receive the height and the width of the board.
  3. ```{r, engine='bash', code_block_name}
  4. $ python DotsAndBoxesUI.py 3 4

Dots and Boxes board

main.py

The main.py was made to test the AI. It receives as parameter the player and the board as a string.

```{r, engine=’bash’, codeblock_name}
$ python main.py W “.
.x.|x|.x..|xBx*|.x._.”
1 4

  1. If no parameter is passed, the default will be used (a board 4x4 with the player white)
  2. ```{r, engine='bash', code_block_name}
  3. $ python main.py
  4. 0 1

License

Dots and Boxes AI is released under the MIT License.