Browse Source

Messages and interface improvements; shortend zilch calculation

master
Lurkars 8 years ago
parent
commit
075143bc21
  1. 72
      js/game.js
  2. 2
      js/interface.js
  3. 6
      style/main.css

72
js/game.js

@ -55,7 +55,7 @@ Game.prototype.setup = function() {
self.Interface.setDices(self.dices);
if (self.cpuStarts) {
self.Interface.showMessage("CPU starts!", 1000, function() {
self.Interface.showMessage("CPU starts!", self.cpuSpeed, function() {
setTimeout(function() {
self.rollDices();
}, self.cpuSpeed);
@ -77,6 +77,12 @@ Game.prototype.rollDices = function(all) {
var self = this;
self.Interface.clearMessage();
if (self.newRound) {
self.points = 0;
self.Interface.setPoints(self.points);
self.newRound = false;
}
var rollCount = 0;
for (var i = 0; i < 6; i++) {
self.dices[i] = self.dices[i] || {};
@ -100,15 +106,15 @@ Game.prototype.rollDices = function(all) {
if (rollCount == 0) {
self.rollDices(true);
} else if (self.checkZilch(rollCount == 6)) {
} else if (self.calculatePoints(true) == 0) {
self.Interface.animateDices(self.dices, function() {
if (self.playing) {
self.player.zilch++;
var history = {};
history['player'] = 'Zilch';
self.history.push(history);
self.points = 0;
if (self.player.zilch > 2) {
if (self.player.score < 500) {
@ -121,13 +127,24 @@ Game.prototype.rollDices = function(all) {
var history = {};
history['player'] = '-500';
self.history.push(history);
self.points = -500;
}
self.Interface.setPoints(self.points);
self.Interface.showMessage("Zilch!", 1500, function() {
self.Interface.showMessage("CPU's turn!", self.cpuSpeed, function() {
self.endRound();
});
});
} else {
self.cpu.zilch++;
var history = {};
history['cpu'] = 'Zilch';
self.history.push(history);
self.points = 0;
if (self.cpu.zilch > 2) {
if (self.cpu.score < 500) {
@ -139,15 +156,20 @@ Game.prototype.rollDices = function(all) {
var history = {};
history['cpu'] = '-500';
self.history.push(history);
self.points = -500;
}
}
self.Interface.setPoints(self.points);
self.Interface.showMessage("Zilch!", 1000, function() {
self.Interface.showMessage("Zilch!", 1500, function() {
self.Interface.showMessage("Player's turn!", 0, function() {
self.endRound();
});
});
}
});
} else {
self.Interface.animateDices(self.dices);
self.Interface.disableTakePoints(true);
@ -160,22 +182,6 @@ Game.prototype.rollDices = function(all) {
}
};
Game.prototype.checkZilch = function(all) {
var self = this;
var rawPoints = [0, 0, 0, 0, 0, 0];
for (var i = 0; i < 6; i++) {
var dice = self.dices[i];
if (all || !dice.disabled) {
rawPoints[dice.value]++;
}
}
// Zilch?
return rawPoints[0] < 1 && rawPoints[1] < 3 && rawPoints[2] < 3 && rawPoints[3] < 3 && rawPoints[4] < 1 && rawPoints[5] < 3;
}
Game.prototype.toggleDice = function(diceIndex) {
var self = this;
var dice = self.dices[diceIndex];
@ -227,12 +233,12 @@ Game.prototype.toggleDice = function(diceIndex) {
self.Interface.setPoints(self.points + points);
};
Game.prototype.calculatePoints = function(diceIndex) {
Game.prototype.calculatePoints = function(all) {
var self = this;
var result = [0, 0, 0, 0, 0, 0];
for (var i = 0; i < 6; i++) {
var dice = self.dices[i];
if (dice.selected && !dice.disabled) {
if ((all || dice.selected) && !dice.disabled) {
result[dice.value]++;
}
}
@ -322,14 +328,23 @@ Game.prototype.takePoints = function() {
self.history.push(history);
}
self.Interface.showMessage("")
if (self.playing) {
self.Interface.showMessage("CPU's turn!", self.cpuSpeed, function() {
self.endRound();
});
} else {
self.Interface.showMessage("Player's turn!", 0, function() {
self.endRound();
});
}
};
Game.prototype.endRound = function() {
var self = this;
// Reset
self.points = 0;
self.newRound = true;
for (var i = 0; i < 6; i++) {
var dice = self.dices[i];
@ -378,6 +393,8 @@ Game.prototype.endRound = function() {
Game.prototype.cpuPlay = function() {
var self = this;
setTimeout(function() {
// first select all available dices
for (var i = 0; i < 6; i++) {
var dice = self.dices[i];
@ -408,14 +425,15 @@ Game.prototype.cpuPlay = function() {
}
setTimeout(function() {
// strategy: end round if points >= 300 and less than 4 dices left
if (self.points + self.calculatePoints() >= 300 && freeDices < 4 && freeDices > 0) {
// strategy: end round if points >= 300 and less than 4 dices left and not loosing
if (self.points + self.calculatePoints() >= 300 && freeDices < 4 && freeDices > 0 && self.player.score < 10000) {
self.takePoints();
} else {
self.addPoints();
self.rollDices();
}
}, self.cpuSpeed);
}, 500);
}

2
js/interface.js

@ -129,7 +129,7 @@ Interface.prototype.animateDices = function(dices, callback) {
if (callback) {
callback();
}
}, Math.random() * 500 + 500);
}, 500);
};
Interface.prototype.disableTakePoints = function(disabled) {

6
style/main.css

@ -99,7 +99,7 @@ hr {
}
.score-container.active {
color: white;
color: #f9f6f2;
}
.score-container .score {
@ -118,7 +118,7 @@ hr {
}
.score-container.active .zilch {
color: white;
color: #f9f6f2;
}
.dices-container {
@ -267,7 +267,7 @@ hr {
right: 0;
bottom: 0;
left: 0;
background: rgba(238, 228, 218, 0.73);
background: rgba(238, 228, 218, 0.5);
z-index: 100;
text-align: center;
z-index: 5;

Loading…
Cancel
Save