diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c0a8943 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,64 @@ +{ + "files.associations": { + "iostream": "cpp", + "ratio": "cpp", + "system_error": "cpp", + "array": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "csignal": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "string_view": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..05054c5 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++ build active file", + "command": "/usr/bin/g++", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/Demo.cpp b/Demo.cpp index 5e148a6..3d548a3 100644 --- a/Demo.cpp +++ b/Demo.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include using namespace std; #include "sources/player.hpp" @@ -16,22 +18,47 @@ using namespace std; #include "sources/card.hpp" using namespace ariel; -int main() { - // Create two players with their names - Player p1("Alice"); - Player p2("Bob"); +int main() +{ + // Player p1("Alice"); + // Player p2("Bob"); + // Game game(p1,p2); + // cout<<"stack size is"<. Also print the draw rate and amount of draws that happand. (draw within a draw counts as 2 draws. ) + } + game.printLastTurn(); // print the last turn stats. For example: + // Alice played Queen of Hearts Bob played 5 of Spades. Alice wins. + // Alice played 6 of Hearts Bob played 6 of Spades. Draw. Alice played 10 of Clubs Bob played 10 of Diamonds. draw. Alice played Jack of Clubs Bob played King of Diamonds. Bob wins. + cout << p1.stacksize() << endl; // prints the amount of cards left. should be 21 but can be less if a draw was played + cout << p2.cardesTaken() << endl; // prints the amount of cards this player has won. + game.playAll(); // playes the game untill the end + game.printWiner(); // prints the name of the winning player + game.printLog(); // prints all the turns played one line per turn (same format as game.printLastTurn()) + game.printStats(); // for each player prints basic statistics: win rate, cards won, . Also print the draw rate and amount of draws that happand. (draw within a draw counts as 2 draws. ) } diff --git a/objects/file.txt b/objects/file.txt new file mode 100644 index 0000000..e69de29 diff --git a/sources/card.cpp b/sources/card.cpp new file mode 100644 index 0000000..95e5c80 --- /dev/null +++ b/sources/card.cpp @@ -0,0 +1,12 @@ +#include +#include +#include +#include "card.hpp" + +using namespace std; + +namespace ariel +{ + +} + diff --git a/sources/card.hpp b/sources/card.hpp new file mode 100644 index 0000000..137e1b3 --- /dev/null +++ b/sources/card.hpp @@ -0,0 +1,131 @@ +#pragma once +#include +#include +#include +#include + +using namespace std; + +namespace ariel +{ + + class Card + { + + public: + enum Suit + { + SPADES, + HEARTS, + CLUBS, + DIAMONDS + }; + + enum Cards{ + ACE=1, + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + TEN, + PRINCE, + QUEEN, + KING + }; + + private: + int value; + Suit suit; + + public: + Card(int m_value, Suit m_suit) : value(m_value), suit(m_suit) {} + + int getValue() const + { + return value; + } + + Suit getSuit() const + { + return suit; + } + + int get_size(); + + static vector Create_cards() + { + + std::vector deck; + int index = 0; + for (int suit = Card::SPADES; suit <= Card::DIAMONDS; suit++) + { + for (int value = ACE; value <= KING; value++) + { + deck.push_back(Card(value, (Card::Suit)suit)); + + } + + } + srand(time(0)); + random_shuffle(deck.begin(), deck.end()); + + + return deck; + } + + + string getSuitName() const + { + switch (suit) + { + case HEARTS: + return "Hearts"; + case DIAMONDS: + return "Diamonds"; + case CLUBS: + return "Clubs"; + case SPADES: + return "Spades"; + } + return "Unknown"; + } + + string getValueName() const + { + switch (value) + { + case ACE: + return "Ace"; + case TWO: + return "2"; + case THREE: + return "3"; + case FOUR: + return "4"; + case FIVE: + return "5"; + case SIX: + return "6"; + case SEVEN: + return "7"; + case EIGHT: + return "8"; + case NINE: + return "9"; + case TEN: + return "10"; + case PRINCE: + return "Prince"; + case QUEEN: + return "Queen"; + case KING: + return "King"; + } + return "Unknown"; + } + }; +} diff --git a/sources/game.cpp b/sources/game.cpp new file mode 100644 index 0000000..60dad7f --- /dev/null +++ b/sources/game.cpp @@ -0,0 +1,282 @@ + +#include "game.hpp" +#include +#include +#include +#include "player.hpp" +#include +using namespace std; +using namespace ariel; + +void Game::playTurn() +{ + this->turn++; + + if(&p1==&p2){ + throw std::runtime_error("Error message"); + } + + if (p1.Start_stack.empty() || p2.Start_stack.empty()) + { + cout << "one of the players is out of cards" << endl; + // log.push("one of the players is out of cards"); + throw std::runtime_error("Error message"); + return; + } + // cout << "playTurn" << endl; + // cout<<"p1 size is"<p1.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p1.name + " " + "wins"+ "\n"); + + p1.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + p1.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + } + + else if(card_p2 == 1 && card_p1 != 1 && card_p1 != 2){ + // cout << "p2 win" << endl; + p2.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p2.name + " " + "wins"+ "\n"); + + p2.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + p2.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + } + + + else if (card_p1 > card_p2) + { + // cout << "p1 win" << endl; + this->p1.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p1.name + " " + "wins"+ "\n"); + + p1.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + p1.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + } + else if (card_p1 < card_p2) + { + // cout << "p2 win" << endl; + p2.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p2.name + " " + "wins"+ "\n"); + + p2.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + p2.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + } + else + { + if (p1.Start_stack.size() == 1 || p2.Start_stack.size() == 1) + { + cout << "last card the winner is the one with the most card taken" << endl; + + if (p1.Card_taken.size() > p2.Card_taken.size()) + { + // cout << "p1 win" << endl; + } + else + { + // cout << "p2 win" << endl; + } + } + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + "draw" +"\n"); + tie(); + } +}; + +void Game::tie() +{ + + draw_amount++; + // cout << "tie" << endl; + vector draw; + + if (p1.Start_stack.empty() || p2.Start_stack.empty()) + { + // cout << "one of the players is out of cards" << endl; + + for (int i = 0; i < draw.size(); i++) + { + if (i % 2 == 0) + { + p1.Start_stack.push_back(draw[(size_t)i]); + } + else + { + p2.Start_stack.push_back(draw[(size_t)i]); + } + }; + }; + + Card p1_tie = p1.Start_stack.back(); + p1.Start_stack.pop_back(); + Card p2_tie = p2.Start_stack.back(); + p2.Start_stack.pop_back(); + draw.push_back(p1_tie); + draw.push_back(p2_tie); + int card_p1 = p1.Start_stack.back().getValue(); + int card_p2 = p2.Start_stack.back().getValue(); + // cout << "p1 card is" << p1.Start_stack.back().getValue() << endl; + // cout << "p2 card is" << p2.Start_stack.back().getValue() << endl; + + + if(card_p1 == 1 && card_p2 != 1 && card_p2 != 2){ + // cout << "p1 win" << endl; + this->p1.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p1.name + " " + "wins"+ "\n"); + + p1.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + p1.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + } + + else if(card_p2 == 1 && card_p1 != 1 && card_p1 != 2){ + // cout << "p2 win" << endl; + p2.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p2.name + " " + "wins"+ "\n"); + + p2.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + p2.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + } + + + + else if (card_p1 > card_p2) + { + p1.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p1.name + " " + "wins"+ "\n"); + + // cout << "p1 win" << endl; + p1.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + p1.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + for (int i = 0; i < draw.size(); i++) + { + p1.Card_taken.push_back(draw[(size_t)i]); + } + draw.clear(); + } + else if (card_p1 < card_p2) + { + p2.wins++; + log.push_back(p1.name + " " + "played" + " " + p1.Start_stack.back().getValueName() + " " + "of" + " " + p1.Start_stack.back().getSuitName() + " " + p2.name + " " + "played" + " " + p2.Start_stack.back().getValueName() + " " + "of" + " " + p2.Start_stack.back().getSuitName() + " " + p2.name + " " + "wins"+ "\n"); + + // cout << "p2 win" << endl; + p2.Card_taken.push_back(p2.Start_stack.back()); + p2.Start_stack.pop_back(); + p2.Card_taken.push_back(p1.Start_stack.back()); + p1.Start_stack.pop_back(); + for (int i = 0; i < draw.size(); i++) + { + p2.Card_taken.push_back(draw[(size_t)i]); + } + draw.clear(); + } + else + { + this->turn++; + tie(); + } +} + +void Game::printLastTurn() +{ + cout< p2.cardesTaken()) + { + // cout << "p1 win" << endl; + } + else if (p1.cardesTaken() < p2.cardesTaken()) + { + // cout << "p2 win" << endl; + } + else + { + // cout << "tie" << endl; + } +}; + +void Game::printWiner() +{ + if (p1.Start_stack.empty() && p2.Start_stack.empty()) + { + + if (p1.cardesTaken() > p2.cardesTaken()) + { + cout << p1.name + " " + "win the game" << endl; + } + else if (p1.cardesTaken() < p2.cardesTaken()) + { + cout << p2.name + " " + "win the game" << endl; + } + else + { + cout << "The game is ending with draw" << endl; + } + } + else{ + cout << "game is not over" << endl; + } +}; + +void Game::printLog() +{ + for (int i = 0; i < log.size(); i++) + { + cout << log[(size_t)i] << endl; + } +}; + +void Game::printStats() +{ + double p1_rate = static_cast(p1.wins)/this->turn * 100; + double p2_rate = static_cast(p2.wins)/this->turn * 100; + double draws_rate = static_cast(draw_amount)/this->turn * 100; + + + cout << "p1 win rate is" << p1_rate << "%" << endl; + cout << "p2 win rate is" << p2_rate << "%" << endl; + + cout << "p1 cardes taken" << p1.cardesTaken() << endl; + for(int i=0; i +#include +#include "player.hpp" + +namespace ariel +{ + + class Game + { + public: + Player &p1; + Player &p2; + int draw_amount = 0; + vector log; + int turn = 0; + + + Game(Player &player1, Player &player2) : p1(player1), p2(player2) + { + vector deck = Card::Create_cards(); + + for (int i = 0; i < deck.size(); i++) + { + // cout<<"the i is"< +#include +#include +#include +#include "card.hpp" + +using namespace std; + +namespace ariel +{ + + class Player + { + + public: + vector Start_stack; + vector Card_taken; + string name ; + int wins=0; + + Player(string name) : name(std::move(name)) + { + + }; + + + int stacksize(); + + int cardesTaken(); + +}; +} \ No newline at end of file diff --git a/test1 b/test1 new file mode 100755 index 0000000..7a7f56e Binary files /dev/null and b/test1 differ