A high-performance Reversi (Reversi) implementation in Julia, built on StaticArrays.jl for efficient board representation. Designed with flexibility for machine learning research and reinforcement learning applications.
- Efficient Implementation: Uses StaticArrays.jl for fast, stack-allocated board representation
- Terminal Gameplay: Play interactively in the terminal
- Flexible Player System: Easy to integrate custom AI players and ML models
- Clean API: Simple, well-documented interface for programmatic control
- Extensible: Abstract player interface allows easy implementation of new strategies
using Pkg
Pkg.add(url="https://github.com/sotashimozono/Reversi.jl")
Pkg.add("Reversi")Or in the Julia REPL package mode (press ]):
pkg> add https://github.com/sotashimozono/Reversi.jl
pkg> add Reversiusing Reversi
# Human vs Random AI
play_game(HumanPlayer(), RandomPlayer())Or run the interactive script:
julia --project examples/play.jlusing Reversi
# Create a new game
game = ReversiGame()
# Display the board
display_board(game)
# Get valid moves
moves = valid_moves(game)
# Make a move
make_move!(game, 3, 4)
# Check game status
if is_game_over(game)
winner = get_winner(game)
endusing Reversi
# Define a custom player type
struct MyAIPlayer <: Player end
# Implement the get_move function
function Reversi.get_move(player::MyAIPlayer, game::ReversiGame)
moves = valid_moves(game)
if isempty(moves)
return nothing # Pass turn
end
# Your AI logic here
# Return a Position(row, col)
return moves[1] # Example: pick first valid move
end
# Play a game
play_game(MyAIPlayer(), RandomPlayer())-
ReversiGame: Main game state structureboard::MMatrix{8,8,Int,64}: 8x8 game boardcurrent_player::Int: Current player (BLACK or WHITE)pass_count::Int: Number of consecutive passes
-
Position: Represents a board positionrow::Int: Row (1-8)col::Int: Column (1-8)
-
Player: Abstract type for player implementations
make_move!(game, row, col): Make a move at the specified positionvalid_moves(game, player): Get all valid moves for a playeris_game_over(game): Check if the game has endedget_winner(game): Get the winner (BLACK, WHITE, or EMPTY for draw)display_board(game): Display the current board stateplay_game(player1, player2; verbose=true): Play a complete game
HumanPlayer(): Interactive terminal playerRandomPlayer(): Makes random valid moves
Reversi.jl is designed to facilitate machine learning research. The flexible player system allows you to integrate:
- Reinforcement Learning agents (e.g., DQN, AlphaZero-style)
- Neural network policies
- MCTS-based players
- Any custom strategy
using Reversi
using Flux # Or your preferred ML framework
struct NeuralPlayer <: Player
model # Your trained neural network
end
function Reversi.get_move(player::NeuralPlayer, game::ReversiGame)
moves = valid_moves(game)
if isempty(moves)
return nothing
end
# Convert board to model input
input = convert_board_to_input(game.board)
# Get policy from neural network
policy = player.model(input)
# Select move based on policy
return select_move_from_policy(policy, moves)
end
# Train your model separately, then:
trained_model = load_trained_model("model.bson")
nn_player = NeuralPlayer(trained_model)
play_game(nn_player, RandomPlayer())See the examples/ directory for more examples:
examples/demo.jl: Demonstrates various featuresexamples/play.jl: Interactive terminal game
Reversi (also known as Reversi) is played on an 8x8 board:
- Black (●) plays first
- Players alternate placing pieces
- A valid move must flip at least one opponent piece
- Pieces are flipped by sandwiching them between your pieces
- If no valid moves exist, the player must pass
- Game ends when both players pass or the board is full
- Winner is the player with more pieces
- Web-based UI using Genie.jl or similar
- Opening book support
- Game replay and analysis tools
- Additional built-in AI strategies
- Tournament system for comparing players
Contributions are welcome! Please feel free to submit a Pull Request.