import random, re, datetime,time
class Agent(object):
def __init__(self, game):
self.game = game
def getAction(self, state):
raise Exception("Not implemented yet")
class RandomAgent(Agent):
def getAction(self, state):
legal_actions = self.game.actions(state)
self.action = random.choice(legal_actions)
class SimpleGreedyAgent(Agent):
# a one-step-lookahead greedy agent that returns action with max vertical advance
def getAction(self, state):
legal_actions = self.game.actions(state)
self.action = random.choice(legal_actions)
player = self.game.player(state)
if player == 1:
max_vertical_advance_one_step = max([action[0][0] - action[1][0] for action in legal_actions])
max_actions = [action for action in legal_actions if
action[0][0] - action[1][0] == max_vertical_advance_one_step]
else:
max_vertical_advance_one_step = max([action[1][0] - action[0][0] for action in legal_actions])
max_actions = [action for action in legal_actions if
action[1][0] - action[0][0] == max_vertical_advance_one_step]
self.action = random.choice(max_actions)
class TeamNameMinimaxAgent(Agent):
def getAction(self, state):
legal_actions = self.game.actions(state)
self.action = random.choice(legal_actions)
player = self.game.player(state)
### START CODE HERE ###
### END CODE HERE ###
class C_zihao_Agent(Agent):
def getAction(self, state):
start = time.time()
legal_actions = self.game.actions(state)
self.action = random.choice(legal_actions)
player = self.game.player(state)
### START CODE HERE ###
global staticDepth, step, repeat # store the initial value for depth for minimax
depth = 2 # starting from 2
staticDepth = depth # store original depth
max_depth = 20 # breadth for each layer, max is 42
board = state[1]
player_status = board.getPlayerPiecePositions(player) ##occupy
last_flag = (player_status in status_queue) ##true or false? all false??
player_lists = [(2, 1), (2, 2), (3, 2)]
status_queue.append(player_status)
step += 1 ##step++
max_error=player_status[9][0]-player_status[0][0]+1
mean_error=0
for i in range(0,10):
mean_error=mean_error+player_status[i][0]
mean_error=mean_error/10
#print(mean_error)
#print(board.board_status)
special_flag=False
if(mean_error==3.1):
x_index=player_status[9][1]
if(x_index>1):
if(board.board_status[4,x_index-1]==0):
self.action=((player_status[9][0],player_status[9][1]),(4,x_index-1))
special_flag=True
if (x_index<4):
if(board.board_status[4, x_index + 1] == 0):
self.action = ((player_status[9][0], player_status[9][1]), (4, x_index + 1))
special_flag=True
if (board.board_status[(3, 2)] == player+2 and board.board_status[(1,1)]==player):
if (board.board_status[(2, 1)] == player+2 and board.board_status[(2, 2)] == 0):
self.action = ((3, 2), (2, 2))
special_flag=True
elif (board.board_status[(2, 2)] == player+2 and board.board_status[(2, 1)] == 0):
self.action = ((3, 2), (2, 1))
special_flag=True
if (board.board_status[(3, 1)] == player+2 and board.board_status[(2, 1)] == player+2 and board.board_status[(3, 2)] == player+2):
self.action = ((3, 2), (2, 2))
special_flag = True
if (board.board_status[(3, 3)] == player+2 and board.board_status[(2, 2)] == player+2 and board.board_status[(3, 2)] == player+2):
self.action = ((3, 2), (2, 1))
special_flag = True
'''
if (board.board_status[(2, 1)] == player+2 and board.board_status[(3, 2)] == player+2 and board.board_status[(4, 3)] == player+2):
self.action = ((3, 2), (2, 2))
special_flag = True
if (board.board_status[(2, 2)] == player+2 and board.board_status[(3, 2)] == player+2 and board.board_status[(4, 2)] == player+2):
self.action = ((3, 2), (2, 1))
special_flag = True
'''
if(board.board_status[(2,1)]==player+2 and board.board_status[(2,2)]==player+2 and board.board_status[(3,2)]==0):
if(board.board_status[(4,4)]==player+2 and board.board_status[(3,3)]==player):
self.action=((4,4),(5,4))
special_flag=True
elif(board.board_status[(4, 1)] == player+2 and board.board_status[(3, 1)] == player):
self.action = ((4, 1), (4, 2))
special_flag = True
if(board.board_status[(3,2)]==player):
if(board.board_status[(4,2)]==player):
self.action=((3,2),(5,2))
special_flag=True
elif(board.board_status[(4,3)]==player):
self.action=((3,2),(5,4))
special_flag=True
second=time.time()
print('second time: %s Seconds' % (second - start))
if step >= 15 and last_flag and special_flag==False: # in case of "stuck" situation, use "greedy"
status_queue.remove(status_queue[0])
repeat += 1
#if repeat == 2: # in case of repeated "stuck" situation
if repeat>5:
self.action = random.choice(legal_actions)
else:
if player == 1: # playing from bottom to top, hence later vertical value is smaller
max_vertical_advance_one_step = max([action[0][0] - action[1][0] for action in legal_actions])
max_actions = [action for action in legal_actions if
action[0][0] - action[1][0] == max_vertical_advance_one_step]
else: # playing from top to bottom, hence later vertical value is larger
max_vertical_advance_one_step = max([action[1][0] - action[0][0] for action in legal_actions])
max_actions = [action for action in legal_actions if
action[1][0] - action[0][0] == max_vertical_advance_one_step]
random_flag=False
random_cnt=0
while(random_flag==False):
random_cnt=random_cnt+1
if(random_cnt<3):
self.action = random.choice(max_actions)
else:
self.action=random.choice(legal_actions)
random_flag=True
if (board.board_status[self.action[0]] == player+2):
if (self.action[0] in player_lists):
random_flag=False
if (self.action[1] == (1, 1)):
random_flag = False
# continue
if (board.board_status[self.action[0]] == player):
if (self.action[1] in player_lists):
random_flag = False
elif(special_flag==False): ##care
if step >= 3:
status_queue.remove(status_queue[0])
repeat = 0
#print(step)
tran_var = -float('inf')
order = PriorityQueue() # search with preference
for action in legal_actions:
order.put((-(3 - 2 * player) * (action[0][0] - action[1][0]), action))
order_next = PriorityQueue()
while True:
count = 0 ##max
while (not order.empty()) and (max_depth > count):
评论0