I took a class covering HTML, CSS, Javascript and for a final project, thinking of a previous community challenge, I created Simon Says.
Going through my code my professor deemed it overly complicated (or did he mean complex?), explicitly referring to my showSequence
method in particular.
I'm just wondering what and how could I have simplified things?
function SimonButton(id, litColor, color) {
this.color = color;
this.id = id;
this.litColor = litColor;
this.highlight = function() {
document.getElementById(id).style.background = litColor;
setTimeout(function() {
document.getElementById(id).style.background = color;
}, 750);
}
}
var buttons = [
new SimonButton("alpha", "red", "darkred"),
new SimonButton("beta", "yellow", "darkorange"),
new SimonButton("gamma", "lightblue", "darkblue"),
new SimonButton("delta", "lightgreen", "darkgreen")
];
var sequence = [];
var round = 0;
var sequenceIterator = 0;
var userTurn = false;
playing = false;
window.onload = function() {
alert("Try to reproduce the sequence shown.\nClick start to begin!");
document.getElementById("newGame").onclick = function() {
document.getElementById("newGame").value = "New Game";
reset();
playing = true;
computerTurn();
}
for (var i = 0; i < buttons.length; i++) {
document.getElementById(buttons[i].id).onclick = function() {
if (playing) {
if (!userTurn) {
alert("Wait for the sequence to end before trying to reproduce.");
} else {
if (buttons[sequence[sequenceIterator++]].id != this.id) {
gameOver();
}
if (sequenceIterator === sequence.length && playing) {
sequenceIterator = 0;
document.getElementById("counter").innerHTML = ++round;
computerTurn();
}
}
} else {
alert("Click upper left button to start a New Game.");
}
}
}
}
function computerTurn() {
userTurn = false;
extendSequence(1);
showSequence();
}
function reset() {
sequence = [];
round = 0;
sequenceIterator = 0;
document.getElementById("counter").innerHTML = round;
}
function gameOver() {
playing = false;
alert("Game Over.\nClick New Game to restart.");
}
/* Adds number items to the sequence.
* e.g. if the sequence is 1, 3, 2,
* calling extendSequence(2)
* could result in a sequence 1, 3, 2, 0, 2
*/
function extendSequence(number) {
for (i = 0; i < number; i++) {
sequence.push(Math.floor(Math.random() * 4));
}
}
function showSequence() {
var i = 0;
sequenceLoop();
function sequenceLoop() {
setTimeout(function() {
buttons[sequence[i++]].highlight();
if (i < sequence.length) {
sequenceLoop();
} else {
userTurn = true;
}
}, 1000);
}
}
body {
background-color: #272822;
}
input:focus {
outline: none;
}
input#newGame {
height: 3em;
width: 7em;
font-size: 15px;
font-family: Arial;
font-weight: bold;
border-radius: 6px;
text-decoration: none;
background-color: #A6E22E;
}
input#newGame:hover {
color: #fff;
}
div#counter {
position: relative;
font-weight: bold;
font-size: 30px;
font-family: Arial;
color: #fff;
left: 9em;
}
.simonButton {
height: 20em;
width: 20em;
margin: .5em;
}
.simonButton:hover {
box-shadow: 0 0 .5em #fff;
}
input#alpha {
background-color: darkred;
border-radius: 11em 2em 5em 2em;
}
input#alpha:active {
background-color: red !important;
}
input#beta {
background-color: darkorange;
border-radius: 2em 11em 0em 5em;
}
input#beta:active {
background-color: yellow !important;
}
input#gamma {
background: darkblue;
border-radius: 2em 5em 0em 11em;
}
input#gamma:active {
background-color: lightblue !important;
}
input#delta {
background-color: darkgreen;
border-radius: 5em 2em 11em 0em;
}
input#delta:active {
background-color: lightgreen !important;
}
<html>
<head>
<meta charset = "utf-8">
<title>Simon Says</title>
<script src="simon-script.js"></script>
<link rel="stylesheet" type="text/css" href="simon-sheets.css" />
</head>
<body>
<div>
<input type='button' id='newGame' value='Start'/>
</div>
<div>
<input type='button' id='alpha' class='simonButton'>
<input type='button' id='beta' class='simonButton'>
</div>
<div id='counter'>0</div>
<div>
<input type='button' id='gamma' class='simonButton'>
<input type='button' id='delta' class='simonButton'>
</div>
</body>
</html>