Browse Source

fixed cpu not loosing state

master
Lurkars 5 years ago
parent
commit
657ffa7ce8
  1. 145
      js/game.js

145
js/game.js

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