From 18215f4e71e1bcb8d1aa90380954cda1b5cc54dc Mon Sep 17 00:00:00 2001 From: Lurkars Date: Tue, 18 Apr 2017 23:04:17 +0200 Subject: [PATCH] message updates; disable new game on cpu due to corrupt game start --- index.html | 2 +- js/game.js | 61 +++++++++++++++++++++++++++++++++---------------- js/interface.js | 14 ++++++++---- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/index.html b/index.html index 6c8f881..96580d5 100644 --- a/index.html +++ b/index.html @@ -70,7 +70,7 @@

GitHub


-

Created by Lukas Haubaum. Design and code inspiration by Gabriele Cirulli's 2048.

+

Created by Lukas Haubaum. Design and code inspiration by Gabriele Cirulli's 2048.

diff --git a/js/game.js b/js/game.js index 3b6da7e..3835215 100644 --- a/js/game.js +++ b/js/game.js @@ -8,9 +8,8 @@ function Game(Interface) { self.Interface.on("addPoints", self.addPoints.bind(this)); self.Interface.on("rollDices", self.rollDices.bind(this)); self.Interface.on("toggleDice", self.toggleDice.bind(this)); + self.Interface.on("toggleCpu", self.toggleCpu.bind(this)); - - self.cpuSpeed = 1500; self.setup(); } @@ -48,6 +47,10 @@ Game.prototype.setup = function() { self.points = 0; + self.animationTime = 500; + self.messageTime = 1500; + self.cpuSpeed = 1500; + self.Interface.setPlaying(self.playing); self.Interface.setPoints(self.points); self.Interface.setPlayer(self.player); @@ -55,7 +58,8 @@ Game.prototype.setup = function() { self.Interface.setDices(self.dices); 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() { self.rollDices(); }, 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) { self.rollDices(true); } else if (self.calculatePoints(true) == 0) { - self.Interface.animateDices(self.dices, function() { + self.Interface.animateDices(self.dices, self.animationTime, function() { if (self.playing) { self.player.zilch++; @@ -130,10 +127,18 @@ Game.prototype.rollDices = function(all) { self.points = -500; } + self.Interface.disableTakePoints(true); + self.Interface.disableRollDices(true); self.Interface.setPoints(self.points); + self.Interface.setPlayer(self.player); - self.Interface.showMessage("Zilch!", 1500, function() { - self.Interface.showMessage("CPU's turn!", self.cpuSpeed, function() { + self.Interface.showMessage("Zilch!", self.messageTime, 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(); }); }); @@ -159,9 +164,17 @@ Game.prototype.rollDices = function(all) { self.points = -500; } + self.Interface.disableTakePoints(true); + self.Interface.disableRollDices(true); 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.endRound(); }); @@ -171,7 +184,7 @@ Game.prototype.rollDices = function(all) { }); } else { - self.Interface.animateDices(self.dices); + self.Interface.animateDices(self.dices, self.animationTime); self.Interface.disableTakePoints(true); self.Interface.disableRollDices(true); self.Interface.setDices(self.dices); @@ -316,6 +329,7 @@ Game.prototype.takePoints = function() { if (self.playing) { self.player.score += self.points; self.player.zilch = 0; + self.Interface.setPlayer(self.player); var history = {}; history['player'] = self.points; self.history.push(history); @@ -323,15 +337,19 @@ Game.prototype.takePoints = function() { } else { self.cpu.score += self.points; self.cpu.zilch = 0; + self.Interface.setCpu(self.cpu); var history = {}; history['cpu'] = self.points; self.history.push(history); } - self.Interface.showMessage("") + + self.Interface.disableTakePoints(true); + self.Interface.disableRollDices(true); + self.Interface.setPoints(self.points); if (self.playing) { - self.Interface.showMessage("CPU's turn!", self.cpuSpeed, function() { + self.Interface.showMessage("CPU's turn!", self.messageTime, function() { self.endRound(); }); } else { @@ -362,8 +380,6 @@ Game.prototype.endRound = function() { self.Interface.setPlayer(self.player); self.Interface.setCpu(self.cpu); - self.Interface.disableTakePoints(true); - // check score var checkScore = self.playing && self.cpuStarts || !self.playing && !self.cpuStarts; @@ -379,6 +395,7 @@ Game.prototype.endRound = function() { // continue if (self.playing) { self.Interface.disableRollDices(false); + self.Interface.disableRestart(false); } if (!self.playing) { @@ -392,7 +409,7 @@ Game.prototype.endRound = function() { Game.prototype.cpuPlay = function() { var self = this; - + self.Interface.disableRestart(true); setTimeout(function() { // first select all available dices @@ -432,9 +449,13 @@ Game.prototype.cpuPlay = function() { self.addPoints(); self.rollDices(); } + }, self.cpuSpeed); }, 500); +} + +Game.prototype.toggleCpu = function() { } diff --git a/js/interface.js b/js/interface.js index 12b9614..cbbdd5c 100644 --- a/js/interface.js +++ b/js/interface.js @@ -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; for (var diceIndex = 0; diceIndex < 6; diceIndex++) { var dice = dices[diceIndex]; @@ -129,7 +129,11 @@ Interface.prototype.animateDices = function(dices, callback) { if (callback) { callback(); } - }, 500); + }, timeout ? timeout : 0); +}; + +Interface.prototype.disableRestart = function(disabled) { + this.restartButton.disabled = disabled; }; Interface.prototype.disableTakePoints = function(disabled) { @@ -178,17 +182,17 @@ Interface.prototype.setCpu = function(cpu) { this.cpuZilch.innerHTML = zilchs; }; -Interface.prototype.showMessage = function(message, fade, callback) { +Interface.prototype.showMessage = function(message, timeout, callback) { var self = this; self.message.innerHTML = '

' + message + '

'; self.message.classList.add('visible'); - if (fade) { + if (timeout) { setTimeout(function() { self.clearMessage(callback); - }, fade); + }, timeout); } else if (callback) { callback(); }