patternjavascriptMinor
My Memory Game "Engine"
Viewed 0 times
enginegamememory
Problem
Mostly for the experience (but also so I could waste hours playing it), I made a simple memory card game in JavaScript. I built it so it has a backbone framework that could be easily extended by stripping off my actual implementation. It's the backbone I'd like reviewed here, and mostly:
```
var Card = (function() {
var self = Object.create({}, {
val: {
value: -1
},
index: {
value: -1
},
addTo: {
value: function(game) {
var random = -1;
counter = 0; //break counter to stop infinite loop. :)
while ((game.cards[random] !== undefined && counter <= 100) | random === -1) {
random = Math.round(game.cards.length * Math.random());
counter++;
}
this.index = random;
game.cards[random] = this;
}
},
isMatch: {
value: function(game) {
if (this.val == game.selected.val) {
game.matches++;
return true;
}
return false;
}
}
});
return self;
})();
var Game = (function() {
var self = Object.create({}, {
cards: {
value: new Array(30)
},
matches: {
value: 0
},
init: {
value: function(func) {
for (i = 0; i < this.cards.length / 2; i++) {
var card = Object.create(Card, {
val: {
value: i
}
});
var card2 = Object.create(Card, {
val: {
value: i
- My OO - anything wrong; anything that could be improved on?
- Obvious code efficiency: is there anything that could make it more efficient?
- Structure: Is the structure of the game a good idea?
```
var Card = (function() {
var self = Object.create({}, {
val: {
value: -1
},
index: {
value: -1
},
addTo: {
value: function(game) {
var random = -1;
counter = 0; //break counter to stop infinite loop. :)
while ((game.cards[random] !== undefined && counter <= 100) | random === -1) {
random = Math.round(game.cards.length * Math.random());
counter++;
}
this.index = random;
game.cards[random] = this;
}
},
isMatch: {
value: function(game) {
if (this.val == game.selected.val) {
game.matches++;
return true;
}
return false;
}
}
});
return self;
})();
var Game = (function() {
var self = Object.create({}, {
cards: {
value: new Array(30)
},
matches: {
value: 0
},
init: {
value: function(func) {
for (i = 0; i < this.cards.length / 2; i++) {
var card = Object.create(Card, {
val: {
value: i
}
});
var card2 = Object.create(Card, {
val: {
value: i
Solution
First I see that both classes depend on each other and are tightly coupled together. Why does a Card object need to know its own index in an Array?
Secondly, the Card object updates the game object, but the game object contains an array of the cards, which means the Game object owns a bunch of Card objects.
If you were to remove the dual dependency, then you could rewrite your isMatch method to be
Secondly, the Card object updates the game object, but the game object contains an array of the cards, which means the Game object owns a bunch of Card objects.
If you were to remove the dual dependency, then you could rewrite your isMatch method to be
isMatch: {
value: function(inCard) {
return (this.val == inCard.val);
}
}Code Snippets
isMatch: {
value: function(inCard) {
return (this.val == inCard.val);
}
}Context
StackExchange Code Review Q#2949, answer score: 2
Revisions (0)
No revisions yet.