clc;
clear all;
close all;
rng('shuffle');

filename = input('Enter a filename: ','s');

ListenChar(2);
HideCursor();

k1 = 'space';
keyOne = KbName(k1);

background_colour = [0 0 0];
main_colour = [255 255 255];

Screen('Preference', 'SkipSyncTests', 1); 
[window, window_size] = Screen('OpenWindow', 0, background_colour, [],32,2);

x_mid = window_size(3)/2;
y_mid = window_size(4)/2;

block_order = [1];
block_order = shuffle(block_order);
number_of_blocks = length(block_order);

percent_match = 0.25;
number_of_trials = 20;

actual_trial_counter = 1;

for block_counter = 1:number_of_blocks

    block_type = block_order(block_counter);

    if block_type == 1
        instructions1a = 'You are going to see a series of digits';
        instructions1b = 'If the digit 2 digits ago is the same as the current digit';
        instructions1c = 'Press the space bar';
        instructions1d = 'For example';    
        instructions1e = 'If you saw: 3 5 6 7 8 3 8';
        instructions1f = 'You would press the space bar as there was an 8 two digits ago';
    end
    Screen(window,'TextFont','Arial');
    Screen(window,'TextSize',20);
    DrawFormattedText(window, instructions1a,'center', y_mid-200, main_colour,[],[],[],2);
    DrawFormattedText(window, instructions1b,'center', y_mid-150, main_colour,[],[],[],2);
    DrawFormattedText(window, instructions1c,'center', y_mid-100, main_colour,[],[],[],2);
    DrawFormattedText(window, instructions1d,'center', y_mid-50, main_colour,[],[],[],2);
    DrawFormattedText(window, instructions1e,'center', y_mid, main_colour,[],[],[],2);
    DrawFormattedText(window, instructions1f,'center', y_mid+50, main_colour,[],[],[],2);
    DrawFormattedText(window, 'Press Any Key To Continue','center', y_mid+200, main_colour,[],[],[],2);
    Screen('Flip',window);

    KbPressWait;
    
    Screen(window,'TextFont','Arial');
    Screen(window,'TextSize',20);
    DrawFormattedText(window, ['Get ready!'],'center', 'center', main_colour,[],[],[],2);
    Screen('Flip',window);

    WaitSecs(3);
    
    memory_bank(1:2) = 0;

    for trial_counter = 1:number_of_trials

        if block_type == 1
        end

        if trial_counter <=3
            current_number = randi([1 9],1);
            should_match = 0;
        else
            check_for_match = rand(1);
            if check_for_match < percent_match
                current_number = memory_bank(1);
                should_match = 1;
            else
                while 1
                    current_number = randi([1 9],1);
                    if current_number ~= memory_bank(1)
                        should_match = 0;
                        break
                    end
                end     
            end
        end
                   
        Screen('Flip',window);
        
        random_delay = 0.4 + rand(1)*0.3;
        WaitSecs(random_delay);
        
        KbReleaseWait;
        
        Screen(window,'TextFont','Arial');
        Screen(window,'TextSize',40);
        DrawFormattedText(window, num2str(current_number),'center', 'center', main_colour,[],[],[],2);
        Screen('Flip',window);

        tic;
        response_type = 0;
        response_correct = 0;
        while 1
            [keyIsDown, secs, keyCode] = KbCheck;

            if keyCode(keyOne) == 1
                response_type = 1;
                if should_match == 0
                    response_correct = -1;
                    response_type = -1;
                end
            end
            if toc > 1
                if response_type == 0 & should_match == 1
                    response_correct = -1;
                end
                break;
            end
        end
        if should_match == 1 & response_type == 1
            response_correct = 1;
        end
        
        student_data(actual_trial_counter,1) = block_counter;
        student_data(actual_trial_counter,2) = trial_counter;
        student_data(actual_trial_counter,3) = current_number;
        student_data(actual_trial_counter,4) = memory_bank(2);
        student_data(actual_trial_counter,5) = memory_bank(1);
        student_data(actual_trial_counter,6) = should_match;
        student_data(actual_trial_counter,7) = response_correct;
        student_data(actual_trial_counter,8) = response_type;

        actual_trial_counter = actual_trial_counter + 1;
        
        memory_bank(1) = memory_bank(2);
        memory_bank(2) = current_number;

    end
    
end

Screen('Flip',window);
WaitSecs(1);

Screen(window,'TextFont','Arial');
Screen(window,'TextSize',20);
DrawFormattedText(window, ['You are now done the experiment. Thank you.'],'center', 'center', main_colour,[],[],[],2);
Screen('Flip',window);

WaitSecs(3);

Screen('CloseAll');

ListenChar(0);
ShowCursor();

save(filename,'student_data');