createCanvas(windowWidth, windowHeight);
buttonLEFT = createButton('LEFT');
buttonLEFT.position(20, 20);
buttonLEFT.mousePressed(leftButton);
buttonRIGHT = createButton('RIGHT');
buttonRIGHT.position(120, 20);
buttonRIGHT.mousePressed(rightButton);
if (last_input == "LEFT")
if (last_input == "RIGHT")
let size = running_str.length;
let display_color = color(0, 255, 0);
if ((size >= history_size) && (last_input != 0)) {
last_batch = running_str.slice(size-history_size, size);
if (last_batch in counts) {
let zeros_count = counts[last_batch][0];
let ones_count = counts[last_batch][1];
if (zeros_count > ones_count) {
display_pred = "Predicted: LEFT";
if (last_input == "LEFT")
display_color = color(255, 0, 0);
else if (zeros_count < ones_count) {
display_pred = "Predicted: RIGHT";
if (last_input == "RIGHT")
display_color = color(255, 0, 0);
let pred = Math.floor(Math.random()*2);
display_pred = "Predicted: LEFT";
if (last_input == "LEFT")
display_color = color(255, 0, 0);
display_pred = "Predicted: RIGHT";
if (last_input == "RIGHT")
display_color = color(255, 0, 0);
counts[last_batch][Number(curr_input)] += 1;
let pred = Math.floor(Math.random()*2);
display_pred = "Predicted: LEFT";
if (last_input == "LEFT")
display_color = color(255, 0, 0);
display_pred = "Predicted: RIGHT";
if (last_input == "RIGHT")
display_color = color(255, 0, 0);
counts[last_batch] = [0, 0];
let normed_accuracy = round(acc/total * 10000) / 10000
text("Accuracy: " + normed_accuracy, width/13.66, height/2.15+height/10);
text("You pressed: " + last_input, width/13.66, height/6.45+height/10);
text(display_pred, width/13.66, height/3.225+height/10);
if (size < history_size) {
text("Start pressing LEFT and RIGHT arrows.\nThe program tries to predict what you will press.", 50, height/3.225);
text("For any combination of three keys\nthe program tracks your most common fourth key pressed.", 50, height/2.15);
if ((last_input == "LEFT") || (last_input == "RIGHT"))
running_str += curr_input;
if (keyCode == LEFT_ARROW)
if (keyCode == RIGHT_ARROW)