Skip to content

Commit

Permalink
Add AI to game
Browse files Browse the repository at this point in the history
  • Loading branch information
dirmeier committed May 4, 2020
1 parent a8a6490 commit 7cb9f79
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 106 deletions.
12 changes: 6 additions & 6 deletions app/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ define(function (require) {
const Player = require("player");

class AI extends Player {
constructor(name, suspect, model) {
constructor(name, suspect, game) {
super(name, suspect);
this._model = model;
this._board = this._model.game.board;
this._allWeapons = model.game.cards.weapons.map((i) => i.name);
this._allSuspects = model.game.cards.suspects.map((i) => i.name);
this._allPlaces = model.game.cards.places.map((i) => i.name);
this._game = game;
this._board = this._game.board;
this._allWeapons = this._game.cards.weapons.map((i) => i.name);
this._allSuspects = this._game.cards.suspects.map((i) => i.name);
this._allPlaces = this._game.cards.places.map((i) => i.name);

this._hasSeenPlaces = [];
this._hasSeenWeapons = [];
Expand Down
172 changes: 86 additions & 86 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,93 @@ define(function (require) {
const intro_players_input_button = "intro_players_input";
const intro_ai_input_button = "intro_ai_input";

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

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;
}

div.append("h1").text("Cluedo - ancient Greece edition");
div.append("h2").text("Expose Socrates' murderer.");
div.append("div").text(
"This version of Cluedo playes 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) => {
// let div = d3.select("#app")
// .append("div")
// .attr("id", "landing_page")
// .attr("class", "introduction")
// .attr("align", "center");
//
// 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;
// }
//
// div.append("h1").text("Cluedo - ancient Greece edition");
// div.append("h2").text("Expose Socrates' murderer.");
// div.append("div").text(
// "This version of Cluedo playes 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 model = new Model(3, 3);
const view = new View(model);
new Controller(nPlayers, model, view);
};
// };
});
21 changes: 7 additions & 14 deletions app/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@ define(function (require) {
}

_initPlayers(nPlayers, nAI) {
const n = nPlayers + nAI;
let players = [];
const randomSuspects = utl.randomElements(this._game.board.suspects, n);
let constructors = utl.shuffle(
[...Array(nPlayers).fill(Player), ...Array(nPlayers).fill(AI)]
);

const randomSuspects = utl.randomElements(
this._game.board.suspects, nPlayers + nAI);

// const constructors = [];
// for (let i = 0; i < nPlayers; i++) {
// constructors.push(Player);
// }
// for (let i = 0; i < nAI; i++) {
// constructors.push(AI);
// }
//

for (let i = 0; i < nPlayers; i++) {
players.push(new Player(i, randomSuspects[i]));
for (let i = 0; i < n; i++) {
players.push(new constructors[i](i, randomSuspects[i], this));
if (i > 0) {
players[i - 1].next = players[i];
players[i].prev = players[i - 1];
Expand Down

0 comments on commit 7cb9f79

Please sign in to comment.