# LUDOpy
This is an implementation of the LUDO game in python for use in AI or whatever you want.
For normal use of ludopy, only "ludopy.Game" should be needed.
[![PyPI version](https://badge.fury.io/py/ludopy.svg)](https://badge.fury.io/py/ludopy) [![GitHub license](https://img.shields.io/github/license/SimonLBSoerensen/LUDOpy.svg)](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/LICENSE)
# Documentation
<!--- [![Documentation Status](https://readthedocs.org/projects/ludopy/badge/?version=latest)](https://ludopy.readthedocs.io/en/latest/?badge=latest) --->
https://ludopy.readthedocs.io/en/latest/index.html
# Installation
### Recommended: Install ludopy from PyPI:
```sh
pip install ludopy
```
### Alternatively: install ludopy from the GitHub source:
First, clone ludopy using `git`:
```sh
git clone https://github.com/SimonLBSoerensen/LUDOpy
```
Then, `cd` to the folder and run the installation command:
```sh
cd LUDOpy
python setup.py install
```
# Examples
## Random "walk" example:
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SimonLBSoerensen/LUDOpy/blob/master/demo/random_walk.ipynb)
```python
import ludopy
import numpy as np
g = ludopy.Game()
there_is_a_winner = False
while not there_is_a_winner:
(dice, move_pieces, player_pieces, enemy_pieces, player_is_a_winner, there_is_a_winner), player_i = g.get_observation()
if len(move_pieces):
piece_to_move = move_pieces[np.random.randint(0, len(move_pieces))]
else:
piece_to_move = -1
_, _, _, _, _, there_is_a_winner = g.answer_observation(piece_to_move)
print("Saving history to numpy file")
g.save_hist(f"game_history.npy")
print("Saving game video")
g.save_hist_video(f"game_video.mp4")
```
If you only want to play with a certain amount of players:
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SimonLBSoerensen/LUDOpy/blob/master/demo/random_walk_two_players.ipynb)
```python
import ludopy
import numpy as np
g = ludopy.Game(ghost_players=[1, 3]) # This will prevent players 1 and 3 from moving out of the start and thereby they are not in the game
there_is_a_winner = False
while not there_is_a_winner:
(dice, move_pieces, player_pieces, enemy_pieces, player_is_a_winner, there_is_a_winner), player_i = g.get_observation()
if len(move_pieces):
piece_to_move = move_pieces[np.random.randint(0, len(move_pieces))]
else:
piece_to_move = -1
_, _, _, _, _, there_is_a_winner = g.answer_observation(piece_to_move)
print("Saving history to numpy file")
g.save_hist(f"game_history.npy")
print("Saving game video")
g.save_hist_video(f"game_video.mp4")
```
If you want to render the environment:
```python
import ludopy
import numpy as np
import cv2
g = ludopy.Game()
there_is_a_winner = False
while not there_is_a_winner:
(dice, move_pieces, player_pieces, enemy_pieces, player_is_a_winner, there_is_a_winner), player_i = g.get_observation()
enviroment_image_rgb = g.render_environment() # RGB image of the enviroment
enviroment_image_bgr = cv2.cvtColor(enviroment_image_rgb, cv2.COLOR_RGB2BGR)
cv2.imshow("Enviroment", enviroment_image_bgr)
cv2.waitKey(1)
if len(move_pieces):
piece_to_move = move_pieces[np.random.randint(0, len(move_pieces))]
else:
piece_to_move = -1
_, _, _, _, _, there_is_a_winner = g.answer_observation(piece_to_move)
cv2.destroyAllWindows()
print("Saving history to numpy file")
g.save_hist(f"game_history.npy")
print("Saving game video")
g.save_hist_video(f"game_video.mp4")
```
# The rules of the game:
### The dice
Six-sided dice are used. With it, you have to hit six to move a piece out of the starting area,
while all other throws give the right to move a piece the number of fields the eyes show.
Extra throws are given when hitting six (no limit to how many times in a row).
### At the start
Players take turns alternating with the dice.
At the stroke of 6, you can move a piece out on the board.
In the first round, you have three attempts to get 6.
The active player goes in a clockwise direction (player 1 to 4).
### The course of the game
A 6's entitles you to an extra roll.
The player moves the number of squares corresponding to the eyes of the dice.
If you land in a field where there is already a piece, the first arrived piece must return to
starting field. If, on the other hand, there are two pieces on the field, then the last to arrive has to return to the starting field.
You have to move a piece (if there is one that can be moved), even if it means you have to back to start.
If you cannot move forward, you must stand for the next turn.
### Fields
On the playing board, there are several special areas and fields.
- The starting area is where the four pieces start. You have to hit a six to take a piece out of the starting area. If you have all your pieces in the starting area, you get up to three strokes with the dice before the tour is passed.
- The target area is where the four pieces should end and can get to the goal. Each player has their own target area, and no other player is allowed to take their pieces in there. To reach the end of the target area (the goal), it must hit precisely - otherwise, you have to move your piece in the opposite direction by the amount you have left over.
- Glob fields protect the pieces from being knocked home. If an opponent's piece lands on a protected piece, it is hit home. However, there is an exception to the coloured globe fields. For example, only red chips can be protected in the red globe field, regardless of the number of chips in the field. If you have two pieces standing on the opponent's coloured globe, they can both be knocked home.
- Starfields act as shortcuts that bring the pieces to the target area faster. If a piece lands on a star, it must be moved to the next star. If it is the star in front of the target area that lands on, the chip is moved directly to the goal.
### The winner
The one who first gets all four pieces in the goal is the winner. (But you choose if the game ends there or if the other players still have to fight)
The goal is the centre. When you have entered the coloured fields, you can not be hit home.
The piece has to be moved precisely into the goal. Otherwise, the piece is struck back the exact number of eyes there are too many.
The rules are taken from this danish site: http://spilregler.dk/ludo/
## The Board
The number inside a piece indicates how many pieces that are at the same tail
### Example 1
![The image show a example of the board](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/board_example.png?raw=true "Board example")
### Example 2
![The image show a example of the board](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/board_example_2.png?raw=true "Board example 2")
### Example of the index method (shown for player 1)
Here the number indicate which index the piece are at
![Example of the index method (shown for player 1)](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/track.png?raw=true "Index method")
Change log:
- 1.5.0
- Better fix for game history
```python
# Convert from new history to old history
old_hist = [[new_hist["pieces"][i], new_hist["current_dice"][i],
new_hist["current_player"][i], new_hist["round"][i]] for i in range(len(new_hist[list(new_hist.keys())[0]]))]
# Convert from old history to new history
new_hist = {"pieces": [], "current_dice": [], "current_player": [], "round": []}
for pieces, current_dice, current_player, round in old_hist:
new_hist["pieces"].append(pieces)
new_hist["current_dice"].append(current_dice)
new_hist["current_player"].append(current_player)
new_hist["round"].append(round)
```
- 1.4.2
- Fix error with numpy when saving the game history (a better fix is needed)
- 1.4.1
- Change the path into the target area (now pieces goes directly from the last star into the target area while before they had to go to the start globe and then to the target area)
- Minor code changes
Raw data
{
"_id": null,
"home_page": "https://github.com/SimonLBSoerensen/LUDOpy",
"name": "ludopy",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "LUDO,game,AI",
"author": "Simon L. B. S\u00f8rensen",
"author_email": "simonlyckbjaert@hotmail.com",
"download_url": "https://files.pythonhosted.org/packages/af/a8/9fb9072973ba7ea78c94e4dbe82d75852288a4c35b2b93d3c4e6ffe3649c/ludopy-1.5.0.tar.gz",
"platform": null,
"description": "# LUDOpy\n\nThis is an implementation of the LUDO game in python for use in AI or whatever you want. \n \nFor normal use of ludopy, only \"ludopy.Game\" should be needed.\n\n[![PyPI version](https://badge.fury.io/py/ludopy.svg)](https://badge.fury.io/py/ludopy) [![GitHub license](https://img.shields.io/github/license/SimonLBSoerensen/LUDOpy.svg)](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/LICENSE) \n\n\n\n# Documentation\n\n<!--- [![Documentation Status](https://readthedocs.org/projects/ludopy/badge/?version=latest)](https://ludopy.readthedocs.io/en/latest/?badge=latest) --->\nhttps://ludopy.readthedocs.io/en/latest/index.html\n\n# Installation\n### Recommended: Install ludopy from PyPI: \n```sh\npip install ludopy\n```\n### Alternatively: install ludopy from the GitHub source:\n\nFirst, clone ludopy using `git`:\n\n```sh\ngit clone https://github.com/SimonLBSoerensen/LUDOpy\n```\n\nThen, `cd` to the folder and run the installation command:\n```sh\ncd LUDOpy\npython setup.py install\n```\n\n# Examples\n## Random \"walk\" example:\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SimonLBSoerensen/LUDOpy/blob/master/demo/random_walk.ipynb)\n```python\nimport ludopy\nimport numpy as np\n\ng = ludopy.Game()\nthere_is_a_winner = False\n\nwhile not there_is_a_winner:\n (dice, move_pieces, player_pieces, enemy_pieces, player_is_a_winner, there_is_a_winner), player_i = g.get_observation()\n\n if len(move_pieces):\n piece_to_move = move_pieces[np.random.randint(0, len(move_pieces))]\n else:\n piece_to_move = -1\n\n _, _, _, _, _, there_is_a_winner = g.answer_observation(piece_to_move)\n\nprint(\"Saving history to numpy file\")\ng.save_hist(f\"game_history.npy\")\nprint(\"Saving game video\")\ng.save_hist_video(f\"game_video.mp4\")\n```\nIf you only want to play with a certain amount of players:\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SimonLBSoerensen/LUDOpy/blob/master/demo/random_walk_two_players.ipynb)\n```python\nimport ludopy\nimport numpy as np\n\ng = ludopy.Game(ghost_players=[1, 3]) # This will prevent players 1 and 3 from moving out of the start and thereby they are not in the game\nthere_is_a_winner = False\n\nwhile not there_is_a_winner:\n (dice, move_pieces, player_pieces, enemy_pieces, player_is_a_winner, there_is_a_winner), player_i = g.get_observation()\n\n if len(move_pieces):\n piece_to_move = move_pieces[np.random.randint(0, len(move_pieces))]\n else:\n piece_to_move = -1\n\n _, _, _, _, _, there_is_a_winner = g.answer_observation(piece_to_move)\n\nprint(\"Saving history to numpy file\")\ng.save_hist(f\"game_history.npy\")\nprint(\"Saving game video\")\ng.save_hist_video(f\"game_video.mp4\")\n```\nIf you want to render the environment:\n```python\nimport ludopy\nimport numpy as np\nimport cv2\n\ng = ludopy.Game()\nthere_is_a_winner = False\n\nwhile not there_is_a_winner:\n (dice, move_pieces, player_pieces, enemy_pieces, player_is_a_winner, there_is_a_winner), player_i = g.get_observation()\n\n enviroment_image_rgb = g.render_environment() # RGB image of the enviroment\n enviroment_image_bgr = cv2.cvtColor(enviroment_image_rgb, cv2.COLOR_RGB2BGR)\n cv2.imshow(\"Enviroment\", enviroment_image_bgr)\n cv2.waitKey(1)\n\n if len(move_pieces):\n piece_to_move = move_pieces[np.random.randint(0, len(move_pieces))]\n else:\n piece_to_move = -1\n\n _, _, _, _, _, there_is_a_winner = g.answer_observation(piece_to_move)\n\ncv2.destroyAllWindows()\n\nprint(\"Saving history to numpy file\")\ng.save_hist(f\"game_history.npy\")\nprint(\"Saving game video\")\ng.save_hist_video(f\"game_video.mp4\")\n```\n\n# The rules of the game:\n\n### The dice\nSix-sided dice are used. With it, you have to hit six to move a piece out of the starting area, \nwhile all other throws give the right to move a piece the number of fields the eyes show.\nExtra throws are given when hitting six (no limit to how many times in a row). \n\n### At the start\nPlayers take turns alternating with the dice.\nAt the stroke of 6, you can move a piece out on the board.\nIn the first round, you have three attempts to get 6.\nThe active player goes in a clockwise direction (player 1 to 4).\n\n### The course of the game\nA 6's entitles you to an extra roll.\nThe player moves the number of squares corresponding to the eyes of the dice. \nIf you land in a field where there is already a piece, the first arrived piece must return to\nstarting field. If, on the other hand, there are two pieces on the field, then the last to arrive has to return to the starting field.\nYou have to move a piece (if there is one that can be moved), even if it means you have to back to start.\nIf you cannot move forward, you must stand for the next turn.\n\n### Fields\nOn the playing board, there are several special areas and fields.\n\n- The starting area is where the four pieces start. You have to hit a six to take a piece out of the starting area. If you have all your pieces in the starting area, you get up to three strokes with the dice before the tour is passed.\n- The target area is where the four pieces should end and can get to the goal. Each player has their own target area, and no other player is allowed to take their pieces in there. To reach the end of the target area (the goal), it must hit precisely - otherwise, you have to move your piece in the opposite direction by the amount you have left over.\n- Glob fields protect the pieces from being knocked home. If an opponent's piece lands on a protected piece, it is hit home. However, there is an exception to the coloured globe fields. For example, only red chips can be protected in the red globe field, regardless of the number of chips in the field. If you have two pieces standing on the opponent's coloured globe, they can both be knocked home.\n- Starfields act as shortcuts that bring the pieces to the target area faster. If a piece lands on a star, it must be moved to the next star. If it is the star in front of the target area that lands on, the chip is moved directly to the goal.\n\n### The winner\nThe one who first gets all four pieces in the goal is the winner. (But you choose if the game ends there or if the other players still have to fight)\nThe goal is the centre. When you have entered the coloured fields, you can not be hit home.\nThe piece has to be moved precisely into the goal. Otherwise, the piece is struck back the exact number of eyes there are too many.\n\nThe rules are taken from this danish site: http://spilregler.dk/ludo/\n\n## The Board\n\nThe number inside a piece indicates how many pieces that are at the same tail\n\n### Example 1\n\n![The image show a example of the board](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/board_example.png?raw=true \"Board example\")\n\n### Example 2\n\n![The image show a example of the board](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/board_example_2.png?raw=true \"Board example 2\")\n\n### Example of the index method (shown for player 1)\n\nHere the number indicate which index the piece are at\n\n![Example of the index method (shown for player 1)](https://github.com/SimonLBSoerensen/LUDOpy/blob/master/track.png?raw=true \"Index method\")\n\n\nChange log:\n- 1.5.0\n - Better fix for game history\n```python \n# Convert from new history to old history\nold_hist = [[new_hist[\"pieces\"][i], new_hist[\"current_dice\"][i],\n new_hist[\"current_player\"][i], new_hist[\"round\"][i]] for i in range(len(new_hist[list(new_hist.keys())[0]]))]\n\n# Convert from old history to new history\nnew_hist = {\"pieces\": [], \"current_dice\": [], \"current_player\": [], \"round\": []}\nfor pieces, current_dice, current_player, round in old_hist:\n new_hist[\"pieces\"].append(pieces)\n new_hist[\"current_dice\"].append(current_dice)\n new_hist[\"current_player\"].append(current_player)\n new_hist[\"round\"].append(round)\n```\n- 1.4.2\n - Fix error with numpy when saving the game history (a better fix is needed)\n- 1.4.1\n - Change the path into the target area (now pieces goes directly from the last star into the target area while before they had to go to the start globe and then to the target area)\n - Minor code changes \n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A implementation of the LUDO game in python for use in AI or whatever you want",
"version": "1.5.0",
"split_keywords": [
"ludo",
"game",
"ai"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cb618de20b7c2efca15fc1e3a0113b9a567ae0632ea37b4e7bb145fd64354b99",
"md5": "37609830f4141e9c81b098a170c4d000",
"sha256": "110e8964c4913f076a0d5981b954e5e08255dbfa009d9e6014e954c68ea71dea"
},
"downloads": -1,
"filename": "ludopy-1.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "37609830f4141e9c81b098a170c4d000",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 88865,
"upload_time": "2023-03-25T16:41:32",
"upload_time_iso_8601": "2023-03-25T16:41:32.968739Z",
"url": "https://files.pythonhosted.org/packages/cb/61/8de20b7c2efca15fc1e3a0113b9a567ae0632ea37b4e7bb145fd64354b99/ludopy-1.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "afa89fb9072973ba7ea78c94e4dbe82d75852288a4c35b2b93d3c4e6ffe3649c",
"md5": "5573998f6a295168e46a9b9ad867d8a2",
"sha256": "4110f3b6f9ce07496b53a97dbbc8325a73a8790234062731762d7c6d8bbf742f"
},
"downloads": -1,
"filename": "ludopy-1.5.0.tar.gz",
"has_sig": false,
"md5_digest": "5573998f6a295168e46a9b9ad867d8a2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 93611,
"upload_time": "2023-03-25T16:41:34",
"upload_time_iso_8601": "2023-03-25T16:41:34.744087Z",
"url": "https://files.pythonhosted.org/packages/af/a8/9fb9072973ba7ea78c94e4dbe82d75852288a4c35b2b93d3c4e6ffe3649c/ludopy-1.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-25 16:41:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "SimonLBSoerensen",
"github_project": "LUDOpy",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "numpy",
"specs": []
},
{
"name": "opencv-python",
"specs": [
[
">=",
"3.1"
]
]
}
],
"lcname": "ludopy"
}