Skip to content

Instantly share code, notes, and snippets.

View morgankenyon's full-sized avatar
💭
Program program program!

morgankenyon

💭
Program program program!
View GitHub Profile
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)")
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()
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)
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)
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
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]
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()
import copy
from .player import Player
MARKER_TO_CHAR = {
None: ' . ',
Player.x: ' x ',
Player.o: ' o ',
}
class Board():
import enum
class Player(enum.Enum):
x = 1
o = 2
@property
def other(self):
return Player.x if self == Player.o else Player.o
# 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))