message updates; disable new game on cpu due to corrupt game start
This commit is contained in:
+1
-1
@@ -70,7 +70,7 @@
|
|||||||
<p><a href="https://github.com/lurkars/openzilch.js" target="_blank">GitHub</a></p>
|
<p><a href="https://github.com/lurkars/openzilch.js" target="_blank">GitHub</a></p>
|
||||||
</div>
|
</div>
|
||||||
<hr />
|
<hr />
|
||||||
<p>Created by Lukas Haubaum. Design and code inspiration by <a href="https://gabrielecirulli.github.io/2048/" target="_blank">Gabriele Cirulli's 2048</a>.</p>
|
<p>Created by <a href="https://www.champonthis.de">Lukas Haubaum</a>. Design and code inspiration by <a href="https://gabrielecirulli.github.io/2048/" target="_blank">Gabriele Cirulli's 2048</a>.</p>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
+41
-20
@@ -8,9 +8,8 @@ function Game(Interface) {
|
|||||||
self.Interface.on("addPoints", self.addPoints.bind(this));
|
self.Interface.on("addPoints", self.addPoints.bind(this));
|
||||||
self.Interface.on("rollDices", self.rollDices.bind(this));
|
self.Interface.on("rollDices", self.rollDices.bind(this));
|
||||||
self.Interface.on("toggleDice", self.toggleDice.bind(this));
|
self.Interface.on("toggleDice", self.toggleDice.bind(this));
|
||||||
|
self.Interface.on("toggleCpu", self.toggleCpu.bind(this));
|
||||||
|
|
||||||
|
|
||||||
self.cpuSpeed = 1500;
|
|
||||||
self.setup();
|
self.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,6 +47,10 @@ Game.prototype.setup = function() {
|
|||||||
|
|
||||||
self.points = 0;
|
self.points = 0;
|
||||||
|
|
||||||
|
self.animationTime = 500;
|
||||||
|
self.messageTime = 1500;
|
||||||
|
self.cpuSpeed = 1500;
|
||||||
|
|
||||||
self.Interface.setPlaying(self.playing);
|
self.Interface.setPlaying(self.playing);
|
||||||
self.Interface.setPoints(self.points);
|
self.Interface.setPoints(self.points);
|
||||||
self.Interface.setPlayer(self.player);
|
self.Interface.setPlayer(self.player);
|
||||||
@@ -55,7 +58,8 @@ Game.prototype.setup = function() {
|
|||||||
self.Interface.setDices(self.dices);
|
self.Interface.setDices(self.dices);
|
||||||
|
|
||||||
if (self.cpuStarts) {
|
if (self.cpuStarts) {
|
||||||
self.Interface.showMessage("CPU starts!", self.cpuSpeed, function() {
|
self.Interface.disableRestart(true);
|
||||||
|
self.Interface.showMessage("CPU starts!", self.messageTime, function() {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
self.rollDices();
|
self.rollDices();
|
||||||
}, self.cpuSpeed);
|
}, self.cpuSpeed);
|
||||||
@@ -97,17 +101,10 @@ Game.prototype.rollDices = function(all) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (self.playing && self.player.zilch == 3) {
|
|
||||||
self.player.zilch = 0;
|
|
||||||
} else if (self.cpu.zilch == 3) {
|
|
||||||
self.cpu.zilch = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rollCount == 0) {
|
if (rollCount == 0) {
|
||||||
self.rollDices(true);
|
self.rollDices(true);
|
||||||
} else if (self.calculatePoints(true) == 0) {
|
} else if (self.calculatePoints(true) == 0) {
|
||||||
self.Interface.animateDices(self.dices, function() {
|
self.Interface.animateDices(self.dices, self.animationTime, function() {
|
||||||
if (self.playing) {
|
if (self.playing) {
|
||||||
self.player.zilch++;
|
self.player.zilch++;
|
||||||
|
|
||||||
@@ -130,10 +127,18 @@ Game.prototype.rollDices = function(all) {
|
|||||||
self.points = -500;
|
self.points = -500;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.Interface.disableTakePoints(true);
|
||||||
|
self.Interface.disableRollDices(true);
|
||||||
self.Interface.setPoints(self.points);
|
self.Interface.setPoints(self.points);
|
||||||
|
self.Interface.setPlayer(self.player);
|
||||||
|
|
||||||
self.Interface.showMessage("Zilch!", 1500, function() {
|
self.Interface.showMessage("Zilch!", self.messageTime, function() {
|
||||||
self.Interface.showMessage("CPU's turn!", self.cpuSpeed, function() {
|
if (self.player.zilch > 2) {
|
||||||
|
self.player.zilch = 0;
|
||||||
|
self.Interface.setPlayer(self.player);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.Interface.showMessage("CPU's turn!", self.messageTime, function() {
|
||||||
self.endRound();
|
self.endRound();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -159,9 +164,17 @@ Game.prototype.rollDices = function(all) {
|
|||||||
self.points = -500;
|
self.points = -500;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.Interface.disableTakePoints(true);
|
||||||
|
self.Interface.disableRollDices(true);
|
||||||
self.Interface.setPoints(self.points);
|
self.Interface.setPoints(self.points);
|
||||||
|
self.Interface.setCpu(self.cpu);
|
||||||
|
|
||||||
|
self.Interface.showMessage("Zilch!", self.messageTime, function() {
|
||||||
|
if (self.cpu.zilch > 2) {
|
||||||
|
self.cpu.zilch = 0;
|
||||||
|
self.Interface.setCpu(self.cpu);
|
||||||
|
}
|
||||||
|
|
||||||
self.Interface.showMessage("Zilch!", 1500, function() {
|
|
||||||
self.Interface.showMessage("Player's turn!", 0, function() {
|
self.Interface.showMessage("Player's turn!", 0, function() {
|
||||||
self.endRound();
|
self.endRound();
|
||||||
});
|
});
|
||||||
@@ -171,7 +184,7 @@ Game.prototype.rollDices = function(all) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
self.Interface.animateDices(self.dices);
|
self.Interface.animateDices(self.dices, self.animationTime);
|
||||||
self.Interface.disableTakePoints(true);
|
self.Interface.disableTakePoints(true);
|
||||||
self.Interface.disableRollDices(true);
|
self.Interface.disableRollDices(true);
|
||||||
self.Interface.setDices(self.dices);
|
self.Interface.setDices(self.dices);
|
||||||
@@ -316,6 +329,7 @@ Game.prototype.takePoints = function() {
|
|||||||
if (self.playing) {
|
if (self.playing) {
|
||||||
self.player.score += self.points;
|
self.player.score += self.points;
|
||||||
self.player.zilch = 0;
|
self.player.zilch = 0;
|
||||||
|
self.Interface.setPlayer(self.player);
|
||||||
var history = {};
|
var history = {};
|
||||||
history['player'] = self.points;
|
history['player'] = self.points;
|
||||||
self.history.push(history);
|
self.history.push(history);
|
||||||
@@ -323,15 +337,19 @@ Game.prototype.takePoints = function() {
|
|||||||
} else {
|
} else {
|
||||||
self.cpu.score += self.points;
|
self.cpu.score += self.points;
|
||||||
self.cpu.zilch = 0;
|
self.cpu.zilch = 0;
|
||||||
|
self.Interface.setCpu(self.cpu);
|
||||||
var history = {};
|
var history = {};
|
||||||
history['cpu'] = self.points;
|
history['cpu'] = self.points;
|
||||||
self.history.push(history);
|
self.history.push(history);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.Interface.showMessage("")
|
|
||||||
|
self.Interface.disableTakePoints(true);
|
||||||
|
self.Interface.disableRollDices(true);
|
||||||
|
self.Interface.setPoints(self.points);
|
||||||
|
|
||||||
if (self.playing) {
|
if (self.playing) {
|
||||||
self.Interface.showMessage("CPU's turn!", self.cpuSpeed, function() {
|
self.Interface.showMessage("CPU's turn!", self.messageTime, function() {
|
||||||
self.endRound();
|
self.endRound();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -362,8 +380,6 @@ Game.prototype.endRound = function() {
|
|||||||
self.Interface.setPlayer(self.player);
|
self.Interface.setPlayer(self.player);
|
||||||
self.Interface.setCpu(self.cpu);
|
self.Interface.setCpu(self.cpu);
|
||||||
|
|
||||||
self.Interface.disableTakePoints(true);
|
|
||||||
|
|
||||||
// check score
|
// check score
|
||||||
var checkScore = self.playing && self.cpuStarts || !self.playing && !self.cpuStarts;
|
var checkScore = self.playing && self.cpuStarts || !self.playing && !self.cpuStarts;
|
||||||
|
|
||||||
@@ -379,6 +395,7 @@ Game.prototype.endRound = function() {
|
|||||||
// continue
|
// continue
|
||||||
if (self.playing) {
|
if (self.playing) {
|
||||||
self.Interface.disableRollDices(false);
|
self.Interface.disableRollDices(false);
|
||||||
|
self.Interface.disableRestart(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!self.playing) {
|
if (!self.playing) {
|
||||||
@@ -392,7 +409,7 @@ Game.prototype.endRound = function() {
|
|||||||
|
|
||||||
Game.prototype.cpuPlay = function() {
|
Game.prototype.cpuPlay = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
self.Interface.disableRestart(true);
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
|
||||||
// first select all available dices
|
// first select all available dices
|
||||||
@@ -432,9 +449,13 @@ Game.prototype.cpuPlay = function() {
|
|||||||
self.addPoints();
|
self.addPoints();
|
||||||
self.rollDices();
|
self.rollDices();
|
||||||
}
|
}
|
||||||
|
|
||||||
}, self.cpuSpeed);
|
}, self.cpuSpeed);
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Game.prototype.toggleCpu = function() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-5
@@ -112,7 +112,7 @@ Interface.prototype.setDices = function(dices) {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Interface.prototype.animateDices = function(dices, callback) {
|
Interface.prototype.animateDices = function(dices, timeout, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
for (var diceIndex = 0; diceIndex < 6; diceIndex++) {
|
for (var diceIndex = 0; diceIndex < 6; diceIndex++) {
|
||||||
var dice = dices[diceIndex];
|
var dice = dices[diceIndex];
|
||||||
@@ -129,7 +129,11 @@ Interface.prototype.animateDices = function(dices, callback) {
|
|||||||
if (callback) {
|
if (callback) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
}, 500);
|
}, timeout ? timeout : 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
Interface.prototype.disableRestart = function(disabled) {
|
||||||
|
this.restartButton.disabled = disabled;
|
||||||
};
|
};
|
||||||
|
|
||||||
Interface.prototype.disableTakePoints = function(disabled) {
|
Interface.prototype.disableTakePoints = function(disabled) {
|
||||||
@@ -178,17 +182,17 @@ Interface.prototype.setCpu = function(cpu) {
|
|||||||
this.cpuZilch.innerHTML = zilchs;
|
this.cpuZilch.innerHTML = zilchs;
|
||||||
};
|
};
|
||||||
|
|
||||||
Interface.prototype.showMessage = function(message, fade, callback) {
|
Interface.prototype.showMessage = function(message, timeout, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
self.message.innerHTML = '<p>' + message + '</p>';
|
self.message.innerHTML = '<p>' + message + '</p>';
|
||||||
|
|
||||||
self.message.classList.add('visible');
|
self.message.classList.add('visible');
|
||||||
|
|
||||||
if (fade) {
|
if (timeout) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
self.clearMessage(callback);
|
self.clearMessage(callback);
|
||||||
}, fade);
|
}, timeout);
|
||||||
} else if (callback) {
|
} else if (callback) {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user