Commit 3a85c10d authored by Ahmad Reza Cheraghi's avatar Ahmad Reza Cheraghi
Browse files

some code cleaning based on the results of sonarqube

parent f688e95e
......@@ -31,7 +31,7 @@ read = 0
write = 1
particle_counter=0
class Particle(matter.matter):
class Particle(matter.Matter):
"""In the classe location all the methods for the characterstic of a location is included"""
def __init__(self, sim, x, y, color=black, alpha=1, mm_limit=0, mm_size=0):
......@@ -118,10 +118,9 @@ class Particle(matter.matter):
:return: True: Success Moving; False: Non moving
"""
dir_coord = self.sim.get_coords_in_dir(self.coords, dir)
#sim = self.sim_to_coords(dir_coord[0], dir_coord[1])
#print ("sim actual coord "+ str(sim))
if self.sim.border==1:
if abs(dir_coord[0]) > self.sim.get_sim_x_size() or abs(dir_coord[1]) > self.sim.get_sim_y_size() :
if self.sim.border==1 and (abs(dir_coord[0]) > self.sim.get_sim_x_size() \
or abs(dir_coord[1]) > self.sim.get_sim_y_size()) :
dir = dir - 3 if dir > 2 else dir + 3
dir_coord = self.sim.get_coords_in_dir(self.coords, dir)
if self.sim.check_coords(dir_coord[0], dir_coord[1]):
......@@ -244,15 +243,12 @@ class Particle(matter.matter):
def get_matter_in_dir(self, matter="tile", dir=E):
if matter=="tile":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="tile" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
def get_tile_in(self, dir=E):
......@@ -312,28 +308,12 @@ class Particle(matter.matter):
def get_matter_in_dir(self, matter="tile", dir=E):
if matter=="tile":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="tile" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
def scan_for_matter_within(self, matter='all', hop=1):
"""
......@@ -377,33 +357,22 @@ class Particle(matter.matter):
y_scan_coord_pos = self.coords[1] + cnt
if cnt == 0:
if matter == "particles":
# if self.coords in self.sim.particle_map_coords:
# hop_list.append(self.sim.particle_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.particle_map_coords:
hop_list.append(self.sim.particle_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.particle_map_coords:
hop_list.append(self.sim.particle_map_coords[(x_scan_neg, y_scan_coord_neg)])
elif matter == "locations":
# if self.coords in self.sim.location_map_coords:
# hop_list.append(self.sim.location_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.location_map_coords:
hop_list.append(self.sim.location_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.location_map_coords:
hop_list.append(self.sim.location_map_coords[(x_scan_neg, y_scan_coord_neg)])
elif matter == "tiles":
# if self.coords in self.sim.tile_map_coords:
# hop_list.append(self.sim.tile_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.tile_map_coords:
hop_list.append(self.sim.tile_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.tile_map_coords:
hop_list.append(self.sim.tile_map_coords[(x_scan_neg, y_scan_coord_neg)])
elif matter == "all":
# if self.coords in self.sim.tile_map_coords:
# hop_list.append(self.sim.tile_map_coords[self.coords])
# if self.coords in self.sim.location_map_coords:
# hop_list.append(self.sim.location_map_coords[self.coords])
# if self.coords in self.sim.particle_map_coords:
# hop_list.append(self.sim.particle_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.tile_map_coords:
hop_list.append(self.sim.tile_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.tile_map_coords:
......@@ -805,134 +774,112 @@ class Particle(matter.matter):
self.__isCarried = False
self.touch()
def create_tile(self, color=gray, alpha=1):
"""
Creates a tile on the particles actual position
:return: None
"""
logging.info("Going to create a tile on position %s", str(self.coords))
self.sim.add_tile(self.coords[0], self.coords[1], color, alpha)
self.sim.tile_map_coords[self.coords[0], self.coords[1]].created = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
def create_tile_in(self, dir=None, color=gray, alpha=1):
"""
Creates a tile either in a given direction
:param dir: The direction on which the tile should be created. Options: E, SE, SW, W, NW, NE,
:return: None
"""
logging.info("particle with id %s is", self.get_id())
logging.info("Going to create a tile in %s ", str(dir))
if dir != None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
def create_tile(self, color=gray, alpha=1):
"""
Creates a tile on the particles actual position
:return: None
"""
logging.info("Going to create a tile on position %s", str(self.coords))
self.sim.add_tile(self.coords[0], self.coords[1], color, alpha)
self.sim.tile_map_coords[self.coords[0], self.coords[1]].created = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
def create_tile_in(self, dir=None, color=gray, alpha=1):
"""
Creates a tile either in a given direction
:param dir: The direction on which the tile should be created. Options: E, SE, SW, W, NW, NE,
:return: None
"""
logging.info("particle with id %s is", self.get_id())
logging.info("Going to create a tile in %s ", str(dir))
if dir != None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
if self.sim.add_tile(coords[0], coords[1], color, alpha) == True:
self.sim.tile_map_coords[coords[0], coords[1]].created = True
logging.info("Tile is created")
self.sim.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
else:
logging.info("Not created tile ")
def create_tile_on(self, x=None, y=None, color=gray, alpha=1):
"""
Creates a tile either on a given x,y coordinates
:param x: x coordinate
:param y: y coordinate
:return: None
"""
logging.info("particle with id %s is", self.get_id())
if x is not None and y is not None:
coords = (x, y)
if self.sim.check_coords(x, y):
logging.info("Going to create a tile on position \(%i , %i\)", x, y)
if self.sim.add_tile(coords[0], coords[1], color, alpha) == True:
self.sim.tile_map_coords[coords[0], coords[1]].created = True
logging.info("Tile is created")
self.sim.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
else:
logging.info("Not created tile ")
def create_tile_on(self, x=None, y=None, color=gray, alpha=1):
"""
Creates a tile either on a given x,y coordinates
:param x: x coordinate
:param y: y coordinate
:return: None
"""
logging.info("particle with id %s is", self.get_id())
if x is not None and y is not None:
coords = (x, y)
if self.sim.check_coords(x, y):
logging.info("Going to create a tile on position \(%i , %i\)", x, y)
if self.sim.add_tile(coords[0], coords[1], color, alpha) == True:
self.sim.tile_map_coords[coords[0], coords[1]].created = True
self.sim.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
return True
else:
logging.info("Not created tile on coords \(%i , %i\)", y, x)
return False
else:
logging.info("Not created tile on coords \(%i , %i\)", y, x)
def delete_tile(self):
"""
Deletes a tile on current position
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile on current position")
if self.coords in self.sim.get_tile_map_coords():
if self.sim.remove_tile_on(self.coords):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Not created tile on coords \(%i , %i\)", y, x)
return False
else:
logging.info("Could not delet tile")
return False
logging.info("Not created tile on coords \(%i , %i\)", y, x)
def delete_tile_with(self, id):
"""
Deletes a tile with a given tile-id
def delete_tile(self):
"""
Deletes a tile on current position
:param tile_id: The id of the tile that should be deleted
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile with tile id %s", str(id))
if self.sim.remove_tile(id):
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile on current position")
if self.coords in self.sim.get_tile_map_coords():
if self.sim.remove_tile_on(self.coords):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile with tile id %s", str(id))
return False
else:
logging.info("Could not delet tile")
return False
def delete_tile_in(self, dir=E):
"""
Deletes a tile either in a given direction
:param dir: The direction on which the tile should be deleted. Options: E, SE, SW, W, NW, NE,
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if -1 < dir < 7:
coords = self.sim.get_coords_in_dir(self.coords, dir)
logging.info("Deleting tile in %s direction", str(dir))
if coords is not None:
if self.sim.remove_tile_on(coords):
logging.info("Deleted tile with tile on coords %s", str(coords))
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
def delete_tile_with(self, id):
"""
Deletes a tile with a given tile-id
def delete_tile_on(self, x=None, y=None):
"""
Deletes a tile either on a given x,y coordinates
,
:param x: x coordinate
:param y: y coordinate
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if x is not None and y is not None:
coords = (x, y)
:param tile_id: The id of the tile that should be deleted
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile with tile id %s", str(id))
if self.sim.remove_tile(id):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile with tile id %s", str(id))
return False
def delete_tile_in(self, dir=E):
"""
Deletes a tile either in a given direction
:param dir: The direction on which the tile should be deleted. Options: E, SE, SW, W, NW, NE,
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if -1 < dir < 7:
coords = self.sim.get_coords_in_dir(self.coords, dir)
logging.info("Deleting tile in %s direction", str(dir))
if coords is not None:
if self.sim.remove_tile_on(coords):
logging.info("Deleted tile with tile on coords %s", str(coords))
self.csv_particle_writer.write_particle(tile_deleted=1)
......@@ -940,77 +887,128 @@ class Particle(matter.matter):
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
def delete_tile_on(self, x=None, y=None):
"""
Deletes a tile either on a given x,y coordinates
,
:param x: x coordinate
:param y: y coordinate
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if x is not None and y is not None:
coords = (x, y)
if self.sim.remove_tile_on(coords):
logging.info("Deleted tile with tile on coords %s", str(coords))
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
def take_tile(self):
"""
Takes a tile on the actual position
def take_tile(self):
"""
Takes a tile on the actual position
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[self.coords]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile has been taken")
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile could not be taken")
return False
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[self.coords]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile has been taken")
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("No tile on the actual position not in the sim")
logging.info("Tile could not be taken")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
logging.info("No tile on the actual position not in the sim")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
return False
def take_tile_with(self, id):
"""
Takes a tile with a given tile id
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if id in self.sim.tile_map_id:
logging.info("Tile with tile id %s is in the sim", str(id))
self.carried_tile = self.sim.tile_map_id[id]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(id))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile with tile id %s could not be taken", str(id))
return False
def take_tile_with(self, id):
"""
Takes a tile with a given tile id
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if id in self.sim.tile_map_id:
logging.info("Tile with tile id %s is in the sim", str(id))
self.carried_tile = self.sim.tile_map_id[id]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(id))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile with tile id %s is not in the sim", str(id))
logging.info("Tile with tile id %s could not be taken", str(id))
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle", str(id))
logging.info("Tile with tile id %s is not in the sim", str(id))
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle", str(id))
return False
def take_tile_in(self, dir):
"""
Takes a tile that is in a given direction
def take_tile_in(self, dir):
"""
Takes a tile that is in a given direction
:param dir: The direction on which the tile should be taken. Options: E, SE, SW, W, NW, NE,
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
:param dir: The direction on which the tile should be taken. Options: E, SE, SW, W, NW, NE,
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
if coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[coords]
logging.info("Tile with tile id %s is in the sim", str(self.carried_tile.get_id()))
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
return False
else:
logging.info("Tile is not in the sim")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
return False
def take_tile_on(self, x=None, y=None):
"""
Takes a tile that is in a given direction
:param x: x coordinate
:param y: y coordinate
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.sim.check_coords(x, y):
coords = (x, y)
if coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[coords]
logging.info("Tile with tile id %s is in the sim", str(self.carried_tile.get_id()))
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
return True
else:
logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
......@@ -1019,73 +1017,72 @@ class Particle(matter.matter):
logging.info("Tile is not in the sim")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
logging.info("Coordinates are wrong")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
return False
def take_tile_on(self, x=None, y=None):
"""
Takes a tile that is in a given direction
def drop_tile(self):
"""
Drops the taken tile on the particles actual position
:param x: x coordinate
:param y: y coordinate
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.sim.check_coords(x, y):
coords = (x, y)
if coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[coords]
logging.info("Tile with tile id %s is in the sim", str(self.carried_tile.get_id()))
if self.carried_tile.take(coords=self.coords):
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
return True
else:
logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
return False
else:
logging.info("Tile is not in the sim")
return False
else:
logging.info("Coordinates are wrong")
return False
:return: None
"""
if self.carried_tile is not None:
if self.coords not in self.sim.tile_map_coords:
try: # cher: insert so to overcome the AttributeError
self.carried_tile.drop_me(self.coords)
except AttributeError:
pass
self.carried_tile = None
logging.info("Tile has been dropped on the actual position")
self.sim.csv_round_writer.update_metrics(tiles_dropped=1)
self.csv_particle_writer.write_particle(tiles_dropped=1)
return True
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
logging.info("Is not possible to drop the tile on that position because it is occupied")
return False
else:
return False
def drop_tile(self):
"""
Drops the taken tile on the particles actual position
def drop_tile_in(self, dir):
"""
Drops the taken tile on a given direction
:return: None
"""
if self.carried_tile is not None:
if self.coords not in self.sim.tile_map_coords:
try: # cher: insert so to overcome the AttributeError
self.carried_tile.drop_me(self.coords)
except AttributeError:
pass
self.carried_tile = None