Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def make_choice(x,y,field):
- my_life = field[x][y]['life']
- act = ''
- #width = len(field)
- #height = len(field[0])
- coin_up = False
- coin_down = False
- coin_left = False
- coin_right = False
- coin = False
- tank = False
- tank_up = False
- tank_down = False
- tank_left = False
- tank_right = False
- tank_life_en = {}
- actions = ['go_left', 'go_right', 'go_down', 'go_up']
- back_up = actions[:]
- print(back_up)
- if x - 1 != 0:
- for i in range(x-1, 0, -1):
- if field[i][y] == 1:
- coin_left = True
- if field[i][y] not in [-1, 0, 1]:
- tank_left = True
- tank_life_en = field[i][y]['life']
- break
- if field[i][y] == -1:
- break
- if x + 1 != len(field):
- for i in range(x+1, len(field)):
- if field[i][y] == 1:
- coin_right = True
- if field[i][y] not in [-1, 0, 1]:
- tank_right = True
- tank_life_en = field[i][y]['life']
- break
- if field[i][y] == -1:
- break
- if y - 1 != 0:
- for i in range(y-1, 0, -1):
- if field[x][i] == 1:
- coin_up = True
- if field[x][i] not in [-1, 0, 1]:
- tank_up = True
- tank_life_en = field[x][i]['life']
- break
- if field[x][i] == -1:
- break
- if y + 1 != len(field[x]):
- for i in range(y+1, len(field[x])):
- if field[x][i] == 1:
- coin_down = True
- if field[x][i] not in [-1, 0, 1]:
- tank_down = True
- tank_life_en = field[x][i]['life']
- break
- if field[x][i] == -1:
- break
- if coin_up == True or coin_down == True or coin_left == True or coin_right == True:
- coin = True
- if coin_up == True:
- coin = True
- act = 'go_up'
- elif coin_down == True:
- coin = True
- act = 'go_down'
- elif coin_left == True:
- coin = True
- act = 'go_left'
- elif coin_right == True:
- coin = True
- act = 'go_right'
- if tank_up == True or tank_down == True or tank_left == True or tank_right == True:
- tank = True
- if tank_up == True:
- if tank_life_en > my_life + 1:
- if field[x+1][y] == 0:
- act = 'go_right'
- elif field[x-1][y] == 0:
- act == 'go_left'
- elif field[x][y]['history'][-1] == 'fire_up' and field[x+1][y] == 0 or field[x+1][y] == 1 and field[x-1][y] == 0 or field[x-1][y] == 1:
- act = random.choice('go_right', 'go_left')
- elif field[x][y]['history'][-1] == 'fire_up' and field[x-1][y] == 0 or field[x-1][y] == 1 and field[x+1][y] == 0 or field[x+1][y] == 1:
- act = random.choice('go_right', 'go_left')
- elif field[x][y]['history'][-1] == 'fire_up' and field[x+1][y] == 0 or field[x+1][y] == 1:
- act = 'go_right'
- elif field[x][y]['history'][-1] == 'fire_up' and field[x-1][y] == 0 or field[x-1][y] == 1:
- act ='go left'
- else:
- act = random.choice('fire_up', 'go_up', 'go_down')
- elif tank_down == True:
- if tank_life_en > my_life + 1:
- if field[x+1][y] == 0:
- act = 'go_right'
- elif field[x-1][y] == 0:
- act == 'go_left'
- elif field[x][y]['history'][-1] == 'fire_down' and field[x+1][y] == 0 or field[x+1][y] == 1 and field[x-1][y] == 0 or field[x-1][y] == 1:
- act = random.choice('go_right', 'go_left')
- elif field[x][y]['history'][-1] == 'fire_down' and field[x-1][y] == 0 or field[x-1][y] == 1 and field[x+1][y] == 0 or field[x+1][y] == 1:
- act = random.choice('go_right', 'go_left')
- elif field[x][y]['history'][-1] == 'fire_down' and field[x+1][y] == 0 or field[x+1][y] == 1:
- act = 'go_right'
- elif field[x][y]['history'][-1] == 'fire_down' and field[x-1][y] == 0 or field[x-1][y] == 1:
- act ='go left'
- else:
- act = random.choice('fire_down', 'go_up', 'go_down')
- elif tank_left == True:
- if tank_life_en > my_life + 1:
- if field[x][y-1] == 0:
- act = 'go_up'
- elif field[x][y+1] == 0:
- act = 'go_down'
- elif field[x][y]['history'][-1] == 'fire_left' and field[x][y-1] == 0 or field[x][y-1] == 1:
- act = 'go_up'
- elif field[x][y]['history'][-1] == 'fire_left' and field[x][y+1] == 0 or field[x][y+1] == 1:
- act = 'go_down'
- else:
- act = 'fire_left'
- elif tank_right == True:
- if tank_life_en > my_life + 1:
- if field[x][y-1] == 0:
- act = 'go_up'
- elif field[x][y+1] == 0:
- act = 'go_down'
- elif field[x][y]['history'][-1] == 'fire_right' and field[x][y+1] == 0 or field[x][y+1] == 1:
- act = 'go_down'
- elif field[x][y]['history'][-1] == 'fire_right' and field[x][y-1] == 0 or field[x][y-1] == 1:
- act = 'go_up'
- else:
- act = 'fire_right'
- if x == len(field):
- actions.remove('go_right')
- if x == 0:
- actions.remove('go_left')
- if y == 0:
- actions.remove('go_up')
- if y == len(field[x]):
- actions.remove('go_down')
- if tank == True:
- return act
- elif coin == True:
- return act
- elif actions == []:
- actions = back_up[:]
- return random.choice(actions)
- else:
- return random.choice(actions)
- '''
- if __name__ == "__main__":
- T1 = {"life": 10, 'history': ['fire_down']}
- T2 = {'life': 10}
- T = {'life': 20}
- my_field = [
- [T1,T2,0,0],
- [0,T,0,1],
- [0,0,T,0],
- [0,0,0,0]
- ]
- my_x = 0
- my_y = 0
- res = make_choice(my_x, my_y, my_field)
- print(res)
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement