ai agent
this is the aiunknown
plain_text
3 years ago
4.3 kB
31
Indexable
# -*- coding: utf-8 -*-
import math
import random
def get_AI_orders(game_board, player_info, current_player):
possible_actions = {}
createGhost = create_ghost(game_board, player_info)
if createGhost:
possible_actions['ghost'] = createGhost
attackerPosition, attackedPosition = attack(game_board, player_info)
if attackerPosition and attackedPosition:
possible_actions['x'] = [attackerPosition, attackedPosition]
moveGhost = move_ghost(game_board, player_info, current_player)
if moveGhost:
possible_actions['@'] = moveGhost
collectMagic = collect_magic(game_board, player_info)
if collectMagic:
possible_actions['$'] = collectMagic
keys = possible_actions.keys()
print("Actions: ", possible_actions[random.choice(list(keys))])
return ""
def collect_magic(game_board, player_info):
possitions = []
for i in range(len(game_board)):
for j in range(len(game_board[i])):
if "magic" in game_board[i][j]['type'].split("+") and "ghost" in game_board[i][j]['type'].split("+"):
possitions.append([i, j])
return possitions
def move_ghost(game_board, player_info, current_position):
current_box = game_board[player_info[2][0]][player_info[2][1]]
possible_positions = []
for i in range(len(game_board)):
for j in range(len(game_board[i])):
if current_box["type"] in ["ghost", "magic+ghost"]:
new_box = game_board[i][j]
if is_empty_box(game_board, [i, j]) != 0 or (
(current_box["type"] in ["ghost", "magic+ghost"]) and (new_box["type"] == "magic")):
possible_positions.append([i, j])
return possible_positions
def create_ghost(game_board, player_info):
if int(player_info[0]) >= 300:
emptySpaces = []
for i in range(len(game_board)):
for j in range(len(game_board[i])):
if is_empty_box(game_board, player_info[2]) == 1:
emptySpaces.append([i, j])
if emptySpaces:
choose = random.choice(emptySpaces)
return choose
else:
print("the case is already full you can't create a ghost here")
return []
else:
print("You don't have enough points:")
return []
def attack(game_board, player_info):
possible_action = {}
for i in range(len(game_board)):
for j in range(len(game_board[i])):
if is_ghost_exists(game_board, [player_info[2][0], player_info[2][1]]):
possible_positions = ([i + 1, j + 1], [i + 1, j], [i + 1, j - 1],
[i, j - 1], [i, j + 1], [i - 1, j - 1], [i - 1, j],
[i - 1, j + 1])
for k in possible_positions:
if is_neighbour([i, j], k):
if possible_action[str(i) + " " + str(j)]:
possible_action[str(i) + " " + str(j)].append(k)
else:
possible_action[str(i) + " " + str(j)] = []
possible_action[str(i) + " " + str(j)].append(k)
if possible_action != {}:
keys = possible_action.keys()
key = random.choice(keys)
attacked_position = random.choice(possible_action[key])
attacker_position = [int(key.split(" ")[0]), int(key.split(" ")[1])]
return attacker_position, attacked_position
return [], []
def is_ghost_exists(game_board, ghost):
return game_board[ghost[1]][ghost[0]] == "ghost"
def is_empty_box(game_board, box_position):
case = game_board[box_position[1]][box_position[0]]
if case["type"] not in ["ghost"]:
return 1
else:
return 0
def is_neighbour(pos, new_position):
print(new_position)
if new_position in (
[pos[0], pos[1]], [pos[0] + 1, pos[1] + 1], [pos[0] + 1, pos[1]], [pos[0] + 1, pos[1] - 1], [pos[0], pos[1] - 1],
[pos[0], pos[1] + 1], [pos[0] - 1, pos[1] - 1], [pos[0] - 1, pos[1]], [pos[0] - 1, pos[1] + 1]):
return 1
else:
return 0
Editor is loading...