fixed cpu not loosing state
This commit is contained in:
+53
-52
@@ -2,7 +2,7 @@
|
||||
* Game (Model + Controller)
|
||||
*/
|
||||
function Game(Interface) {
|
||||
var self = this;
|
||||
let self = this;
|
||||
|
||||
// get interface
|
||||
self.Interface = new Interface;
|
||||
@@ -29,7 +29,7 @@ Game.prototype.restart = function() {
|
||||
* Setup new game
|
||||
*/
|
||||
Game.prototype.setup = function () {
|
||||
var self = this;
|
||||
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;
|
||||
@@ -93,7 +93,7 @@ Game.prototype.random = function(int) {
|
||||
* Roll dices
|
||||
*/
|
||||
Game.prototype.rollDices = function (all) {
|
||||
var self = this;
|
||||
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) {
|
||||
@@ -126,7 +126,7 @@ Game.prototype.rollDices = function(all) {
|
||||
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;
|
||||
@@ -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;
|
||||
@@ -212,8 +212,8 @@ Game.prototype.rollDices = function(all) {
|
||||
* Toggle dice selection
|
||||
*/
|
||||
Game.prototype.toggleDice = function (diceIndex) {
|
||||
var self = this;
|
||||
var dice = self.dices[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) {
|
||||
@@ -266,20 +266,20 @@ 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];
|
||||
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;
|
||||
@@ -325,12 +325,12 @@ Game.prototype.calculatePoints = function(all) {
|
||||
* Add points for current player
|
||||
*/
|
||||
Game.prototype.addPoints = function () {
|
||||
var self = this;
|
||||
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;
|
||||
@@ -346,7 +346,7 @@ Game.prototype.addPoints = function() {
|
||||
* Take points and end round
|
||||
*/
|
||||
Game.prototype.takePoints = function () {
|
||||
var self = this;
|
||||
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);
|
||||
}
|
||||
@@ -379,12 +379,12 @@ Game.prototype.takePoints = function() {
|
||||
* End round for current player
|
||||
*/
|
||||
Game.prototype.endRound = function () {
|
||||
var self = this;
|
||||
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);
|
||||
@@ -437,22 +437,22 @@ Game.prototype.endRound = function() {
|
||||
* CPU playing logic
|
||||
*/
|
||||
Game.prototype.cpuPlay = function () {
|
||||
var self = this;
|
||||
let self = this;
|
||||
self.Interface.disableRestart(true);
|
||||
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 () {
|
||||
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);
|
||||
Reference in New Issue
Block a user