Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created September 3, 2021 02:39
Show Gist options
  • Save natafaye/28c03615abf94a1899c00f00574425d5 to your computer and use it in GitHub Desktop.
Save natafaye/28c03615abf94a1899c00f00574425d5 to your computer and use it in GitHub Desktop.
/*** THIS CODE IS NOT FULLY FUNCTIONAL, IT'S JUST A ROUGH IDEA OF WHAT WE MIGHT DO ***/
class Board {
constructor() {
this.dice = [];
// TODO: fill this up with 16 dice
for(let i = 0; i < 16; i++) {
this.dice.push(new Die());
}
this.shuffle()
}
shuffle() {
this.dice.sort( () => .5 - Math.random() ) // crappy shuffle
for(let die of this.dice) {
die.reroll();
}
//this.dice.forEach(die => die.reroll())
}
}
class Die {
constructor() {
this.sides = ["A", "E", "N", "T", "R"] // should be an array of letters
this.letter = this.sides[0]
}
reroll() {
const randomIndex = Math.floor(Math.random() * this.sides.length);
this.letter = this.sides[randomIndex];
}
}
const board = new Board();
const expect = chai.expect;
/*** THIS CODE IS NOT FULLY FUNCTIONAL, IT'S JUST A ROUGH IDEA OF WHAT WE MIGHT DO ***/
describe("Board", function() {
describe("#constructor", function() {
it("should put 16 dice in the dice property", function() {
const board = new Board();
expect(board.dice).to.be.a("array");
expect(board.dice.length).to.equal(16);
// TODO: check that they're all dice
})
})
describe("#shuffle", function() {
it("should put all the dice in random positions with random letters on top", function() {
})
})
})
describe("Die", function() {
describe("#constructor", function() {
it("should pick a letter on top", function() {
})
})
describe("#reroll", function() {
it("should put a random letter on top", function() {
const die = new Dice();
die.reroll();
expect(die.letter).to.be.a("string");
})
})
})
/**
* A prompt that asks a question and only allows certain answers, case insensitive
*/
class QuestionPrompt {
constructor(question, acceptedAnswers) {
this.question = question;
this.acceptedAnswers = acceptedAnswers;
}
getAcceptedAnswer(potentialAnswer) {
for(let a of this.acceptedAnswers) {
if(a.toLowerCase() === potentialAnswer.toLowerCase()) {
return a;
}
}
return null;
}
prompt() {
let answer = prompt(this.question);
let acceptedAnswer = this.getAcceptedAnswer(answer);
while(acceptedAnswer === null) {
answer = prompt(this.question + "\n\nExcuse me, pick from this list: \n" + this.acceptedAnswers.join(", "))
acceptedAnswer = this.getAcceptedAnswer(answer);
}
return acceptedAnswer;
}
}
const expect = chai.expect;
describe('QuestionPrompt', function() {
describe('#constructor', function() {
it('should set up the properties correctly', function() {
let question = "Question?";
let acceptedAnswers = ["accepted", "answers"];
let qPrompt = new QuestionPrompt(question, acceptedAnswers)
expect(qPrompt.question).to.be.a('string');
expect(qPrompt.question).to.equal(question);
expect(qPrompt.acceptedAnswers).to.be.a('array');
expect(qPrompt.acceptedAnswers).to.equal(acceptedAnswers);
})
})
describe('getAcceptedAnswers', function() {
it('should return the matching accepted answer (case insensitive)', function() {
const acceptedAnswers = ["AaAA", "BBBB", "Cccc", "dddd"]
let qPrompt = new QuestionPrompt("Q", acceptedAnswers);
expect(qPrompt.getAcceptedAnswer("aaaa")).to.equal(acceptedAnswers[0]);
expect(qPrompt.getAcceptedAnswer("aAaA")).to.equal(acceptedAnswers[0]);
expect(qPrompt.getAcceptedAnswer("AAAA")).to.equal(acceptedAnswers[0]);
expect(qPrompt.getAcceptedAnswer("BBbb")).to.equal(acceptedAnswers[1]);
expect(qPrompt.getAcceptedAnswer("cCCC")).to.equal(acceptedAnswers[2]);
expect(qPrompt.getAcceptedAnswer("DDDd")).to.equal(acceptedAnswers[3]);
})
it('should return undefined if does not match any of the accepted answers', function() {
const acceptedAnswers = ["AaAA", "bBBB", "Cccc", "dddd"]
let qPrompt = new QuestionPrompt("Q", acceptedAnswers);
expect(qPrompt.getAcceptedAnswer("aaaB")).to.equal(undefined);
expect(qPrompt.getAcceptedAnswer("aaaaa")).to.equal(undefined);
expect(qPrompt.getAcceptedAnswer("AaAAbBBB")).to.equal(undefined);
})
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"><p><a href=".">Index</a></p></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>
mocha.setup('bdd');
</script>
<script src="QuestionPrompt.js"></script>
<script src="QuestionPrompt.test.js"></script>
<script src="boggle.js"></script>
<script src="boggle.test.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment