Skip to content

Commit

Permalink
Fix AI but when not moving
Browse files Browse the repository at this point in the history
  • Loading branch information
dirmeier committed May 7, 2020
1 parent b8c0a16 commit f3838bc
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 219 deletions.
27 changes: 13 additions & 14 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
module.exports = {
parser: 'babel-eslint',
env: {
browser: true,
es6: true,
node: true
},
extends: [
'standard'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
parser: "babel-eslint"
'sourceType': 'module',
'ecmaFeatures': {
'jsx': true,
'modules': true
}
},
plugins: ['standard'],
rules: {
"semi": ["error", "always"],
"quotes": ["error", "double"],
"array-element-newline": ["error", "consistent"]
'semi': ['error', 'always'],
'quotes': ['error', 'double'],
'array-element-newline': ['error', 'consistent']
}
};
}


11 changes: 6 additions & 5 deletions app/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ define(function (require) {
}

wantToCast() {
if (!this.isInPlace)
return true;
const hasSeenRoom = this._hasSeenPlaces.includes(
this.suspect.tile.place.name);
if (!hasSeenRoom)
return Math.random() < .25;
return Math.random() < .5;
}

getPath(pips) {
computeDestination(pips) {
if (this._target === null) {
const hasNotSeenPlaces = utl.distinct(
this._allPlaces, this._hasSeenPlaces);
Expand All @@ -65,9 +67,7 @@ define(function (require) {

// here we should implement the closest free tile not just any tile
this._targetTile = this._board.getFreeTile(this._target);
const entirePath = this._board.computePath(this.tile,
this._targetTile
);
const entirePath = this._board.computePath(this.tile, this._targetTile);

const moveablePath = entirePath.slice(0, pips);
return moveablePath;
Expand All @@ -86,7 +86,8 @@ define(function (require) {
}

wantsToAccuse() {
const can = this._hasSeenPlaces.length + 1 === this._allPlaces.length &&
const can =
this._hasSeenPlaces.length + 1 === this._allPlaces.length &&
this._hasSeenWeapons.length + 1 === this._allWeapons.length &&
this._hasSeenSuspects.length + 1 === this._allSuspects.length;
return can;
Expand Down
48 changes: 24 additions & 24 deletions app/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ define(function () {
}

ai_turn = async () => {
if (this._model.currentPlayer.isAI)
if (this._model.currentPlayerIsAI)
await this._ai();
};

_ai_think = async () => {
const player = this._model.currentPlayer;
const player = await this._model.currentPlayer;
await this._view.aiMove(player);
const cast = player.wantToCast();
const cast = await player.wantToCast();
return cast;
};

_ai_cast = async () => {
const player = this._model.currentPlayer;
const player = await this._model.currentPlayer;
const pips = await this.castDie();
const path = await player.getPath(pips);
const path = await player.computeDestination(pips);
await this._view.aiCast(player);
await this._makeMove(
this._model.currentPlayer.tile, path[path.length - 1], path);
};

_ai_suggest = async () => {
const player = this._model.currentPlayer;
const sugg = player.suggest();
const player = await this._model.currentPlayer;
const sugg = await player.suggest();
await this._view.aiSuggest(player, sugg);
const holds = await this.makeSuggestion(sugg.suspect, sugg.weapon);

if (holds !== null)
player.addSeenCard(holds.card);
await player.addSeenCard(holds.card);
};

_ai_accuse = async () => {
const player = this._model.currentPlayer;
const player = await this._model.currentPlayer;
const acc = await player.accuse();
await this._view.aiAccuse(player, acc);
await this.makeAccusation(acc.suspect, acc.weapon, acc.place);
Expand All @@ -74,7 +74,7 @@ define(function () {
await this._ai_accuse();
}

await this._view.finishAIMove();
await this._view.aiFinishMove();
};

castDie = () => {
Expand All @@ -92,9 +92,9 @@ define(function () {
if (!this._isMove)
return;

const oldTile = this._model.getPlayerTile();
const tile = this._model.getTile(row, col);
const path = this._model.computePath(oldTile, tile);
const oldTile = await this._model.getPlayerTile();
const tile = await this._model.getTile(row, col);
const path = await this._model.computePath(oldTile, tile);

if (path.length > this._pips) {
this._view.appendInfo("You cannot walk that far.");
Expand All @@ -106,7 +106,7 @@ define(function () {
_makeMove = async (oldTile, tile, path) => {
await this._view.makeMove(oldTile, tile, path);

this._model.putCurrPlayerSuspectPieceOn(tile);
await this._model.putCurrPlayerSuspectPieceOn(tile);
this._isMove = false;

if (!this._model.currentPlayer.isAI) {
Expand All @@ -118,19 +118,19 @@ define(function () {

suggest = () => {
this._view.hideButtons();
this._view.showSuggestions(this._model.currentPlayer.isAI);
this._view.showSuggestions(this._model.currentPlayerIsAI);
};

makeSuggestion = async (suspect, weapon) => {
let holds = await this._model.ask(suspect, weapon);

for (let itemName of [suspect, weapon]) {
const tiles = this._model.moveToPlayerPlace(itemName);
this._view.updatePiece(tiles.oldTile, tiles.newTile);
const tiles = await this._model.moveToPlayerPlace(itemName);
await this._view.updatePiece(tiles.oldTile, tiles.newTile);
}

await this._view.showHolds(holds, this._model.currentPlayer.isAI);
if (!this._model.currentPlayer.isAI) {
await this._view.showHolds(holds, this._model.currentPlayerIsAI);
if (!this._model.currentPlayerIsAI) {
this._view.showAccuseButton();
this._view.showFinishButton();
}
Expand All @@ -140,19 +140,19 @@ define(function () {

accuse = () => {
this._view.hideButtons();
this._view.showAccusations(this._model.currentPlayer.isAI);
this._view.showAccusations(this._model.currentPlayerIsAI);
};

makeAccusation = async (suspect, weapon, place) => {
let isSolved = this._model.isSolved(suspect, place, weapon);
let isSolved = await this._model.isSolved(suspect, place, weapon);
await this._view.makeAccusation(isSolved);

if (!isSolved) {
this._model.removeCurrentPlayer();
this._view.removePlayerFromLegend();
await this._model.removeCurrentPlayer();
await this._view.removePlayerFromLegend();
}

if (!this._model.currentPlayer.isAI) {
if (!this._model.currentPlayerIsAI) {
this._view.showFinishButton();
}
this._checkExit();
Expand Down
171 changes: 86 additions & 85 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,96 +7,97 @@ define(function (require) {
const intro_players_input_button = "intro_players_input";
const intro_ai_input_button = "intro_ai_input";

function _parse(id) {
const nmb = d3.select("#" + id).property("value");
try {
if (nmb !== "" && !isNaN(nmb)) {
if (nmb > 5 || nmb < 0)
throw null;
else
return parseInt(nmb);
}
else {
throw null;
}
}
catch (err) {
d3.select("#intro_span")
.text("Enter integers only!")
.style("color", "darkred");
}
return null;
}

let div = d3.select("#app")
.append("div")
.attr("id", "landing_page")
.attr("class", "introduction")
.attr("align", "center");

div.append("h1").text("Cluedo - ancient Greece edition");
div.append("h2").text("Expose Socrates' murderer.");
div.append("div").text(
"This version of Cluedo plays in ancient Greece, where" +
" Socrates, a true champion of the open society," +
" has been murdered by one of his enemies." +
" Players need to identify the murderer," +
" the weapon the crime was committed with, and its place.");
div = div.append("div")
.style("margin-top", "10px")
.attr("align", "center");

div.append("input")
.style("margin-right", "10px")
.attr("type", "text")
.style("width", "350px")
.style("height", "40px")
.style("display", "inline-block")
.attr("id", intro_players_input_button)
.attr("class", "nes-input")
.attr("placeholder", "How many players?");

div.append("input")
.attr("type", "text")
.style("width", "350px")
.style("height", "40px")
.style("display", "inline-block")
.attr("id", intro_ai_input_button)
.attr("class", "nes-input")
.attr("placeholder", "How many AIs?");

div.append("button")
.style("margin-top", "10px")
.attr("class", "nes-btn")
.style("height", "40px")
.style("display", "inline-block")
.attr("type", "button")
.attr("name", "action")
.on("click", function () {
const nmbPl = _parse(intro_players_input_button);
const nmbAi = _parse(intro_ai_input_button);
if (nmbPl !== null && nmbAi !== null) {
if (nmbPl + nmbAi > 6) {
d3.select("#intro_span")
.text("Number of players + AIs needs to be <= 6!")
.style("color", "darkred");
}
else {
_run(nmbPl, nmbAi);
}
}
})
.text("Play");

div.append("div")
.style("margin-top", "10px")
.append("span")
.attr("id", "intro_span");
// function _parse(id) {
// const nmb = d3.select("#" + id).property("value");
// try {
// if (nmb !== "" && !isNaN(nmb)) {
// if (nmb > 5 || nmb < 0)
// throw null;
// else
// return parseInt(nmb);
// }
// else {
// throw null;
// }
// }
// catch (err) {
// d3.select("#intro_span")
// .text("Enter integers only!")
// .style("color", "darkred");
// }
// return null;
// }
//
// let div = d3.select("#app")
// .append("div")
// .attr("id", "landing_page")
// .attr("class", "introduction")
// .attr("align", "center");
//
// div.append("h1").text("Cluedo - ancient Greece edition");
// div.append("h2").text("Expose Socrates' murderer.");
// div.append("div").text(
// "This version of Cluedo plays in ancient Greece, where" +
// " Socrates, a true champion of the open society," +
// " has been murdered by one of his enemies." +
// " Players need to identify the murderer," +
// " the weapon the crime was committed with, and its place.");
// div = div.append("div")
// .style("margin-top", "10px")
// .attr("align", "center");
//
// div.append("input")
// .style("margin-right", "10px")
// .attr("type", "text")
// .style("width", "350px")
// .style("height", "40px")
// .style("display", "inline-block")
// .attr("id", intro_players_input_button)
// .attr("class", "nes-input")
// .attr("placeholder", "How many players?");
//
// div.append("input")
// .attr("type", "text")
// .style("width", "350px")
// .style("height", "40px")
// .style("display", "inline-block")
// .attr("id", intro_ai_input_button)
// .attr("class", "nes-input")
// .attr("placeholder", "How many AIs?");
//
// div.append("button")
// .style("margin-top", "10px")
// .attr("class", "nes-btn")
// .style("height", "40px")
// .style("display", "inline-block")
// .attr("type", "button")
// .attr("name", "action")
// .on("click", function () {
// const nmbPl = _parse(intro_players_input_button);
// const nmbAi = _parse(intro_ai_input_button);
// if (nmbPl !== null && nmbAi !== null) {
// if (nmbPl + nmbAi > 6) {
// d3.select("#intro_span")
// .text("Number of players + AIs needs to be <= 6!")
// .style("color", "darkred");
// }
// else {
// _run(nmbPl, nmbAi);
// }
// }
// })
// .text("Play");
//
// div.append("div")
// .style("margin-top", "10px")
// .append("span")
// .attr("id", "intro_span");

const _run = (nPlayers, nAI) => {
d3.select("#landing_page").style("display", "none");
const model = new Model(nPlayers, nAI);
const view = new View(model);
new Controller(nPlayers + nAI, model, view);
};
_run(2, 0);
});
Loading

0 comments on commit f3838bc

Please sign in to comment.