From b49946e42c923625e84bac1ed02dd8312f63450c Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Fri, 9 Mar 2018 14:19:31 +0100 Subject: [PATCH] Added player type and possibility of playing against network as player --- board.py | 11 +++++++++-- cup.py | 2 +- game.py | 24 ++++++++++++++++++++++++ player.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 player.py diff --git a/board.py b/board.py index 8124a55..c42b777 100644 --- a/board.py +++ b/board.py @@ -134,8 +134,15 @@ class Board: score = 2 if gammon else 1 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): # Find all points with checkers on them belonging to the player diff --git a/cup.py b/cup.py index a6bef37..8d9f273 100644 --- a/cup.py +++ b/cup.py @@ -5,6 +5,6 @@ class Cup: def __init__(self): self.dice_1 = Dice self.dice_2 = Dice - + def roll(self): return [self.dice_1.roll(), self.dice_2.roll()] diff --git a/game.py b/game.py index b14d472..a622b9a 100644 --- a/game.py +++ b/game.py @@ -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 = [] diff --git a/player.py b/player.py new file mode 100644 index 0000000..3c8e592 --- /dev/null +++ b/player.py @@ -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