Beispiel #1
0
def test_player_make_move():
    board_1 = Board(4, 4, 100)
    gm_1 = GameManager(board_1)
    gm_1.player_make_move(10, 100)
    assert gm_1.legal_move == {
        (0, 1): [(1, 1)],
        (1, 0): [(1, 1)],
        (2, 3): [(2, 2)],
        (3, 2): [(2, 2)]
    }
    assert gm_1.board.spaces[0][1].color == 0

    board_2 = Board(8, 8, 100)
    gm_2 = GameManager(board_2)
    gm_2.player_make_move(10, 10)
    assert gm_2.legal_move == {
        (5, 4): [(4, 4)],
        (2, 3): [(3, 3)],
        (4, 5): [(4, 4)],
        (3, 2): [(3, 3)]
    }
    assert not gm_2.board.spaces[0][0]

    board_3 = Board(6, 6, 100)
    gm_3 = GameManager(board_3)
    gm_3.player_make_move(400, 300)
    assert gm_3.legal_move == {
        (1, 2): [(2, 2)],
        (4, 3): [(3, 3)],
        (3, 4): [(3, 3)],
        (2, 1): [(2, 2)]
    }
    assert gm_3.board.spaces[4][3].color == 0
Beispiel #2
0
def test_constructor():
    board_1 = Board(2, 2, 100)
    gm_1 = GameManager(board_1)
    assert gm_1.turn
    assert gm_1.board == board_1
    assert not gm_1.no_ai_moves
    assert not gm_1.no_player_moves
    assert gm_1.directions == [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1),
                               (-1, -1), (-1, 0), (-1, 1)]
    assert gm_1.legal_move == dict()

    board_2 = Board(4, 4, 50)
    gm_2 = GameManager(board_2)
    assert gm_2.turn
    assert gm_2.board == board_2
    assert not gm_2.no_ai_moves
    assert not gm_2.no_player_moves
    assert gm_2.directions == [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1),
                               (-1, -1), (-1, 0), (-1, 1)]
    assert gm_2.legal_move == dict()

    board_3 = Board(8, 8, 100)
    gm_3 = GameManager(board_3)
    assert gm_3.turn
    assert gm_3.board == board_3
    assert not gm_3.no_ai_moves
    assert not gm_3.no_player_moves
    assert gm_3.directions == [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1),
                               (-1, -1), (-1, 0), (-1, 1)]
    assert gm_3.legal_move == dict()
Beispiel #3
0
def test_player_make_move():
    board_1 = Board(4, 4, 100)
    gm_1 = GameManager(board_1)
    gm_1.ai_make_move()
    assert gm_1.legal_move == {
        (0, 2): [(1, 2)],
        (1, 3): [(1, 2)],
        (2, 0): [(2, 1)],
        (3, 1): [(2, 1)]
    }

    board_2 = Board(8, 8, 100)
    gm_2 = GameManager(board_2)
    gm_2.ai_make_move()
    assert gm_2.legal_move == {
        (2, 4): [(3, 4)],
        (3, 5): [(3, 4)],
        (4, 2): [(4, 3)],
        (5, 3): [(4, 3)]
    }

    board_3 = Board(6, 6, 100)
    gm_3 = GameManager(board_3)
    gm_3.ai_make_move()
    assert gm_3.legal_move == {
        (1, 3): [(2, 3)],
        (2, 4): [(2, 3)],
        (3, 1): [(3, 2)],
        (4, 2): [(3, 2)]
    }
def test_get_all_valid_moves():
    """test GameManager get_all_valid_moves method"""
    game_manager = GameManager(400, 400, 100)
    game_manager.get_all_valid_moves("AI")
    assert game_manager.all_valid_moves == [[0, 2], [1, 3], [2, 0], [3, 1]]
    game_manager = GameManager(200, 200, 100)
    game_manager.get_all_valid_moves("PLAYER")
    assert game_manager.all_valid_moves == []
def init_board():
    ZERO = 0
    game = GameManager(width, ZERO, ZERO, SIDE)
    assert game.board == []
    TWO = 2
    game = GameManager(width, TWO, TWO, SIDE)
    assert game.board == [[0, 0], [0, 0]]
    FOUR = 4
    game = GameManager(width, FOUR, FOUR, SIDE)
    assert game.board == [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
                          [0, 0, 0, 0]]
def test_is_game_ended():
    """test GameManager is_game_ended method"""
    game_manager1 = GameManager(400, 400, 100)
    assert game_manager1.is_game_ended() is False
    game_manager2 = GameManager(200, 200, 100)
    assert game_manager2.is_game_ended() is True
    game_manager2.tile_grid[0][0].change_state("PLAYER")
    game_manager2.tile_grid[1][1].change_state("BACKGROUND")
    assert game_manager2.get_all_valid_moves("AI") is None
    assert game_manager2.tile_grid[1][1].current_state == "BACKGROUND"
    assert game_manager2.is_game_ended() is True
def test_get_max_count_move_AI():
    """test GameManager get_max_count_move_AI method"""
    game_manager = GameManager(400, 400, 100)
    game_manager.get_all_valid_moves("AI")
    col, row = game_manager.get_max_count_move_AI()
    assert col == 2
    assert row == 0
    game_manager = GameManager(600, 600, 100)
    game_manager.get_all_valid_moves("PLAYER")
    col, row = game_manager.get_max_count_move_AI()
    assert col == 1
    assert row == 2
def test___init__():
    """A test for the constructor of the GameManager"""
    gm = GameManager(8)
    assert (gm.SIZE == 8 and gm.winner is None and gm.win_score == 0
            and gm.lose_score == 0 and gm.total_piece_count == 64
            and gm.game_over is False and len(gm.flip_pieces) == 0)
    gm = GameManager(4)
    assert (gm.SIZE == 4 and gm.winner is None and gm.win_score == 0
            and gm.lose_score == 0 and gm.total_piece_count == 16
            and gm.game_over is False and len(gm.flip_pieces) == 0)
    gm = GameManager(0)
    assert (gm.SIZE == 0 and gm.winner is None and gm.win_score == 0
            and gm.lose_score == 0 and gm.total_piece_count == 0
            and gm.game_over is False and len(gm.flip_pieces) == 0)
def test_on_board():
    """A test for on_board()"""
    gm = GameManager(4)
    assert gm.on_board(4, 4) is False
    assert gm.on_board(0, 0) is True
    assert gm.on_board(4, 0) is False
    assert gm.on_board(0, 4) is False
    assert gm.on_board(3, 3) is True

    gm = GameManager(8)
    assert gm.on_board(8, 8) is False
    assert gm.on_board(0, 0) is True
    assert gm.on_board(8, 0) is False
    assert gm.on_board(0, 8) is False
    assert gm.on_board(4, 7) is True
Beispiel #10
0
 def setUp(self):
     """Initialises all test cases."""
     self.game = GameManager()
     self.game.frame_counter = 0
     self.game.frame_scores = None
     self.game.roll = 0
     self.maxDiff = None
def test_check_valid_move():
    """A test for check_valid_move()"""
    gm = GameManager(4)
    black_piece = GamePiece(0, 0, BLACK, 0)
    white_piece = GamePiece(0, 0, WHITE, 0)
    game_pieces = []
    for _i in range(4):
        piece_list = []
        for _j in range(4):
            piece_list.append(None)
        game_pieces.append(piece_list)

    # game set up
    game_pieces[1][1] = white_piece
    game_pieces[1][2] = black_piece
    game_pieces[2][1] = black_piece
    game_pieces[2][2] = white_piece
    assert gm.check_valid_move(0, 1, game_pieces, BLACK) is True
    exp_flip_pieces = [(1, 1)]
    assert gm.flip_pieces == exp_flip_pieces
    assert gm.check_valid_move(0, 1, game_pieces, WHITE) is False

    game_pieces[0][0] = black_piece
    game_pieces[2][2] = white_piece
    assert gm.check_valid_move(3, 3, game_pieces, BLACK) is True
    exp_flip_pieces = [(1, 1), (2, 2)]
    assert gm.flip_pieces == exp_flip_pieces
    assert gm.check_valid_move(3, 3, game_pieces, WHITE) is False

    game_pieces[0][3] = white_piece
    assert gm.check_valid_move(3, 0, game_pieces, WHITE) is True
    exp_flip_pieces = [(1, 2), (2, 1)]
    assert gm.flip_pieces == exp_flip_pieces
def test_get_valid_moves():
    """A test for get_valid_moves()"""
    gm = GameManager(4)
    black_piece = GamePiece(0, 0, BLACK, 0)
    white_piece = GamePiece(0, 0, WHITE, 0)
    game_pieces = []
    for _i in range(4):
        piece_list = []
        for _j in range(4):
            piece_list.append(None)
        game_pieces.append(piece_list)

    # first 4 pieces game set up
    game_pieces[1][1] = white_piece
    game_pieces[1][2] = black_piece
    game_pieces[2][1] = black_piece
    game_pieces[2][2] = white_piece

    exp_list = [(1, 0), (0, 1), (2, 3), (3, 2)]
    valid_moves = gm.get_valid_moves(game_pieces, BLACK)
    for move in valid_moves:
        assert exp_list.count(move) == 1

    exp_list = [(2, 0), (1, 3), (0, 2), (3, 1)]
    valid_moves = gm.get_valid_moves(game_pieces, WHITE)
    for move in valid_moves:
        assert exp_list.count(move) == 1
Beispiel #13
0
 def setup_gui(self):
     self.create_window()
     self.game_manager = GameManager()
     self.create_canvas()
     self.create_stat_bar()
     self.create_description()
     self.create_info_bar()
Beispiel #14
0
def main():

    game = GameManager()
    game.init()
    game.main()

    sys.exit()
Beispiel #15
0
def start_with_existing_ga_agent():
    agent_src = "Training Output/best_agent_100.pkl"
    with open(agent_src, 'rb') as input:
        agent = pickle.load(input)
    gm = GameManager()
    score = gm.start_game(agent, display=True)
    print("Score:", score)
def test_minimax():
    game = GameManager(width, ROW_COUNT, COLUMN_COUNT, SIDE)
    board = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0,
                                     0], [0, 0, 0, 0, 1, 0, 0],
             [0, 0, 0, 1, 0, 2, 0], [0, 0, 1, 0, 2, 0, 2],
             [0, 0, 0, 2, 0, 0, 2]]
    assert game.minimax(board, 3, True)[0] == 1
def test_is_valid_location():
    game = GameManager(width, ROW_COUNT, COLUMN_COUNT, SIDE)
    game.board = [[1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 2, 0]]
    assert game.is_valid_location(game.board, 5) is False
    assert game.is_valid_location(game.board, 6) is True
def test_score_position():
    game = GameManager(width, ROW_COUNT, COLUMN_COUNT, SIDE)
    board = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0,
                                     0], [0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 2, 0], [0, 0, 0, 0, 2, 0, 0],
             [0, 0, 0, 2, 0, 0, 0]]
    assert game.score_position(board, 2) == 10
def test_count_colors():
    """A test for count_colors()"""
    gm = GameManager(8)
    black_piece = GamePiece(0, 0, BLACK, 0)
    white_piece = GamePiece(0, 0, WHITE, 0)
    game_pieces = []
    for _i in range(8):
        piece_list = []
        for _j in range(8):
            piece_list.append(None)
        game_pieces.append(piece_list)

    game_pieces[0][0] = black_piece
    black_count, white_count = gm.count_colors(game_pieces)
    assert black_count == 1 and white_count == 0

    game_pieces[1][1] = white_piece
    black_count, white_count = gm.count_colors(game_pieces)
    assert black_count == 1 and white_count == 1

    game_pieces[0][1] = black_piece
    game_pieces[1][2] = white_piece
    game_pieces[0][2] = black_piece
    black_count, white_count = gm.count_colors(game_pieces)
    assert black_count == 3 and white_count == 2

    game_pieces[0][0] = None
    black_count, white_count = gm.count_colors(game_pieces)
    assert black_count == 2 and white_count == 2
Beispiel #20
0
def test_on_board():
    board = Board(8, 8, 100)
    gm = GameManager(board)
    assert gm.on_board((1, 1))
    assert not gm.on_board((8, 7))
    assert gm.on_board((0, 0))
    assert not gm.on_board((9, 10))
Beispiel #21
0
def test_find_move():
    board = Board(8, 8, 100)
    gm = GameManager(board)

    gm.find_moves(gm.board.spaces[3][4], 255)
    assert gm.legal_move == {(5, 4): [(4, 4)], (3, 2): [(3, 3)]}

    gm.find_moves(gm.board.spaces[4][3], 255)
    assert gm.legal_move == {
        (5, 4): [(4, 4)],
        (2, 3): [(3, 3)],
        (4, 5): [(4, 4)],
        (3, 2): [(3, 3)]
    }

    # reset the dictionary to empty
    gm.legal_move = dict()

    gm.find_moves(gm.board.spaces[3][3], 0)
    assert gm.legal_move == {(3, 5): [(3, 4)], (5, 3): [(4, 3)]}

    gm.find_moves(gm.board.spaces[4][4], 0)
    assert gm.legal_move == {
        (3, 5): [(3, 4)],
        (5, 3): [(4, 3)],
        (4, 2): [(4, 3)],
        (2, 4): [(3, 4)]
    }
Beispiel #22
0
def test_decide_next_step():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)
    y, x = gm.decide_next_step()

    assert y == 2 and x == 4
def test_ai_move():
    game_manager = GameManager(400, 400, 100)
    game_manager.ai_move()
    assert game_manager.tile_grid[2][0].current_state == "AI"
    assert game_manager.tile_grid[2][1].current_state == "AI"
    game_manager.tile_grid[3][1].change_state == "AI"
    assert game_manager.is_player_turn is True
Beispiel #24
0
def main():
    pygame.init()
    network = Network('127.0.0.1', 12345)
    network.start()
    import consts
    from game_manager import GameManager
    from snake import Snake

    screen = pygame.display.set_mode((consts.height, consts.width))
    screen.fill(consts.back_color)
    game = GameManager(consts.table_size, screen, consts.sx, consts.sy, consts.block_cells)
    snakes = list()
    for snake in consts.snakes:
        snakes.append(Snake(snake['keys'], game, (snake['sx'], snake['sy']), snake['color'], snake['direction']))

    while True:
        events = pygame.event.get()
        keys = []
        for event in events:
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                keys.append(event.unicode)
        network.send_data(keys)
        keys = network.get_data()
        game.handle(keys)
        pygame.time.wait(100)
