def test_is_losing_play(self): play = Play('white1', 'N', 'N') self.assertTrue( self.strategy._is_losing_play('white', play, self.board)) self.assertFalse( self.strategy._is_losing_play('white', Play('white1', 'N', 'SE'), self.board))
def _all_possible_plays(self, worker, board): plays = [] for move_direction in board.DIRECTION_MAP: for build_direction in board.DIRECTION_MAP: play = Play(worker, move_direction, build_direction) plays.append(play) no_build_play = Play(worker, move_direction) plays.append(no_build_play) return plays
def test_is_valid_play(self): self.assertTrue( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('blue1', 'W'))) self.assertTrue( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('blue2', 'SW'))) self.assertFalse( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('blue1', 'E', 'N'))) self.assertFalse( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('blue1', 'E', 'E'))) self.assertFalse( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('white1', 'W'))) self.assertFalse( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('white1', 'N', 'NE'))) self.assertTrue( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('white1', 'S', 'W'))) self.assertFalse( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('white1', 'S', 'E'))) self.assertFalse( self.rule_checker.is_valid_play(self.GAME_BOARD1, Play('white2', 'W', 'SW')))
def execute(self, input): command = input[0] if not self.check_input(input): return self.error_message() if command == 'Register' and self.game_state == 0: self.game_state = 1 return self.name if command == 'Place' and self.game_state == 1: self.color = str(input[1]) b = Board(input[2]) self.last_board = b if not self.RuleChecker.validate_initial_board(b, self.color): return self.error_message() self.game_state = 2 posns = self.place_workers(b) self.last_board.worker_locations[self.color + '1'] = Position( posns[0][0], posns[0][1]) self.last_board.worker_locations[self.color + '2'] = Position( posns[1][0], posns[1][1]) return posns if command == 'Play' and self.game_state == 2: b = Board(input[1]) if not self.is_possible_board(b): return self.error_message() self.last_board = b if not self.RuleChecker.validate_board(b): return self.error_message() play_options = self.get_plays(b) if play_options: index = random.randint(0, len(play_options) - 1) playmade = self.format_plays(play_options)[index] else: play_options = self.strategy.get_legal_plays(self.color, b) if play_options: index = random.randint(0, len(play_options) - 1) playmade = self.format_plays(play_options)[index] else: playmade = [] if playmade: if len(playmade) == 3: Playmade = Play(playmade[0], playmade[1], playmade[2]) else: Playmade = Play(playmade[0], playmade[1]) self.last_board = Playmade.resulting_board(self.last_board) return playmade if command == 'Game Over': self.game_state = 0 self.last_board = Board([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]) self.color = '' return 'OK' return self.error_message()
def _check_play(self, test_play): game_board = test_play[0] worker = test_play[1] directions = test_play[2] move_direction = directions[0] build_direction = directions[1] if len(directions) == 2 else None board = Board(game_board) play = Play(worker, move_direction, build_direction) return self.rule_checker.is_valid_play(board, play)
def check_play(self, worker, directions): # checks if its valid play if len(directions) < 2: directions.append(None) play = Play(worker, directions[0], directions[1]) is_legal = self.rule_checker.is_valid_play(self.board, play) if not is_legal or self.turn != self.get_player_color(play): return self.otherplayerwins() self.board.move_worker(play.worker, play.move_direction) if play.build_direction: self.board.build(play.worker, play.build_direction) self.turn = self.other_color(self.turn) return self.board else: return [self.color_to_name[self.turn]]