-
Notifications
You must be signed in to change notification settings - Fork 15
/
bot.py
115 lines (83 loc) · 3.55 KB
/
bot.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
104
105
106
107
108
109
110
111
112
113
114
115
"""
The Brandeis Quant Club ML/AI Competition (November 2023)
Author: @Ephraim Zimmerman
Email: [email protected]
Website: brandeisquantclub.com; quants.devpost.com
Description:
For any technical issues or questions please feel free to reach out to
the "on-call" hackathon support member via email at [email protected]
Website/GitHub Repository:
You can find the latest updates, documentation, and additional resources for this project on the
official website or GitHub repository: https://github.com/EphraimJZimmerman/chess_hackathon_23
License:
This code is open-source and released under the MIT License. See the LICENSE file for details.
"""
import random
import chess
import time
from collections.abc import Iterator
from contextlib import contextmanager
import test_bot
@contextmanager
def game_manager() -> Iterator[None]:
"""Creates context for game."""
print("===== GAME STARTED =====")
ping: float = time.perf_counter()
try:
# DO NOT EDIT. This will be replaced w/ judging context manager.
yield
finally:
pong: float = time.perf_counter()
total = pong - ping
print(f"Total game time = {total:.3f} seconds")
print("===== GAME ENDED =====")
class Bot:
def __init__(self, fen=None):
self.board = chess.Board(fen if fen else "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
def check_move_is_legal(self, initial_position, new_position) -> bool:
"""
To check if, from an initial position, the new position is valid.
Args:
initial_position (str): The starting position given chess notation.
new_position (str): The new position given chess notation.
Returns:
bool: If this move is legal
"""
return chess.Move.from_uci(initial_position + new_position) in self.board.legal_moves
def next_move(self) -> str:
"""
The main call and response loop for playing a game of chess.
Returns:
str: The current location and the next move.
"""
# Assume that you are playing an arbitrary game. This function, which is
# the core "brain" of the bot, should return the next move in any circumstance.
move = str(random.choice([_ for _ in self.board.legal_moves]))
print("My move: " + move)
return move
# Add promotion stuff
if __name__ == "__main__":
chess_bot = Bot() # you can enter a FEN here, like Bot("...")
with game_manager():
"""
Feel free to make any adjustments as you see fit. The desired outcome
is to generate the next best move, regardless of whether the bot
is controlling the white or black pieces. The code snippet below
serves as a useful testing framework from which you can begin
developing your strategy.
"""
playing = True
while playing:
if chess_bot.board.turn:
chess_bot.board.push_san(test_bot.get_move(chess_bot.board))
else:
chess_bot.board.push_san(chess_bot.next_move())
print(chess_bot.board, end="\n\n")
if chess_bot.board.is_game_over():
if chess_bot.board.is_stalemate():
print("Is stalemate")
elif chess_bot.board.is_insufficient_material():
print("Is insufficient material")
# EX: Outcome(termination=<Termination.CHECKMATE: 1>, winner=True)
print(chess_bot.board.outcome())
playing = False