Package: ludopy
LUDOpyThis 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.Documentationhttps://ludopy.readthedocs.io/en/latest/index.htmlInstallationRecommended: Install ludopy from PyPI:pipinstallludopyAlternatively: install ludopy from the GitHub source:First, clone ludopy usinggit:gitclonehttps://github.com/SimonLBSoerensen/LUDOpyThen,cdto the folder and run the installation command:cdLUDOpy
pythonsetup.pyinstallExamplesRandom "walk" example:importludopyimportnumpyasnpg=ludopy.Game()there_is_a_winner=Falsewhilenotthere_is_a_winner:(dice,move_pieces,player_pieces,enemy_pieces,player_is_a_winner,there_is_a_winner),player_i=g.get_observation()iflen(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:importludopyimportnumpyasnpg=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 gamethere_is_a_winner=Falsewhilenotthere_is_a_winner:(dice,move_pieces,player_pieces,enemy_pieces,player_is_a_winner,there_is_a_winner),player_i=g.get_observation()iflen(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:importludopyimportnumpyasnpimportcv2g=ludopy.Game()there_is_a_winner=Falsewhilenotthere_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 enviromentenviroment_image_bgr=cv2.cvtColor(enviroment_image_rgb,cv2.COLOR_RGB2BGR)cv2.imshow("Enviroment",enviroment_image_bgr)cv2.waitKey(1)iflen(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 diceSix-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 startPlayers 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 gameA 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.FieldsOn 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 winnerThe 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 BoardThe number inside a piece indicates how many pieces that are at the same tailExample 1Example 2Example of the index method (shown for player 1)Here the number indicate which index the piece are atChange log:1.5.0Better fix for game history# Convert from new history to old historyold_hist=[[new_hist["pieces"][i],new_hist["current_dice"][i],new_hist["current_player"][i],new_hist["round"][i]]foriinrange(len(new_hist[list(new_hist.keys())[0]]))]# Convert from old history to new historynew_hist={"pieces":[],"current_dice":[],"current_player":[],"round":[]}forpieces,current_dice,current_player,roundinold_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.2Fix error with numpy when saving the game history (a better fix is needed)1.4.1Change 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