Fix computer moves randomness, rename random() into randInt()
authorBenjamin Auder <benjamin.auder@somewhere>
Tue, 21 Jan 2020 17:05:33 +0000 (18:05 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Tue, 21 Jan 2020 17:05:33 +0000 (18:05 +0100)
client/src/base_rules.js
client/src/utils/alea.js
client/src/variants/Antiking.js
client/src/variants/Baroque.js
client/src/variants/Grand.js
client/src/variants/Losers.js
client/src/variants/Upsidedown.js
client/src/variants/Wildebeest.js
client/src/views/Game.vue

index 15609eb..5fb0027 100644 (file)
@@ -2,7 +2,7 @@
 // Variants generally inherit from it, and modify some parts.
 
 import { ArrayFun } from "@/utils/array";
-import { random, sample, shuffle } from "@/utils/alea";
+import { randInt, sample, shuffle } from "@/utils/alea";
 
 export const PiPo = class PiPo //Piece+Position
 {
@@ -243,25 +243,25 @@ export const ChessRules = class ChessRules
       let positions = ArrayFun.range(8);
 
       // Get random squares for bishops
-      let randIndex = 2 * random(4);
+      let randIndex = 2 * randInt(4);
       const bishop1Pos = positions[randIndex];
       // The second bishop must be on a square of different color
-      let randIndex_tmp = 2 * random(4) + 1;
+      let randIndex_tmp = 2 * randInt(4) + 1;
       const bishop2Pos = positions[randIndex_tmp];
       // Remove chosen squares
       positions.splice(Math.max(randIndex,randIndex_tmp), 1);
       positions.splice(Math.min(randIndex,randIndex_tmp), 1);
 
       // Get random squares for knights
-      randIndex = random(6);
+      randIndex = randInt(6);
       const knight1Pos = positions[randIndex];
       positions.splice(randIndex, 1);
-      randIndex = random(5);
+      randIndex = randInt(5);
       const knight2Pos = positions[randIndex];
       positions.splice(randIndex, 1);
 
       // Get random square for queen
-      randIndex = random(4);
+      randIndex = randInt(4);
       const queenPos = positions[randIndex];
       positions.splice(randIndex, 1);
 
@@ -1199,12 +1199,11 @@ export const ChessRules = class ChessRules
       candidates.push(j);
     let currentBest = moves1[sample(candidates)];
 
-    // From here, depth >= 3: may take a while, so we control time
-    const timeStart = Date.now();
-
     // Skip depth 3+ if we found a checkmate (or if we are checkmated in 1...)
     if (V.SEARCH_DEPTH >= 3 && Math.abs(moves1[0].eval) < V.THRESHOLD_MATE)
     {
+      // From here, depth >= 3: may take a while, so we control time
+      const timeStart = Date.now();
       for (let i=0; i<moves1.length; i++)
       {
         if (Date.now()-timeStart >= 5000) //more than 5 seconds
@@ -1225,7 +1224,7 @@ export const ChessRules = class ChessRules
     candidates = [0];
     for (let j=1; j<moves1.length && moves1[j].eval == moves1[0].eval; j++)
       candidates.push(j);
-    return moves1[sample(candidates)];
+    return moves1[candidates[randInt(candidates.length)]];
   }
 
   alphabeta(depth, alpha, beta)
index 76d9c38..0456393 100644 (file)
@@ -5,7 +5,7 @@ export function getRandString()
     .toUpperCase();
 }
 
-export function random (min, max)
+export function randInt(min, max)
 {
   if (!max)
   {
@@ -22,7 +22,7 @@ export function sample (arr, n)
   let cpArr = arr.map(e => e);
   for (let index = 0; index < n; index++)
   {
-    const rand = random(index, n);
+    const rand = randInt(index, arr.length);
     const temp = cpArr[index];
     cpArr[index] = cpArr[rand];
     cpArr[rand] = temp;
index 890da58..22fd2b4 100644 (file)
@@ -154,21 +154,21 @@ class AntikingRules extends ChessRules
 
                        // Get random squares for bishops, but avoid corners; because,
                        // if an antiking blocks a cornered bishop, it can never be checkmated
-                       let randIndex = 2 * random(1,4);
+                       let randIndex = 2 * randInt(1,4);
                        const bishop1Pos = positions[randIndex];
-                       let randIndex_tmp = 2 * random(3) + 1;
+                       let randIndex_tmp = 2 * randInt(3) + 1;
                        const bishop2Pos = positions[randIndex_tmp];
                        positions.splice(Math.max(randIndex,randIndex_tmp), 1);
                        positions.splice(Math.min(randIndex,randIndex_tmp), 1);
 
-                       randIndex = random(6);
+                       randIndex = randInt(6);
                        const knight1Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
-                       randIndex = random(5);
+                       randIndex = randInt(5);
                        const knight2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(4);
+                       randIndex = randInt(4);
                        const queenPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
@@ -177,7 +177,7 @@ class AntikingRules extends ChessRules
                        const rook2Pos = positions[2];
 
                        // Random squares for antikings
-                       antikingPos[c] = random(8);
+                       antikingPos[c] = randInt(8);
 
                        pieces[c][rook1Pos] = 'r';
                        pieces[c][knight1Pos] = 'n';
index c394257..ce9e667 100644 (file)
@@ -551,31 +551,31 @@ class BaroqueRules extends ChessRules
                        let positions = range(8);
                        // Get random squares for every piece, totally freely
 
-                       let randIndex = random(8);
+                       let randIndex = randInt(8);
                        const bishop1Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(7);
+                       randIndex = randInt(7);
                        const bishop2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(6);
+                       randIndex = randInt(6);
                        const knight1Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(5);
+                       randIndex = randInt(5);
                        const knight2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(4);
+                       randIndex = randInt(4);
                        const queenPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(3);
+                       randIndex = randInt(3);
                        const kingPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(2);
+                       randIndex = randInt(2);
                        const rookPos = positions[randIndex];
                        positions.splice(randIndex, 1);
                        const immobilizerPos = positions[0];
index c056244..2ce8542 100644 (file)
@@ -332,35 +332,35 @@ class GrandRules extends ChessRules
                        let positions = range(10);
 
                        // Get random squares for bishops
-                       let randIndex = 2 * random(5);
+                       let randIndex = 2 * randInt(5);
                        let bishop1Pos = positions[randIndex];
                        // The second bishop must be on a square of different color
-                       let randIndex_tmp = 2 * random(5) + 1;
+                       let randIndex_tmp = 2 * randInt(5) + 1;
                        let bishop2Pos = positions[randIndex_tmp];
                        // Remove chosen squares
                        positions.splice(Math.max(randIndex,randIndex_tmp), 1);
                        positions.splice(Math.min(randIndex,randIndex_tmp), 1);
 
                        // Get random squares for knights
-                       randIndex = random(8);
+                       randIndex = randInt(8);
                        let knight1Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
-                       randIndex = random(7);
+                       randIndex = randInt(7);
                        let knight2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
                        // Get random square for queen
-                       randIndex = random(6);
+                       randIndex = randInt(6);
                        let queenPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
                        // ...random square for marshall
-                       randIndex = random(5);
+                       randIndex = randInt(5);
                        let marshallPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
                        // ...random square for cardinal
-                       randIndex = random(4);
+                       randIndex = randInt(4);
                        let cardinalPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
index e1fb54c..7c6b422 100644 (file)
@@ -136,30 +136,30 @@ class LosersRules extends ChessRules
                        let positions = range(8);
 
                        // Get random squares for bishops
-                       let randIndex = 2 * random(4);
+                       let randIndex = 2 * randInt(4);
                        let bishop1Pos = positions[randIndex];
                        // The second bishop must be on a square of different color
-                       let randIndex_tmp = 2 * random(4) + 1;
+                       let randIndex_tmp = 2 * randInt(4) + 1;
                        let bishop2Pos = positions[randIndex_tmp];
                        // Remove chosen squares
                        positions.splice(Math.max(randIndex,randIndex_tmp), 1);
                        positions.splice(Math.min(randIndex,randIndex_tmp), 1);
 
                        // Get random squares for knights
-                       randIndex = random(6);
+                       randIndex = randInt(6);
                        let knight1Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
-                       randIndex = random(5);
+                       randIndex = randInt(5);
                        let knight2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
                        // Get random square for queen
-                       randIndex = random(4);
+                       randIndex = randInt(4);
                        let queenPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
                        // Random square for king (no castle)
-                       randIndex = random(3);
+                       randIndex = randInt(3);
                        let kingPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
index 7198c0b..c3e8716 100644 (file)
@@ -18,7 +18,7 @@ class UpsidedownRules extends ChessRules
                {
                        let positions = range(8);
 
-                       let randIndex = random(8);
+                       let randIndex = randInt(8);
                        const kingPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
@@ -29,24 +29,24 @@ class UpsidedownRules extends ChessRules
                        else if (kingPos == V.size.y-1)
                                knight1Pos = V.size.y-2;
                        else
-                               knight1Pos = kingPos + (Math.random() < 0.5 ? 1 : -1);
+                               knight1Pos = kingPos + (Math.randInt() < 0.5 ? 1 : -1);
                        // Search for knight1Pos index in positions and remove it
                        const knight1Index = positions.indexOf(knight1Pos);
                        positions.splice(knight1Index, 1);
 
                        // King+knight1 are on two consecutive squares: one light, one dark
-                       randIndex = 2 * random(3);
+                       randIndex = 2 * randInt(3);
                        const bishop1Pos = positions[randIndex];
-                       let randIndex_tmp = 2 * random(3) + 1;
+                       let randIndex_tmp = 2 * randInt(3) + 1;
                        const bishop2Pos = positions[randIndex_tmp];
                        positions.splice(Math.max(randIndex,randIndex_tmp), 1);
                        positions.splice(Math.min(randIndex,randIndex_tmp), 1);
 
-                       randIndex = random(4);
+                       randIndex = randInt(4);
                        const knight2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(3);
+                       randIndex = randInt(3);
                        const queenPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
index 509586a..d0baa6f 100644 (file)
@@ -247,19 +247,19 @@ class WildebeestRules extends ChessRules
                                positions.splice(idx, 1);
                        }
 
-                       let randIndex = random(7);
+                       let randIndex = randInt(7);
                        let knight1Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
-                       randIndex = random(6);
+                       randIndex = randInt(6);
                        let knight2Pos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
-                       randIndex = random(5);
+                       randIndex = randInt(5);
                        let queenPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
                        // Random square for wildebeest
-                       randIndex = random(4);
+                       randIndex = randInt(4);
                        let wildebeestPos = positions[randIndex];
                        positions.splice(randIndex, 1);
 
index 09d9df8..2d085c8 100644 (file)
@@ -8,6 +8,7 @@
         h3#abortBoxTitle.section {{ st.tr["Terminate game?"] }}
         button(@click="abortGame") {{ st.tr["Sorry I have to go"] }}
         button(@click="abortGame") {{ st.tr["Game seems over"] }}
+        button(@click="abortGame") {{ st.tr["Opponent is gone"] }}
     BaseGame(:game="game" :vr="vr" ref="basegame"
       @newmove="processMove" @gameover="gameOver")
     div Names: {{ game.players[0].name }} - {{ game.players[1].name }}