def get_boards(self, headers): try: # Checking if the token is valid or not payload = JwtHandler.decode_auth_token(headers['Authorization']) if (payload): board_obj = Board() board_obj.user_id = payload['sub'] # Fetching thee board collection boards_collection = self.__get_board_collection() cursor = boards_collection.find({"user_id": board_obj.user_id}) # Creating a list named boards boards = [] for board in cursor: board_obj = Board() board_obj.board_id = str(board['_id']) board_obj.user_id = board['user_id'] board_obj.description = board['description'] board_obj.color = board['color'] board_obj.name = board['name'] board_obj.image = board['image'] boards.append(board_obj) # Returning list return boards else: return "Invalid Authorization" except Exception as exception: raise exception
def test_init_and_play_random(): board = Board(4, 2, 3, 3) grid.init_and_play_board(board, True, 1, 1, .1) # single cell should die for row in board.cell_grid: for cell in row: assert not cell or not cell.is_alive()
def test_board_place_building(): b = Board(2,2) players = [ Player(0, PlayerColour.RED), Player(1, PlayerColour.ORANGE), Player(2, PlayerColour.YELLOW) ] b.place_building(BuildingType.SETTLEMENT, players[0], 0, 0) b.place_building(BuildingType.SETTLEMENT, players[1], 0, 2) b.place_building(BuildingType.SETTLEMENT, players[2], 0, 4) b.place_building(BuildingType.SETTLEMENT, players[0], 1, 1) b.place_building(BuildingType.SETTLEMENT, players[1], 1, 3) b.place_building(BuildingType.SETTLEMENT, players[2], 2, 2) assert b.corners[0][0].owner.id == 0 assert b.corners[0][1].owner is None assert b.corners[0][2].owner.id == 1 assert b.corners[0][3].owner is None assert b.corners[0][4].owner.id == 2 assert b.corners[1][0].owner is None assert b.corners[1][1].owner.id == 0 assert b.corners[1][2].owner is None assert b.corners[1][3].owner.id == 1 assert b.corners[1][4].owner is None assert b.corners[2][0].owner is None assert b.corners[2][1].owner is None assert b.corners[2][2].owner.id is 2 assert b.corners[2][3].owner is None assert b.corners[2][4].owner is None
def __init(self): ''' Initialisation of game ''' # Create and display map self.board = Board(self) # Create game window self.py.create_screen(self.board.map_size) # Load Images for building the interface and characters self.py.load_image() # Print the map on screen self.py.display_map(self.board.map_size, self.board.paths) # Add characters to the board self.board.add_sprites() # Play game music in infinite loop self.py.sounds["game_music"].play(-1) self.win = False self.loose = False self.__start()
def __init__(self): print("Let's play a game of tic tac toe!") self.board = Board() self.game_rules = Game_Rules() self.player_letter = "X" self.game_over = False while self.game_over != True: self.valid_input = False while self.valid_input != True: print( f'Pick a spot from 0-8. Player letter: {self.player_letter}' ) self.player_choice = int(input()) if self.player_choice >= 0 and self.player_choice < 9: self.valid_input = True else: print("Number not between 0 and 8!") self.board.the_board[int(self.player_choice)] = self.player_letter self.player_letter = self.game_rules.switch_player( self.player_letter) self.game_over = self.game_rules.check_for_win( self.board.the_board) print("Game Over!")
def edit_board(self, board, headers): try: # Checking if the token is valid or not payload = JwtHandler.decode_auth_token(headers['Authorization']) if (payload): # Creating a board object board_obj = Board() board_obj.user_id = payload['sub'] board_obj.name = board['name'] board_obj.board_id = board['board_id'] board_obj.description = board['description'] board_obj.color = board['color'] board_obj.image = board['image'] # Checking if the image is being passed or not if (board['image'] and len(board['image']) > 0): # Storing the image in Amazon S3 bucket amazon_s3_handler = AmazonS3Handler() board_obj.image = amazon_s3_handler.store_in_S3( board['image'], board['board_id']) # Updating the board boards_collection = self.__get_board_collection() query = {'_id': ObjectId(board_obj.board_id)} updated_values = {"$set": board_obj.__dict__} boards_collection.update_one(query, updated_values) # Closing the DB connection self.mongo_db.disconnect() return board_obj else: return "Invalid Authorization" except Exception as exception: raise exception
def create_board(self, board, headers): try: # Checking if the token is valid or not payload = JwtHandler.decode_auth_token(headers['Authorization']) if (payload): # Creating a board object board_obj = Board() board_obj.user_id = payload['sub'] board_obj.name = board['name'] board_obj.color = board['color'] # Storing the board in boards collection boards_collection = self.__get_board_collection() saved_board = boards_collection.insert_one(board_obj.__dict__) # Stroing the board_id in the object and returning board_obj.board_id = str(saved_board.inserted_id) # Closing the DB connection self.mongo_db.disconnect() return board_obj else: return "Invalid Authorization" except Exception as exception: raise exception
def create_a_new_board(user_id): """ Creates a new board appended to the user id and the returns the id of the new board """ user = storage.get(User, user_id) # Create a new Board board = Board() board.nodes = '{}' board.user_id = user_id # Create a service inside the board d_service = CustomNode() # Set the service settings d_service.user_id = user_id d_service.board_id = board.id d_service.type = 'service' d_service.work_type = 'process' d_service.name = 'Result' # store the service in the board objects = json.loads(board.nodes) objects[d_service.id] = {'x': 100, 'y': 100} board.nodes = json.dumps(objects) # Save the created instances d_service.save() board.save() return Response(json.dumps({'board_id': board.id}), mimetype='application/json')
def test_board_init_errors(): """ Check that invalid inits raise exceptions """ with pytest.raises(Exception): # negative size Board(-1, 2, 3, 3) with pytest.raises(Exception): # negative min_neighbor Board(1, -2, 3, 3) with pytest.raises(Exception): # negative max_neighbor Board(1, 2, -3, 3) with pytest.raises(Exception): # max less than min Board(1, 3, 2, 3) with pytest.raises(Exception): # negative cells to revive Board(1, 2, 3, -3)
def create(self): board = Board() board_id = board.short_id board.id = board_id self.boards.insert_one(board.to_mongo()) return {"board_id": board_id}
def __init__(self): self.board = Board(None) self.view = ConsoleBoardView(self, self.board) self.white_player = None self.black_player = None self.atual_player = None self.finish_game = 0
def test_init_and_play_manual(): board = Board(4, 2, 3, 3) allow(grid.plt).ginput.and_return(((1, 1), (0, 0))) grid.init_and_play_board(board, False, 1, 1, .1) # single cell should die for row in board.cell_grid: for cell in row: assert not cell or not cell.is_alive()
def test_board_road_lengths_2x2(): b = Board(2,2) assert len(b.roads) == 5 assert len(b.roads[0]) == 4 assert len(b.roads[1]) == 3 assert len(b.roads[2]) == 4 assert len(b.roads[3]) == 2
def test_next_gen_without_cells(): """ Check next_gen checking all cells. """ board = Board(4, 2, 3, 3) board.make_alive(1, 1) board.set_grid() board.next_gen() # single cell should die assert not board.get_cell(1, 1).is_alive()
def test_next_gen_with_bad_cell_to_check(): """ Check next_gen with set of cells to check. """ board = Board(4, 2, 3, 3) board.make_alive(1, 1) board.set_grid() board.cells_to_check = set() board.cells_to_check.add((10, 10)) with pytest.raises(IndexError): board.next_gen()
def test_various_index_errors(): """ Some things that won'f work with input outside grid. """ board = Board(4, 2, 3, 3) with pytest.raises(IndexError): board._Board__init_next_gen(10, 10) with pytest.raises(IndexError): board._Board__update_binary_grid(10, 10, 1) with pytest.raises(IndexError): board._Board__revive_next_gen(10, 10)
def __init__(self, white, black, p): self.board = Board(None) self.white = white self.black = black self.view = ConsoleBoardView(self.board) self.win = 0 self.p1 = p[0] self.p2 = p[1] self.p3 = p[2]
def create_board(args): board_object = Board() board_object.header = args['header'] board_object.admin = g.user board_object.save() return jsonify({ "response" : "board created", "board_id" : str(board_object.id) })
def test_board_init_make_alive_and_kill(): """ Check that you can make alive then kill a cell """ board = Board(4, 2, 3, 3) board.make_alive(1, 1) board.set_grid() assert board.is_cell_alive(1, 1) board.kill(1, 1) board.set_grid() assert not board.is_cell_alive(1, 1)
def restart_game(self): """Reinicia o jogo Othelo.""" self.board = Board(None) self.view.reiniciar_jogo(self.board) self.white_player = None self.black_player = None self.atual_player = None self.finish_game = 0
def test_board_revive_cell(): """ Test that a cell can be revived """ board = Board(4, 2, 3, 3) board.make_alive(1, 1) board.make_alive(1, 2) board.make_alive(1, 3) board.set_grid() board.next_gen() assert board.get_cell(2, 2).is_alive()
def test_next_gen_with_cells(): """ Check next_gen with set of cells to check. """ board = Board(4, 2, 3, 3) board.make_alive(1, 1) board.set_grid() board.cells_to_check = set() board.cells_to_check.add((1, 1)) board.next_gen() # single cell should die assert not board.get_cell(1, 1).is_alive()
def main(): print("zaczynamy") board_model = Board((255, 255, 255), 640, 480) board_view = BoardView(board_model) board_controller = BoardController(board_model, board_view) board_controller.create_buttons() board_controller.create_inputs() board_view.show()
def __init__(self, app_ref): super().__init__(app_ref) self.current_player_label = Label(self, '', 20) self.current_player_checker_label = Button(self, '') self.current_player_checker_label.config(state=tk.DISABLED, width=5) self.max_depth_label = Label(self, '', 12) self.board = Board(self) self.players: {PlayerType: Player} = { PlayerType.COMPUTER: Player(self, PlayerType.COMPUTER, self.board.blue_checkers), PlayerType.USER: Player(self, PlayerType.USER, self.board.orange_checkers) }
def test_board_get_cell_ok(): """ Can get cell either by default or if already exists """ board = Board(4, 2, 3, 3) cell = board.get_cell(1, 1) # if cell isnt yet assigned is not alive assert not cell.is_alive() board.make_alive(1, 1) board.set_grid() cell = board.get_cell(1, 1) # cell is assigned and is alive assert cell.is_alive()
def test_queen_moves(): board = Board() # Check move generation when queen is on A1 queen = Queen(Square.from_notation("a1"), board) moves = queen.get_moves() assert len(moves) == 21 assert all(isinstance(move, Square) for move in moves) assert Square.from_notation("h1") in moves assert Square.from_notation("a8") in moves assert Square.from_notation("h8") in moves # Check move generation when queen is on D4 queen = Queen(Square.from_notation("d4"), board) moves = queen.get_moves() assert len(moves) == 27 assert all(isinstance(move, Square) for move in moves) assert Square.from_notation("a4") in moves assert Square.from_notation("h4") in moves assert Square.from_notation("d8") in moves assert Square.from_notation("d1") in moves assert Square.from_notation("b2") in moves assert Square.from_notation("h8") in moves assert Square.from_notation("a7") in moves assert Square.from_notation("g1") in moves # Check move generation when queen is on D4 # and there is a blockin piece on F4 queen = Queen(Square.from_notation("d4"), board) blocking_bishop1 = Bishop(Square.from_notation("c4"), board) blocking_bishop2 = Bishop(Square.from_notation("c3"), board) board.add_piece(blocking_bishop1) board.add_piece(blocking_bishop2) moves = queen.get_moves() assert len(moves) == 21 assert all(isinstance(move, Square) for move in moves) assert Square.from_notation("h4") in moves assert Square.from_notation("e4") in moves assert Square.from_notation("d1") in moves assert Square.from_notation("d8") in moves assert Square.from_notation("f4") in moves assert Square.from_notation("b6") in moves assert Square.from_notation("g1") in moves assert Square.from_notation("a7") in moves assert Square.from_notation("a1") not in moves assert Square.from_notation("a4") not in moves
def prepare_match(self): self.winner = None self.turn = WHITE self.turn_counter = 1 self.selected_piece_house = None self.choice = False self.checked = False self.is_checkmate = False self.is_stalemate = False self.board = Board(houses=self.build_houses()) self.set_up_pieces() self.screen_name = Screens.MENU self.whiteCapturePieceIncator = 0 self.blackCapturePieceIncator = 0
def __init__(self): # set main pygame screen size self.__screen_size = (1000, 720) self.__screen = pygame.display.set_mode(self.__screen_size[:2]) # change display icon pygame.display.set_icon(pygame.image.load("../assets/icon.png")) self.__generator = Generator() self.__board = self.__generator.generate() # create board object self.__board_model = Board(self.__screen_size, self.__board, self.__screen) # create solver object self.__solver = Solver(self.__board_model, 500) # create left panel object self.__left_panel = LeftPanel(self.__solver, self.__screen_size, self.__screen) # set screen title pygame.display.set_caption("Sudoku")
def create_example_data(): if not Cell.select(): user = User(name='Example User', login_name='user', password='******') batman = User(name='Batman', login_name='batman', password='******') user.save() batman.save() board1 = Board(name='First') board2 = Board(name='Second') board3 = Board(name='Third') batman1 = Board(name='Batmobil') batman2 = Board(name='BatBarlang') batman3 = Board(name='BatRobin') board1.save() board2.save() board3.save() batman1.save() batman2.save() batman3.save() Boardstable.create(board=board1, user=user) Boardstable.create(board=board2, user=user) Boardstable.create(board=board3, user=user) Boardstable.create(board=batman1, user=batman) Boardstable.create(board=batman2, user=batman) Boardstable.create(board=batman3, user=batman) Cell.create(text="Rocket", name="Weapon", order=1, board=board1, status=Status.get(Status.status == "new")) Cell.create(text="Pistol", name="Weapon", order=2, board=board1, status=Status.get(Status.status == "new")) Cell.create(text="Sword", name="Weapon", order=3, board=board1, status=Status.get(Status.status == "new")) Cell.create(text="Kalasnyikov", name="Weapon", order=1, board=board1, status=Status.get(Status.status == "progress")) Cell.create(text="Grenade", name="Weapon", order=1, board=board1, status=Status.get(Status.status == "done"))
def test_king_moves(): board = Board() # Check move generation when king is on A1 king = King(Square.from_notation("a1"), board) moves = king.get_moves() assert len(moves) == 3 assert all(isinstance(move, Square) for move in moves) assert Square(file=0, rank=1) in moves assert Square.from_notation("b1") in moves assert Square.from_notation("b1") in moves # Check move generation when king is on D4 king = King(Square.from_notation("d4"), board) moves = king.get_moves() assert len(moves) == 8 assert all(isinstance(move, Square) for move in moves) assert Square.from_notation("c3") in moves assert Square.from_notation("c4") in moves assert Square.from_notation("c5") in moves assert Square.from_notation("d3") in moves assert Square.from_notation("d5") in moves assert Square.from_notation("e3") in moves assert Square.from_notation("e4") in moves assert Square.from_notation("e5") in moves # Check move generation when king is on D4 # and there is a blockin piece on C5 king = King(Square.from_notation("d4"), board) blocking_bishop = Bishop(Square.from_notation("c5"), board) board.add_piece(blocking_bishop) moves = king.get_moves() assert len(moves) == 7 assert all(isinstance(move, Square) for move in moves) assert Square.from_notation("c3") in moves assert Square.from_notation("c4") in moves assert Square.from_notation("c5") not in moves assert Square.from_notation("d3") in moves assert Square.from_notation("d5") in moves assert Square.from_notation("e3") in moves assert Square.from_notation("e4") in moves assert Square.from_notation("e5") in moves