Added player type and possibility of playing against network as player

This commit is contained in:
Alexander Munch-Hansen 2018-03-09 14:19:31 +01:00
parent f3f0e40aa4
commit b49946e42c
4 changed files with 63 additions and 3 deletions

View File

@ -135,6 +135,13 @@ class Board:
return {winner: score, -winner: -score}
@staticmethod
def apply_moves_to_board(board, player, moves):
for move in moves:
from_idx, to_idx = move.split("/")
board[int(from_idx)] -= int(player)
board[int(to_idx)] += int(player)
return board
@staticmethod
def calculate_legal_states(board, player, roll):

24
game.py
View File

@ -1,4 +1,5 @@
from board import Board
from player import Player
from bot import Bot
from restore_bot import RestoreBot
from cup import Cup
@ -7,6 +8,7 @@ import sys
class Game:
def __init__(self, config = None):
self.config = config
self.board = Board.initial_state
@ -37,6 +39,28 @@ class Game:
def board_state(self):
return self.board
def play_against_player(self):
self.board = Board.initial_state
coin_flip = random.random()
if coin_flip > 0.5:
user_color = input("Pick a number, 1 (white) or -1 (black)")
if int(user_color) == 1:
p1 = player(1)
p2 = bot(-1)
else:
p1 = bot(1)
p2 = player(-1)
else:
p1 = bot(1)
p2 = player(-1)
while Board.outcome(self.board) == None:
roll = self.roll()
self.board = p1.make_move(self.board, p1.get_sym(), roll)
roll = self.roll()
self.board = p2.make_move(self.board, p2.get_sym(), roll)
def train_model(self, episodes=1000, save_step_size = 100, init_ep = 0):
sys.stderr.write("[TRAIN] Training {} episodes and save_step_size {}\n".format(episodes, save_step_size))
outcomes = []

29
player.py Normal file
View File

@ -0,0 +1,29 @@
from board Import Board
class Player:
def __init__(self, sym):
self.sym = sym
def get_sym(self):
return self.sym
def make_move(self, board, sym, roll):
print(Board.pretty(board))
legal_moves = Board.calculate_legal_states(board, sym, roll)
if roll[0] == roll[1]:
print("Example of move: 4/6,6/8,12/14,13/15")
else:
print("Example of move: 4/6,13/17")
user_moves = input("Enter your move: ").strip().split(",")
board = Board.apply_moves_to_board(board, sym, user_moves)
while board not in legal_moves:
print("Move is invalid, please enter a new move")
user_moves = input("Enter your move: ").strip().split(",")
board = Board.apply_moves_to_board(board, sym, user_moves)
return board