diff --git a/cpp/Makefile b/cpp/Makefile new file mode 100644 index 0000000..eed288a --- /dev/null +++ b/cpp/Makefile @@ -0,0 +1,27 @@ +# Makefile for shuffling + +CC = g++ +CFLAGS = -W -Wall -ansi -pedantic +LFLAGS = +EXEC=$(BIN)shuffle + +BIN =bin/ +OBJ =obj/ +INCLUDE =include/ +LIB =lib/ +SRC =src/ + + +H= + +clean: + rm -rf $(OBJ)*.o $(BIN)* + + +all: $(EXEC) +$(BIN)shuffle: $(OBJ)shuffling.o $(OBJ)shuffle.o + $(CC) $(LFLAGS) -o $@ $^ +$(OBJ)shuffling.o: $(SRC)shuffling.cpp + $(CC) $(LFLAGS) -o $@ -c $< $(CFLAGS) +$(OBJ)shuffle.o: $(SRC)shuffle.cpp $(INCLUDE)shuffling.h + $(CC) $(LFLAGS) -o $@ -c $< $(CFLAGS) diff --git a/cpp/bin/shuffle b/cpp/bin/shuffle new file mode 100755 index 0000000..e1b01d8 Binary files /dev/null and b/cpp/bin/shuffle differ diff --git a/cpp/include/shuffling.h b/cpp/include/shuffling.h new file mode 100644 index 0000000..0405bbb --- /dev/null +++ b/cpp/include/shuffling.h @@ -0,0 +1,65 @@ +// +// Shuffling.h +// Shuffling cards +// +// Created by Levan Loria on 22/11/17. +// + +#ifndef SHUFLLING_H +#define SHUFLLING_H + + +/* ------------------- Structure of Deck (Double - LinkedList) ----------------------------- */ + + +struct Card{ + int value; // value of the card + Card *next; // next card + Card *previous; // previous card +}; + +typedef struct Card Card; + + +struct Deck{ + int size; // size of deck + Card *top_card; // card on the top of the deck + Card *bottom_card; // card on the bottom of the deck + Deck(); + void addCardOnTop(Card*); // adding card on the top of the deck + Card* removeTopCard(); // removing card from top of the deck + void putBottom(); // putting the top card on the bottom of the deck +}; + +typedef struct Deck Deck; + + +void addCardOnTop(Deck *deck, Card *card); // adding card on the top of the deck +Card* removeTopCard(); // removing top card from the deck +void putBottom(); // putting top card on the bottom + + +/* -------------------------------- class of Shuffling ----------------------------------------- */ + +class Shuffling +{ +private: + int nCards; // number of cards + int rounds; // rounds + Deck* handDeck; // hand deck + Deck* tableDeck; // table deck + Card* cards; // array of all cards (initial position of cards depends on this array indices) +public: + Shuffling(int numberOfCards); + void shuffle(); // shuffle 1 round + bool compareDecks(); // comparing initial and current hand positions + void start(); // starting shuffle + + int getRounds(); // get rounds + Deck* getHandDeck(); // get handDeck + Deck* getTableDeck(); // get tableDeck + Card* getCards(); // get cards array +}; + + +#endif diff --git a/cpp/obj/shuffle.o b/cpp/obj/shuffle.o new file mode 100644 index 0000000..52571fd Binary files /dev/null and b/cpp/obj/shuffle.o differ diff --git a/cpp/obj/shuffling.o b/cpp/obj/shuffling.o new file mode 100644 index 0000000..81d0cd5 Binary files /dev/null and b/cpp/obj/shuffling.o differ diff --git a/cpp/src/output b/cpp/src/output new file mode 100755 index 0000000..942f7b5 Binary files /dev/null and b/cpp/src/output differ diff --git a/cpp/src/shuffle.cpp b/cpp/src/shuffle.cpp new file mode 100644 index 0000000..c51c17f --- /dev/null +++ b/cpp/src/shuffle.cpp @@ -0,0 +1,41 @@ +// +// Shuffle.cpp +// Shuffling cards +// +// Created by Levan Loria on 22/11/17. +// + + + +#include + +#include "../include/shuffling.h" + +using namespace std; + + +int main(int argc, const char **argv) +{ + + int numberOfCards; + + if(argc != 2){ + cout<<"Program takes one integer argument!"<start(); + + cout<getRounds()< + +#include "../include/shuffling.h" + +using namespace std; +/* -------------------------------- Deck structure ----------------------------------------- */ + + +Deck::Deck(){ + size = 0; + top_card = NULL; + bottom_card = NULL; +} + +void Deck::addCardOnTop(Card* card){ + size++; + + if(top_card == NULL){ + bottom_card = top_card = card; + }else{ + + top_card->next = card; + card->previous = top_card; + top_card = card; + + } +} + +Card* Deck::removeTopCard(){ + + Card *card = top_card; + + // If only 1 card left in the deck + if(size == 1){ + top_card = NULL; + bottom_card = NULL; + size = 0; + + return card; + } + + + // else removing top card from deck + size--; + top_card = card->previous; + top_card->next = NULL; + + card->previous = NULL; // removing link from old deck + + return card; //returns old top card +} + + +void Deck::putBottom(){ + + // new bottom card + Card* card = top_card; + + // top pointing on 2nd card from top + top_card = card->previous; + top_card->next = NULL; + + card->previous = NULL; + + // adding old top card on the bottom + bottom_card->previous = card; + card->next = bottom_card; + bottom_card = card; + +} + + +/* -------------------------------- Shuffling class ----------------------------------------- */ + + +Shuffling::Shuffling(int numberOfCards){ + + nCards = numberOfCards; + rounds = 0; + + handDeck = new Deck(); + tableDeck = new Deck(); + + cards = new Card[numberOfCards]; + for(int i=0;iaddCardOnTop(cards+i); + } +} + +// shuffle 1 round +void Shuffling::shuffle(){ + + while(handDeck->size != 2){ + Card* card = handDeck->removeTopCard(); // removing card form the handDeck + tableDeck->addCardOnTop(card); // putting removed card on the tableDeck + handDeck->putBottom(); // putting top card of handDeck on the bottom of handDeck + + } + + + + // if only 2 cards left in the handDeck, putting them on the tableDeck + tableDeck->addCardOnTop(handDeck->removeTopCard()); + tableDeck->addCardOnTop(handDeck->removeTopCard()); + + rounds++; + + handDeck = tableDeck; // taking cards from table + tableDeck = new Deck(); // 0 cards on the table +} + +// compares initial and current handDeck +bool Shuffling::compareDecks(){ + + + Card* top = handDeck->top_card; + + // values of card in array = [1,2,..,nCards] and initial handDeck values were [nCards,..,1] + for(int i=nCards-1; i>=0; i--){ + + if(top->value != cards[i].value){ + return false; + } + + top = top->previous; + + } + + return true; +} + + +// starts shuffling +void Shuffling::start(){ + + // If only 1 card in the deck ==> 1 + if(nCards == 1){ + rounds++; + return; + } + + // shuffle -> rounds++ -> check -> shuffle ... + do { + shuffle(); + } while (!compareDecks()); + +} + + +// Shuffling class getters +int Shuffling::getRounds(){ + return rounds; +} + +Deck* Shuffling::getHandDeck(){ + return handDeck; +} + +Deck* Shuffling::getTableDeck(){ + return tableDeck; +} + +Card* Shuffling::getCards(){ + return cards; +} diff --git a/js/lib/shuffling.js b/js/lib/shuffling.js index 0c2e625..0df5484 100644 --- a/js/lib/shuffling.js +++ b/js/lib/shuffling.js @@ -1,6 +1,6 @@ /* ------------------- Structure of Deck (Double - LinkedList) ----------------------------- */ function Card(value){ - this.value = value; // value of card + this.value = value; // value of the card this.next = null; // next card (node) this.previous = null; // previous card (node) }; @@ -13,7 +13,7 @@ function Deck(){ }; -// adding card on the deck +// adding card on the top of the deck Deck.prototype.add = function(card){ if(this._length){ @@ -77,7 +77,7 @@ function Shuffling(){ this.cards = []; // all cards - this.round = 0; // number of rounds + this.rounds = 0; // number of rounds this.handDeck = new Deck(); // holding deck @@ -109,7 +109,7 @@ Shuffling.prototype.shuffle = function(){ this.tableDeck.add(this.handDeck.removeTop()); this.tableDeck.add(this.handDeck.removeTop()); - this.round++; + this.rounds++; this.handDeck = this.tableDeck; // taking cards from table this.tableDeck = new Deck(); // 0 cards on the table @@ -143,16 +143,15 @@ Shuffling.prototype.start = function(){ // If only 1 card in the deck ==> 1 if(this.cards.length == 1){ - return 1; + this.rounds++; + return; } - // shuffle -> round++ -> check -> shuffle ... + // shuffle -> rounds++ -> check -> shuffle ... do { this.shuffle(); } while (!this.compareDecks()); - - return this.round; }; diff --git a/js/shufflingDeck.js b/js/shufflingDeck.js index 16be171..698ef68 100644 --- a/js/shufflingDeck.js +++ b/js/shufflingDeck.js @@ -26,7 +26,7 @@ if(numberOfCards === undefined || !Number.isInteger(numberOfCards) || numberOfCa let shaffleCards = new Shuffling(); // Initialization of Shuffling object shaffleCards.initDecks(numberOfCards); // Initialization of decks -let rounds = shaffleCards.start(); // start shuffling +shaffleCards.start(); // start shuffling -console.log(rounds); +console.log(shaffleCards.rounds);