Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution question categories: Use a questionDeck map to enumerate categories and link them to questions #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 29 additions & 37 deletions javascript/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Game {
places: any[];
players: any[];

private questionsDeck;

constructor() {
this.players = new Array();
this.places = new Array(6);
Expand All @@ -31,6 +33,12 @@ class Game {
this.sportsQuestions.push("Sports Question " + i);
this.rockQuestions.push(this.createRockQuestion(i));
}
this.questionsDeck = {
Pop: this.popQuestions,
Science: this.scienceQuestions,
Sports: this.sportsQuestions,
Rock: this.rockQuestions,
};

}

Expand All @@ -54,26 +62,28 @@ class Game {
return !(this.purses[this.currentPlayer] == 6)
};

/**
* This code expresses the business rules that categories
* - are evenly distributed over the board
* - are equally frequent
* The fact that currentCategory() and askQuestion() use the same data - questionsDeck
* instead of a random collection of if-statements makes adds the necessary cohesion
* for introducing a new category for instance.
*
* A key aspect is the use of a data structure (questionsDeck) that models the Domain well,
* indeed often when we replace conditional logic with a data-structure (List, Set, Map, Maybe)
* we gain cohesion
*/
currentCategory() {
if (this.places[this.currentPlayer] == 0)
return 'Pop';
if (this.places[this.currentPlayer] == 4)
return 'Pop';
if (this.places[this.currentPlayer] == 8)
return 'Pop';
if (this.places[this.currentPlayer] == 1)
return 'Science';
if (this.places[this.currentPlayer] == 5)
return 'Science';
if (this.places[this.currentPlayer] == 9)
return 'Science';
if (this.places[this.currentPlayer] == 2)
return 'Sports';
if (this.places[this.currentPlayer] == 6)
return 'Sports';
if (this.places[this.currentPlayer] == 10)
return 'Sports';
return 'Rock';
const categoryNames = Object.keys(this.questionsDeck);
let categoryIndex = this.places[this.currentPlayer] % categoryNames.length;
return categoryNames[categoryIndex]
};

askQuestion() {
let currentCategory = this.currentCategory();
let questions = this.questionsDeck[currentCategory];
console.log(questions.shift());
};

createRockQuestion(index) {
Expand All @@ -85,22 +95,6 @@ class Game {
return howManyPlayers >= 2;
};


askQuestion() {
if (this.currentCategory() == 'Pop') {
console.log(this.popQuestions.shift());
}
if (this.currentCategory() == 'Science') {
console.log(this.scienceQuestions.shift());
}
if (this.currentCategory() == 'Sports') {
console.log(this.sportsQuestions.shift());
}
if (this.currentCategory() == 'Rock') {
console.log(this.rockQuestions.shift());
}
};

roll(roll) {
console.log(this.players[this.currentPlayer] + " is the current player");
console.log("They have rolled a " + roll);
Expand Down Expand Up @@ -178,6 +172,4 @@ class Game {

}

exports = typeof window !== "undefined" && window !== null ? window : global;

module.exports = Game