Advertisement
Guest User

card game

a guest
May 5th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.89 KB | None | 0 0
  1. // Cards.cpp : Defines the entry point for the console application.
  2.  
  3. #include "stdafx.h"
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7. #include <ios>
  8.  
  9. using namespace std;
  10.  
  11. void clear(){
  12.     system("cls");
  13. }
  14.  
  15. namespace cards{
  16.     class card{
  17.     private:
  18.         string suit;
  19.  
  20.     public:
  21.         int num;
  22.  
  23.         card(){ num = 0; suit = 4 + ""; }
  24.         card(int _num, int _suit){
  25.             num = _num;
  26.             suit = (_suit == 0) ? "Spades" : (_suit == 1) ? "Clubs" : (_suit == 2) ? "Diamonds" : "Hearts";
  27.         }
  28.  
  29.         string toString(){
  30.             string toReturn;
  31.  
  32.             if (num <= 10)
  33.                 toReturn += to_string(num) + " ";
  34.             else if (num == 11)
  35.                 toReturn += "Jack ";
  36.             else if (num == 12)
  37.                 toReturn += "Queen ";
  38.             else if (num == 13)
  39.                 toReturn += "King ";
  40.             else if (num == 14)
  41.                 toReturn += "Ace ";
  42.  
  43.             toReturn += "of " + suit;
  44.             return toReturn;
  45.         }
  46.  
  47.         bool operator==(card a){
  48.             return (a.num == num && a.suit == suit);
  49.         }
  50.         bool operator==(int n){
  51.             return (num == n);
  52.         }
  53.         bool operator!=(card a){
  54.             return (num != a.num) || (suit != a.suit);
  55.         }
  56.         bool operator>(card a){
  57.             return (num > a.num);
  58.         }
  59.         bool operator<(card a){
  60.             return (num < a.num);
  61.         }
  62.         bool operator>=(card a){
  63.             return (num >= a.num);
  64.         }
  65.         bool operator<=(card a){
  66.             return (num <= a.num);
  67.         }
  68.     };
  69.     class deck{
  70.     private:
  71.         vector<card>* _deck;
  72.  
  73.         bool checksize(){
  74.             return _deck->size() != 0;
  75.         }
  76.         void reInit(){
  77.             _deck->clear();
  78.             for (int i = 0; i <= 3; ++i){
  79.                 for (int j = 2; j <= 14; ++j){
  80.                     _deck->push_back(card(j, i));
  81.                 }
  82.             }
  83.         }
  84.  
  85.     public:
  86.         deck(){
  87.             _deck = new vector<card>();
  88.             for (int i = 0; i <= 3; ++i){
  89.                 for (int j = 2; j <= 14; ++j){
  90.                     _deck->push_back(card(j, i));
  91.                 }
  92.             }
  93.         }
  94.  
  95.         void shuffle(){
  96.             vector<card>::size_type index1 = rand() % _deck->size();
  97.             vector<card>::size_type index2 = rand() % _deck->size();
  98.             auto c = _deck[index1];
  99.             _deck[index1] = _deck[index2];
  100.             _deck[index2] = c;
  101.         }
  102.         void shuffle(int t){
  103.             for (int i = 0; i < t; ++i)
  104.                 shuffle();
  105.         }
  106.         card takeTop(){
  107.             card toReturn = _deck->at(0);
  108.             _deck->erase(_deck->begin());
  109.             return toReturn;
  110.         }
  111.         string toString(){
  112.             string toReturn;
  113.             for (vector<card>::iterator it = _deck->begin(); it != _deck->end(); ++it){
  114.                 toReturn += (*it).toString() + "\n";
  115.             }
  116.             return toReturn;
  117.         }
  118.     };
  119.     class hand{
  120.     private:
  121.         string owner;
  122.  
  123.     protected:
  124.         vector<card> cards;
  125.         deck* _deck = new deck();
  126.  
  127.     public:
  128.         hand(string name, deck* d){
  129.             owner = name;
  130.             _deck = d;
  131.         }
  132.  
  133.         virtual string toString(){
  134.             string toReturn = owner + "'s Hand:\n";
  135.             for (vector<card>::iterator it = cards.begin(); it != cards.end(); it++)
  136.             {
  137.                 toReturn += (*it).toString() + "\n";
  138.             }
  139.             return toReturn + "\n";
  140.         }
  141.         virtual void draw(){
  142.             cards.push_back(_deck->takeTop());
  143.         }
  144.         card playCard(int i){
  145.             card toReturn = cards[i];
  146.             cards.erase(cards.begin() + i);
  147.             return toReturn;
  148.         }
  149.         virtual void pickup(vector<card>& v){
  150.             for (vector<card>::iterator it = v.begin(); it != v.end(); ++it){
  151.                 cards.push_back(*it);
  152.                 v.erase(it);
  153.             }
  154.         }
  155.     };
  156.     class game{
  157.     protected:
  158.         string name;
  159.         deck d;
  160.         vector<hand*> players;
  161.         vector<hand*>::iterator turn;
  162.         vector<card> played;
  163.  
  164.     public:
  165.         game(){}
  166.         game(const char* n){
  167.             name = n;
  168.             d.shuffle(1000);
  169.         }
  170.         game(string& n, vector<string>& players_){
  171.             name = n;
  172.             d.shuffle(1000);
  173.             for each (string var in players_){
  174.                 hand* x = new hand(var, &d);
  175.                 players.push_back(x);
  176.             }
  177.         }
  178.         game(const char* n, vector<string>& players_){
  179.             name = n;
  180.             d.shuffle(1000);
  181.             for each(string var in players_){
  182.                 hand* x = new hand(var, &d);
  183.                 players.push_back(x);
  184.             }
  185.         }
  186.  
  187.         virtual void deal(){
  188.             for (int i = 0; i < 5; ++i){
  189.                 for (vector<hand*>::iterator it = players.begin(); it < players.end(); ++it)
  190.                 {
  191.                     (*it)->draw();
  192.                 }
  193.             }
  194.         }
  195.         virtual string toString(){
  196.             string toReturn = "Now Playing " + name + "\n\n";
  197.  
  198.             for (vector<hand*>::iterator it = players.begin(); it != players.end(); ++it)
  199.                 toReturn += (*it)->toString();
  200.  
  201.             return toReturn;
  202.         }
  203.         virtual void nextTurn(){
  204.             if (turn == players.end())
  205.                 turn = players.begin();
  206.             else
  207.                 turn++;
  208.         }
  209.         virtual void play(int i){
  210.             played.push_back((*turn)->playCard(i));
  211.         }
  212.     };
  213. }
  214.  
  215. namespace village_idiot{
  216.     using namespace cards;
  217.     class VI_hand : public hand{
  218.     public:
  219.         vector<card> faceUp;
  220.         vector<card> faceDown;
  221.  
  222.         VI_hand(string name, deck* d) : hand(name, d){}
  223.  
  224.         string toString(){
  225.             string toReturn = ((hand*)this)->toString();
  226.             toReturn += "\nFace Up :";
  227.             for (vector<card>::iterator it = faceUp.begin(); it != faceUp.end(); it++){
  228.                 toReturn += "\n" + (*it).toString();
  229.             }
  230.             toReturn != "\n\nFace Down :";
  231.             for (vector<card>::iterator it = faceDown.begin(); it != faceDown.end(); it++){
  232.                 toReturn += "\n???";
  233.             }
  234.             return toReturn;
  235.         }
  236.     };
  237.     class VI_game : public game{
  238.     private:
  239.         int lastValue;
  240.         card lastPlayed(){
  241.             if (!played.empty()){
  242.                 return *played.end();
  243.             }
  244.             return card();
  245.         }
  246.         bool validPlay(card& c){
  247.             if (c.num == 0){ return true; }
  248.             return c >= lastPlayed() | c == 7 | c == 2 | c == 10;
  249.         }
  250.         void updateValue(card& c){
  251.             if (c == 2 | c == 10){
  252.                 lastValue = 0;
  253.             }
  254.             else if (c == 7){}
  255.             else{
  256.                 lastValue = c.num;
  257.             }
  258.         }
  259.         void clear(){
  260.             for (vector<card>::iterator x = played.begin(); x != played.end(); x++){
  261.                 played.erase(x);
  262.             }
  263.         }
  264.  
  265.     public:
  266.         VI_game(vector<string>& players_) : game("Village Idiot"){
  267.             for each(string it in players_){
  268.                 VI_hand& x = VI_hand(it, &d);
  269.                 players.push_back(&x);
  270.             }
  271.         }
  272.  
  273.         void reverseTurn(){
  274.             if (!played.empty()){
  275.                 if (turn == players.begin())
  276.                     turn = players.end();
  277.                 else
  278.                     turn--;
  279.             }
  280.         }
  281.         void play(int i){
  282.             card x = (*turn)->playCard(i);
  283.             if (validPlay(x)){
  284.                 if (x == 10)
  285.                     clear();
  286.                 else
  287.                     updateValue(x);
  288.             }
  289.             else{
  290.                 (*turn)->pickup(played);
  291.             }
  292.         }
  293.     };
  294. }
  295.  
  296.  
  297. int _tmain(int argc, _TCHAR* argv[])
  298. {
  299.     using namespace village_idiot;
  300.  
  301.     // ===== SELECT GAME =====
  302.     cout << "What game would you like to play?" << endl;
  303.     string g_id;
  304.     cout << "1 Village Idiot" << endl;
  305.     cout << "- Other" << endl;
  306.     cin >> g_id;
  307.  
  308.     string s;
  309.     vector<string> players;
  310.  
  311.     string g_name;
  312.  
  313.     if (g_id == "-"){
  314.         cout << "Name of this game?" << endl;
  315.         cin >> g_name;
  316.     }
  317.  
  318.  
  319.     // ===== PLAYER NAMES =====
  320.     cout << "Enter player names! Type done when finished." << endl;
  321.  
  322.     while (cin >> s){
  323.         if (s == "done")
  324.             break;
  325.         players.push_back(s);
  326.     }
  327.  
  328.  
  329.     // ===== INIT GAME =====
  330.     game g;
  331.  
  332.     if (g_id == "1")
  333.         g = VI_game(players);
  334.     else if (g_id == "-"){
  335.         g = game(g_name, players);
  336.     }
  337.     else{
  338.         cout << "Error: Game not Initialized";
  339.         return 1;
  340.     }
  341.  
  342.     clear();
  343.  
  344.  
  345.     // ===== PLAY GAME =====
  346.     g.deal();
  347.  
  348.     cout << g.toString() << endl;
  349.  
  350.     cin >> s;
  351.  
  352.     return 0;
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement