patternjavascriptMinor
JavaScript adaptation of 70s electronic game "Simon Says"
Viewed 0 times
javascriptsimonadaptationgamesays70selectronic
Problem
I programmed a web adaption of the game Simon for to improving my JavaScript skills. A live version of the results are here.
I got two more ideas which I like to incorporate. Moreover, I'd like to improve the look. But before I go on I would like to make some improvements. I guess there are a few necessary.
`function SimonSays( timeLapse,
elements,
cssClass,
messageBoard,
display,
breakTime,
board) {
var order = [];
if (!Array.isArray(elements)) {
// Change nodeList into an real array.
elements = Array.prototype.slice.apply(elements);
}
// Every round the count
// of the moves is incremented.
var moves = (function() {
var moves = 1;
return function() {
return moves++;
}
})();
var getRounds = (function() {
var rounds = 1;
return function() {
return rounds++;
}
})();
var incrementDisplay = function() {
var tmp = '000' + getRounds();
display.innerHTML = tmp.slice(-3);
}
var reactToMove = function() {
this.classList.add(cssClass);
var that = this;
setTimeout(function() {
that.classList.remove(cssClass);
}, timeLapse);
var currentFirst = elements[order.shift()].id;
if (currentFirst !== this.id) {
SimonSays.prototype.toggleReact();
messageBoard.style['display'] = 'block';
} else if (!order.length) {
setTimeout(SimonSays.prototype.setUpRound, breakTime);
}
}
SimonSays.prototype.toggleReact = function(direction) {
elements.forEach(function(element) {
direction === true ?
element.addEventListener('click', reactToMove) :
element.removeEventListener('click', reactToMove);
});
}
SimonSays.prototype.setUpRound = function() {
var limit = moves();
order.length = 0;
// Update GUI-counter.
incrementDisplay();
// Put random numbers into an array. =>
I got two more ideas which I like to incorporate. Moreover, I'd like to improve the look. But before I go on I would like to make some improvements. I guess there are a few necessary.
`function SimonSays( timeLapse,
elements,
cssClass,
messageBoard,
display,
breakTime,
board) {
var order = [];
if (!Array.isArray(elements)) {
// Change nodeList into an real array.
elements = Array.prototype.slice.apply(elements);
}
// Every round the count
// of the moves is incremented.
var moves = (function() {
var moves = 1;
return function() {
return moves++;
}
})();
var getRounds = (function() {
var rounds = 1;
return function() {
return rounds++;
}
})();
var incrementDisplay = function() {
var tmp = '000' + getRounds();
display.innerHTML = tmp.slice(-3);
}
var reactToMove = function() {
this.classList.add(cssClass);
var that = this;
setTimeout(function() {
that.classList.remove(cssClass);
}, timeLapse);
var currentFirst = elements[order.shift()].id;
if (currentFirst !== this.id) {
SimonSays.prototype.toggleReact();
messageBoard.style['display'] = 'block';
} else if (!order.length) {
setTimeout(SimonSays.prototype.setUpRound, breakTime);
}
}
SimonSays.prototype.toggleReact = function(direction) {
elements.forEach(function(element) {
direction === true ?
element.addEventListener('click', reactToMove) :
element.removeEventListener('click', reactToMove);
});
}
SimonSays.prototype.setUpRound = function() {
var limit = moves();
order.length = 0;
// Update GUI-counter.
incrementDisplay();
// Put random numbers into an array. =>
Solution
I don't consider myself a javascript expert yet (proof of that is my own attempt at Simon in javascript!), but I see some things that may help you improve your code.
Use responsive design
Unfortunately, the css uses absolute
Amend the gameplay
Each round starts a brand new sequence. That's not necessarily an error, but the way the original worked was that it would add one to the sequence each time, so if the sequence for round 4 was "RGYB", the sequence for round 5 might be "RGYBG".
Is the
There is this line in the code:
Generally, I understand that
Disable input until the sequence is done
Right now, a user can "cheat" by just following the game as it lights each button rather than having to wait for the sequence to be done before replaying. This effectively eliminates the memory aspect of the game. Better would be to disable input until the computer has played the entire sequence once.
Add sound
You probably already have that in mind, but that was an important aspect of the original game.
Use responsive design
Unfortunately, the css uses absolute
px measurements instead of relative measurements, so the result is that on a small screen, like a phone, this isn't playable because most of the game is off the screen. If you're using the Firefox browser, you can use the built-in responsive design view to see how your page looks at various screen sizes.Amend the gameplay
Each round starts a brand new sequence. That's not necessarily an error, but the way the original worked was that it would add one to the sequence each time, so if the sequence for round 4 was "RGYB", the sequence for round 5 might be "RGYBG".
Is the
+ operator needed here?There is this line in the code:
var step = 2 * +i;Generally, I understand that
+i would give the numeric representation of i, but it doesn't seem possible that i is anything but numeric there. Could be I'm missing something. Disable input until the sequence is done
Right now, a user can "cheat" by just following the game as it lights each button rather than having to wait for the sequence to be done before replaying. This effectively eliminates the memory aspect of the game. Better would be to disable input until the computer has played the entire sequence once.
Add sound
You probably already have that in mind, but that was an important aspect of the original game.
Code Snippets
var step = 2 * +i;Context
StackExchange Code Review Q#117860, answer score: 2
Revisions (0)
No revisions yet.