Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created January 20, 2022 02:48
Show Gist options
  • Save natafaye/b9a46ecd7a3fb9fb247bb2ecab4ff703 to your computer and use it in GitHub Desktop.
Save natafaye/b9a46ecd7a3fb9fb247bb2ecab4ff703 to your computer and use it in GitHub Desktop.
class AcceptedAnswerPrompt {
constructor(acceptedAnswers) {
this.acceptedAnswers = acceptedAnswers;
}
checkIfAccepted(answer) {
// return true if it is in the list of accepted answer
for(const acceptedAnswer of this.acceptedAnswers) {
if(acceptedAnswer === answer) {
return true;
}
}
// false if not
return false;
}
promptForAnswer(question) {
// prompt the user for an answer
let answer = prompt(question);
// if they didn't give a good one, prompt again (loop)
while( !this.checkIfAccepted(answer) ) {
answer = prompt(question);
}
// once they give us a good one, return that answer
return answer;
}
}
const expect = chai.expect;
describe("AcceptedAnswerPrompt", function() {
describe("#constructor", function() {
it("should set up the accepted answers array", function() {
const yesNoPrompt = new AcceptedAnswerPrompt( [ "Yes", "No", "y", "n" ] );
expect(yesNoPrompt.acceptedAnswers).to.be.a("array")
expect(yesNoPrompt.acceptedAnswers.length).to.equal(4)
expect(yesNoPrompt.acceptedAnswers).to.eql( ["Yes", "No", "y", "n" ])
})
it("should do something else", function() {
})
})
})
// WARNING: This code is incomplete and untested! It's just an example of how you might break something down
const shuffle = (array) => {
return array.slice().sort(() => Math.random() - 0.5); // bad sorting
}
class Die {
// input of an array of letters to be on the die (not empty array)
constructor(letters) {
this.letters = letters;
this.topLetter = ""
this.reroll();
}
// Picks a new random top letter
reroll() {
let randomIndex = Math.floor(Math.random() * this.letters.length);
this.topLetter = this.letters[randomIndex];
}
}
class Board {
// makes a new freshly shuffled board
constructor() {
this.dice = [];
for(let i = 0; i < 16; i++) {
this.dice.push( new Die( ["A", "E", "L", "N", "F", "R"] ) );
}
this.shuffle();
}
// Move all the dice around randomly and reroll each dice
shuffle() {
// shuffle the dice array
this.dice = shuffle(this.dice);
// go through each dice and call reroll on it
for(const die of this.dice) {
die.reroll();
}
//this.dice.forEach(die => die.reroll());
}
// Gets all the words in the board and returns in an array
getAllWords() {
}
// Takes in a word and returns true if it is in the board
checkIfValid(word) {
}
}
class Player {
constructor() {
// words found so far property
}
takeTurn() {
}
}
class Game {
constructor() {
// players array property
// current player index property
// board property
this.board = new Board();
}
startGame() {
this.board.shuffle();
// if this was turn-based
// pick the first player
// loop until someone wins
// have that player do their turn
// go to the next player
}
}
const game = new Game();
game.startGame();
// WARNING: This code is incomplete and untested! It's just an example of how you might approach testing something
const expect = chai.expect;
describe("Die", function() {
describe("#constructor", function() {
it("should set up the letters array", function() {
const die = new Die(["A", "E", "L", "N", "F", "R"]);
expect(die.letters).to.be.a("array")
expect(die.letters).to.equal(6)
})
it("should set up the top letter property", function() {
const die = new Die(["A", "E", "L", "N", "F", "R"]);
expect(die.topLetter).to.be.a("string")
expect(die.letters.includes(die.topLetter)).to.equal(true)
})
})
describe("#reroll", function() {
it("should get a new random top letter", function() {
const die = new Die(["A", "E", "L", "N", "F", "R"]);
const firstTopLetter = die.topLetter;
for(let i = 0; i < 200; i++) {
die.reroll();
if(die.topLetter !== firstTopLetter) {
// trigger that it worked
}
}
// trigger that something went wrong
})
})
})
describe("Board", function() {
describe("#constructor", function() {
it("should set up the dice array", function() {
})
it("should shuffle itself", function() {
})
})
describe("#shuffle", function() {
it("rerolled all the dice and moved all the dice", function() {
})
})
})
class Animal {
constructor(name, typeOfAnimal) {
this.name = name;
this.type = typeOfAnimal;
}
makeDisplayString() {
return this.type + " " + this.name
}
}
class Cat extends Animal {
constructor(name) {
super(name, "cat");
}
playWithYarn() {
}
makeDisplayString() {
return this.type + " " + this.name + " is the best"
}
}
class PieceOfThePage extends Component {
constructor(props) {
super(props);
}
render() {
}
}
const cat = new Cat("Fluffy");
cat.makeDisplayString();
cat.name;
cat.type;
<!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="AcceptedAnswerPrompt.js"></script>
<script src="AcceptedAnswerPrompt.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