Game
RunGame
connectFourLab.game.game.RunGame(player_one=None, player_two=None, first_player_randomized=True, time_limit=None, print_result_on_console=False, start=True, async=False)
Run matches of Connect Four (7x7)
This class uses Agents to take actions
each turn and Timer to control the time_limit
.
Arguments
player_one
- type or instance, optional, defaultNone
- Type or instance of any
AgentBase
object - If
None
theAgentRandom
object will be assigned
- Type or instance of any
player_two
- same asplayer_one
first_player_randomized
- bool, optional, default False- Defines if the first turn will be randomized or the player one will start the game
time_limit
- int, optional, default None- Defines the limit of time each player have to play the match
None
- Unlimited time
print_result_on_console
- bool, optional, default False- Defines if in the end of the match the result will be printed on the console. Good for debug.
start
- bool, optional, default True- If
True
the game will be started in the init or else you have to callstart
- If
async
- bool, optional, default False- If
True
the game will be run asynchronously
- If
Attributes
GameStatus
- Enum -> (running, winner, tie, timeout, killed, exception)- Example: RunGame.GameStatus.winner
BOARD_FORMAT
- tuple, constant, value (7,7), define the board dimensionsMAX_TURNS_POSSIBLE
- int, the maximum number of turns in a matchwinner
-AgentBase
object- The Agent winner of the last match, if the is one.
status
- GameStatus, check the example(2) bellowNone
- New instance, not started yetrunning
- Game is runningwinner
- Last game ended with a winnertie
- Last game ended in a tietimeout
- In the last game one of the players left the time run outkilled
- The game was stoped with thekill
methodexception
- Exception during the game execution
Properties
is_running
- ReturnTrue
if the game is currently running
Exeptions
InvalidColumn
- raised when the agent return a column out of range or a column already fulfilled
Example 1
from connectFourLab.game import RunGame RunGame(print_result_on_console=True)
Example 2
import time from connectFourLab.game import RunGame from connectFourLab.game.agents.monteCarlo import AgentSimulation game = RunGame(player_one=AgentSimulation, async=True) while game.is_running: time.sleep(1) if game.status == game.GameStatus.tie: print('The game ended in a tie.') elif game.status == game.GameStatus.winner: print('Winner:', game.winner.name) elif etc...
UI implementation
To implement an User Interface you can override the events and
implement the get_human_input
Events
on_game_start
- Triggered when the game startson_new_turn
- Triggered when a new turn startson_end_turn
- Triggered when the turn endson_game_end
- Triggered when the game ends
User input
get_human_input
- It's called by any AgentHuman in the match
UI Attributes
memory
- list, register all turns data- Item: (player_id, current_board, column_choosed)
players
- dict, two Agents one for each player- player one: id
1
- two: id-1
- player one: id
clock
- dict, two clocks (Chronometer
orTimer
) one for each player- player one: id
1
- two: id-1
- player one: id
UI Example
RunGame methods
start
start()
Starts a new match
kill
kill()
Stop the current match.
Force the game to stop and wait for the status confirmation.
get_human_input
get_human_input(player, board)
Event game start
To be overridden by an UI.
This method will be called by every AgentHuman
present in the match, if the is one. This method
should return the user input, the player
column of choice.
If this method is not overridden, the AgentHuman
won't work
Arguments
player
-AgentHuman
, owner of the turnboard
- current state of the board
Return
User input, column of choice, as int 0-6
on_game_start
on_game_start()
Event game start
To be overridden by an UI. Called in the beginning of a match.
on_game_end
on_game_end()
Event game end
To be overridden by an UI. Called in the end of a match.
on_new_turn
on_new_turn(player, clock)
Event new turn
To be overridden by an UI. Called in the beginning of each turn.
Arguments
player
-AgentBase
object, owner of the turnclock
-Chronometer
orTimer
, clock of the owner of the turn
on_end_turn
on_end_turn()
Event end turn
To be overridden by an UI. Called in the end of a turn.