Skip to content

Commit

Permalink
Adding cpp example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Levan Loria committed Sep 22, 2017
1 parent c0e81dc commit 686ab0d
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 10 deletions.
27 changes: 27 additions & 0 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -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)
Binary file added cpp/bin/shuffle
Binary file not shown.
65 changes: 65 additions & 0 deletions cpp/include/shuffling.h
Original file line number Diff line number Diff line change
@@ -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
Binary file added cpp/obj/shuffle.o
Binary file not shown.
Binary file added cpp/obj/shuffling.o
Binary file not shown.
Binary file added cpp/src/output
Binary file not shown.
41 changes: 41 additions & 0 deletions cpp/src/shuffle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Shuffle.cpp
// Shuffling cards
//
// Created by Levan Loria on 22/11/17.
//



#include <iostream>

#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!"<<endl;
exit(1);
}

numberOfCards = atoi(argv[1]);
if(numberOfCards<=0){
cout<<"Program takes one integer argument!"<<endl;
exit(1);
}


Shuffling *shuffling = new Shuffling(numberOfCards);

shuffling->start();

cout<<shuffling->getRounds()<<endl;

return 0;
}
176 changes: 176 additions & 0 deletions cpp/src/shuffling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//
// Shuffling.cpp
// Shuffling cards
//
// Created by Levan Loria on 22/11/17.
//

#include <iostream>

#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;i<numberOfCards;i++){
cards[i].value = (i+1);
cards[i].next = NULL;
cards[i].previous = NULL;

handDeck->addCardOnTop(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;
}
15 changes: 7 additions & 8 deletions js/lib/shuffling.js
Original file line number Diff line number Diff line change
@@ -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)
};
Expand All @@ -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){
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};


Expand Down
4 changes: 2 additions & 2 deletions js/shufflingDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 686ab0d

Please sign in to comment.