def put(): data = request.get_json() b1 = data["board"] x = int(data["x"]) y = int(data["y"]) adv = data["adv"] if adv: W = deepcopy(data["W"]) w = deepcopy(data["w"]) b2 = [] for i in range(8): b2.append(list(b1[i * 8:(i + 1) * 8])) mode = "maxmin" if data["mode"] == 'settai' else "minmax" if data["difficulty"] == 0: mode = "random" depth = int(data["difficulty"]) p = get_player_instance(mode, "B") q = get_player_instance(mode, "W") b = Board(b2) # PLAYER'S TURN if x >= 0: if validate(b.board_data, "B", [x, y]): do_move(b.board_data, "B", [x, y]) plisult = deepcopy(b.board_data) if adv and depth > 0: nm = q.next_move(b.board_data, "W", depth, W, w) elif depth > 0: nm = q.next_move(b.board_data, "W", depth) else: nm = q.next_move(b.board_data, "W") if validate(b.board_data, "W", nm): do_move(b.board_data, "W", nm) qlisult = b.board_data return jsonify({"player": plisult, "com": qlisult, "pos": nm})
def successors(self, state, color): suclist = [] movs = [] for y in range(8): for x in range(8): if game.validate(state, color, [x, y]): movs.append([x, y]) for mov in movs: nb = deepcopy(state) game.do_move(nb, color, mov) suclist.append([mov, nb]) return suclist
def get_successors(self, poslist): if self._color == "B": idx = 0 elif self._color: idx = 1 suclist = [] before_score = game.score(self._board) for pos in poslist: nb = deepcopy(self._board) game.do_move(nb, self._color, pos) after_score = game.score(nb) suclist.append([pos, nb, after_score[idx] - before_score[idx] - 1]) return suclist
def test_do_move_cant_finish(): p0 = Player.get(0) status = [Piece(0, 0, 58)] success = do_move(status, p0, 0, 6) assert not success assert status[0].progress() == 58
def test_do_move_on_target(): p0 = Player.get(0) status = [Piece(0, 0, 56), Piece(1, 0, 33)] success = do_move(status, p0, 0, 1) assert success assert status[0].progress() == 57 assert status[1].progress() == 33
def test_do_move_blocked_out_of_home(): p1 = Player.get(1) piece = Piece(1, 0, 0) status = [piece, Piece(0, 0, 15), Piece(0, 1, 15)] assert not is_valid_move(piece, 6, status) success = do_move(status, p1, 0, 6) assert not success assert status[0].progress() == 0
def test_do_move_cant_out_of_home(): p1 = Player.get(1) piece = Piece(1, 0, 0) status = [piece] assert not is_valid_move(piece, 5, status) success = do_move(status, p1, 0, 5) assert not success assert status[0].progress() == 0
def test_do_move_on_path(): p1 = Player.get(1) piece = Piece(0, 0, 16) status = [piece, Piece(1, 0, 1)] assert is_valid_move(piece, 1, status) success = do_move(status, p1, 0, 1) assert success assert status[0].progress() == 0 assert status[1].progress() == 2
def get_game_result(self, board_data, game_ended=False, opponent=None): b = Board(deepcopy(board_data)) act = opponent.next_move(b.board_data, opponent.color) game.do_move(b.board_data, opponent.color, act) is_over = b.is_game_over() r = 0 if is_over: tmp = b.board_data nb, nw = tmp.count("B"), tmp.count("W") is_bwin = nb > nw is_wwin = nb < nw is_draw = nb == nw if is_draw: r = 0 elif self.color == "B": r = 1 if is_bwin else -1 elif self.color == "W": r = 1 if is_wwin else -1 if self.last_move != None: self.learn(self.last_board, self.last_move, r, b, is_over) if not is_over: self.act_ctr += 1 self.last_move = None self.last_board = None
def test_random_moves_get_game_over(): num_games = 20 num_moves = 500 for _ in range(num_games): board = STARTING_BOARD active_player = Player.RED try: for _ in range(num_moves): piece_loc, player, move = get_move_randomly( active_player, board) board = do_move(move, piece_loc, player, board) active_player = active_player.enemy assert False, "Game did not finish with {} random moves!".format( num_moves) except GameOver: pass assert True
def play(num_players: int, num_pieces: int, first_player: Player) -> None: status = set_board(num_players, num_pieces) next = first_player.number win = False while not win: redraw(status) dice = roll_dice(next) player = Player.get(next) valid_moves = get_valid_moves(player, dice, status) valid = not valid_moves while not valid: piece_to_move = ask_move(valid_moves) valid = do_move(status, player, piece_to_move, dice) win = check_endgame(status) if not win and dice != 6: next = (next + 1) % num_players end_game(status, player)
def main(): board = STARTING_BOARD active_player = Player.RED print() print("Welcome to checkers!") print() print("X = Red's pieces") print("O = Black's pieces") print("The rules have been simplified for your convenience! Good luck!") try: while True: print_board(board) print("{}'s turn".format(active_player)) piece_loc, player, move = get_move_from_stdin(active_player, board) board = do_move(move, piece_loc, player, board) active_player = active_player.enemy except GameOver: print("Game Over!")