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

@ -134,8 +134,15 @@ class Board:
score = 2 if gammon else 1 score = 2 if gammon else 1
return {winner: score, -winner: -score} 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 @staticmethod
def calculate_legal_states(board, player, roll): def calculate_legal_states(board, player, roll):
# Find all points with checkers on them belonging to the player # Find all points with checkers on them belonging to the player

2
cup.py
View File

@ -5,6 +5,6 @@ class Cup:
def __init__(self): def __init__(self):
self.dice_1 = Dice self.dice_1 = Dice
self.dice_2 = Dice self.dice_2 = Dice
def roll(self): def roll(self):
return [self.dice_1.roll(), self.dice_2.roll()] return [self.dice_1.roll(), self.dice_2.roll()]

24
game.py
View File

@ -1,4 +1,5 @@
from board import Board from board import Board
from player import Player
from bot import Bot from bot import Bot
from restore_bot import RestoreBot from restore_bot import RestoreBot
from cup import Cup from cup import Cup
@ -7,6 +8,7 @@ import sys
class Game: class Game:
def __init__(self, config = None): def __init__(self, config = None):
self.config = config self.config = config
self.board = Board.initial_state self.board = Board.initial_state
@ -37,6 +39,28 @@ class Game:
def board_state(self): def board_state(self):
return self.board 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): 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)) sys.stderr.write("[TRAIN] Training {} episodes and save_step_size {}\n".format(episodes, save_step_size))
outcomes = [] 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