From 4ca60f1d4ba6e32769e90c6f206cb015613f3e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Thu, 24 May 2018 21:26:40 +0200 Subject: [PATCH] fixes to board --- board.py | 11 ++++++++-- test.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/board.py b/board.py index d32197c..f1e4c68 100644 --- a/board.py +++ b/board.py @@ -97,7 +97,7 @@ class Board: board_rep += bar_trans(board, player) board_rep += (15 - Board.num_of_checkers_for_player(board, player),) - board_rep += ([1,0] if cur_player == 1 else [1,0]) + board_rep += ([1,0] if cur_player == 1 else [0,1]) return np.array(board_rep).reshape(1,198) @@ -189,6 +189,13 @@ class Board: def can_bear_off(): checker_idxs = Board.idxs_with_checkers_of_player(board, player) + + def moving_directly_off(): + if player == 1: + return to_idx == 25; + if player == -1: + return to_idx == 0; + def is_moving_backmost_checker(): if player == 1: return all([(idx >= from_idx) for idx in checker_idxs]) @@ -201,7 +208,7 @@ class Board: else: return all([(idx <= 6) for idx in checker_idxs]) - return all([ is_moving_backmost_checker(), + return all([ moving_directly_off() or is_moving_backmost_checker(), all_checkers_in_last_quadrant() ]) # TODO: add switch here instead of wonky ternary in all diff --git a/test.py b/test.py index 6c9c130..0c6d8a6 100644 --- a/test.py +++ b/test.py @@ -141,6 +141,56 @@ class TestIsMoveValid(unittest.TestCase): # TODO: More tests for bearing off are needed + def test_bear_off_non_backmost(self): + board = ( 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 1, + 0 ) + self.assertEqual(Board.is_move_valid(board, 1, 2, (23, 25)), True) + self.assertEqual(Board.is_move_valid(board, 1, 1, (24, 25)), True) + self.assertEqual(Board.is_move_valid(board, 1, 2, (24, 26)), False) + + def test_bear_off_quadrant_limits_white(self): + board = ( 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, + 0 ) + self.assertEqual(Board.is_move_valid(board, 1, 2, (23, 25)), False) + self.assertEqual(Board.is_move_valid(board, 1, 1, (24, 25)), False) + + def test_bear_off_quadrant_limits_black(self): + board = ( 0, + -1, -1, -1, -1, -1, -1, + -1, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0 ) + self.assertEqual(Board.is_move_valid(board, -1, 2, (2, 0)), False) + self.assertEqual(Board.is_move_valid(board, -1, 1, (1, 0)), False) + + def test_bear_off_quadrant_limits_white_2(self): + board = ( 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 0, 1, + 0 ) + self.assertEqual(Board.is_move_valid(board, 1, 1, (24, 25)), True) + + def test_bear_off_quadrant_limits_black_2(self): + board = ( 0, + -1, 0, 0, 0, 0, -1, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0 ) + self.assertEqual(Board.is_move_valid(board, -1, 1, (1, 0)), True) + + class TestNumOfChecker(unittest.TestCase): def test_simple_1(self): board = ( 0, @@ -687,6 +737,23 @@ class TestBoardFlip(unittest.TestCase): self.assertTrue((Board.board_features_tesauro(board, 1) == np.array(expected).reshape(1, 198)).all()) + def test_pubeval_features(self): + board = Board.initial_state + + expected = (0, + 2, 0, 0, 0, 0, -5, + 0, -3, 0, 0, 0, 5, + -5, 0, 0, 0, 3, 0, + 5, 0, 0, 0, 0, -2, + 0, + 0, 0) + + import numpy as np + self.assertTrue((Board.board_features_to_pubeval(board, 1) == + np.array(expected).reshape(1, 28)).all()) + self.assertTrue((Board.board_features_to_pubeval(board, -1) == + np.array(expected).reshape(1, 28)).all()) + def test_tesauro_bars(self): board = list(Board.initial_state) board[1] = 0