示例#1
0
    def setUp(self):
        # Initializing Service and invoking register(...)
        user_service = UserService("test")
        user = user_service.register({
            "email": "*****@*****.**",
            "name": "John",
            "password": "******",
            "confirmPassword": "******",
            "phone": "+11231231231"
        })
        self.user_id = user.user_id
        self.headers['Authorization'] = self.__get_jwt_token(user.user_id)
        self.board = {'name': 'board1', "color": "#fefefe"}

        # Initializing BoardService and creating board
        board_service = BoardService("test")
        response = board_service.create_board(self.board, self.headers)
        self.board_id = response.board_id

        # Initializing SwimLaneService and creating a swim lane
        swim_lane_service = SwimLaneService("test")
        self.swim_lane = {
            "title": "todo",
            "boardId": self.board_id,
            "position": "0"
        }
        response = swim_lane_service.create_swim_lane(self.swim_lane,
                                                      self.headers)
示例#2
0
    def test_create_board(self):
        # Initializing BoardService and creating board
        board_service = BoardService("test")
        response = board_service.create_board(self.board, self.headers)

        # Validating the response
        assert isinstance(response, Board)
        assert response.user_id is not None
示例#3
0
    def test_create_board_with_invalid_authorization(self):
        # Initializing BoardService and creating board
        board_service = BoardService("test")
        headers = {}
        headers['Authorization'] = "somerandomstringhere"
        response = board_service.create_board(self.board, headers)

        # Validating the response
        assert isinstance(response, str)
        self.assertEqual(response, "Invalid Authorization")
示例#4
0
    def test_get_boards(self):
        # Initializing BoardService
        board_service = BoardService("test")
        # Creating a board
        board_service.create_board(self.board, self.headers)
        # Retrieving boards
        response = board_service.get_boards(self.headers)

        # Validating the response
        assert isinstance(response, list)
        self.assertGreaterEqual(len(response), 1)
示例#5
0
    def test_minimax(self):
        count_won = 100
        count = 100
        for _ in range(count):
            print(_)
            history_repository = Repository()
            history_service = HistoryService(history_repository)

            board_repository = Repository()
            board = BoardService(board_repository, history_service)

            computer = ComputerPlayer(board, history_service)
            won = 0
            first = True
            while not won:
                i, j = computer.find_move()

                board.make_move(i, j, 1)
                won = board.ctrl_win()
                if won:
                    count_won = count_won - 1
                    print("lost")
                    break
                if first:
                    i, j = computer.find_move()
                    first = not first
                else:
                    score, i, j = computer.maximize()
                board.make_move(i, j, 2)
                won = board.ctrl_win()
                if won:
                    print("won")
        print(count_won)
        self.assertGreaterEqual(count_won, 66)
示例#6
0
def get_boards():
    try:
        # Fetching data from post body
        headers = request.headers
        # Invoking register method present in services
        board_service = BoardService()
        response = board_service.get_boards(headers)

        # Returning appropriate message
        if isinstance(response, list):
            return jsonpickle.encode(response, unpicklable=False), 200
        else:
            return jsonify({'errorMessage': response}), 500
    except Exception as e:
        return jsonify({'errorMessage': e}), 500
class BoardManager(object):
    def __init__(self, board=None, players=None):
        self.board = {}
        self.players = []
        self.player_positions = {}
        self.board_service = BoardService()

    def add_player(self, name):
        self.players.append(name)
        self.player_positions[name] = 0

    def add_board(self, snakes, ladders, cells=100):
        self.board = Board(snakes, ladders, cells)

    def get_player_position(self, player):
        return self.player_positions[player]

    def update_player_position(self, player, new_position):
        self.player_positions[player] = new_position

    def play_turn(self, player, steps):
        curr_position = self.player_positions[player]
        if curr_position + steps == 100:
            return 100
        else:
            return self.board_service.move_position(self.board, curr_position,
                                                    steps)
示例#8
0
def editBoard():
    try:
        # Fetching data from post body
        data = request.json
        headers = request.headers

        # Invoking edit_board method present in services
        board_service = BoardService()
        response = board_service.edit_board(data, headers)

        # Returning appropriate message
        if isinstance(response, Board):
            return jsonpickle.encode(response, unpicklable=False), 200
        else:
            return jsonify({'errorMessage': response}), 500
    except Exception as e:
        return jsonify({'errorMessage': e}), 500
