-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Levan Loria
committed
Sep 22, 2017
1 parent
c0e81dc
commit 686ab0d
Showing
10 changed files
with
318 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters