Skip to content

Commit

Permalink
Copied to base_client_2
Browse files Browse the repository at this point in the history
  • Loading branch information
brandongasser committed Dec 29, 2023
1 parent 0dfa0a7 commit df6596a
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions base_client_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from game.client.user_client import UserClient
from game.common.enums import *
from game.utils.vector import Vector


class State(Enum):
Expand All @@ -19,7 +20,7 @@ def team_name(self):
Allows the team to set a team name.
:return: Your team name
"""
return 'Amanda and the Carsons'
return 'Unpaid Intern'

def first_turn_init(self, world, avatar):
"""
Expand All @@ -28,9 +29,10 @@ def first_turn_init(self, world, avatar):
self.company = avatar.company
self.my_station_type = ObjectType.TURING_STATION if self.company == Company.TURING else ObjectType.CHURCH_STATION
self.current_state = State.MINING
self.base_position = self.find_all_by_type(world, self.my_station_type)[0]

# This is where your AI will decide what to do
def take_turn(self, turn, actions, world, avatar):
def take_turn(self, turn: int, actions: ActionType, world, avatar):
"""
This is where your AI will decide what to do.
:param turn: The current turn of the game.
Expand All @@ -47,19 +49,56 @@ def take_turn(self, turn, actions, world, avatar):
self.current_state = State.MINING

# Make action decision for this turn
# if self.current_state == State.SELLING:
# actions = [ActionType.MOVE_LEFT if self.company == Company.TURING else ActionType.MOVE_RIGHT] # If I'm selling, move towards my base
# else:
if current_tile.occupied_by.object_type == ObjectType.ORE_OCCUPIABLE_STATION:
# If I'm mining and I'm standing on an ore, mine it and set my state to selling
actions = [ActionType.MINE]
self.current_state = State.SELLING
if self.current_state == State.SELLING:
# actions = [ActionType.MOVE_LEFT if self.company == Company.TURING else ActionType.MOVE_RIGHT] # If I'm selling, move towards my base
actions = self.generate_moves(avatar.position, self.base_position, turn % 2 == 0)
else:
# If I'm mining and I'm not standing on an ore, move away from my station to try to find an ore
actions = [self.move_logic()]
# actions = [ActionType.MOVE_RIGHT if self.company == Company.TURING else ActionType.MOVE_LEFT]

if current_tile.occupied_by.object_type == ObjectType.ORE_OCCUPIABLE_STATION:
# If I'm mining and I'm standing on an ore, mine it and set my state to selling
actions = [ActionType.MINE]
self.current_state = State.SELLING
else:
# If I'm mining and I'm not standing on an ore, move randomly
actions = [random.choice([ActionType.MOVE_LEFT, ActionType.MOVE_RIGHT, ActionType.MOVE_UP, ActionType.MOVE_DOWN])]

return actions

def move_logic(self):
return random.choice([ActionType.MOVE_LEFT, ActionType.MOVE_RIGHT, ActionType.MOVE_UP, ActionType.MOVE_DOWN])
def generate_moves(self, start_position, end_position, vertical_first):
"""
This function will generate a path between the start and end position. It does not consider walls and will
try to walk directly to the end position.
:param start_position: Position to start at
:param end_position: Position to get to
:param vertical_first: True if the path should be vertical first, False if the path should be horizontal first
:return: Path represented as a list of ActionType
"""
dx = end_position.x - start_position.x
dy = end_position.y - start_position.y

horizontal = [ActionType.MOVE_LEFT] * -dx if dx < 0 else [ActionType.MOVE_RIGHT] * dx
vertical = [ActionType.MOVE_UP] * -dy if dy < 0 else [ActionType.MOVE_DOWN] * dy

return vertical + horizontal if vertical_first else horizontal + vertical

def find_all_by_type(self, world, object_type):
"""
Finds all tiles on the board with the given object_type
:param world: Generic world information
:param object_type: The desired object type
:return: List of all Vectors for positions with Tiles with the object type
"""
return self.find_all(world, lambda tile: tile.occupied_by is not None and tile.occupied_by.object_type == object_type)

def find_all(self, world, criteria):
"""
Finds all tiles on the board that match the given criteria
:param world: Generic world information
:param criteria: The required criteria as a function that takes a Tile and returns a bool
:return: List of all Vectors for positions with Tiles that meet the criteria
"""
result = []
for y in range(len(world.game_map)):
for x in range(len(world.game_map[y])):
if criteria(world.game_map[y][x]):
result.append(Vector(x=x, y=y))
return result

0 comments on commit df6596a

Please sign in to comment.