-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_agent.py
103 lines (81 loc) · 4.22 KB
/
random_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
File Name: random_agent.py
Authors: Michael Johnson and Leng Ghuy
Date: March 9th, 2019
Description: Python file of a random bot
Source: Adapted from recon-chess (https://pypi.org/project/reconchess/)
"""
import random
import chess
from player import Player
from fen_string_convert import process_sense, convert_fen_string, create_blank_emission_matrix
class Random(Player):
def handle_game_start(self, color, board):
"""
This function is called at the start of the game.
:param color: chess.BLACK or chess.WHITE -- your color assignment for the game
:param board: chess.Board -- initial board state
"""
pass
def handle_opponent_move_result(self, captured_piece, captured_square):
"""
This function is called at the start of your turn and gives you the chance to update your board.
:param captured_piece: bool - true if your opponents captured your piece with their last move
:param captured_square: chess.Square - position where your piece was captured
"""
pass
def choose_sense(self, possible_sense, possible_moves, seconds_left):
"""
This function is called to choose a square to perform a sense on.
:param possible_sense: List(chess.SQUARES) -- list of squares to sense around
:param possible_moves: List(chess.Moves) -- list of acceptable moves based on current board
:param seconds_left: float -- seconds left in the game
:return: chess.SQUARE -- the center of 3x3 section of the board you want to sense
:example: choice = chess.A1
"""
return random.choice(possible_sense)
def handle_sense_result(self, sense_result):
"""
This is a function called after your picked your 3x3 square to sense and gives you the chance to update your
board.
:param sense_result: A list of tuples, where each tuple contains a :class:`Square` in the sense, and if there
was a piece on the square, then the corresponding :class:`chess.Piece`, otherwise `None`.
:example:
[
(A8, Piece(ROOK, BLACK)), (B8, Piece(KNIGHT, BLACK)), (C8, Piece(BISHOP, BLACK)),
(A7, Piece(PAWN, BLACK)), (B7, Piece(PAWN, BLACK)), (C7, Piece(PAWN, BLACK)),
(A6, None), (B6, None), (C8, None)
]
"""
process_sense(sense_result)
pass
def choose_move(self, possible_moves, seconds_left):
"""
Choose a move to enact from a list of possible moves.
:param possible_moves: List(chess.Moves) -- list of acceptable moves based only on pieces
:param seconds_left: float -- seconds left to make a move
:return: chess.Move -- object that includes the square you're moving from to the square you're moving to
:example: choice = chess.Move(chess.F2, chess.F4)
:condition: If you intend to move a pawn for promotion other than Queen, please specify the promotion parameter
:example: choice = chess.Move(chess.G7, chess.G8, promotion=chess.KNIGHT) *default is Queen
"""
return random.choice(possible_moves)
def handle_move_result(self, requested_move, taken_move, reason, captured_piece, captured_square):
"""
This is a function called at the end of your turn/after your move was made and gives you the chance to update
your board.
:param requested_move: chess.Move -- the move you intended to make
:param taken_move: chess.Move -- the move that was actually made
:param reason: String -- description of the result from trying to make requested_move
:param captured_piece: bool -- true if you captured your opponents piece
:param captured_square: chess.Square -- position where you captured the piece
"""
pass
def handle_game_end(self, winner_color, win_reason): # possible GameHistory object...
"""
This function is called at the end of the game to declare a winner.
:param winner_color: Chess.BLACK/chess.WHITE -- the winning color
:param win_reason: String -- the reason for the game ending
"""
pass