-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_vs_bot_engine.py
115 lines (91 loc) · 3.93 KB
/
player_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
105
106
107
108
109
110
111
112
113
114
# 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 bot
mcts_agent = Jaspers_MCTS_Agent()
# Get user's name
players_name = input("Enter your name: ")
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 = self.get_player_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 = players_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
def get_player_move(self, board_dict):
# Print board state to the user with a list of their valid moves
board_state = board_dict['board_state']
valid_moves = board_dict['valid_moves']
print("Current board state:")
print(self.format_board(board_state))
print("Your valid moves:")
print(valid_moves)
# Get user inputted tuple in the correct format (provide message if format is incorrect)
user_input = list(input("Enter one of your valid moves in 00 format: "))
user_input[0], user_input[1] = int(user_input[0]), int(user_input[1])
user_input = tuple(user_input)
# Return their move tuple
return user_input
# 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()
# Run the game
winner, end_state = bvb_engine.simulate_bot_game(game_engine, board_dict)
print(game_engine.format_board(end_state))
print("The winner is: ")
print(winner)