From 656b187886e5187e52fafe44b7dc0fb45ccd9222 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Tue, 21 Jan 2020 18:05:33 +0100
Subject: [PATCH] Fix computer moves randomness, rename random() into randInt()

---
 client/src/base_rules.js          | 19 +++++++++----------
 client/src/utils/alea.js          |  4 ++--
 client/src/variants/Antiking.js   | 12 ++++++------
 client/src/variants/Baroque.js    | 14 +++++++-------
 client/src/variants/Grand.js      | 14 +++++++-------
 client/src/variants/Losers.js     | 12 ++++++------
 client/src/variants/Upsidedown.js | 12 ++++++------
 client/src/variants/Wildebeest.js |  8 ++++----
 client/src/views/Game.vue         |  1 +
 9 files changed, 48 insertions(+), 48 deletions(-)

diff --git a/client/src/base_rules.js b/client/src/base_rules.js
index 15609eb1..5fb00277 100644
--- a/client/src/base_rules.js
+++ b/client/src/base_rules.js
@@ -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)
diff --git a/client/src/utils/alea.js b/client/src/utils/alea.js
index 76d9c38e..04563938 100644
--- a/client/src/utils/alea.js
+++ b/client/src/utils/alea.js
@@ -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;
diff --git a/client/src/variants/Antiking.js b/client/src/variants/Antiking.js
index 890da580..22fd2b40 100644
--- a/client/src/variants/Antiking.js
+++ b/client/src/variants/Antiking.js
@@ -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';
diff --git a/client/src/variants/Baroque.js b/client/src/variants/Baroque.js
index c394257a..ce9e6672 100644
--- a/client/src/variants/Baroque.js
+++ b/client/src/variants/Baroque.js
@@ -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];
diff --git a/client/src/variants/Grand.js b/client/src/variants/Grand.js
index c056244d..2ce85422 100644
--- a/client/src/variants/Grand.js
+++ b/client/src/variants/Grand.js
@@ -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);
 
diff --git a/client/src/variants/Losers.js b/client/src/variants/Losers.js
index e1fb54c0..7c6b4220 100644
--- a/client/src/variants/Losers.js
+++ b/client/src/variants/Losers.js
@@ -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);
 
diff --git a/client/src/variants/Upsidedown.js b/client/src/variants/Upsidedown.js
index 7198c0b1..c3e8716e 100644
--- a/client/src/variants/Upsidedown.js
+++ b/client/src/variants/Upsidedown.js
@@ -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);
 
diff --git a/client/src/variants/Wildebeest.js b/client/src/variants/Wildebeest.js
index 509586a9..d0baa6f9 100644
--- a/client/src/variants/Wildebeest.js
+++ b/client/src/variants/Wildebeest.js
@@ -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);
 
diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue
index 09d9df8a..2d085c87 100644
--- a/client/src/views/Game.vue
+++ b/client/src/views/Game.vue
@@ -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 }}
-- 
2.44.0