Skip to content

Instantly share code, notes, and snippets.

@OlyDLG
Created June 5, 2017 06:57
Show Gist options
  • Save OlyDLG/0913c4e2ae6bc34c8b13bd3c43f013a1 to your computer and use it in GitHub Desktop.
Save OlyDLG/0913c4e2ae6bc34c8b13bd3c43f013a1 to your computer and use it in GitHub Desktop.
C++14 MTG
/* Base Classes for DLGsMTG
__author__ = 'David Lawrence Goldsmith'
__copyright__ = 2017
__license__ = 'BSD'
__warranty__ = None
__all__ = ('Card', # object representing a single card
'Library' # a deque of cards
'CardSet' # an unordered_set of cards
)
*/
#include "MTG_BaseClasses.h"
#ifdef BASECLASSES
string cKind_str(const cKind kind)
{
string temp;
switch (kind)
{
case land : temp = "Land"; break;
case creat: temp = "Creature"; break;
case art : temp = "Artifact"; break;
case ench : temp = "Enchantment"; break;
case pwlkr: temp = "Planeswalker"; break;
case inst : temp = "Instant"; break;
case sorc : temp = "Sorcery"; break;
}
return temp;
}
string zKind_str(const zKind kind)
{
string temp;
switch (kind)
{
case hand : temp = "Hand"; break;
case field : temp = "Battlefield"; break;
case gyard : temp = "Graveyard"; break;
case stack : temp = "Stack"; break;
case ex : temp = "Exile"; break;
case cmnd : temp = "Command"; break;
}
return temp;
}
void moveCards(deque<Card*>* from, u_set<Card*>* to, uint n=1, bool t=1)
{
uint size = from->size();
if (n <= size)
{
for (uint i=0; i<n; i++)
{
if (t)
{
to->insert(from->at(i));
from->pop_front();
}
else
{
to->insert(from->at(size-1-i));
from->pop_back();
}
}
from->shrink_to_fit();
}
}
Card::Card(const string name,
const vec<cKind> kind,
const string desc,
const bool tapt)
{
this->name = name;
this->kind = kind;
this->desc = desc;
this->tapt = tapt;
}
Card::Card(const Card& card)
{
this->name = card.name;
this->kind = card.kind;
this->desc = card.desc;
this->tapt = card.tapt;
}
Card::~Card(){;}
string Card::str()
{
string strrep = "Card name: " + this->name + '\n';
strrep += "Card types: ";
int i;
for (i=0; i < this->kind.size()-1; i++)
{
strrep += cKind_str(this->kind[i]) + ", ";
}
strrep += cKind_str(this->kind[i]) + '\n';
strrep += "Card description: " + this->desc + '\n';
strrep += "Tapped: ";
strrep += ((this->tapt) ? "yes" : "no");
strrep += '\n';
return strrep;
}
Lib::Lib(uint num)
{
for (uint i=0; i<num; i++)
{
this->cards.push_back(new Card());
}
}
Lib::~Lib()
{
// delete this->player;
// delete this->game;
}
string Lib::str(uint n)
{
uint i;
string strrep;
if (n)
{
strrep = "Your first " + to_string(n) + " cards:\n";
for (i=0; i<n; i++)
{
strrep += this->cards[i]->str();
}
}
else
{
strrep = "Your Library of Cards:\n";
for (i=0; i<this->cards.size(); i++)
{
strrep += "Card #" + to_string(i+1) + "\n";
strrep += this->cards[i]->str();
}
}
return strrep;
}
void Lib::draw(u_set<Card*>* zone, uint n, bool t)
{
if (n <= this->cards.size())
{
moveCards(&this->cards, zone, n, t);
}
}
/*
void Lib::undraw (u_set<Card*>* zone);
*/
void Lib::shuffle()
{
random_device rd;
mt19937 g(rd());
::shuffle(this->cards.begin(), this->cards.end(), g);
}
CardSet::CardSet()
{
this->cards = new u_set<Card*>;
}
CardSet::~CardSet()
{
delete this->cards;
// delete this->player;
// delete this->game;
}
string CardSet::str()
{
string strrep = "This set contains:\n";
for (auto iter = this->cards->begin();
iter != this->cards->end();
++iter)
{
strrep += (*iter)->str();
}
return strrep;
}
void CardSet::addCards(CardSet* cards) // Doesn't yet work (2017-06-04)
{
for (auto iter = cards->cards->begin();
iter != cards->cards->end();
++iter)
{
this->cards->insert(*iter);
}
}
void CardSet::moveCards(CardSet* cards, CardSet* dest)
{
}
#endif
int main()
{
Card* card = new Card();
cout << card->str();
Card* mcard = new Card(*card);
mcard->name = "Mountian";
mcard->kind.push_back(ench);
mcard->kind.push_back(sorc);
mcard->tapt = 1;
cout << mcard->str();
cout << "Original card:\n" << card->str();
delete mcard;
delete card;
Lib* lib = new Lib();
for (uint i=0; i < lib->cards.size(); i++)
{
lib->cards[i]->desc = to_string(i);
}
cout << lib->str();
lib->shuffle();
cout << lib->str();
delete lib;
};
/* Base Classes for DLGsMTG
__author__ = 'David Lawrence Goldsmith'
__copyright__ = 2017
__license__ = 'BSD'
__warranty__ = None
__all__ = ('Card', # object representing a single card
'Library' # a deque of cards
'CardSet' # an unordered_set of cards
)
*/
#ifndef BASECLASSES
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>
#include <vector>
#define vec vector
#include <deque>
#include <unordered_set>
#define u_set unordered_set
using namespace std;
enum cKind {land, creat, art, ench, pwlkr, inst, sorc};
enum zKind {hand, field, gyard, stack, ex, cmnd};
struct Card
{
Card(const string name="", // use const here b/c ctor doesn't
const vec<cKind> kind={land}, // doesn't modify arguments
const string desc="mana",
const bool tapt=0);
Card(const Card& card); // Copy ctor
~Card();
string name;
vec<cKind> kind;
string desc;
bool tapt;
string str();
};
struct Lib
{
Lib(uint num=5);
~Lib();
deque<Card*> cards;
// Player* player=NULL;
// Game* game=NULL;
string str(uint n=0); // n=number of cards
void draw(u_set<Card*>* zone, uint n=1, bool t=1); // t=1 = draw card(s) from top, 0 = bottom
// void setPlayer(Player* p){this->player = p;};
// Player* getPlayer()
// void setGame(Game* g){this->game = g;};
// Game* getGame(){return this->game;};
// void undraw (u_set<Card*>& cards);
void shuffle();
};
struct CardSet
{
CardSet();
~CardSet();
u_set<Card *>* cards;
string str();
void addCards(CardSet* cards);
void moveCards(CardSet* cards, CardSet* dest);
};
#define BASECLASSES 1
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment