Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cards.cpp : Defines the entry point for the console application.
- #include "stdafx.h"
- #include <string>
- #include <vector>
- #include <iostream>
- #include <ios>
- using namespace std;
- void clear(){
- system("cls");
- }
- namespace cards{
- class card{
- private:
- string suit;
- public:
- int num;
- card(){ num = 0; suit = 4 + ""; }
- card(int _num, int _suit){
- num = _num;
- suit = (_suit == 0) ? "Spades" : (_suit == 1) ? "Clubs" : (_suit == 2) ? "Diamonds" : "Hearts";
- }
- string toString(){
- string toReturn;
- if (num <= 10)
- toReturn += to_string(num) + " ";
- else if (num == 11)
- toReturn += "Jack ";
- else if (num == 12)
- toReturn += "Queen ";
- else if (num == 13)
- toReturn += "King ";
- else if (num == 14)
- toReturn += "Ace ";
- toReturn += "of " + suit;
- return toReturn;
- }
- bool operator==(card a){
- return (a.num == num && a.suit == suit);
- }
- bool operator==(int n){
- return (num == n);
- }
- bool operator!=(card a){
- return (num != a.num) || (suit != a.suit);
- }
- bool operator>(card a){
- return (num > a.num);
- }
- bool operator<(card a){
- return (num < a.num);
- }
- bool operator>=(card a){
- return (num >= a.num);
- }
- bool operator<=(card a){
- return (num <= a.num);
- }
- };
- class deck{
- private:
- vector<card>* _deck;
- bool checksize(){
- return _deck->size() != 0;
- }
- void reInit(){
- _deck->clear();
- for (int i = 0; i <= 3; ++i){
- for (int j = 2; j <= 14; ++j){
- _deck->push_back(card(j, i));
- }
- }
- }
- public:
- deck(){
- _deck = new vector<card>();
- for (int i = 0; i <= 3; ++i){
- for (int j = 2; j <= 14; ++j){
- _deck->push_back(card(j, i));
- }
- }
- }
- void shuffle(){
- vector<card>::size_type index1 = rand() % _deck->size();
- vector<card>::size_type index2 = rand() % _deck->size();
- auto c = _deck[index1];
- _deck[index1] = _deck[index2];
- _deck[index2] = c;
- }
- void shuffle(int t){
- for (int i = 0; i < t; ++i)
- shuffle();
- }
- card takeTop(){
- card toReturn = _deck->at(0);
- _deck->erase(_deck->begin());
- return toReturn;
- }
- string toString(){
- string toReturn;
- for (vector<card>::iterator it = _deck->begin(); it != _deck->end(); ++it){
- toReturn += (*it).toString() + "\n";
- }
- return toReturn;
- }
- };
- class hand{
- private:
- string owner;
- protected:
- vector<card> cards;
- deck* _deck = new deck();
- public:
- hand(string name, deck* d){
- owner = name;
- _deck = d;
- }
- virtual string toString(){
- string toReturn = owner + "'s Hand:\n";
- for (vector<card>::iterator it = cards.begin(); it != cards.end(); it++)
- {
- toReturn += (*it).toString() + "\n";
- }
- return toReturn + "\n";
- }
- virtual void draw(){
- cards.push_back(_deck->takeTop());
- }
- card playCard(int i){
- card toReturn = cards[i];
- cards.erase(cards.begin() + i);
- return toReturn;
- }
- virtual void pickup(vector<card>& v){
- for (vector<card>::iterator it = v.begin(); it != v.end(); ++it){
- cards.push_back(*it);
- v.erase(it);
- }
- }
- };
- class game{
- protected:
- string name;
- deck d;
- vector<hand*> players;
- vector<hand*>::iterator turn;
- vector<card> played;
- public:
- game(){}
- game(const char* n){
- name = n;
- d.shuffle(1000);
- }
- game(string& n, vector<string>& players_){
- name = n;
- d.shuffle(1000);
- for each (string var in players_){
- hand* x = new hand(var, &d);
- players.push_back(x);
- }
- }
- game(const char* n, vector<string>& players_){
- name = n;
- d.shuffle(1000);
- for each(string var in players_){
- hand* x = new hand(var, &d);
- players.push_back(x);
- }
- }
- virtual void deal(){
- for (int i = 0; i < 5; ++i){
- for (vector<hand*>::iterator it = players.begin(); it < players.end(); ++it)
- {
- (*it)->draw();
- }
- }
- }
- virtual string toString(){
- string toReturn = "Now Playing " + name + "\n\n";
- for (vector<hand*>::iterator it = players.begin(); it != players.end(); ++it)
- toReturn += (*it)->toString();
- return toReturn;
- }
- virtual void nextTurn(){
- if (turn == players.end())
- turn = players.begin();
- else
- turn++;
- }
- virtual void play(int i){
- played.push_back((*turn)->playCard(i));
- }
- };
- }
- namespace village_idiot{
- using namespace cards;
- class VI_hand : public hand{
- public:
- vector<card> faceUp;
- vector<card> faceDown;
- VI_hand(string name, deck* d) : hand(name, d){}
- string toString(){
- string toReturn = ((hand*)this)->toString();
- toReturn += "\nFace Up :";
- for (vector<card>::iterator it = faceUp.begin(); it != faceUp.end(); it++){
- toReturn += "\n" + (*it).toString();
- }
- toReturn != "\n\nFace Down :";
- for (vector<card>::iterator it = faceDown.begin(); it != faceDown.end(); it++){
- toReturn += "\n???";
- }
- return toReturn;
- }
- };
- class VI_game : public game{
- private:
- int lastValue;
- card lastPlayed(){
- if (!played.empty()){
- return *played.end();
- }
- return card();
- }
- bool validPlay(card& c){
- if (c.num == 0){ return true; }
- return c >= lastPlayed() | c == 7 | c == 2 | c == 10;
- }
- void updateValue(card& c){
- if (c == 2 | c == 10){
- lastValue = 0;
- }
- else if (c == 7){}
- else{
- lastValue = c.num;
- }
- }
- void clear(){
- for (vector<card>::iterator x = played.begin(); x != played.end(); x++){
- played.erase(x);
- }
- }
- public:
- VI_game(vector<string>& players_) : game("Village Idiot"){
- for each(string it in players_){
- VI_hand& x = VI_hand(it, &d);
- players.push_back(&x);
- }
- }
- void reverseTurn(){
- if (!played.empty()){
- if (turn == players.begin())
- turn = players.end();
- else
- turn--;
- }
- }
- void play(int i){
- card x = (*turn)->playCard(i);
- if (validPlay(x)){
- if (x == 10)
- clear();
- else
- updateValue(x);
- }
- else{
- (*turn)->pickup(played);
- }
- }
- };
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- using namespace village_idiot;
- // ===== SELECT GAME =====
- cout << "What game would you like to play?" << endl;
- string g_id;
- cout << "1 Village Idiot" << endl;
- cout << "- Other" << endl;
- cin >> g_id;
- string s;
- vector<string> players;
- string g_name;
- if (g_id == "-"){
- cout << "Name of this game?" << endl;
- cin >> g_name;
- }
- // ===== PLAYER NAMES =====
- cout << "Enter player names! Type done when finished." << endl;
- while (cin >> s){
- if (s == "done")
- break;
- players.push_back(s);
- }
- // ===== INIT GAME =====
- game g;
- if (g_id == "1")
- g = VI_game(players);
- else if (g_id == "-"){
- g = game(g_name, players);
- }
- else{
- cout << "Error: Game not Initialized";
- return 1;
- }
- clear();
- // ===== PLAY GAME =====
- g.deal();
- cout << g.toString() << endl;
- cin >> s;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement