Exemplo n.º 1
0
def test_weigh_columns():
    test_board = c4.create_board()
    assert ai.weigh_columns(test_board)[2] == 0
    assert ai.weigh_columns(test_board)[3] == 4
    for i in range(c4.ROWS):
        test_board[i][3] = 99
    assert ai.weigh_columns(test_board)[3] == 0
    test_board = c4.create_board()
    test_board[5][2] = c4.AI
    test_board[5][3] = c4.AI
    test_board[5][4] = c4.HUMAN
    assert ai.weigh_columns(test_board)[1] == ai.TRIPLE_WEIGHT
Exemplo n.º 2
0
def test_create_board():
    """ Creates an empty board """
    board = c4.create_board()
    model = np.zeros((c4.ROWS, c4.COLS))
    for i in range(c4.ROWS):
        for j in range(c4.COLS):
            assert board[i][j] == model[i][j]
Exemplo n.º 3
0
def test_score_diagonals():
    test_board = c4.create_board()
    assert ai.score_diagonals(test_board, 3, 3) == 0
    test_board[5][1] = c4.AI
    assert ai.score_diagonals(test_board, 3, 3) == ai.DOUBLE_WEIGHT
    test_board[4][2] = c4.HUMAN
    assert ai.score_diagonals(test_board, 3, 3) == ai.DOUBLE_WEIGHT - 1
Exemplo n.º 4
0
def test_check_vertical():
    """ Finds a vertical win. Returns False if not """
    test_board = c4.create_board()
    assert c4.check_vertical(test_board) == False
    for i in range(4):
        test_board[i][5] = c4.AI
    assert c4.check_vertical(test_board) == True
    test_board[2][5] = c4.HUMAN
    assert c4.check_horizontal(test_board) == False
Exemplo n.º 5
0
def test_check_diagonals():
    """ Finds a diagonal win. Returns false if not """
    test_board = c4.create_board()
    assert c4.check_diagonals(test_board) == False
    for i in range(4):
        test_board[i][i] = c4.HUMAN
    assert c4.check_diagonals(test_board) == True
    test_board[1][1] = c4.AI
    assert c4.check_diagonals(test_board) == False
Exemplo n.º 6
0
def game():
    global message
    global game_started

    TIME_TO_WAIT = 18
    # Thread loop
    while True:
        if game_started:
            # with open("current.txt", "w") as f:
            #     f.write("Game in progress!")
            view.updateCurrentText("Game in progress!")
            board = connect4.create_board()
            view.screen.fill((0, 0, 0))
            view.removeText()

            game_over = False
            # Gather each input, MAX of 1 input per user, in a time frame of x seconds, return winning input
            if message != "":
                while not game_over:
                    t_end = time.time() + TIME_TO_WAIT
                    array = [0, 0, 0, 0, 0, 0, 0]
                    print("----- GATHERING INPUT -----")
                    while time.time() < t_end:
                        try:
                            num = int(message)
                            if 1 <= num <= 7:
                                array[num - 1] += 1
                                message = ""
                        except ValueError:
                            pass

                    maxElem = array.index(max(array))
                    maxes = [maxElem]
                    # Checking if more than 1 elements have the same value
                    for i in range(len(array)):
                        if array[i] == array[maxElem] and i != maxElem:
                            maxes.append(i)
                    if len(maxes) > 1:
                        game_tuple = connect4.chooseLocation(
                            random.choice(maxes), board)
                    else:
                        game_tuple = connect4.chooseLocation(maxElem, board)

                    if not game_tuple == None:
                        print("Player {player} wins!".format(
                            player=game_tuple[1]))
                        game_over = game_tuple[0]
                        view.updateWinner(game_tuple[1])
                        message = ""
                    print(game_over)
                    if not game_over:
                        time.sleep(4.2)
            game_started = False
            view.updateCurrentText("Game over, waiting to restart...")
Exemplo n.º 7
0
def test_check_horizontal():
    """ Finds a horizontal win. Returns false if not """
    test_board = c4.create_board()
    assert c4.check_horizontal(test_board) == False
    for i in range(4):
        test_board[3][i] = c4.HUMAN
    assert c4.check_horizontal(test_board) == True
    test_board[3][2] = c4.AI
    assert c4.check_horizontal(test_board) == False
    for i in range(4):
        test_board[5][i + 3] = c4.AI
    assert c4.check_horizontal(test_board) == True
Exemplo n.º 8
0
def test_score_horizontals():
    test_board = c4.create_board()
    assert ai.score_horizontals(test_board, 5, 3) == 0
    test_board[5][6] = c4.AI
    assert ai.score_horizontals(test_board, 5, 3) == ai.DOUBLE_WEIGHT
    test_board[5][5] = c4.AI
    assert ai.score_horizontals(test_board, 5, 3) == \
            ai.DOUBLE_WEIGHT + ai.TRIPLE_WEIGHT
    test_board[5][4] = c4.AI
    assert ai.score_horizontals(test_board, 5, 3) > ai.QUADRUPLE_WEIGHT
    test_board[5][4] = c4.HUMAN
    assert ai.score_horizontals(test_board, 5, 3) == ai.DOUBLE_WEIGHT - 1
Exemplo n.º 9
0
def test_score_verticals():
    test_board = c4.create_board()
    assert ai.score_verticals(test_board, 3, 3) == 0
    test_board[5][3] = c4.AI
    assert ai.score_verticals(test_board, 3, 3) == ai.DOUBLE_WEIGHT
    test_board[4][3] = c4.AI
    assert ai.score_verticals(test_board, 3, 3) == \
            ai.TRIPLE_WEIGHT + ai.DOUBLE_WEIGHT
    test_board[2][3] = c4.AI
    assert ai.score_verticals(test_board, 3, 3) > ai.QUADRUPLE_WEIGHT
    test_board[4][3] = c4.HUMAN
    test_board[1][3] = c4.HUMAN
    assert ai.score_verticals(test_board, 3, 3) == 0
Exemplo n.º 10
0
def test_drop_piece():
    """ Adds a new piece to the board & returns True. False if new piece
    can't be added to that column.
    """
    test_board = c4.create_board()
    c4.drop_piece(1, c4.HUMAN, test_board)
    assert test_board[-1][1] == c4.HUMAN
    c4.drop_piece(2, c4.AI, test_board)
    assert test_board[-1][2] == c4.AI
    c4.drop_piece(1, c4.HUMAN, test_board)
    assert test_board[-2][1] == c4.HUMAN
    for i in range(6):
        c4.drop_piece(6, c4.HUMAN, test_board)
    assert c4.drop_piece(6, c4.AI, test_board) == False
Exemplo n.º 11
0
def test_is_winning_move():
    """ Checks for a winner. Returns bool """
    test_board = c4.create_board()
    assert c4.is_winning_move(test_board) == False
    for i in range(4):
        test_board[i][i] = c4.HUMAN
    assert c4.is_winning_move(test_board) == True
    for i in range(4):
        test_board[i][6 - i] = c4.AI
    assert c4.is_winning_move(test_board) == True
    for i in range(4):
        test_board[1][2 + i] = c4.AI
    assert c4.is_winning_move(test_board) == True
    for i in range(4):
        test_board[1 + i][4] = c4.HUMAN
    assert c4.is_winning_move(test_board) == True
    for i in range(0, 6, 2):
        test_board[3][i] = c4.HUMAN
        test_board[3][i + 1] = c4.AI
        test_board[4][i] = c4.AI
        test_board[4][i + 1] = c4.HUMAN
    assert c4.is_winning_move(test_board) == False
Exemplo n.º 12
0
def main():
    board = c4.create_board()
    game_over = False
    is_human = True
    while not game_over:
        c4.print_board(board)
        player = c4.HUMAN if is_human else c4.AI
        if is_human:
            print("Your turn")
            selection = c4.make_selection(c4.HUMAN)
            if selection is None:
                continue
        else:
            print("AI turn")
            time.sleep(1)
            selection = select_column(board)
        d = c4.drop_piece(selection, player, board)
        if not d:
            continue
        if c4.is_winning_move(board):
            c4.print_board(board)
            print(f"-----------\nPLAYER {player} IS THE WINNER!")
            game_over = True
        is_human = not is_human
Exemplo n.º 13
0
def test_build_all_vals():
    """ Concatenates matrix into single list """
    board = c4.create_board()
    assert c4.build_all_vals(board) == [0 for _ in range(c4.ROWS * c4.COLS)]
Exemplo n.º 14
0
def test_select_column():
    board = c4.create_board()
    assert ai.select_column(board) == 3
Exemplo n.º 15
0
                user = getUser(line)
                print(message)
                try:
                    if 1 <= int(message) <= 7 and not game_started:
                        print("Game started")
                        game_started = True
                        # event = pygame.event.Event(pygame.JOYBUTTONUP)
                        # pygame.event.post(event)
                except ValueError:
                    pass


if __name__ == '__main__':
    # Initiate program
    join(server)
    board = connect4.create_board()
    view.removeText()
    view.loadGame(board)
    view.updateCurrentText("Waiting to start...")
    game_started = False

    t1 = threading.Thread(target=twitch)
    t1.daemon = True
    t1.start()

    t2 = threading.Thread(target=game)
    t2.daemon = True
    t2.start()

    difficulties = ["debug", "Easy", "Medium", "Hard"]
    with open("difficulty.txt", "w") as f: