This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ttt.player import Player | |
from ttt.game import Game | |
from ttt.random_bot import RandomBot | |
from ttt.one_layer_bot import OneLayerBot | |
def main(): | |
xbot = RandomBot(Player.x) | |
obot = OneLayerBot(Player.o) | |
game = Game(1000) | |
print ("randombot (x) vs onelayerbot (o)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import copy | |
class OneLayerBot(): | |
def __init__(self, player): | |
self.player = player | |
def select_move(self, board): | |
# gets legal moves | |
candidates = board.get_legal_moves() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ttt.player import Player | |
from ttt.game import Game | |
from ttt.random_bot import RandomBot | |
def main(): | |
xbot = RandomBot(Player.x) | |
obot = RandomBot(Player.o) | |
game = Game(1000) | |
game.simulate(xbot, obot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class RandomBot(): | |
def __init__(self, player): | |
self.player = player | |
def select_move(self, board): | |
candidates = board.get_legal_moves() | |
return random.choice(candidates) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .board import Board | |
from .player import Player | |
class Game(): | |
def __init__(self, num_of_games): | |
self.num_of_games = num_of_games | |
self.x_wins = 0 | |
self.o_wins = 0 | |
self.ties = 0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_move(self, row, col, player): | |
if (self.is_space_empty(row, col)): | |
self.grid[row][col] = player | |
self.moves.append([row,col]) | |
else: | |
raise Exception("Attempting to move onto already occupied space") | |
def last_move(self): | |
return self.moves[-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def has_winner(self): | |
# need at least 5 moves before x hits three in a row | |
if (len(self.moves) < 5): | |
return None | |
# check rows for win | |
for row in range(self.dimension): | |
unique_rows = set(self.grid[row]) | |
if (len(unique_rows) == 1): | |
value = unique_rows.pop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
from .player import Player | |
MARKER_TO_CHAR = { | |
None: ' . ', | |
Player.x: ' x ', | |
Player.o: ' o ', | |
} | |
class Board(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import enum | |
class Player(enum.Enum): | |
x = 1 | |
o = 2 | |
@property | |
def other(self): | |
return Player.x if self == Player.o else Player.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initialize our state | |
current_node = root | |
is_max = True | |
while (True): | |
# run minimax on current node | |
choice = minimax(current_node, is_max) | |
# make choice based on minimax search | |
if (choice.move == "left"): | |
print ("Moving left to node with value " + str(current_node.left.value)) |