diff --git a/search/main.py b/search/main.py index f252d304d4a5759497bd5cf1e1af5e8ffc1f0bd0..5a067833529a7c47de513cd2c2a0a6acaf258df8 100644 --- a/search/main.py +++ b/search/main.py @@ -12,6 +12,7 @@ import json # If you want to separate your code into separate files, put them # inside the `search` directory (like this one and `util.py`) and # then import from them like this: +from search.movement_logic import * from search.util import print_board, print_slide, print_swing # Why don't we just make the pieces and blocks global then? @@ -19,6 +20,8 @@ from search.util import print_board, print_slide, print_swing dictpieces = {} setblocks = set() +dicttargets = {} + def main(): try: with open(sys.argv[1]) as file: @@ -38,8 +41,12 @@ def main(): # This is greedy, but it's a lot faster than trying to path optimally. # And for search algorithm choice let's try starting with depth-first search, depth limit 1, 2 or 3 # Not sure which is best at the moment, looking ahead is good but looking too far ahead costs too much time + # We'll use a dictionary to track current targets # ALGORITHM GOES HERE + # Add starting targets + + # Algorithm start @@ -94,38 +101,60 @@ def parse_input(data): # Would be nice to use comparator but can't have r>s>p>r using it # Return 1 if piecea wins, 2 if pieceb wins and 0 if neither win # Only look at first character of string -# so we don't have to worry about stripping numbers off def piece_collision(piecea, pieceb) -> int: - piecea = piecea.lower() - pieceb = pieceb.lower() - if piecea[0] == "r": - if pieceb[0] == "s": + piecea = piecea[0].lower() + pieceb = pieceb[0].lower() + if piecea == "r": + if pieceb == "s": return 1 - elif pieceb[0] == "p": + elif pieceb == "p": return 2 - elif piecea[0] == "s": - if pieceb[0] == "p": + elif piecea == "s": + if pieceb == "p": return 1 - elif pieceb[0] == "r": + elif pieceb == "r": return 2 - elif piecea[0] == "p": - if pieceb[0] == "r": + elif piecea == "p": + if pieceb == "r": return 1 - elif pieceb[0] == "s": + elif pieceb == "s": return 2 return 0 -# We may have to edit this to strip numbers later + +# Input is the key of the piece to find a target for +# This function changes the value of the key given in the +# target dictionary to the closest enemy piece it can beat +def find_target(piece_key): + currentpos = dictpieces[piece_key] + # Set the target + target = "" + if piece_key[0] == "R": + target = "s" + elif piece_key[0] == "S": + target = "p" + elif piece_key[0] == "P": + target = "r" + # If we haven't set a target by now, we're dealing with the wrong kind of piece + else: + return + #Now we check which target is closest + #Start with dummy information + targetpiece = "" + targetdist = 99 + for key in dictpieces: + if key[0] == target: + distance = distance_between(dictpieces[piece_key],dictpieces[key]) + if distance < targetdist: + targetpiece = key + targetdist = distance + # Now add target to dictionary + if targetdist == 99: + dicttargets[piece_key] = () + else: + dicttargets[piece_key] = dictpieces[key] + return + -# We will have dictionary containing pieces -# This is so we don't have to search the entire board every time -# we need to find pieces -# Blocks can be stored in a separate list -# Other option is to have dictionary of entire board -#Sample dictionary -sample_dict = { - "R": (0, -2), - "s": (-1, 1) -}