Package: zarena
Zarena🦀 Rust Game Collection with Reninforcement Learning gym environments.
This library aims to serve the same purpose as OpenSpiel, except in Rust to make it easier to use & maintain. The current games are gato, blackjack, chess & poker texas hold'em. All of these additionally support Web Assembly. You can play gato & chess against our Artificial Intelligence atZeti GamesConfigurationsDepending on the cargo file you want. You must change your cargo.toml to match that build.Cargo.py.toml-> Python BuildCargo.rs.toml-> Development BuildCargo.wa.toml-> Web Assembly BuildCargo.toml-> The actual file that Rust will build on. Copy from py/rs/wa to this file.CommandsIf you don't have Rust, no worries. Download Rust for Linux or Windows Subsystem.If you need more help.curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shDownload the C compilersudo apt-get update && sudo apt-get install build-essentialInstall poetrycurl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -Install Maturin via Poetrypoetry installBuild the Maturin Develop Buildpoetry run maturin developBuild the Maturin Test Buildpoetry run maturin buildBuild the Maturin Production Build. The Python Wheel & source distribution.poetry run maturin build --releaseBuild the Web Assembly filewasm-pack build --target web -- --features wasmUsageYou can import the Python classes directly, or create pre-defined environments withgymin this case it is also necessary to import the class:# import gym to use training environmentsimportgym# from zarena import the training environment of your choice, for:# option 1.- use the python class directly# option 2.- register the environment in gym and use it with gym.make(environment_name)fromzarenaimportgym_chessenv=gym_chess.ChessEnv()# Option 1env=gym.make('ChessEnv-v3')# Option 2# reset the environment and get the initial state observationobservation=env.reset()# obtain legal actionsactions=env.legal_actions()# select action according to a criterion, in this case randomaction=random.choice(actions)# pass it to the env and get the next state observation, reward, if the game is over and environment informationobservation,reward,done,info=env.step(action)# get the player to playenv.to_play()# properly close the gameenv.close()# display the game ovservationenv.render()Environments idTictactoe:GatoEnv-v2Chess:ChessEnv-v3Blackjack:BlackjackEnv-v1Poker:PokerEnv-v1CheckersCheckersEnv-v1TestingRun all the tests withpytest.Code linting and fixingPython code is formatted withblack.Rust code is formatted withcargo fmt.Building the Rust codeThe environment uses a chess engine implemented in Rust that usesPyO3Maturin to bind to the Python interpreter. Rust is an amazing compiled language and this project holds 2 configurations:Cargo.py.tomlis used to build the library into a Python module with maturinCargo.rs.tomlis used to build directly withcargoin Rust to access the library in themain.rsscript for developmentCargo.wa.tomlis used to build to build for Javascript with Web Assembly. The games can be played via Web Assembly on Zeti's websitehttps://zeti.aiNote: we haven't found a way to specify the Cargo toml file to either process, so copy the contents of the config you want to use intoCargo.tomlto make it work.Game of GatoThe game of Xs & OsAPIInitialize environment>>>env=BlackjackEnv(n_players=1)n_players: specify the number of players2<=n_players<=7(default:1)Set actions>>>env.step(action)action: mark a position, could be0<=action<=8>0|1|2>3|4|5>6|7|8BlackjackAPIInitialize environment>>>env=BlackjackEnv(n_players=1)n_players: specify the number of players2<=n_players<=7(default:1)Set actions>>>env.step(action)action: can be0-> stand1-> HIT2-> double down3-> pull apart (currently disabled)ChessSee the chess board and movesVisualise the current state of the chess game:env.render()-------------------------8|♖♘♗♕♔♗♘♖|7|♙♙♙♙♙♙♙♙|6|........|5|........|4|........|3|........|2|♟♟♟♟♟♟♟♟|1|♜♞♝♛♚♝♞♜|-------------------------abcdefghYou can also visualise multiple moves:>>>moves=env.possible_moves>>>env.render_moves(moves[10:12]+moves[16:18])APIInitialize environment>>>env=ChessEnv(player_color="WHITE",opponent="random",log=True,initial_state=DEFAULT_BOARD)opponent: can be"random","none"or a function. Tells the environment whether to use a bot that picks a random move, play against self or use a specific bot policy (default:"random")log:TrueorFalse, specifies whether to log every move and render every new state (default:True)initial_state: initial board positions, the default value is the default chess starting board. You can specify a custom board. View scriptsgym_chess/test/for some examplesplayer_color:"WHITE"or"BLACK", only useful if playing against a bot (default:"WHITE")>>>env.get_possible_moves(state=state,player="WHITE",attack=False)This method will calculate the possible moves. By default they are calculated at the current state for the current player (state.current_player).state: (optional) state for which to calculate the movesplayer: (optional) "WHITE" or "BLACK", specifies the playerMove specification:Moves are encoded as either:a tuple of coordinates((from_x, from_y), (to_x, to_y))or a string e.g."CASTLE_KING_SIDE_WHITE","CASTLE_QUEEN_SIDE_BLACK","RESIGN"Moves are pre-calculated for every new state and stored inpossible_moves.Get State>>>print(env.state['board'])[[-3,-5,-4,-2,-1,-4,-5,-3],[-6,-6,-6,-6,-6,-6,-6,-6],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[6,6,6,6,6,6,6,6],[3,5,4,2,1,4,5,3]]Every integer represents a piece. Positive pieces are white and negative ones are black.Piece IDs are stored in constants that can be imported.fromgym_chess.envs.chessimport(KING_ID,QUEEN_ID,ROOK_ID,BISHOP_ID,KNIGHT_ID,PAWN_ID,)The schema is:EMPTY_SQUARE_ID=0KING_ID=1QUEEN_ID=2ROOK_ID=3BISHOP_ID=4KNIGHT_ID=5PAWN_ID=6Additional information can be found in other attributes of the environment:env.current_playerenv.white_king_castle_possibleenv.white_queen_castle_possibleenv.black_king_castle_possibleenv.black_queen_castle_possibleenv.white_king_on_the_boardenv.black_king_on_the_boardNotes:En-passant has not been implemented yet.PokerAPIInitialize environment>>>env=PokerEnv(n_players=2,infinite_game=True)n_players: specify the number of players2<=n_players<=9(default:2)infinite_game:TrueorFalse, specify if players get their starting credit back after each round (default:True)Set actions>>>env.step(action)action: can be0-> small blind1-> big blind2-> fold3-> check4-> bet5-> call6-> raise to 257-> raise to 508-> raise to 1009-> raise to 50010-> raise to 100011-> all inCheckersAPIInitialize environment>>>env=CheckersEnv()Set actions>>>env.step(action)action: mark a position, could be0<=action<1024To encode the coordinates use something like this:// positions -> [[from_row, from_col], [to_row, to_col]]fnpositions_to_action(&self,positions:&Vec<BoardPosition>)->usize{letfrom=positions[0].row*4+(positions[0].column-ifpositions[0].row%2==0{0}else{1})/2;letto=positions[1].row*4+(positions[1].column-ifpositions[1].row%2==0{0}else{1})/2;from*32+to}Get State>>>print(env.get_state())//Return(current_player,board,is_game_over)Board:[[0,3,0,3,0,3,0,3],[3,0,3,0,3,0,3,0],[0,3,0,3,0,3,0,3],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[1,0,1,0,1,0,1,0],[0,1,0,1,0,1,0,1],[1,0,1,0,1,0,1,0]]Every integer represents a piece.Piece IDs:0: empty1: man_12: king_13: man_24: king_2Set State>>>//state->(current_player,board)>>>env.set_state(state)//ReturnobservationReferenceshttps://github.com/PyO3/maturinhttps://github.com/genyrosk/gym-chess(Thanks to genyrosk for gym-chess)https://github.com/deepmind/open_spielContrbutionsPull Request Are Welcomed!LicenseGPL-3.0SocialDiscordTwitterYoutubeFacebook