def play_one_step(self): N = 5 piece_type, previous_board, board = readInput(N) go = GO(N) go.set_board(piece_type, previous_board, board) action = self.get_input(go, piece_type) writeOutput(action)
def alphabeta(self, state, depth, a, b, side): go = GO(5) go.set_board(side, state, state) go.remove_died_pieces(side) state = go.board # Reach the max depth if depth == 0: return (go.score(2) + 3 - go.score(1), (1, 2)) possible_placements = [] for i in range(go.size): for j in range(go.size): if go.valid_place_check(i, j, self.side, test_check=True): possible_placements.append((i, j)) # No possible move: if possible_placements == []: return (go.score(2) + 3 - go.score(1), (1, 2)) str_state = ''.join( [str(state[i][j]) for i in range(N) for j in range(N)]) curr_qvalues = self.findq(str_state) max_moves = self._find_max(possible_placements, curr_qvalues) # Choose Max. (Side = 2) if side == 2: next_side = 1 re_move = max_moves[0] for next_move in max_moves: next_state = deepcopy(state) next_state[next_move[0]][next_move[1]] = next_side if a < self.alphabeta(next_state, depth - 1, a, b, next_side)[0]: a = self.alphabeta(next_state, depth - 1, a, b, next_side)[0] re_move = next_move if b <= a: break return (a, re_move) # Choose Min. (Side = 1) else: next_side = 2 re_move = max_moves[0] for next_move in max_moves: next_state = deepcopy(state) next_state[next_move[0]][next_move[1]] = next_side if b > self.alphabeta(next_state, depth - 1, a, b, next_side)[0]: b = self.alphabeta(next_state, depth - 1, a, b, next_side)[0] re_move = next_move if b <= a: break return (b, re_move)
line = f.read(2) f.close() win = int(line[0]) side = int(line[1]) if win == 0: result = 1 elif win == side: result = 2 else: result = 0 player = QLearner(side=side) player.read() player.update(result) player.save(1) else: """ Get current state and piece_type. Move (action) """ N = 5 piece_type, previous_board, board = readInput(N) go = GO(N) go.set_board(piece_type, previous_board, board) curr_state = ''.join( [str(board[i][j]) for i in range(N) for j in range(N)]) player = QLearner(side=piece_type) player.read() action = player.get_input(go, curr_state) writeOutput(action) player.save(0)
# cd /Users/yijingyang/Documents/usc/2020spring/AI/hw/2/test from host import GO curr_state = "1111111111112111111111111" state = [] for i in range(0, 25, 5): state.append([int(x) for x in curr_state[i:i + 5]]) go = GO(5) board = [[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] go.set_board(1, state, state) print(state) go.remove_died_pieces(2) print(state)
def judge(self, n_move, verbose): N = 5 piece_type, previous_board, board = readInput(N) go = GO(N) go.verbose = verbose go.set_board(piece_type, previous_board, board) go.n_move = n_move try: action, x, y = readOutput() except: cond_print(verbose, "output.txt not found or invalid format") return (3 - piece_type) if action == "MOVE": if not go.place_chess(x, y, piece_type): cond_print(verbose, 'Game end.') cond_print( verbose, 'The winner is {}'.format('X' if 3 - piece_type == 1 else 'O')) return (3 - piece_type) go.died_pieces = go.remove_died_pieces(3 - piece_type) if go.game_end(piece_type, action): result = go.judge_winner() if verbose: cond_print(verbose, 'Game end.') if result == 0: cond_print(verbose, 'The game is a tie.') else: cond_print( verbose, 'The winner is {}'.format('X' if result == 1 else 'O')) return (result) piece_type = 2 if piece_type == 1 else 1 if action == "PASS": go.previous_board = go.board writeNextInput(piece_type, go.previous_board, go.board) return (0)
from host import GO from random_player import RandomPlayer from copy import deepcopy from collections import defaultdict go = GO(5) init_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], ] go.set_board(1, deepcopy(init_board), deepcopy(init_board)) player = RandomPlayer() piece_type = 1 previous_action = 'MOVE' actions = defaultdict(list) board_int = defaultdict(list) while not go.game_end(piece_type, previous_action): action = player.get_input(go, piece_type) actions[piece_type].append(action) if action != 'PASS': go.place_chess(*action, piece_type) go.remove_died_pieces(piece_type) previous_action = 'MOVE' else: previous_action = 'PASS' go.n_move += 1 piece_type = 3 - piece_type print(actions)