示例#9
0
    def test_edit_board(self):
        # Initializing BoardService and editing board
        board_service = BoardService("test")
        response = board_service.create_board(self.board, self.headers)
        # Building the board object
        self.board['board_id'] = response.board_id
        self.board['name'] = "new name"
        self.board['description'] = "some description"
        self.board['color'] = "#cccccc"
        self.board['image'] = None

        # Editing the board
        response = board_service.edit_board(self.board, self.headers)

        # Validating the response
        assert isinstance(response, Board)
        assert response.user_id is not None
        self.assertEqual(response.name, "new name")
        self.assertEqual(response.description, "some description")
        self.assertEqual(response.color, "#cccccc")
        self.assertEqual(response.board_id, response.board_id)
        assert response.image is None
示例#10
0
                    won = self.__board.ctrl_win()
                    if won:
                        print("\nPLAYER 1 WON!")
                        break
                except Exception as err:
                    print("error:", err)
            if first:
                i, j = self.__computer.find_move()
                first = not first
            else:
                score, i, j = self.__computer.maximize()
            print("Computer moved:", i, j)
            self.__board.make_move(i, j, 2)
            self.print_board()
            won = self.__board.ctrl_win()
            if won:
                print("\nComputer WON!")
                break


history_repository = Repository()
history_service = HistoryService(history_repository)

board_repository = Repository()
board = BoardService(board_repository, history_service)

computer = ComputerPlayer(board, history_service)
ui = UI(board, computer)
ui.run()
示例#11
0
    def test_board_undo(self):
        repo = Repository()
        history_repo = Repository()
        history_service = HistoryService(history_repo)
        board = BoardService(repo, history_service)

        self.assertRaises(AttributeError, lambda: history_service.undo())
        self.assertRaises(AttributeError, lambda: history_service.redo())
        board.make_move(2, 2, 2, assignUndo=True)

        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3))
        for i in [1, 3]:
            for j in [1, 3]:
                if i != 2 or j != 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        history_service.undo()

        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 0))
        for i in [1, 3]:
            for j in [1, 3]:
                self.assertEqual(board.get_cell(i, j),
                                 Cell(board.cell_id(i, j), 0))

        history_service.redo()
        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3))
        for i in [1, 3]:
            for j in [1, 3]:
                if i != 2 or j != 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        board.make_move(0, 0, 2, assignUndo=True)
        history_service.undo()
        for i in [0, 1, 2]:
            for j in [0, 1, 2]:
                if not i or not j:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 0))
                elif i == 2 and j == 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 3))
                else:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))
示例#12
0
    def test_board(self):
        repo = Repository()
        board = BoardService(repo)

        self.assertEqual(len(board.get_board()), 6)
        self.assertEqual(len(board.get_board()[randint(0, 5)]), 6)

        board.make_move(0, 0, 1)
        self.assertEqual(board.get_cell(0, 0), Cell(board.cell_id(0, 0), 2))
        for i in [0, 1]:
            for j in [0, 1]:
                if i + j:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        board.make_move(2, 2, 2)
        self.assertEqual(board.get_cell(2, 2), Cell(board.cell_id(2, 2), 3))
        for i in [1, 3]:
            for j in [1, 3]:
                if i != 2 or j != 2:
                    self.assertEqual(board.get_cell(i, j),
                                     Cell(board.cell_id(i, j), 1))

        self.assertRaises(AttributeError, lambda: board.make_move(1, 1, 1))
        self.assertRaises(AttributeError, lambda: board.make_move(6, 1, 1))
        self.assertRaises(AttributeError, lambda: board.make_move(4, 4, 3))

        self.assertFalse(board.ctrl_win())
        board.make_move(3, 0, 2)
        board.make_move(5, 1, 2)
        board.make_move(4, 4, 2)
        board.make_move(1, 4, 2)
        self.assertFalse(board.ctrl_win())
        board.make_move(0, 2, 2)
        self.assertTrue(board.ctrl_win())
 def __init__(self, board=None, players=None):
     self.board = {}
     self.players = []
     self.player_positions = {}
     self.board_service = BoardService()