// Argument is a move:
const move = moveOrSquare;
const [sx,sy,ex] = [move.start.x,move.start.y,move.end.x];
- // TODO: next conditions are first for Atomic, and last for Checkered
+ // NOTE: next conditions are first for Atomic, and last for Checkered
if (move.appear.length > 0 && Math.abs(sx - ex) == 2
&& move.appear[0].p == V.PAWN && ["w","b"].includes(move.appear[0].c))
{
// Search depth: 2 for high branching factor, 4 for small (Loser chess, eg.)
static get SEARCH_DEPTH() { return 3; }
- // Assumption: at least one legal move
// NOTE: works also for extinction chess because depth is 3...
getComputerMove()
{
// Some variants may show a bigger moves list to the human (Switching),
// thus the argument "computer" below (which is generally ignored)
let moves1 = this.getAllValidMoves("computer");
+ if (moves1.length == 0) //TODO: this situation should not happen
+ return null;
// Can I mate in 1 ? (for Magnetic & Extinction)
for (let i of shuffle(ArrayFun.range(moves1.length)))
<script>
import BaseGame from "@/components/BaseGame.vue";
import { store } from "@/store";
-import Worker from 'worker-loader!@/playCompMove';
+import Worker from "worker-loader!@/playCompMove";
export default {
- name: 'my-computer-game',
+ name: "my-computer-game",
components: {
BaseGame,
},
data: function() {
return {
st: store.state,
- game: { },
+ game: {},
vr: null,
// Web worker to play computer moves without freezing interface:
timeStart: undefined, //time when computer starts thinking
- lockCompThink: false, //to avoid some ghost moves
+ compThink: false, //avoid asking a new move while one is being searched
compWorker: null,
};
},
watch: {
"gameInfo.fen": function() {
- // (Security) No effect if a computer move is in progress:
- if (this.lockCompThink)
- return this.$emit("computer-think");
this.launchGame();
},
- "gameInfo.userStop": function() {
- if (this.gameInfo.userStop)
+ "gameInfo.score": function(newScore) {
+ if (newScore != "*")
{
- // User stopped the game: unknown result
+ this.game.score = newScore; //user action
this.game.mode = "analyze";
+ if (!this.compThink)
+ this.$emit("game-stopped"); //otherwise wait for comp
}
},
},
// Modal end of game, and then sub-components
created: function() {
- // Computer moves web worker logic: (TODO: also for observers in HH games ?)
- this.compWorker = new Worker(); //'/javascripts/playCompMove.js'),
+ // Computer moves web worker logic:
+ this.compWorker = new Worker();
this.compWorker.onmessage = e => {
- this.lockCompThink = true; //to avoid some ghost moves
let compMove = e.data;
+ if (!compMove)
+ return; //after game ends, no more moves, nothing to do
if (!Array.isArray(compMove))
compMove = [compMove]; //to deal with MarseilleRules
// Small delay for the bot to appear "more human"
- const delay = Math.max(500-(Date.now()-this.timeStart), 0);
+// TODO: debug delay, 2000 --> 0
+ const delay = 2000 + Math.max(500-(Date.now()-this.timeStart), 0);
+console.log("GOT MOVE: " + this.compThink);
setTimeout(() => {
+ // NOTE: Dark and 2-moves are incompatible
const animate = (this.gameInfo.vname != "Dark");
this.$refs.basegame.play(compMove[0], animate);
+ const waitEndOfAnimation = () => {
+ // 250ms = length of animation (TODO: some constant somewhere)
+ setTimeout( () => {
+console.log("RESET: " + this.compThink);
+ this.compThink = false;
+ if (this.game.score != "*") //user action
+ this.$emit("game-stopped");
+ }, animate ? 250 : 0);
+ };
if (compMove.length == 2)
- setTimeout( () => { this.$refs.basegame.play(compMove[1], animate); }, 750);
- else //250 == length of animation (TODO: should be a constant somewhere)
- setTimeout( () => this.lockCompThink = false, 250);
+ {
+ setTimeout( () => {
+ this.$refs.basegame.play(compMove[1], animate);
+ waitEndOfAnimation();
+ }, 750);
+ }
+ else
+ waitEndOfAnimation();
}, delay);
}
if (!!this.gameInfo.fen)
this.compWorker.postMessage(["init",this.gameInfo.fen]);
this.vr = new V(this.gameInfo.fen);
const mycolor = (Math.random() < 0.5 ? "w" : "b");
- let players = ["Myself","Computer"];
+ let players = [{name:"Myself"},{name:"Computer"}];
if (mycolor == "b")
players = players.reverse();
- // NOTE: (TODO?) fen and fenStart are redundant in game object
+ // NOTE: fen and fenStart are redundant in game object
this.game = Object.assign({},
this.gameInfo,
{
},
playComputerMove: function() {
this.timeStart = Date.now();
+ this.compThink = true;
+console.log("ASK MOVE (SET TRUE): " + this.compThink);
this.compWorker.postMessage(["askmove"]);
},
- // TODO: do not process if game is over (check score ?)
processMove: function(move) {
// Send the move to web worker (including his own moves)
this.compWorker.postMessage(["newmove",move]);
}
},
gameOver: function(score) {
- // Just switch to analyze mode: no user action can set score
+ this.game.score = score;
this.game.mode = "analyze";
+ this.$emit("game-over", score); //bubble up to Rules.vue
},
},
};
.row
.col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
.button-group
- button(@click="display='rules'") Read the rules
+ button(@click="clickReadRules") Read the rules
button(v-show="!gameInProgress" @click="() => startGame('auto')")
| Observe a sample game
button(v-show="!gameInProgress" @click="() => startGame('versus')")
| Beat the computer!
- button(v-show="gameInProgress" @click="stopGame")
+ button(v-show="gameInProgress" @click="() => stopGame()")
| Stop game
.section-content(v-show="display=='rules'" v-html="content")
ComputerGame(v-show="display=='computer'" :game-info="gameInfo"
- @computer-think="gameInProgress=false" @game-over="stopGame")
+ @computer-think="gameInProgress=false" @game-over="stopGame"
+ @game-stopped="gameStopped")
</template>
<script>
vname: "_unknown",
mode: "versus",
fen: "",
- userStop: false,
+ score: "*",
}
};
},
this.tryChangeVariant(this.$route.params["vname"]);
},
methods: {
+ clickReadRules: function() {
+ if (this.display != "rules")
+ this.display = "rules";
+ else if (this.gameInProgress)
+ this.display = "computer";
+ },
parseFen(fen) {
const fenParts = fen.split(" ");
return {
this.gameInProgress = true;
this.display = "computer";
this.gameInfo.mode = mode;
- this.gameInfo.userStop = false;
+ this.gameInfo.score = "*";
this.gameInfo.fen = V.GenRandInitFen();
},
- stopGame: function() {
- this.gameInProgress = false;
- this.gameInfo.userStop = true;
+ // user is willing to stop the game:
+ stopGame: function(score) {
+ this.gameInfo.score = score || "?";
this.gameInfo.mode = "analyze";
},
+ // The game is effectively stopped:
+ gameStopped: function() {
+ this.gameInProgress = false;
+ },
},
};
</script>