Beispiel #25
0
def main():
    pygame.init()
    screen = pygame.display.set_mode((consts.height, consts.width))
    screen.fill(consts.back_color)
    game = GameManager(consts.table_size, screen, consts.sx,
                       consts.sy, consts.block_cells)
    snakes = list()
    for snake in consts.snakes:
        print(snake)
        snakes.append(Snake(snake['keys'], game, (snake['sx'],
                                                  snake['sy']), snake['color'], snake['head_color'], snake['direction']))
    temp = True
    while temp:
        events = pygame.event.get()
        keys = []
        for event in events:
            if event.type == pygame.QUIT:
                temp = False
            if event.type == pygame.KEYDOWN:
                keys.append(event.unicode)
        if not game.handle(keys):
            font1 = pygame.font.SysFont("freesansbold", 100)
            text1 = font1.render("GAME OVER", True, (255, 0, 0))
            font2 = pygame.font.SysFont("comicsansms", 50)
            text2 = font2.render(
                "score: "+str(snakes[0].score), True, (0, 0, 155))
            screen.blit(text1, (390 - text1.get_width() //
                                2, 355 - text1.get_height() // 2))
            screen.blit(text2, (390 - text2.get_width() //
                                2, 425 - text2.get_height() // 2))
            pygame.display.flip()
        pygame.time.wait(250)
Beispiel #26
0
def main():
    # change the current directory to the one of the game
    # this is to allow executions like ``python src/tct.py''
    try:
        os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
    except IOError as e:
        print(e)
        exit(constants.FILE_ERR)

    # get the command line options; return the option flags
    game_opts = get_parsed_opts()

    # MVC stuff
    event_manager = EventManager()
    gui_view = MainGUIView(event_manager, game_opts)

    # the game manager
    game_manager = GameManager(game_opts)

    # controller which handles the main game loop
    main_controller = MainController(event_manager, gui_view, game_manager)

    # keep running the game until a quit event occurs
    main_controller.run()

    # if control somehow reaches this point close all the pygame subsystems
    pygame.quit()
    safe_exit()
def test_flip_tle():
    game_manager = GameManager(600, 600, 100)
    game_manager.is_valid_tile_to_place(1, 2, "PLAYER")
    game_manager.flip_tile("PLAYER")
    assert game_manager.tile_grid[2][2].current_state == "PLAYER"
    game_manager.flip_tile("AI")
    assert game_manager.tile_grid[0][0].current_state == "BACKGROUND"
Beispiel #28
0
def test_constructor():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    assert gm.black_turn == True
    assert gm.WIDTH == 800
    assert gm.HEIGHT == 800

    for y in range(ROW_NUM):
        for x in range(ROW_NUM):
            if y == 3 and x == 3:
                assert gm.tile_list[y][x].color == 'white'
            elif y == 4 and x == 4:
                assert gm.tile_list[y][x].color == 'white'
            elif y == 4 and x == 3:
                assert gm.tile_list[y][x].color == 'black'
            elif y == 3 and x == 4:
                assert gm.tile_list[y][x].color == 'black'
            else:
                assert gm.tile_list[y][x] == None

    assert gm.black_count == 2
    assert gm.white_count == 2
    assert gm.X_ADD == [0, 1, -1, 0, 1, -1, 1, -1]
    assert gm.Y_ADD == [1, 0, 0, -1, 1, -1, -1, 1]
    assert gm.has_input_name == False
    assert gm.TIME_DURATION == 1500
    assert gm.FILE_NAME == 'scores.txt'
Beispiel #29
0
async def handleRoomCreation(ws, initObj, setRoom):
    if "name" in initObj and initObj["name"].strip() != "":
        gm = GameManager(initObj["packs"], initObj["questions"],
                         initObj["answers"])

        while gm.joinCode in games:
            gm.regenJoinCode()

        m = Member(ws, initObj["name"])
        gm.addMember(m)
        games[gm.joinCode] = gm
        setRoom(gm.joinCode)
        await send_object(
            ws, {
                "action": "roomMade",
                "joinCode": gm.joinCode,
                "setName": initObj["name"]
            })
        nextObj = await recv_object(ws)

        if "action" in nextObj:
            if nextObj["action"] == "startGame":
                await asyncio.wait(
                    [games[gm.joinCode].startGame(),
                     m.runGameLoop()],
                    return_when=asyncio.FIRST_COMPLETED)
    else:
        await send_object(ws, {
            "action": "error",
            "content": "Please make sure to set a name."
        })
Beispiel #30
0
def test_white_make_move():
    cb = ChessBoard(WIDTH, HEIGHT, ROW_NUM)
    tiles = Tiles(cb)
    gm = GameManager(cb, tiles)

    gm.white_make_move()

    assert gm.tile_list[2][4].color == 'white'