-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_vs_bot_engine.py
105 lines (85 loc) · 3.32 KB
/
bot_vs_bot_engine.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
# Import the bots and neccessary libraries:
from random_bot import Random_Bot
from mcts_bot import Jaspers_MCTS_Agent
import numpy as np
class bvb_engine:
def __init__(self, name: str = 'bvb engine'):
self.name = name
def simulate_bot_game(self, board_dict):
''' Simulate a game between two bots given the initial board state'''
# Create instances of bots
random_agent = Random_Bot()
mcts_agent = Jaspers_MCTS_Agent()
game_result = None
while True:
# Get move from the first bot
selected_move = mcts_agent.move(board_dict)
# Update the board dictionary
new_state, new_valid_moves, new_active_box = mcts_agent.make_move(selected_move, board_dict['board_state'], 1)
board_dict['board_state'] = new_state
board_dict['active_box'] = new_active_box
board_dict['valid_moves'] = new_valid_moves
# Check if anyone has won
game_result = mcts_agent.get_outcome(board_dict['board_state'])
if game_result != -3:
break
# Get move from the second bot
selected_move = random_agent.move(board_dict)
# Update the board dictionary and check if anyone has won
new_state, new_valid_moves, new_active_box = mcts_agent.make_move(selected_move, board_dict['board_state'], -1)
board_dict['board_state'] = new_state
board_dict['active_box'] = new_active_box
board_dict['valid_moves'] = new_valid_moves
# Check if anyone has won
game_result = mcts_agent.get_outcome(board_dict['board_state'])
if game_result != -3:
break
# Get the winner
winner = None
if game_result == 0:
winner = random_agent.name
elif game_result == 1:
winner = mcts_agent.name
else:
winner = "Draw!"
# Return the winner and the final board state
return winner, board_dict['board_state']
def format_board(self, board):
formatted_board = ''
for i in range(len(board)):
if i % 3 == 0 and i != 0:
formatted_board += '-' * 25 + '\n'
for j in range(len(board[i])):
if j % 3 == 0 and j != 0:
formatted_board += '| '
formatted_board += ' '
if board[i][j] == 1:
formatted_board += 'X '
elif board[i][j] == -1:
formatted_board += 'O '
else:
formatted_board += '. '
formatted_board += '\n'
return formatted_board
# Create an initial game state to feed to a bot
board_dict = {
'board_state': np.zeros((9, 9)), # Initial board
'active_box': (1, 1), # Initial active box
'valid_moves': [(3, 3), (3, 4), (3, 5), (4, 3), (4, 5), (5, 3), (5, 4),(5, 5), (4, 4)] # Initial valid moves
}
# Create instance of the engine
game_engine = bvb_engine()
wins = 0
i = 0
while i < 1:
# Run the game
winner, end_state = bvb_engine.simulate_bot_game(game_engine, board_dict)
if winner == "mcts_bot":
wins+=1
# print("W")
else:
print("L")
i+=1
print(game_engine.format_board(end_state))
print("The winner is: ")
print(winner)