Connect2 is a two player game on a board of four cells: the players take turns to put a stone in an empty cell, and the first to have two stones in cells next to each other wins. A full board with no winner, such as 1, 2, 1, 2, is a draw. The game is small enough to search completely, which makes it a test bed for self-play algorithms such as AlphaZero. This example plays it with Connect2.
What you need
Nothing beyond bitrl itself.
The environment
step(action) puts a stone of the player to move in cell action, and the time step carries the valid moves left as an extra, valid_moves, a std::vector<uint_t>. Playing a cell that is taken, or one that does not exist, throws std::logic_error. move(player, action) plays for a given player, 1 or 2; the observation shows their stones as 1 and 2. A win ends the game with a reward of 1 for the player who made the last move, a draw with a reward of 0.
Running the example
The example plays a cell, tries an invalid one and a taken one, wins, and then plays a game with both players.
The code
#include <any>
#include <ios>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
{
std::cout << "Name: " << env.name << std::endl;
std::cout << "Number of actions: " << env.n_actions() << std::endl;
std::unordered_map<std::string, std::any> options;
env.make("v1", options, options);
std::cout << "Is active? " << env.is_created() << std::noboolalpha << std::endl;
std::cout << "Reseting the environment... " << std::endl;
auto time_step = env.reset();
std::cout << "Time step after reset: " << std::endl;
std::cout << time_step << std::endl;
std::cout << "Acting on the environment... " << std::endl;
time_step = env.step(0);
std::cout << "Observation vector..." << time_step.observation() << std::endl;
auto val_moves = time_step.template get_extra<std::vector<bitsim::uint_t>>("valid_moves");
std::cout << "Valid moves remaining..." << val_moves << std::endl;
try
{
env.step(5);
}
catch (std::logic_error &e)
{
std::cout << e.what() << std::endl;
}
try
{
}
catch (std::logic_error &e)
{
std::cout << e.what() << std::endl;
}
std::cout << "Winning the game..." << std::endl;
std::cout << time_step << std::endl;
std::cout << "Starting the game..." << std::endl;
std::cout << time_step.observation() << std::endl;
std::cout << "Player 1 moves..." << std::endl;
time_step =
env.move(1, 2);
std::cout << time_step.observation() << std::endl;
std::cout << "Player 2 moves..." << std::endl;
time_step =
env.move(2, 3);
std::cout << time_step.observation() << std::endl;
std::cout << "Player 1 moves..." << std::endl;
time_step =
env.move(1, 1);
std::cout << time_step.observation() << std::endl;
std::cout << "Closing the environment... " << std::endl;
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Implementation of Connect2 environment from https://github.com/JoshVarty/AlphaZeroSimple The board ha...
Definition connect2_env.h:39
int main()
Definition discounted_returns.cpp:19
Definition connect2_env.h:29
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40