def create_game(): if (request.method == "POST"): form = request.form; level = form["level"]; #problems = list(g.db.problems.find({"level": level})); problems = list(g.db.problems.find().limit(25)); problems = shuffle(problems)[:BOARD_SIZE ** 2]; game = { "_id": g.db.game_info.count() + 1, "name": form["name"], "passwd": form["passwd"], "user_count": int(form["user_count"]), "problems_board": [["" for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)], "users": [], "is_active": False, "level": level, }; for i in range(len(problems)): game["problems_board"][i / BOARD_SIZE][i % BOARD_SIZE] = problems[i]["id"]; g.db.game_info.save(game); g.db.board_info.save({ "_id": g.db.board_info.count() + 1, "game_id": game["_id"], "board": [["" for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)], "time": datetime.datetime.now(), }); return redirect("/"); else: return render_template("create_game.html");
def get_data(image_paths, label_name, patch_size, nb_samples): """Return data(containing patches and labels) for training the model Args: image_paths: list Paths for all training data label_name: string name of label of interest patch_size: int nb_samples: int The number of samples extracted for each image """ data = [] labels = [] for path in image_paths: x, y = extract_patches_from_image(path, label_name, patch_size, nb_samples) data.append(x) labels.append(y) data = np.concatenate(data) labels = np.concatenate(labels) data, labels = shuffle(data, labels) return data, labels
def create_game(): if (request.method == "POST"): form = request.form level = form["level"] #problems = list(g.db.problems.find({"level": level})); problems = list(g.db.problems.find().limit(25)) problems = shuffle(problems)[:BOARD_SIZE**2] game = { "_id": g.db.game_info.count() + 1, "name": form["name"], "passwd": form["passwd"], "user_count": int(form["user_count"]), "problems_board": [["" for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)], "users": [], "is_active": False, "level": level, } for i in range(len(problems)): game["problems_board"][i / BOARD_SIZE][i % BOARD_SIZE] = problems[i]["id"] g.db.game_info.save(game) g.db.board_info.save({ "_id": g.db.board_info.count() + 1, "game_id": game["_id"], "board": [["" for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)], "time": datetime.datetime.now(), }) return redirect("/") else: return render_template("create_game.html")
def load(): """Load all review data and labels""" dec_data = neg_dec_data + pos_dec_data true_data = neg_true_data + pos_true_data dec_label = neg_dec_label + pos_dec_label true_label = neg_true_label + pos_true_label # Combine data data = dec_data + true_data label = [] for l in dec_label: label.append(l[0]) for l in true_label: label.append(l[0]) data, label = utility.shuffle(data, label) return data, label
def create(self): # TODO: build a new level from scratch based on the code on the beginning of the main game loop. roomNr = 0 rooms = self.rooms for i in range(self.roomRowWidth): self.rooms.append([]) # creating the room's attributes for rectColumn in range(self.roomRowWidth): # x for rectRow in range(self.roomRowLength): # y xCoord_Room = rectColumn * self.roomRect[X] + random.randint( 2, int(self.roomRect[X] / 3)) yCoord_Room = rectRow * self.roomRect[Y] + random.randint( 2, int(self.roomRect[Y] / 3)) roomWidth = random.randint( 5, self.roomRect[X] - (xCoord_Room - rectColumn * self.roomRect[X]) - 1) roomLength = random.randint(4, int(self.roomRect[Y] * 2 / 3)) currentRoom = Room(xCoord_Room, yCoord_Room, rectColumn, rectRow, roomWidth, roomLength) self.rooms[rectColumn].append(currentRoom) currentRoom.imprintOnLandscape(self.landscape) roomNr += 1 # deleting a few rooms for i in range(self.missingRooms): xNumber = random.randint(0, self.roomRowWidth - 1) yNumber = random.randint(0, self.roomRowLength - 1) self.rooms[xNumber][yNumber] = False # counting the number of rooms for column in self.rooms: for room in column: if room: self.roomtally += 1 ################################################################################### # place doors where doors are needed and store there coordinates as well as their properties # getting a list of the rooms with greater range visited = [[False for i in range(self.roomRowLength)] for i in range(self.roomRowWidth)] # creating doors # TODO: Implement range() for loops instead of 'roomRectX' variables for roomRectX, roomRectY, direction in itertools.product( range(self.roomRowLength), range(self.roomRowWidth), DIRECTIONS): roomPos = pos(roomRectX, roomRectY) setAt(visited, roomPos, True) otherDirection = util.getOppositeDirection(direction) directionVector = util.toPosition(direction) otherDirectionVector = util.toPosition(otherDirection) otherRoomPos = roomPos + directionVector room = rooms[roomRectX][roomRectY] if util.isInBounds(rooms, otherRoomPos) and room and getAt( rooms, otherRoomPos) and not getAt(visited, otherRoomPos): otherRoom = rooms[otherRoomPos.x][otherRoomPos.y] radial = pos((room.width - 1) * int( (directionVector.x + 1) * 0.5), (room.length - 1) * int( (directionVector.y + 1) * 0.5)) otherRadial = pos((otherRoom.width - 1) * int( (otherDirectionVector.x + 1) * 0.5), (room.length - 1) * int( (otherDirectionVector.y + 1) * 0.5)) tangential = pos( random.randint(1, room.width - 2) * abs(directionVector.y), random.randint(1, room.length - 2) * abs(directionVector.x)) otherTangential = pos( random.randint(1, otherRoom.width - 2) * abs(otherDirectionVector.y), random.randint(1, otherRoom.length - 2) * abs(otherDirectionVector.x)) ################################################################################################### # Create Hallways CoordDoor1 = radial + tangential + room.origin CoordDoor2 = otherRadial + otherTangential + otherRoom.origin directionVec = util.toPosition(direction) CoordIndent1 = CoordDoor1 + directionVec CoordIndent2 = CoordDoor2 - directionVec orient = util.toOrientation(direction) Middle = int(((CoordDoor2 - CoordDoor1) * directionVec) / 2) CoordMiddle1 = CoordDoor1 + Middle * directionVec CoordMiddle2 = CoordDoor2 + ( (CoordDoor1 - CoordDoor2) * directionVec + Middle) * directionVec newHallwayGeo = ((CoordDoor1, CoordIndent1), (CoordIndent1, CoordMiddle1), (CoordMiddle1, CoordMiddle2), (CoordMiddle2, CoordIndent2), (CoordDoor2, CoordIndent2)) newHallway = Hallway( (Door(CoordDoor1, direction, room), Door(CoordDoor2, otherDirection, otherRoom)), newHallwayGeo) self.hallways.append(newHallway) # Tell the rooms about there new doors room.doors.append(Door(CoordDoor1, direction, newHallway)) otherRoom.doors.append( Door(CoordDoor2, otherDirection, newHallway)) room.imprintOnLandscape(self.landscape) otherRoom.imprintOnLandscape(self.landscape) imprintHallway(newHallway, self.landscape) #################################################################################### # Set up the player, monsters and other things in the rooms # put objects and enemies into rooms content = util.shuffle(['start', 'exit', 'heaven'] + ['treasure' for i in range(self.roomtally - 3)]) possibleContent = (item for item in content) for room in util.iter2D(self.rooms): if room: item = next(possibleContent) if item == 'start': room.attributeObjs[LADDER_UP] = Object( LADDER_UP, randomPositionIn(room)) self.specialRooms[LADDER_UP] = room elif item == 'exit' and self.nr < LEVELTALLY - 1: room.attributeObjs[LADDER_DOWN] = Object( LADDER_DOWN, randomPositionIn(room)) self.specialRooms[LADDER_DOWN] = room elif item == 'heaven': room.attributeObjs[FOUNTAIN] = Object( FOUNTAIN, randomPositionIn(room)) elif item == 'treasure': room.massObjs.append( Object(TREASURE, randomPositionIn(room))) if not item in ['start', 'exit', 'heaven']: room.monsters.append( Object(IMPWARRIOR, randomPositionIn(room))) print("LEVELNUMBER", self.nr) # finish the level self.build = True
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # Train Module # Module to train a CNN model on character data. import numpy as np import cnn import utility input_arguments = utility.parse_input_arguments(module="train") images, labels = utility.load_data(input_arguments.image_path) class_count = len(np.unique(labels)) images, labels = utility.preprocess(images, labels) images, labels = utility.shuffle(images, labels) x_train, x_test, y_train, y_test = utility.split(images, labels, test_size=0.2) cnn = cnn.CNN(x_train.shape[1], x_train.shape[2], x_train.shape[3], class_count) cnn.summary() cnn.train(x_train, y_train, epochs=input_arguments.epochs, batch_size=input_arguments.batch_size, validation_split=input_arguments.validation_split, output_path=input_arguments.output_path) cnn.test(x_test, y_test, output_path=input_arguments.output_path)
def load_neg(): """Load only negative review data and labels""" data = neg_dec_data + neg_true_data label = [0] * len(neg_dec_data) + [1] * len(neg_true_data) data, label = utility.shuffle(data, label) return data, label
def load_pos(): """Load only positive review data and labels""" data = pos_dec_data + pos_true_data label = [0] * len(pos_dec_data) + [1] * len(pos_true_data) data, label = utility.shuffle(data, label) return data, label
# the Free Software Foundation, either version 3 of the License, or # at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # Train Module # Module to train a DCGAN model on image data. import dcgan import utility input_arguments = utility.parse_input_arguments(module="train") images = utility.load_images(input_arguments.image_path) images = utility.preprocess(images) utility.shuffle(images) dcgan = dcgan.DCGAN(images.shape[1], images.shape[2], images.shape[3]) dcgan.summary() dcgan.train(images, epochs=input_arguments.epochs, batch_size=input_arguments.batch_size, saving_frequency=input_arguments.saving_frequency, output_path=input_arguments.output_path)