def power_up(self, arena, power_symbol): """ Take appropriate actions depending on the Power up symbol. """ if power_symbol == RESET: # if the map cell is a bot symbol and not the head of the trail # ie, not the current position of the bike itself, delete all the map_size = arena.map.size for i in range(map_size): for j in range(map_size): position = Position(i, j, map_size, map_size) if (arena.map.get_symbol(position) in [BIKE_1_SYMBOL, BIKE_2_SYMBOL] and (arena.bikes[0].curr_posn != position) and (arena.bikes[1].curr_posn != position)): arena.map.set_symbol(position, EMPTY) elif power_symbol == TRAVERSER: self.bot.traverser_left += TRAVERSER_CAPACITY + 1 # The + 1 is necessary because later, during this move, # it is going to be decreased by 1. elif power_symbol == NITRO: self.bot.nitro_left += NITRO_QUANTITY + 1
def BuildInitialWindow(grid): windowGrid = [] for i in range(Commons.GAME_ROWS): windowGrid.append([]) for j in range(Commons.GAME_ROWS): if grid[i][j] == 1: color = Commons.LIGHT_GREEN weight = 1 elif grid[i][j] == 2: color = Commons.LIGHT_BROWN weight = 5 elif grid[i][j] == 3: color = Commons.LIGHT_BLUE weight = 10 elif grid[i][j] == 4: color = Commons.LIGHT_RED weight = 15 position = Position.Position(i, j, color, weight) windowGrid[i].append(position) return windowGrid
def update_vector_calc(self): """ This method returns a position vector, where; - the x and y values are in pixels per second - the angle value is in degrees per second """ upd_vec = Position.Position() upd_vec.x = (self.new_pos.x - self.start_pos.x) / self.duration upd_vec.y = (self.new_pos.y - self.start_pos.y) / self.duration if self.new_pos.angle - self.start_pos.angle >= 180.0: print("large positive angle change") upd_vec.angle = (self.new_pos.angle - 360.0 - self.start_pos.angle) / self.duration elif self.new_pos.angle - self.start_pos.angle <= -180.0: print("large negative angle change") upd_vec.angle = (self.new_pos.angle + 360.0 - self.start_pos.angle) / self.duration else: upd_vec.angle = (self.new_pos.angle - self.start_pos.angle) / self.duration print(type(self), self.foot, self.start_pos.print(), self.new_pos.print()) return upd_vec
def getNeighbourPaths(self, pos, paths): x = pos.getX() y = pos.getY() z = pos.getZ() neighbouringPaths = [] # look in all directions options = [[x + 1, y, z], [x - 1, y, z], [x, y + 1, z], [x, y - 1, z], [x, y, z + 1], [x, y, z - 1]] for option in options: x = option[0] y = option[1] z = option[2] if x <= self.length - 1 and y <= self.width - 1 and z <= self.height - 1 and x >= 0 and y >= 0 and z >= 0 and self.grid[ x][y][z] == 2: neighbouringPaths.append( self.searchPath(Position.Position(x, y, z), paths)) if len(neighbouringPaths) == 0: return None return neighbouringPaths
def __init__(self, leader=None): """ initialize the dancer. """ # the current position of the dancer's feet self.position = [Position.Position(), Position.Position()] # the update vector of the dancer's feet. in pixels per second and degrees per second self.delta_pos = [Position.Position(), Position.Position()] # the position of the dancer's feet at the end of the next step self.next_pos = [Position.Position(), Position.Position()] # the texture images to draw for the dancer's feet. self.free_foot_texture = [None, None] self.supporting_foot_texture = [None, None] # indicate what foot will move next self.free_foot = None # a reference to the leader. If this dancer is the leader, the variable is set to None self.leader = leader
def __init__(self): self.BlockData = None self.Position = Position.Position() self.Rotation = 0 self.EffectData = ()
def getPrint2(self): return [ Position.Position(1, 1, 0), Position.Position(4, 1, 0), Position.Position(7, 1, 0), Position.Position(10, 1, 0), Position.Position(13, 1, 0), Position.Position(16, 1, 0), Position.Position(1, 4, 0), Position.Position(4, 4, 0), Position.Position(7, 4, 0), Position.Position(10, 4, 0), Position.Position(13, 4, 0), Position.Position(16, 4, 0), Position.Position(1, 7, 0), Position.Position(4, 7, 0), Position.Position(7, 7, 0), Position.Position(10, 7, 0), Position.Position(13, 7, 0), Position.Position(16, 7, 0), Position.Position(1, 10, 0), Position.Position(4, 10, 0), Position.Position(7, 10, 0), Position.Position(10, 10, 0), Position.Position(13, 10, 0), Position.Position(16, 10, 0), Position.Position(8, 12, 0) ]
def do_move(self, P_o, P_n, S_i): """Move piece from Position Orig (P_o) to Position New (P_n) """ # Get information about P_o R_P_o = self.get_piece(P_o) if R_P_o[0]: # Get object and color of piece at P_o Piece = R_P_o[1] white_move = Piece.color # Check if P_n is a valid move for Piece at P_o if Piece.valid_move(P_n): # Check for castling if Piece.type == 'P': Piece_t = None R_P_n = self.get_piece(P_n) # Check if there is a piece at P_n take = R_P_n[0] if take: # Get object of piece at P_n Piece_t = R_P_n[1] Piece_t = None # Get information about P_n R_P_n = self.get_piece(P_n) # Check if there is a piece at P_n take = R_P_n[0] if take: # Get object of piece at P_n Piece_t = R_P_n[1] # Make proposed move from P_o to P_n self.set_piece(P_n, Piece) self.set_piece(P_o, None) # Check if the current Player's king is now in check check_status = self.check_any() mate_status = self.mate_any() if (white_move and check_status['w'] or not white_move and check_status['b']): # Revert move and return false self.set_piece(P_o, Piece) self.set_piece(P_n, None) if take: self.set_piece(P_n, Piece_t) return False if take: self.taken.append(Piece_t) checks = True in map(lambda c_s: c_s[1], check_status.items()) mates = True in map(lambda m_s: m_s[1], mate_status.items()) move = History.Move.Move(white_move, Piece, P_o, P_n, True, Piece_t, checks, mates, None) self.log.add_move(move) return True else: ep_check = abs(P_n.y - P_o.y) == 1 and abs(P_n.x - P_o.x) == 1 if ep_check: P_ep_p = P(P_n.x, P_o.y) ep_p = self.get_piece(P_ep_p)[1] self.set_piece(P_n, Piece) self.set_piece(P_o, None) self.set_piece(P_ep_p, None) # Check if the current Player's king is now in check check_status = self.check_any() mate_status = self.mate_any() if (white_move and check_status['w'] or not white_move and check_status['b']): # Revert move and return false self.set_piece(P_o, Piece) self.set_piece(P_n, None) self.set_piece(P_ep_p, ep_p) return False self.taken.append(ep_p) checks = True in map(lambda c_s: c_s[1], check_status.items()) mates = True in map(lambda m_s: m_s[1], mate_status.items()) move = History.Move.Move(white_move, Piece, P_o, P_n, True, ep_p, checks, mates, None) self.log.add_move(move) return True else: Piece_t = None # Get information about P_n R_P_n = self.get_piece(P_n) # Check if there is a piece at P_n take = R_P_n[0] if take: # Get object of piece at P_n Piece_t = R_P_n[1] # Make proposed move from P_o to P_n self.set_piece(P_n, Piece) self.set_piece(P_o, None) # Check if the current Player's king is now in check check_status = self.check_any() mate_status = self.mate_any() if (white_move and check_status['w'] or not white_move and check_status['b']): # Revert move and return false self.set_piece(P_o, Piece) self.set_piece(P_n, None) if take: self.set_piece(P_n, Piece_t) return False if take: self.taken.append(Piece_t) checks = True in map(lambda c_s: c_s[1], check_status.items()) mates = True in map(lambda m_s: m_s[1], mate_status.items()) move = History.Move.Move(white_move, Piece, P_o, P_n, True, Piece_t, checks, mates, None) self.log.add_move(move) return True elif Piece.type == 'K': castle_check = abs(P_n.x - P_o.x) == 2 if castle_check: # Check if last move was check if self.log.get_last_move().check: return False else: # Move king self.set_piece(P_n, Piece) self.set_piece(P_o, None) # Compute P_o and P_n for rook, as well as side castle_type = '' if (P_n.x > P_o.x): P_rook_o = Position.Position(7, P_o.y) P_rook_n = Position.Position(4, P_o.y) if white_move: castle_type = 'Q' else: castle_type = 'K' else: P_rook_o = Position.Position(0, P_o.y) P_rook_n = Position.Position(2, P_o.y) if white_move: castle_type = 'K' else: castle_type = 'Q' # Move rook self.set_piece(P_rook_n, self.get_piece(P_rook_o)) self.set_piece(P_rook_o, None) check_status = self.check_any() mate_status = self.check_any() checks = True in map(lambda c_s: c_s[1], check_status.items()) mates = True in map(lambda m_s: m_s[1], mate_status.items()) if (white_move and check_status['w'] or not white_move and check_status['b']): # Revert move and return false self.set_piece(P_rook_o, self.get_piece(P_rook_n)) self.set_piece(P_o, self.get_piece(P_n)) self.set_piece(P_rook_n, None) self.set_piece(P_n, None) return False move = History.Move.Move(white_move, Piece, P_o, P_n, False, None, checks, mates, castle_type) self.log.add_move(move) return True else: # Normal move Piece_t = None # Get information about P_n R_P_n = self.get_piece(P_n) # Check if there is a piece at P_n take = R_P_n[0] if take: # Get object of piece at P_n Piece_t = R_P_n[1] # Make proposed move from P_o to P_n self.set_piece(P_n, Piece) self.set_piece(P_o, None) # Check if the current Player's king is now in check check_status = self.check_any() mate_status = self.mate_any() if (white_move and check_status['w'] or not white_move and check_status['b']): # Revert move and return false self.set_piece(P_o, Piece) self.set_piece(P_n, None) if take: self.set_piece(P_n, Piece_t) return False if take: self.taken.append(Piece_t) checks = True in map(lambda c_s: c_s[1], check_status.items()) mates = True in map(lambda m_s: m_s[1], mate_status.items()) move = History.Move.Move(white_move, Piece, P_o, P_n, take, Piece_t, checks, mates, None) self.log.add_move(move) return True # Run normal moves else: Piece_t = None # Get information about P_n R_P_n = self.get_piece(P_n) # Check if there is a piece at P_n take = R_P_n[0] if take: # Get object of piece at P_n Piece_t = R_P_n[1] # Make proposed move from P_o to P_n self.set_piece(P_n, Piece) self.set_piece(P_o, None) # Check if the current Player's king is now in check check_status = self.check_any() mate_status = self.mate_any() if (white_move and check_status['w'] or not white_move and check_status['b']): # Revert move and return false self.set_piece(P_o, Piece) self.set_piece(P_n, None) if take: self.set_piece(P_n, Piece_t) return False if take: self.taken.append(Piece_t) checks = True in map(lambda c_s: c_s[1], check_status.items()) mates = True in map(lambda m_s: m_s[1], mate_status.items()) move = History.Move.Move(white_move, Piece, P_o, P_n, True, Piece_t, checks, mates, None) self.log.add_move(move) return True else: return False else: return False
def testPos_devientTouche(): pos = Position(5, 2) pos.devientTouche() return pos.touche()
def testPos_y(): pos = Position(10, 7) return pos.y() == 7
def __init__(self, x, y): self.position = Position.Position(x, y) self.walls = [True, True, True, True] #Left Top Right Bottom
def getPixelScale(self): return Position(self.PPM_X, self.PPM_Y)
def __init__(self, country): # Give each player a unique ID # self.id = len(players) # Generate a name self.forename = country.nameset.gen_forename() self.surname = country.nameset.gen_surname() self.country = country.nameset.get_country # Give our player a height, 'listed height', armspan, and standing reach # Height uses our predefined average and standard deviation self.height = round(random.normalvariate(avg_height, height_sd), 1) # NBA average "ape index" is 1.06, with a minimum of roughly 1.0 and a maximum of roughly 1.12 self.armspan = round( random.normalvariate(self.height * 1.06, (self.height * 0.06) / 3), 1) # Standing reach in the NBA has an average of (height+armspan)/1.5477. # The divisor of 1.5477 has a sd of 0.02251, due to some of players' heights being above their shoulders. self.reach = round((self.height + self.armspan) / random.normalvariate(1.5477, 0.02251), 1) # Listed height in basketball is the height in shoes, usually (somewhat randomly) rounded up. if self.height < 183: short_syndrome = random.random() * 0.4 * (183 - self.height) + 0.1 else: short_syndrome = 0 self.l_height = Imperial.from_metric_round( self.height + (random.random() * 5) + 1.27 + short_syndrome, 0) # Generate empty dictionaries for the player's skills and tendencies self.skills = {} self.tendencies = {} # For each attribute, generate random number between the minimum and maximum rating # The constants 'skills' and 'tendencies' are defined at the beginning of the code. for i in skills: self.skills[i] = random.randint(min_rating, max_rating) for i in tendencies: self.tendencies[i] = random.randint(min_rating, max_rating) # A player's 'height' (actually based on standing reach) will be one of our strongest indicators of position self.skills["ht"] = int( (self.reach - ((avg_height * 2.06) / 1.5477 - 4 * height_sd) / 8 * height_sd) * 100) # If ht is outside the range 0-100, set ht to 0 or 100 if self.skills["ht"] < 0: self.skills["ht"] = 0 elif self.skills["ht"] > 100: self.skills["ht"] = 100 # Give each player an average value of their ratings self.avg = int(BasketballAverages.average(self.skills) + 0.5) # Give each player a random pot that is greater than their avg self.pot = maths.ceil((random.random() * random.random() * (max_rating - self.avg)) + self.avg) # Create an empty list that will contain the players' game stats self.game_stats = [] # Determine the position of the player self.pos = Position(self) # Assign the player to first available team with an open spot valid_team = False i = 1 while not valid_team: # If there are less than 5 players on the team, assign the player if len(teams[i].players) < 5: teams[i].addplayer(self) self.team = i valid_team = True elif i == len(teams) - 1: teams[0].addplayer(self) self.team = 0 valid_team = True else: i += 1
def create_player(conn, country, team_id=0): forename = country.nameset.gen_forename() surname = country.nameset.gen_surname() # Give our player a height (listed), weight, barefoot height, armspan, and standing reach # Height uses our predefined average and standard deviation height = round(random.normalvariate(avg_height, height_sd), 1) # NBA average "ape index" is 1.06, with a minimum of roughly 1.0 and a maximum of roughly 1.12 armspan = round(random.normalvariate(height * 1.06, (height * 0.06) / 3), 1) # Standing reach in the NBA has an average of (height+armspan)/1.5477. # The divisor of 1.5477 has a sd of 0.02251, due to some of a player's height being above their shoulders. reach = round((height + armspan) / random.normalvariate(1.5477, 0.02251), 1) l_height = maths.floor(height + (random.random() * 5) + 1.27) l_height_feet = Imperial.from_metric_round(l_height, 0) # NBA average BMI is 24.88 with a sd of 1.6633 weight = round(random.normalvariate(24.9, 1.6633) * ((height / 100)**2), 1) # # Assign the player to first available team with an open spot # valid_team = False # i = 1 # while not valid_team: # # If there are less than 5 players on the team, assign the player # if len(teams[i].players) < 5: # teams[i].addplayer(self) # self.team = i # valid_team = True # elif i == len(teams) - 1: # teams[0].addplayer(self) # self.team = 0 # valid_team = True # else: # i += 1 sql = """INSERT INTO players (forename, surname, team_id) VALUES(?, ?, ?)""" cur = conn.cursor() cur.execute(sql, (forename, surname, team_id)) current_id = cur.lastrowid data = [ current_id, str(l_height_feet), weight, l_height, height, armspan, reach ] sql = """INSERT INTO physicals (id, height, weight, 'metric height', barefoot, armspan, reach) VALUES(?, ?, ?, ?, ?, ?, ?)""" cur.execute(sql, data) skill_dict = {} for i in skills: if i == "3pt": sql = "INSERT INTO skills (id, '3pt'" skill_dict[i] = (random.randint(min_rating, max_rating)) elif i == "speed": # Speed is related to weight (see Google Sheet) sql += ", " + i temp_rating = random.randint(min_rating, max_rating) if weight >= avg_weight: factor = (weight - avg_weight) / 17 skill_dict[i] = int(factor * random.random() * temp_rating + (1 - factor) * temp_rating) else: factor = (avg_weight - weight) / 17 rxn = random.random() skill_dict[i] = int(factor * (max_rating - (rxn * temp_rating)) + (1 - factor) * (max_rating - temp_rating)) elif i == "strength": # Strength is related to weight sql += ", " + i temp_rating = random.randint(min_rating, max_rating) if weight < avg_weight: factor = (avg_weight - weight) / 17 skill_dict[i] = int(factor * random.random() * temp_rating + (1 - factor) * temp_rating) else: factor = (weight - avg_weight) / 17 rxn = random.random() skill_dict[i] = int(factor * (max_rating - (rxn * temp_rating)) + (1 - factor) * (max_rating - temp_rating)) else: sql += ", " + i skill_dict[i] = (random.randint(min_rating, max_rating)) sql += ", avg, ovr, pot, grade)\n" skill_dict["avg"] = int(BasketballAverages.average_dict(skill_dict)) # Overall rating, roughly scaled like 2K skill_dict["ovr"] = (int(((skill_dict["avg"] - 55) * 1) + 75.5)) # Give each player a random pot that is greater than their ovr skill_dict["pot"] = (maths.ceil((random.random() * random.random() * (max_rating - skill_dict["ovr"])) + skill_dict["ovr"])) # Give each player a grade based on their ovr and pot temp_grade = (skill_dict["pot"] + skill_dict["ovr"]) / 2 if temp_grade > 90: skill_dict["grade"] = "A+" elif temp_grade > 87: skill_dict["grade"] = "A" elif temp_grade > 84: skill_dict["grade"] = "A-" elif temp_grade > 81: skill_dict["grade"] = "B+" elif temp_grade > 78: skill_dict["grade"] = "B" elif temp_grade > 75: skill_dict["grade"] = "B-" elif temp_grade > 72: skill_dict["grade"] = "C+" elif temp_grade > 69: skill_dict["grade"] = "C" elif temp_grade > 66: skill_dict["grade"] = "C-" elif temp_grade > 63: skill_dict["grade"] = "D" else: skill_dict["grade"] = "F" # If overall rating outside 1-99, fix that for i in "speed", "strength", "avg", "ovr", "pot": if skill_dict[i] >= 100: skill_dict[i] = 99 elif skill_dict[i] <= 0: skill_dict[i] = 1 for i in skills: if i == "3pt": sql += "VALUES(?, ?" else: sql += ", ?" sql += ", ?, ?, ?, ?);" data = [current_id] skill_list = skill_dict.values() data.extend(skill_list) data = tuple(data) cur.execute(sql, data) position = (Position.Position(l_height, skill_dict)) sql = """INSERT INTO positions (id, position) VALUES(?, ?)""" cur.execute(sql, (current_id, str(position))) sql = """INSERT INTO hometowns (player_id, hometown_id) VALUES(?, ?)""" cur.execute(sql, (current_id, country.cities.gen_hometown())) return cur.lastrowid
def main(): rook = Rook() pos = Position(4, 5) rook.possibleConflict(pos)
def testFlotte_nbBatCoule2(): flot = Flotte() pos = Position(8, 7) bat = Bateau(4, 0, pos) flot.ajouterBateau(bat) return flot.nbBatCoule == 0
def testPos_x(): pos = Position(3, 7) return pos.x() == 3
def __init__(self): self.employees_info = Employee().get_all_db_data() self.positions_info = Position().get_all_db_data() self.tables_info = Table().get_all_db_data()
def testPos_touche(): pos = Position(12, 2) return not (pos.touche())
def testBat_taille(): pos = Position(3, 7) bat = Bateau(3, 0, pos) return bat.taille() == 3
def __init__(self, fn, text): self.fn = fn self.text = text self.pos = Position(-1, 0, -1, fn, text) self.current_char = None self.advance()
def testBat_positions(): pos = Position(8, 12) bat = Bateau(3, 0, pos) # Impossible de tester toutes les valeurs, on en teste deux aléatoires return (bat.position(1).x() == 8) and (bat.position(2).y() == 10)
def __init__(self): self.motorSerial = None self.position = Position(0, 0) self.side = "Left" self.dataFile = "SensorData.db" self.atomicDataFile = "AtomicSensorData.db" self.neutralY = 55 self.nextPosition = Position(0, 0) self.phase = "Wait" self.xDirection = 1 self.speed = 50 self.frSpeed = self.speed self.brSpeed = self.speed self.blSpeed = self.speed self.flSpeed = self.speed self.rotation = 0 self.setupMotorSerial() self.readSideAndPosition() self.taskPositions = [ Position(30, 15), Position(10, 90), Position(40, 15), Position(20, 90), Position(50, 15), Position(30, 90), Position(60, 15), Position(40, 90), Position(70, 15), Position(50, 90) ] self.taskCount = 0
def testBat_nbPosTouche(): pos = Position(3, 7) bat = Bateau(3, 0, pos) return bat.nbPosTouche() == 0
def getPrint3(self): return [ Position.Position(1, 11, 0), Position.Position(3, 2, 0), Position.Position(12, 2, 0), Position.Position(5, 3, 0), Position.Position(6, 1, 0), Position.Position(1, 1, 0), Position.Position(3, 8, 0), Position.Position(3, 10, 0), Position.Position(15, 1, 0), Position.Position(13, 7, 0), Position.Position(8, 4, 0), Position.Position(9, 10, 0), Position.Position(9, 8, 0), Position.Position(1, 9, 0), Position.Position(4, 5, 0), Position.Position(11, 8, 0), Position.Position(15, 10, 0), Position.Position(10, 1, 0), Position.Position(11, 5, 0), Position.Position(12, 11, 0), Position.Position(6, 8, 0), Position.Position(1, 5, 0), Position.Position(14, 2, 0), Position.Position(16, 7, 0), Position.Position(16, 5, 0) ]
def testBat_touchePosition(): pos = Position(9, 7) bat = Bateau(4, 0, pos) bat.touchePosition(pos) return bat.nbPosTouche() == 1
def __init__(self): self.FeatureBlocks = [] self.Rotation = 0 self.Position = Position.Position()
def testFlotte_nbBat2(): flot = Flotte() pos = Position(9, 7) bat = Bateau(4, 0, pos) return flot.nbBat() == 1
# [email protected] from Fourmi import * from Domaine import * from Position import * dt = 1. dx = 1. # Dimensions du domaine lx = 10 ly = 10 # Position de la nouriture ... hg = Position(2,2) bd = Position(2,3) # Nid position_du_nid = Position( 5,5 ) # Objet de gestion du parametre de source source = SourceSansEffet(0) # Domaine dans lequel evoluent la fourmi domaine = Domaine( lx, ly, dx, dt, source, hg, bd, 0.02, 10. ) domaine.n[4][4] = 1 domaine.n[5][4] = 2 domaine.n[6][4] = 3 domaine.n[4][5] = 4
def testFlotte_bateaux(): flot = Flotte() pos = Position(9, 7) bat = Bateau(4, 0, pos) flot.ajouterBateau(bat) return flot.bateaux()[0] is bat