-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMain.py
More file actions
130 lines (94 loc) · 3.72 KB
/
ChessMain.py
File metadata and controls
130 lines (94 loc) · 3.72 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Interacts through berserk with the lichess API to play online against any Lichess player upon invitation.
"""
import time
import src.GameState as GameState
from src.GameState import Move
import berserk
import threading
class Game(threading.Thread):
def __init__(self, client, game_id, **kwargs):
super().__init__(**kwargs)
self.game_id = game_id
self.client = client
self.stream = client.bots.stream_game_state(game_id)
self.current_state = next(self.stream)
def run(self):
for event in self.stream:
if event['type'] == 'gameState':
self.handle_state_change(event)
elif event['type'] == 'chatLine':
self.handle_chat_line(event)
def handle_state_change(self, game_state):
pass
def handle_chat_line(self, chat_line):
pass
def main():
#token = "lMysRaNxd9sNQkXj" rodfish
token = "b0X9NKGbCQ3XWpz3" # coffeebot
session = berserk.TokenSession(token)
client = berserk.Client(session)
#client.account.upgrade_to_bot()
in_game = False
numberOfMoves = 0
while(not in_game):
"Waiting for challenge.."
time.sleep(2)
for event in client.bots.stream_incoming_events():
if event['type'] == 'gameStart':
game_id = event['game']['id']
in_game = True
break
elif event['type'] == 'challenge':
game_id = event['challenge']['id']
client.bots.accept_challenge(game_id)
client.bots.post_message(game_id, "glhf")
in_game = True
print("The game has started!")
gs = GameState.GameState()
moveMade = False # flag variable for when a move is made
numberOfMoves = 1
while in_game:
g = Game(client, game_id)
moveList = g.current_state["state"]["moves"]
moves = moveList.split()
print(moves)
if len(moves) < numberOfMoves or moveList[0] == "":
time.sleep(1)
continue
numberOfMoves == len(moveList)
# print(gs.board)
if gs.whiteToMove: # importing enemy move
if len(moves) % 2 == 1:
# if True:
moveE = gs.notationTransform(str(moves[-1]), gs.board) # enemy move
# moveE = Move(move[-1][:2], move[-1][2:], gs.board) # enemy move
print(moveE.getChessNotation())
move = gs.makeMove(moveE)
print(gs.board)
print("white move made")
if moveMade := True:
validMoves = gs.getValidMoves(gs.whiteToMove, False) # detects checkmate, stalemate
if gs.checkMate:
client.bots.post_message(game_id, "gg Easy")
print("MATE")
moveMade = False
else:
time.sleep(0.5)
if (not gs.whiteToMove): # bot playing a move
validMoves = gs.getValidMoves(gs.whiteToMove, False)
if len(validMoves) > 0:
# move = gs.computerMoveRandom()
move = gs.computerMoveProoo()
print(str(move.getChessNotation()))
client.bots.make_move(game_id, str(move.getChessNotation()))
print("evaluation: ", round(gs.evaluation(gs.whiteToMove), 2))
if moveMade := True:
validMoves = gs.getValidMoves(gs.whiteToMove, False) # detects checkmate, stalemate
if gs.checkMate:
client.bots.post_message(game_id, "gg")
print("MATE")
moveMade = False
time.sleep(0.5)
if __name__ == "__main__":
main()