<commit_before>$(document).ready(function() {
  //Boolean used to check if the game should continue
  var gameOn = false;
  //Ids of the buttons in the game
  const buttons = ['red-button', 'yellow-button', 'blue-button', 'green-button'];
  //Array that stores the button sequence that has been randomly generated throughout the game
  var buttonHistory = [];
  //Index corresponding to the user's position in the button sequence
  var currentIndex = 0;
  //Counter to display length of buttonHistory (probably not be needed)
  var count = 0;
  //Boolean used to check if the device is on
  var deviceOn = false;
  var strictMode = false;
  var colorButtonsDisabled = true

  const soundMap = {
  	"red-button": "sounds/redSound.mp3",
  	"yellow-button":"sounds/yellowSound.mp3",
  	"blue-button": "sounds/blueSound.mp3",
  	"green-button": "sounds/greenSound.mp3"};

  $("#strict-button").click(function() {
  	console.log("deviceOn: " + deviceOn + " gameOn: " + gameOn + " strictMode: " + strictMode);
  	if (deviceOn && gameOn) {
  		if (strictMode) {
  			strictMode = false;
  			$("#strict-LED").css("background-color","black");
  		} else {
				strictMode = true;
  			$("#strict-LED").css("background-color","red");
  		}
  	}
  });

  $("#on-off-button").click(function() {
  	if (deviceOn) {
  		turnDeviceOff();
  	} else {
  		turnDeviceOn();
  	}
  });

  $(".color-button").mousedown(function() {
  	if (deviceOn && gameOn && !colorButtonsDisabled){
  		console.log("clicked on " + $(this)[0].id)
  		animateSingleButton($(this)[0].id);
	// b. Compare button y to button at current index in button history array
			console.log("button id: " + $(this)[0].id + " buttonHistory[currentIndex]: " + buttonHistory[currentIndex])
			if ($(this)[0].id == buttonHistory[currentIndex]){
				if (currentIndex < buttonHistory.length - 1) {
					currentIndex++;
				} else {
					computerTurn();
				}
			}
	// c. If equal and current index < length of button history array: i. Increment current index
	// ii. Wait for user to click on any button
	// d. If equal and current index = length of button history array
	// i. Computer’s Turn (call computer_turn function)
	// e. If not equal
	// i. If strict (check color of LED using jquery)
	// 1. Indicate in count-box wrong click
	// 2. Restart Game
	// ii. If not strict
	// 1. Set current index = 0
	// 2. Animate Button History Array
	// 3. Wait for user to click on any button
  	}
  });

  $("#start-button").click(function(){
  	if (deviceOn) {
  		restartGame();
  	}
  });

  function restartGame() {
  	// 0. Turn game on
  	gameOn = true;
		// a. Set count = 0
		count = 0;
		// b. Count-box set to zero
		$("#count-box").html(count);
		// c. Button history array is empty
		buttonHistory = [];
		// d. Start game engine (Computer Turn)
		computerTurn();
  }

  function turnDeviceOn() {
  	gameOn = false;
  	deviceOn = true;
  }

  function turnDeviceOff() {
		// a. Set game_on variable to false
		gameOn = false;
		deviceOn = false;
		// b. Strict-LED set off
		$("#strict-LED").css("background-color","black");
		// c. Count set to “”
		$("#count-box").html("");
		// d. count set to 0
		count = 0;
		buttonHistory = [];
  }

  function computerTurn() {
  	// a. Increment count
  	count++;
  	$("#count-box").html(count);
		// b. Randomly generate button x: Function that randomly generates a button (Math.random 0-0.25, 0.25-0.5, 0.5-0.75, 0.75-1 => a,b,c,d)
		var nextButton = randomButton();
		// c. Append button x to button history array
		buttonHistory.push(nextButton);
		// d. Set current index = 0
		currentIndex = 0;
		// e. Disable color buttons
		colorButtonsDisabled = true;
		// f. Animate button history array (including sounds)
		animateButtonHistory();
		// g. Enable color buttons
		colorButtonsDisabled = false;
		// h. Wait for user to click on any button
		//(don’t need to call anything since jquery handlers are listening)
  }

  function animateButtonHistory() {
		// a. Set loop index = 0
		var loopIndex = 0;
		// b. While current index < length of button history array AND game_on (is true)
		while ((loopIndex < buttonHistory.length) && gameOn && deviceOn){
			//animateSingleButton(buttonHistory[loopIndex])
			setTimeout(function(){animateSingleButton(buttonHistory[loopIndex])}, loopIndex*2000);
			//function(){animateSingleButton(buttonHistory[loopIndex])}
			//animateSingleButton(buttonHistory[loopIndex]);
			loopIndex++;
		}
  }

  function animateSingleButton(buttonID) {
  	console.log(buttonID);
  	playSound(buttonID);
		// Change button x color to lighter color of button x’s normal color
		$("#" + buttonID).css("opacity","0.5");
		setTimeout(function(){$("#" + buttonID).css("opacity","1")}, 1000);
  }

  function playSound(buttonID) {
  //Play sound for button x (store sound urls in sound_array in same order as button names)
  //for a duration (window.setInterval(callback, duration_in_milliseconds) maybe)
  	var buttonSound = new sound(soundMap[buttonID])
  	buttonSound.play();
	}

  function sound(src) {
    this.sound = document.createElement("audio");
    this.sound.src = src;
    this.sound.setAttribute("preload", "auto");
    this.sound.setAttribute("controls", "none");
    this.sound.style.display = "none";
    document.body.appendChild(this.sound);
    this.play = function(){
        this.sound.play();
    }
    this.stop = function(){
        this.sound.pause();
    }
	}
  function colorButtonsDisabled(bool) {
  	console.log("disabling buttons " + bool);
  	document.getElementById("red-button").disabled = true;
  	console.log(document.getElementById("red-button").disabled)
  }

  function randomButton() {<commit_msg>fixed animate button history, added incorrect input handling<commit_after>$(document).ready(function() {
  //Boolean used to check if the game should continue
  var gameOn = false;
  //Ids of the buttons in the game
  const buttons = ['red-button', 'yellow-button', 'blue-button', 'green-button'];
  //Array that stores the button sequence that has been randomly generated throughout the game
  var buttonHistory = [];
  //Index corresponding to the user's position in the button sequence
  var currentIndex = 0;
  //Counter to display length of buttonHistory (probably not be needed)
  var count = 0;
  //Boolean used to check if the device is on
  var deviceOn = false;
  var strictMode = false;
  var colorButtonsDisabled = true

  $("#strict-button").click(function() {
  	console.log("deviceOn: " + deviceOn + " gameOn: " + gameOn + " strictMode: " + strictMode);
  	if (deviceOn && gameOn) {
  		if (strictMode) {
  			strictMode = false;
  			$("#strict-LED").css("background-color","black");
  		} else {
				strictMode = true;
  			$("#strict-LED").css("background-color","red");
  		}
  	}
  });

  $("#on-off-button").click(function() {
  	if (deviceOn) {
  		turnDeviceOff();
  	} else {
  		turnDeviceOn();
  	}
  });

  $(".color-button").mousedown(function() {
  	if (deviceOn && gameOn && !colorButtonsDisabled){
  		console.log("clicked on " + $(this)[0].id)
  		
	// b. Compare button y to button at current index in button history array
			console.log("button id: " + $(this)[0].id + " buttonHistory[currentIndex]: " + buttonHistory[currentIndex])
			if ($(this)[0].id == buttonHistory[currentIndex]){
				animateSingleButton($(this)[0].id);
				if (currentIndex < buttonHistory.length - 1) {
					currentIndex++;
				} else {
					colorButtonsDisabled = true;
					setTimeout(function(){computerTurn()},2000);
				}
			} else {
					if (strictMode) {
						$("#count-box").html = "!!";
						colorButtonsDisabled = true;
						setTimeout(function(){restartGame()},2000);
					} else {
						colorButtonsDisabled = true;
						setTimeout(function(){replay()},2000);
					}
				}
	// c. If equal and current index < length of button history array: i. Increment current index
	// ii. Wait for user to click on any button
	// d. If equal and current index = length of button history array
	// i. Computer’s Turn (call computer_turn function)
	// e. If not equal
	// i. If strict (check color of LED using jquery)
	// 1. Indicate in count-box wrong click
	// 2. Restart Game
	// ii. If not strict
	// 1. Set current index = 0
	// 2. Animate Button History Array
	// 3. Wait for user to click on any button
  	}
  });
  function replay() {
  	// d. Set current index = 0
		currentIndex = 0;
		// e. Disable color buttons
		colorButtonsDisabled = true;
		// f. Animate button history array (including sounds)
		animateButtonHistory();
		// g. Enable color buttons
		colorButtonsDisabled = false;
		// h. Wait for user to click on any button
		//(don’t need to call anything since jquery handlers are listening)
  }

  $("#start-button").click(function(){
  	if (deviceOn) {
  		restartGame();
  	}
  });

  function restartGame() {
  	// 0. Turn game on
  	gameOn = true;
		// a. Set count = 0
		count = 0;
		// b. Count-box set to zero
		$("#count-box").html(count);
		// c. Button history array is empty
		buttonHistory = [];
		// d. Start game engine (Computer Turn)
		computerTurn();
  }

  function turnDeviceOn() {
  	gameOn = false;
  	deviceOn = true;
  }

  function turnDeviceOff() {
		// a. Set game_on variable to false
		gameOn = false;
		deviceOn = false;
		// b. Strict-LED set off
		$("#strict-LED").css("background-color","black");
		// c. Count set to “”
		$("#count-box").html("");
		// d. count set to 0
		count = 0;
		buttonHistory = [];
  }

  function computerTurn() {
  	// a. Increment count
  	count++;
  	$("#count-box").html(count);
		// b. Randomly generate button x: Function that randomly generates a button (Math.random 0-0.25, 0.25-0.5, 0.5-0.75, 0.75-1 => a,b,c,d)
		var nextButton = randomButton();
		// c. Append button x to button history array
		buttonHistory.push(nextButton);
		// d. Set current index = 0
		currentIndex = 0;
		// e. Disable color buttons
		colorButtonsDisabled = true;
		// f. Animate button history array (including sounds)
		animateButtonHistory();
		// g. Enable color buttons
		colorButtonsDisabled = false;
		// h. Wait for user to click on any button
		//(don’t need to call anything since jquery handlers are listening)
  }

  function animateButtonHistory() {
		// a. Set loop index = 0
		var loopIndex = 0;
		// var bH = ["red-button","yellow-button","blue-button"];
		// eval("setTimeout(function(){animateSingleButton(bH[0])}, 2000);" +
		// "setTimeout(function(){animateSingleButton(bH[1])}, 4000);" +
		// "setTimeout(function(){animateSingleButton(bH[2])}, 6000);")
		var evalString = "";
		// b. While current index < length of button history array AND game_on (is true)
		while ((loopIndex < buttonHistory.length) && gameOn && deviceOn){
			console.log(buttonHistory[loopIndex])
			evalString = evalString + "setTimeout(function(){animateSingleButton('" + buttonHistory[loopIndex] + "')}, " + loopIndex * 2000 + ");"

			// This doesn't work:
		 	// setTimeout(function() {
		 	// 	animateSingleButton.bind(this,buttonHistory[loopIndex]);
		 	// }.bind(this,loopIndex,buttonHistory), loopIndex*2000);

			loopIndex++;
		}
		console.log(evalString);
		eval(evalString);
  }

  function animateSingleButton(b) {
  	console.log("animate single button: buttonID = " + b);
  	playSound(b);
		// Change button x color to lighter color of button x’s normal color
		$("#" + b).css("opacity","0.5");
		setTimeout(function(){$("#" + b).css("opacity","1")}, 1000);
  }

  function playSound(buttonID) {
  //Play sound for button x (store sound urls in sound_array in same order as button names)
  //for a duration (window.setInterval(callback, duration_in_milliseconds) maybe)
  	const soundMap = {
  	"red-button": "sounds/redSound.mp3",
  	"yellow-button":"sounds/yellowSound.mp3",
  	"blue-button": "sounds/blueSound.mp3",
  	"green-button": "sounds/greenSound.mp3"};

  	var buttonSound = new sound(soundMap[buttonID])
  	buttonSound.play();
	}

  function sound(src) {
    this.sound = document.createElement("audio");
    this.sound.src = src;
    this.sound.setAttribute("preload", "auto");
    this.sound.setAttribute("controls", "none");
    this.sound.style.display = "none";
    document.body.appendChild(this.sound);
    this.play = function(){
        this.sound.play();
    }
    this.stop = function(){
        this.sound.pause();
    }
	}
  function colorButtonsDisabled(bool) {
  	console.log("disabling buttons " + bool);
  	document.getElementById("red-button").disabled = true;
  	console.log(document.getElementById("red-button").disabled)
  }

  function randomButton() {
{'fraction_non_alphanumeric': 0.1486745448738422, 'fraction_numerical': 0.011418077291600128, 'mean_word_length': 3.2674616695059626, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5271525', 'n_tokens_mistral': 4299, 'n_tokens_neox': 3940, 'n_words': 1116}