Skip to content

Instantly share code, notes, and snippets.

@Flare576
Created January 2, 2017 23:31
Show Gist options
  • Save Flare576/f5fa3b4f03f2cbb2bc8973cebe502fb7 to your computer and use it in GitHub Desktop.
Save Flare576/f5fa3b4f03f2cbb2bc8973cebe502fb7 to your computer and use it in GitHub Desktop.
var myDeck = makeDeck()
myDeck.shuffleDeck()
myDeck.printDeck()
function makeTable(deck) {
var theDeck = deck
var table = []
return {
drawThree: drawThree
}
function drawThree () {
for (var i = 0; i < 3; i++) {
table.push(deck.nextCard())
}
}
}
function makeDeck () {
var theDeck = createDeck()
for (var clr = 0; clr < 3; clr++) {
for (var shp = 0; shp < 3; shp++) {
for (var shd = 0; shd < 3; shd++) {
for (var cnt = 0; cnt < 3; cnt++) {
theDeck.addCard(createCard(clr, shp, shd, cnt))
}
}
}
}
return theDeck
}
function createDeck () {
var theDeck = []
return {
addCard: addCard,
printDeck: printDeck,
shuffleDeck: shuffleDeck
}
function addCard (card) {
theDeck.push(card)
}
function nextCard () {
return theDeck.pop()
}
function printDeck () {
var deckString = ''
theDeck.forEach(function (curCard) {
deckString +=
curCard.getCount() + " " +
curCard.getColor() + " " +
curCard.getShade() + " " +
curCard.getShape() + "\n\n"
})
console.log(deckString)
document.getElementById('deckOutput').innerHTML = deckString.replace(/\n/g,'<br/>')
}
function shuffleDeck () {
var currentIndex = theDeck.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = theDeck[currentIndex];
theDeck[currentIndex] = theDeck[randomIndex];
theDeck[randomIndex] = temporaryValue;
}
}
}
function createCard (color, shape, shade, count) {
var color = color
var shape = shape
var shade = shade
var count = count
return {
color: color,
shape: shape,
shade: shade,
count: count,
getColor: getColor,
getShape: getShape,
getShade: getShade,
getCount: getCount
}
function getColor () {
switch (color) {
case 0: return "Red"
case 1: return "Green"
case 2: return "Purple"
default: return color
}
}
function getShape () {
switch (shape) {
case 0: return "Diamond"
case 1: return "Oval"
case 2: return "Squiggle"
default: return shape
}
}
function getShade () {
switch (shade) {
case 0: return "Empty"
case 1: return "Solid"
case 2: return "Lined"
default: return shade
}
}
function getCount () {
return count+1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment