9from datetime
import datetime
11from convert_data
import ConvertData
12from global_variables
import GlobalVariables, Commands, Items
13from custom_functions
import pinfo, psuccess, perror, pdebug, pwarning
18 This is the class in charge of containing the ai which provides a purpose for this program
21 Thread (_type_): _description_: The class that is to be used in order to start threads
24 def __init__(self, global_variables: GlobalVariables) ->
None:
46 Generate a random seed
50 c_time = datetime.now()
51 final_seed = c_time.hour + c_time.minute + c_time.second + c_time.microsecond
57 Add the forward command
60 data=self.
global_variables.translation_reference.text_equivalence[Commands.FORWARD],
64 output = e.to_external()
73 data=self.
global_variables.translation_reference.text_equivalence[Commands.LEFT],
77 output = e.to_external()
86 data=self.
global_variables.translation_reference.text_equivalence[Commands.RIGHT],
90 output = e.to_external()
94 def _my_randint(self, minimum: int = 0, maximum: int = 3) -> int:
96 This is a function that will be used in order to provide a randome number without using the random library.
100 while counter < artificial_turn:
102 c_time = datetime.now()
103 current_time = c_time.hour + c_time.minute + c_time.second + c_time.microsecond
104 final_number = (current_time + self.
random_seed) % (maximum + 1)
105 if minimum <= final_number <= maximum:
112 This function is the one in charge of creating a random movement
117 f
"Selected direction: {self.direction_options_list[move]}"
123 This function works like a onetime valve, if one error ever gets raised, the variable will never go back to 0.
125 status (int): _description_: the run status of the previous function
130 f
"The global status if not a success, error code: {status}"
137 This function will set the variables corresponding to the initialisation process
139 int: _description_: The processing status
141 if "WELCOME" in data:
144 "Welcome key received"
153 f
"(logistics): response = {data}, nb_responses = {self.nb_responses}"
155 self.
_exit_error(
"Did not receive the expected welcome message")
160 This function will check if we have received the client number
162 data (str): _description_: The incoming data
165 int: _description_: The status of the processed data
168 response = f
"Client number cannot be empty.\nYou entered:'{data}'"
171 if isinstance(data, (dict)):
172 data = data[list(data)[0]]
173 if isinstance(data, list):
182 This is a function that will make sure that the dimensions of the map.
184 data (str): _description_
190 response = f
"Map dimensions cannot be empty.\nYou entered:'{data}'"
193 dimensions = data.split(
" ")
194 if len(dimensions) > 0
and dimensions[0] ==
'':
196 if len(dimensions) > 0
and dimensions[-1] ==
'':
198 if len(dimensions) == 2:
199 if dimensions[0].isnumeric()
and dimensions[1].isnumeric():
201 int(dimensions[0]), int(dimensions[1]))
204 f
"Client coordinates = {self.client_coordinates}"
210 response = f
"Map dimensions cannot be empty.\nYou entered:'{data}'"
216 This function checks if the ai can evolve.
218 command (str | list): _description_: The content of the Look response
221 bool: _description_: Returns True if the ai can get to the next level, otherwise False is returned.
226 f
"(logistic) _can_evolve: max_i = {max_i}, Provided command = {command}"
231 "(logistics) _can_evolve: The command is empty"
234 if Items.LINEMATE
in command
or "linemate" in command
or "Linemate" in command:
237 "The item Linemate was found on the ground"
242 "(logistics) _can_evolve: The Linemate item is not found"
248 This function should never be triggered in a normal use case.
249 This function will send an error message and set the global status to self.global_variable.error
251 int: _description_: The error status defined in global_variables
261 This function appends the look command to the response buffer.
264 data=self.
global_variables.translation_reference.text_equivalence[Commands.LOOK],
268 output = e.to_external()
274 Replace the code with the corresponding state.
276 code (int): _description_: The corresponding code
279 str: _description_: The corresponding text that the human can understand
287 This function will calculate the next move based on the response from the server.
290 response (dict[Commands, any]): _description_: The query that came in from the tcp protocol.
293 int: _description_: The status of the execution.
302 if response[list(response)[0]]
in (
"ko",
"KO",
"Ko",
"kO"):
305 f
"Command = '{self._to_human_readable(list(response)[0])}' failed"
310 if Commands.LOOK
in response:
313 if self.
_can_evolve(response[Commands.LOOK])
is True:
325 elif Commands.LEFT
in response
and response[Commands.LEFT].lower() ==
"ok":
328 elif Commands.RIGHT
in response
and response[Commands.RIGHT].lower() ==
"ok":
331 elif Commands.FORWARD
in response
and response[Commands.FORWARD].lower() ==
"ok":
334 elif Commands.INCANTATION
in response
and response[Commands.INCANTATION].lower() ==
"ok":
347 response (str): _description_: The converted response
350 int: _description_: The status response of the program
356 if Commands.UNKNOWN
in response
and self.
nb_responses == 1:
368if __name__ ==
"__main__":
369 print(
"This script is not meant to be run as main.")
392 GI.current_buffer.append(
ConvertData(
"WELCOME").to_internal())
395 GI.colourise_output.init_pallet()
396 GI.colourise_error.init_pallet()
397 while GI.continue_running
is True:
398 if len(GI.sender_data) == 0:
400 if GI.response_buffer[0] == NAME:
401 GI.current_buffer.append(
404 GI.current_buffer.append(
407 if GI.response_buffer[0] == FORWARD:
408 GI.current_buffer.append(
ConvertData(
"ok\n").to_internal())
409 if GI.response_buffer[0] == LEFT:
410 GI.current_buffer.append(
ConvertData(
"ok\n").to_internal())
411 if GI.response_buffer[0] == RIGHT:
412 GI.current_buffer.append(
ConvertData(
"ok\n").to_internal())
413 if LIFE_INDEX == LIFE_LENGTH:
414 GI.current_buffer.append(
ConvertData(
"Dead").to_internal())
416 pinfo(GI, f
"Program stopped with status: {GI.current_status}")
417 GI.colourise_output.unload_ressources()
418 sys.exit(GI.current_status)
None _append_look_command(self)
None _change_my_mind(self)
int _process_client_number(self, str data)
None __init__(self, GlobalVariables global_variables)
int _process_map_dimensions(self, str data)
int dispatcher(self, dict[Commands, any] response)
int _my_randint(self, int minimum=0, int maximum=3)
None _update_ai_status(self, int status)
str _to_human_readable(self, int code)
int _calculate_next_move(self, dict[Commands, any] response)
int _process_welcome(self, str data)
bool _can_evolve(self, str command)
int _exit_error(self, str string)
int _create_random_seed(self)