|
|
@ -2,7 +2,7 @@ |
|
|
|
* Game (Model + Controller) |
|
|
|
*/ |
|
|
|
function Game(Interface) { |
|
|
|
var self = this; |
|
|
|
let self = this; |
|
|
|
|
|
|
|
// get interface
|
|
|
|
self.Interface = new Interface; |
|
|
@ -20,7 +20,7 @@ function Game(Interface) { |
|
|
|
/** |
|
|
|
* Restart game (reset) |
|
|
|
*/ |
|
|
|
Game.prototype.restart = function() { |
|
|
|
Game.prototype.restart = function () { |
|
|
|
this.Interface.setup(); |
|
|
|
this.setup(); |
|
|
|
}; |
|
|
@ -28,8 +28,8 @@ Game.prototype.restart = function() { |
|
|
|
/** |
|
|
|
* Setup new game |
|
|
|
*/ |
|
|
|
Game.prototype.setup = function() { |
|
|
|
var self = this; |
|
|
|
Game.prototype.setup = function () { |
|
|
|
let self = this; |
|
|
|
self.player = { |
|
|
|
score: 0, |
|
|
|
zilch: 0 |
|
|
@ -40,8 +40,8 @@ Game.prototype.setup = function() { |
|
|
|
}; |
|
|
|
self.dices = [] |
|
|
|
|
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var dice = {}; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = {}; |
|
|
|
dice.value = this.random(6); |
|
|
|
dice.disabled = true; |
|
|
|
self.dices[i] = dice; |
|
|
@ -67,13 +67,13 @@ Game.prototype.setup = function() { |
|
|
|
|
|
|
|
if (self.cpuStarts) { |
|
|
|
self.Interface.disableRestart(true); |
|
|
|
self.Interface.showMessage("CPU starts!", self.messageTime, function() { |
|
|
|
setTimeout(function() { |
|
|
|
self.Interface.showMessage("CPU starts!", self.messageTime, function () { |
|
|
|
setTimeout(function () { |
|
|
|
self.rollDices(); |
|
|
|
}, self.cpuSpeed); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
self.Interface.showMessage("Player starts!", 0, function() { |
|
|
|
self.Interface.showMessage("Player starts!", 0, function () { |
|
|
|
self.Interface.disableRestart(false); |
|
|
|
self.Interface.disableRollDices(false); |
|
|
|
}); |
|
|
@ -85,15 +85,15 @@ Game.prototype.setup = function() { |
|
|
|
/** |
|
|
|
* Random helper for integer numbers |
|
|
|
*/ |
|
|
|
Game.prototype.random = function(int) { |
|
|
|
Game.prototype.random = function (int) { |
|
|
|
return Math.floor((Math.random() * int)); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Roll dices |
|
|
|
*/ |
|
|
|
Game.prototype.rollDices = function(all) { |
|
|
|
var self = this; |
|
|
|
Game.prototype.rollDices = function (all) { |
|
|
|
let self = this; |
|
|
|
self.Interface.clearMessage(); |
|
|
|
|
|
|
|
if (self.newRound) { |
|
|
@ -102,10 +102,10 @@ Game.prototype.rollDices = function(all) { |
|
|
|
self.newRound = false; |
|
|
|
} |
|
|
|
|
|
|
|
var rollCount = 0; |
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
let rollCount = 0; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
self.dices[i] = self.dices[i] || {}; |
|
|
|
var dice = self.dices[i]; |
|
|
|
let dice = self.dices[i]; |
|
|
|
if (all || !dice.disabled) { |
|
|
|
dice.value = this.random(6); |
|
|
|
if (all) { |
|
|
@ -122,11 +122,11 @@ Game.prototype.rollDices = function(all) { |
|
|
|
if (rollCount == 0) { |
|
|
|
self.rollDices(true); |
|
|
|
} else if (self.calculatePoints(true) == 0) { |
|
|
|
self.Interface.animateDices(self.dices, self.animationTime, function() { |
|
|
|
self.Interface.animateDices(self.dices, self.animationTime, function () { |
|
|
|
if (self.playing) { |
|
|
|
self.player.zilch++; |
|
|
|
|
|
|
|
var history = {}; |
|
|
|
let history = {}; |
|
|
|
history['player'] = 'Zilch'; |
|
|
|
self.history.push(history); |
|
|
|
self.points = 0; |
|
|
@ -139,7 +139,7 @@ Game.prototype.rollDices = function(all) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var history = {}; |
|
|
|
let history = {}; |
|
|
|
history['player'] = '-500'; |
|
|
|
self.history.push(history); |
|
|
|
self.points = -500; |
|
|
@ -150,7 +150,7 @@ Game.prototype.rollDices = function(all) { |
|
|
|
self.Interface.setPoints(self.points); |
|
|
|
self.Interface.setPlayer(self.player); |
|
|
|
|
|
|
|
self.Interface.showMessage("Zilch!", self.messageTime, function() { |
|
|
|
self.Interface.showMessage("Zilch!", self.messageTime, function () { |
|
|
|
if (self.player.zilch > 2) { |
|
|
|
self.player.zilch = 0; |
|
|
|
self.Interface.setPlayer(self.player); |
|
|
@ -162,7 +162,7 @@ Game.prototype.rollDices = function(all) { |
|
|
|
} else { |
|
|
|
self.cpu.zilch++; |
|
|
|
|
|
|
|
var history = {}; |
|
|
|
let history = {}; |
|
|
|
history['cpu'] = 'Zilch'; |
|
|
|
self.history.push(history); |
|
|
|
self.points = 0; |
|
|
@ -174,7 +174,7 @@ Game.prototype.rollDices = function(all) { |
|
|
|
self.cpu.score -= 500; |
|
|
|
} |
|
|
|
|
|
|
|
var history = {}; |
|
|
|
let history = {}; |
|
|
|
history['cpu'] = '-500'; |
|
|
|
self.history.push(history); |
|
|
|
self.points = -500; |
|
|
@ -185,7 +185,7 @@ Game.prototype.rollDices = function(all) { |
|
|
|
self.Interface.setPoints(self.points); |
|
|
|
self.Interface.setCpu(self.cpu); |
|
|
|
|
|
|
|
self.Interface.showMessage("Zilch!", self.messageTime, function() { |
|
|
|
self.Interface.showMessage("Zilch!", self.messageTime, function () { |
|
|
|
if (self.cpu.zilch > 2) { |
|
|
|
self.cpu.zilch = 0; |
|
|
|
self.Interface.setCpu(self.cpu); |
|
|
@ -211,9 +211,9 @@ Game.prototype.rollDices = function(all) { |
|
|
|
/** |
|
|
|
* Toggle dice selection |
|
|
|
*/ |
|
|
|
Game.prototype.toggleDice = function(diceIndex) { |
|
|
|
var self = this; |
|
|
|
var dice = self.dices[diceIndex]; |
|
|
|
Game.prototype.toggleDice = function (diceIndex) { |
|
|
|
let self = this; |
|
|
|
let dice = self.dices[diceIndex]; |
|
|
|
|
|
|
|
if (!dice || dice.disabled) { |
|
|
|
console.error("This should not happen!") |
|
|
@ -222,15 +222,15 @@ Game.prototype.toggleDice = function(diceIndex) { |
|
|
|
|
|
|
|
|
|
|
|
dice.selected = !dice.selected; |
|
|
|
var points = self.calculatePoints(); |
|
|
|
var valid = true; |
|
|
|
let points = self.calculatePoints(); |
|
|
|
let valid = true; |
|
|
|
|
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var toggleDice = self.dices[i]; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let toggleDice = self.dices[i]; |
|
|
|
|
|
|
|
if (toggleDice.selected && !toggleDice.disabled) { |
|
|
|
toggleDice.selected = false; |
|
|
|
var togglePoints = self.calculatePoints(); |
|
|
|
let togglePoints = self.calculatePoints(); |
|
|
|
if (points > togglePoints) { |
|
|
|
toggleDice.invalid = false; |
|
|
|
} else if (togglePoints == points) { |
|
|
@ -265,21 +265,21 @@ Game.prototype.toggleDice = function(diceIndex) { |
|
|
|
/** |
|
|
|
* Calculate points on selected dices or all dices |
|
|
|
*/ |
|
|
|
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]; |
|
|
|
Game.prototype.calculatePoints = function (all) { |
|
|
|
let self = this; |
|
|
|
let result = [0, 0, 0, 0, 0, 0]; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = self.dices[i]; |
|
|
|
if ((all || dice.selected) && !dice.disabled) { |
|
|
|
result[dice.value]++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var straight = true; |
|
|
|
var pairs = 0; |
|
|
|
var triple1 = 0; |
|
|
|
var triple2 = 0; |
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
let straight = true; |
|
|
|
let pairs = 0; |
|
|
|
let triple1 = 0; |
|
|
|
let triple2 = 0; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
straight &= (result[i] == 1); |
|
|
|
if (result[i] == 2) { |
|
|
|
pairs++; |
|
|
@ -290,15 +290,15 @@ Game.prototype.calculatePoints = function(all) { |
|
|
|
triple2 = i + 1; |
|
|
|
} |
|
|
|
} |
|
|
|
var points = 0; |
|
|
|
let points = 0; |
|
|
|
|
|
|
|
if (straight) { |
|
|
|
points += 1500; |
|
|
|
} else if (pairs == 3) { |
|
|
|
points += 1500; |
|
|
|
} else if (triple1) { |
|
|
|
var triple1Points = triple1 * (triple1 == 1 ? 1000 : 100); |
|
|
|
for (var i = 0; i < result[triple1 - 1] - 3; i++) { |
|
|
|
let triple1Points = triple1 * (triple1 == 1 ? 1000 : 100); |
|
|
|
for (let i = 0; i < result[triple1 - 1] - 3; i++) { |
|
|
|
triple1Points *= 2; |
|
|
|
} |
|
|
|
points += triple1Points; |
|
|
@ -324,13 +324,13 @@ Game.prototype.calculatePoints = function(all) { |
|
|
|
/** |
|
|
|
* Add points for current player |
|
|
|
*/ |
|
|
|
Game.prototype.addPoints = function() { |
|
|
|
var self = this; |
|
|
|
Game.prototype.addPoints = function () { |
|
|
|
let self = this; |
|
|
|
|
|
|
|
self.points += self.calculatePoints(); |
|
|
|
|
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var dice = self.dices[i]; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = self.dices[i]; |
|
|
|
if (dice.selected) { |
|
|
|
dice.selected = false; |
|
|
|
dice.disabled = true; |
|
|
@ -345,8 +345,8 @@ Game.prototype.addPoints = function() { |
|
|
|
/** |
|
|
|
* Take points and end round |
|
|
|
*/ |
|
|
|
Game.prototype.takePoints = function() { |
|
|
|
var self = this; |
|
|
|
Game.prototype.takePoints = function () { |
|
|
|
let self = this; |
|
|
|
|
|
|
|
self.addPoints(); |
|
|
|
|
|
|
@ -354,7 +354,7 @@ Game.prototype.takePoints = function() { |
|
|
|
self.player.score += self.points; |
|
|
|
self.player.zilch = 0; |
|
|
|
self.Interface.setPlayer(self.player); |
|
|
|
var history = {}; |
|
|
|
let history = {}; |
|
|
|
history['player'] = self.points; |
|
|
|
self.history.push(history); |
|
|
|
|
|
|
@ -362,7 +362,7 @@ Game.prototype.takePoints = function() { |
|
|
|
self.cpu.score += self.points; |
|
|
|
self.cpu.zilch = 0; |
|
|
|
self.Interface.setCpu(self.cpu); |
|
|
|
var history = {}; |
|
|
|
let history = {}; |
|
|
|
history['cpu'] = self.points; |
|
|
|
self.history.push(history); |
|
|
|
} |
|
|
@ -378,13 +378,13 @@ Game.prototype.takePoints = function() { |
|
|
|
/** |
|
|
|
* End round for current player |
|
|
|
*/ |
|
|
|
Game.prototype.endRound = function() { |
|
|
|
var self = this; |
|
|
|
Game.prototype.endRound = function () { |
|
|
|
let self = this; |
|
|
|
|
|
|
|
self.newRound = true; |
|
|
|
|
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var dice = self.dices[i]; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = self.dices[i]; |
|
|
|
if (dice.disabled) { |
|
|
|
dice.selected = true; |
|
|
|
} else { |
|
|
@ -400,7 +400,7 @@ Game.prototype.endRound = function() { |
|
|
|
self.Interface.setCpu(self.cpu); |
|
|
|
|
|
|
|
// check score
|
|
|
|
var checkScore = self.playing && self.cpuStarts || !self.playing && !self.cpuStarts; |
|
|
|
let checkScore = self.playing && self.cpuStarts || !self.playing && !self.cpuStarts; |
|
|
|
|
|
|
|
if (checkScore && self.player.score >= 10000 && self.player.score > self.cpu.score) { |
|
|
|
self.Interface.disableRestart(false); |
|
|
@ -421,9 +421,9 @@ Game.prototype.endRound = function() { |
|
|
|
self.Interface.disableRestart(false); |
|
|
|
self.Interface.showMessage("Player's turn!"); |
|
|
|
} else { |
|
|
|
self.Interface.showMessage("CPU's turn!", self.messageTime, function() { |
|
|
|
self.Interface.showMessage("CPU's turn!", self.messageTime, function () { |
|
|
|
if (!self.playing) { |
|
|
|
setTimeout(function() { |
|
|
|
setTimeout(function () { |
|
|
|
self.rollDices(); |
|
|
|
}, self.cpuspeed); |
|
|
|
} |
|
|
@ -436,23 +436,23 @@ Game.prototype.endRound = function() { |
|
|
|
/** |
|
|
|
* CPU playing logic |
|
|
|
*/ |
|
|
|
Game.prototype.cpuPlay = function() { |
|
|
|
var self = this; |
|
|
|
Game.prototype.cpuPlay = function () { |
|
|
|
let self = this; |
|
|
|
self.Interface.disableRestart(true); |
|
|
|
setTimeout(function() { |
|
|
|
setTimeout(function () { |
|
|
|
|
|
|
|
// first select all available dices
|
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var dice = self.dices[i]; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = self.dices[i]; |
|
|
|
if (!dice.disabled) { |
|
|
|
self.toggleDice(i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// check if dice gains points
|
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var dice = self.dices[i]; |
|
|
|
var tmpPoints = self.calculatePoints(); |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = self.dices[i]; |
|
|
|
let tmpPoints = self.calculatePoints(); |
|
|
|
if (!dice.disabled) { |
|
|
|
self.toggleDice(i); |
|
|
|
if (self.calculatePoints() < tmpPoints) { |
|
|
@ -462,17 +462,18 @@ Game.prototype.cpuPlay = function() { |
|
|
|
} |
|
|
|
|
|
|
|
// count free dices
|
|
|
|
var freeDices = 0; |
|
|
|
for (var i = 0; i < 6; i++) { |
|
|
|
var dice = self.dices[i]; |
|
|
|
let freeDices = 0; |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
let dice = self.dices[i]; |
|
|
|
if (!dice.disabled && !dice.selected) { |
|
|
|
freeDices++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
setTimeout(function() { |
|
|
|
setTimeout(function () { |
|
|
|
let cpuPoints = self.points + self.calculatePoints(); |
|
|
|
// 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) { |
|
|
|
if (cpuPoints >= 300 && freeDices < 4 && freeDices > 0 && (self.player.score < 10000 || (self.cpu.score + cpuPoints) > self.player.score)) { |
|
|
|
self.takePoints(); |
|
|
|
} else { |
|
|
|
self.addPoints(); |
|
|
@ -485,4 +486,4 @@ Game.prototype.cpuPlay = function() { |
|
|
|
} |
|
|
|
|
|
|
|
// create new game
|
|
|
|
var game = new Game(Interface); |
|
|
|
let game = new Game(Interface); |