Skip to content

Commit

Permalink
Add testcase and class cardDeck.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosbth committed Oct 30, 2017
1 parent af8be3b commit a945ec4
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 42 deletions.
8 changes: 8 additions & 0 deletions REVISION.md
@@ -1,6 +1,14 @@
Revision history
===========================

v1.0.2 (2017-10-30)
---------------------------

* Add testcase and class cardDeck.
* Upgrade validators and remove csslint and jcsc.
* Add scrutinizer as code quality and code coverage tool.


v1.0.1 (2017-10-26)
---------------------------

Expand Down
30 changes: 0 additions & 30 deletions src/blackJack/blackJackGame.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/card/card.js
Expand Up @@ -63,7 +63,4 @@ class Card {
}
}



// Export module
module.exports = Card;
60 changes: 52 additions & 8 deletions src/cardDeck/cardDeck.js
Expand Up @@ -3,28 +3,72 @@
*
* @module
*/
import Card from "../card/card";
"use strict";

export default class cardDeck {
//const Card = require("../card/card");

class CardDeck {
/**
* @constructor
*
* @param {object} options - Configure by sending options.
*/
constructor(options = {}) {
this.x = options.x || 0;
this.numberOfDecks = options.numberOfDecks || 2;
this.numberOfCards = options.numberOfCards || 52;
this.decks = [];
for (let i = 0; i < this.numberOfDecks * 52; i++) {
this.decks.push(i % this.numberOfCards);
}
}



/**
* TDD
* Get number of cards in deck.
*
* @param {object} options - Configure by sending options.
* @returns {integer} The amount of cards in the deck.
*/
getNumberOfCards() {
return (this.decks.length);
}



/**
* Show all cards by their id.
*
* @returns {array} With all cards.
*/
showAllCardsById() {
return (this.decks.slice());
}



/**
* Shuffle the deck.
*
* @returns {void}
*/
draw() {
var card = new Card();
shuffle() {
for (let i = this.decks.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));

[this.decks[i], this.decks[j]] = [this.decks[j], this.decks[i]];
}
}

console.log(card);


/**
* Get a card from the deck, remove it from the deck.
*
* @returns {integer} As card id.
*/
getCard() {
return (this.decks.pop());
}
}

module.exports = CardDeck;
2 changes: 1 addition & 1 deletion test/card/cardParameterized.js
@@ -1,5 +1,5 @@
/**
* Test for class Car, parameterized version of testsuite.
* Test for class Card, parameterized version of testsuite.
*/
"use strict";

Expand Down
78 changes: 78 additions & 0 deletions test/cardDeck/cardDeck.js
@@ -0,0 +1,78 @@
/**
* Test for class CardDeck.
*/
"use strict";

/* global describe it */

const assert = require("assert");
const CardDeck = require("../../src/cardDeck/cardDeck");



/**
* Compare two integers.
*/
function compareInteger(a, b) {
return a - b;
}



/**
* Testsuite
*/
describe("Check CardDeck", function() {
describe("use default card deck", function() {
it("should be 104 cards", function() {
let deck = new CardDeck();
let numberOfCards = deck.getNumberOfCards();

assert.equal(numberOfCards, 104);
});

it("id should be between 0 and 103", function() {
let deck = new CardDeck();
let cards = deck.showAllCardsById();

assert.equal(cards.length, 104);
assert.equal(cards[0], 0);
assert.equal(cards[51], 51);
assert.equal(cards[52], 0);
assert.equal(cards[103], 51);
});

it("shuffle it", function() {
let deck = new CardDeck();
let numberOfCards;

deck.shuffle();
numberOfCards = deck.getNumberOfCards();

assert.equal(numberOfCards, 104);
});

it("get all cards, one by one", function() {
let deck = new CardDeck();
let allCards = deck.showAllCardsById();
let cardId;
let shuffledCards = [];
let equal;

deck.shuffle();
while ((cardId = deck.getCard()) !== undefined) {
shuffledCards.push(cardId);
}

allCards.sort(compareInteger);
shuffledCards.sort(compareInteger);

equal = shuffledCards.length == allCards.length &&
shuffledCards.every((element, index) => {
return element === allCards[index];
});

assert.equal(equal, true);
});
});
});

0 comments on commit a945ec4

Please sign in to comment.