HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Mancala Game made with Meteor

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
madewithmeteormancalagame

Problem

This is my first project I've written totally from scratch. It hasn't been styled yet so it's pretty ugly and it's missing some features but the core functionality works. I would love to get feedback on things I can improve on and any bad practices that I should work on correcting now while I'm still learning.

Demo

Source

Game class:

```
Mancala.Game = function ( gameId ) {
//if game id is given then find collection and load data into object
if( gameId ) {
var game = Matches.findOne( gameId );
this._id = game._id;
this.boardData = game.boardData;
this.challenger = game.challenger;
this.opponent = game.opponent;
this.status = game.status;
this.turn = game.turn;
this.startTime = game.startTime;
}else{
this.boardData = [4,4,4,4,4,4,0,4,4,4,4,4,4,0];
this.challenger = Meteor.userId();
this.status = 'waiting';
this.turn = this.challenger;//TODO change this to be random
//saves data to db
this.save();
}
}

Mancala.Game.prototype.move = function ( index ) {
if(this.isValidMove( index ) !== true){
return 'invalid move';
}
// Removes stones from clicked pit
var numStones = this.boardData[index];

this.boardData[index] = 0

//adds one stone to each next pit counter clockwise till out
for(var i = numStones; i > 0; i--){

//13 is the end of the board, start over at index 0
if(index === 13) {
index = 0;
//increment index by 1 to get the next pit
}else {
index += 1;
}

//this skips other players store
if (index === 13 && this.turn ===this.challenger) {
index = 0; //skips opponent store
}else if(index === 6 && this.turn ===this.opponent){
index++; //skips opponent score
}

//if placing the last stone check if its being placed in empty pit on player side or in players store

Solution

Add more spacing. Everything is very clumped.

For example,

}else {


Looks a little neater as

} else {


And

[4,4,4,4,4,4,0,4,4,4,4,4,4,0]


Looks neater as

[4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]


At one point you do:

index += 1


This could easily be changed to:

index++


or

++index


You're forgetting semicolons in a lot of places.

While they aren't necessary, they show the JavaScript-ness in your code.

At the end of Mancala.Game.prototype.gameOver, you have an unnecessary conditional check.

You wrote:

if(challengerPits === 0 || opponentPits === 0) {

    // code

    return true;
}else {
    return false;
}


Which could easily be shortened to (I believe this will enhance efficiency):

if(challengerPits === 0 || opponentPits === 0) {

    // code

    return true;
}

return false;


Or, you can even omit the return false part entirely, as long as you don't check that the return statement from this function is false and that you only check if it is true (because if true is returned, then undefined will be returned which will not pass a check of == true)

Code Snippets

[4,4,4,4,4,4,0,4,4,4,4,4,4,0]
[4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]
if(challengerPits === 0 || opponentPits === 0) {

    // code

    return true;
}else {
    return false;
}
if(challengerPits === 0 || opponentPits === 0) {

    // code

    return true;
}

return false;

Context

StackExchange Code Review Q#78926, answer score: 4

Revisions (0)

No revisions yet.