fixed cpu not loosing state
This commit is contained in:
+53
-52
@@ -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;
|
||||||
@@ -29,7 +29,7 @@ Game.prototype.restart = function() {
|
|||||||
* Setup new game
|
* Setup new game
|
||||||
*/
|
*/
|
||||||
Game.prototype.setup = function () {
|
Game.prototype.setup = function () {
|
||||||
var self = this;
|
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++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = {};
|
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;
|
||||||
@@ -93,7 +93,7 @@ Game.prototype.random = function(int) {
|
|||||||
* Roll dices
|
* Roll dices
|
||||||
*/
|
*/
|
||||||
Game.prototype.rollDices = function (all) {
|
Game.prototype.rollDices = function (all) {
|
||||||
var self = this;
|
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;
|
let rollCount = 0;
|
||||||
for (var i = 0; i < 6; i++) {
|
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) {
|
||||||
@@ -126,7 +126,7 @@ Game.prototype.rollDices = function(all) {
|
|||||||
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;
|
||||||
@@ -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;
|
||||||
@@ -212,8 +212,8 @@ Game.prototype.rollDices = function(all) {
|
|||||||
* Toggle dice selection
|
* Toggle dice selection
|
||||||
*/
|
*/
|
||||||
Game.prototype.toggleDice = function (diceIndex) {
|
Game.prototype.toggleDice = function (diceIndex) {
|
||||||
var self = this;
|
let self = this;
|
||||||
var dice = self.dices[diceIndex];
|
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();
|
let points = self.calculatePoints();
|
||||||
var valid = true;
|
let valid = true;
|
||||||
|
|
||||||
for (var i = 0; i < 6; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var toggleDice = self.dices[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) {
|
||||||
@@ -266,20 +266,20 @@ 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) {
|
Game.prototype.calculatePoints = function (all) {
|
||||||
var self = this;
|
let self = this;
|
||||||
var result = [0, 0, 0, 0, 0, 0];
|
let result = [0, 0, 0, 0, 0, 0];
|
||||||
for (var i = 0; i < 6; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = self.dices[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;
|
let straight = true;
|
||||||
var pairs = 0;
|
let pairs = 0;
|
||||||
var triple1 = 0;
|
let triple1 = 0;
|
||||||
var triple2 = 0;
|
let triple2 = 0;
|
||||||
for (var i = 0; i < 6; i++) {
|
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);
|
let triple1Points = triple1 * (triple1 == 1 ? 1000 : 100);
|
||||||
for (var i = 0; i < result[triple1 - 1] - 3; i++) {
|
for (let i = 0; i < result[triple1 - 1] - 3; i++) {
|
||||||
triple1Points *= 2;
|
triple1Points *= 2;
|
||||||
}
|
}
|
||||||
points += triple1Points;
|
points += triple1Points;
|
||||||
@@ -325,12 +325,12 @@ Game.prototype.calculatePoints = function(all) {
|
|||||||
* Add points for current player
|
* Add points for current player
|
||||||
*/
|
*/
|
||||||
Game.prototype.addPoints = function () {
|
Game.prototype.addPoints = function () {
|
||||||
var self = this;
|
let self = this;
|
||||||
|
|
||||||
self.points += self.calculatePoints();
|
self.points += self.calculatePoints();
|
||||||
|
|
||||||
for (var i = 0; i < 6; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = self.dices[i];
|
let dice = self.dices[i];
|
||||||
if (dice.selected) {
|
if (dice.selected) {
|
||||||
dice.selected = false;
|
dice.selected = false;
|
||||||
dice.disabled = true;
|
dice.disabled = true;
|
||||||
@@ -346,7 +346,7 @@ Game.prototype.addPoints = function() {
|
|||||||
* Take points and end round
|
* Take points and end round
|
||||||
*/
|
*/
|
||||||
Game.prototype.takePoints = function () {
|
Game.prototype.takePoints = function () {
|
||||||
var self = this;
|
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);
|
||||||
}
|
}
|
||||||
@@ -379,12 +379,12 @@ Game.prototype.takePoints = function() {
|
|||||||
* End round for current player
|
* End round for current player
|
||||||
*/
|
*/
|
||||||
Game.prototype.endRound = function () {
|
Game.prototype.endRound = function () {
|
||||||
var self = this;
|
let self = this;
|
||||||
|
|
||||||
self.newRound = true;
|
self.newRound = true;
|
||||||
|
|
||||||
for (var i = 0; i < 6; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = self.dices[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);
|
||||||
@@ -437,22 +437,22 @@ Game.prototype.endRound = function() {
|
|||||||
* CPU playing logic
|
* CPU playing logic
|
||||||
*/
|
*/
|
||||||
Game.prototype.cpuPlay = function () {
|
Game.prototype.cpuPlay = function () {
|
||||||
var self = this;
|
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++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = self.dices[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++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = self.dices[i];
|
let dice = self.dices[i];
|
||||||
var tmpPoints = self.calculatePoints();
|
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;
|
let freeDices = 0;
|
||||||
for (var i = 0; i < 6; i++) {
|
for (let i = 0; i < 6; i++) {
|
||||||
var dice = self.dices[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);
|
||||||
Reference in New Issue
Block a user