// card.h ////////////////////////////////////////////////////////////////////////////// #ifndef CARD_H #define CARD_H #ifndef RECT_H # include "rect.h" #endif ////////////////////////////////////////////////////////////////////////////// // Card is inherited from rectange by adding suit, rank and state (faceup). // Card is drawn differently depending if it's faceUp (suit,rank,colored circle) // or down (X). // There is special empty card to hold place for empty pile. // Empty card dosn't need any special handling here, just have // different name (suit=0) and rank=0. // There is lot of methods tro ask internal values (suit(),rank(),faceUp()..). ////////////////////////////////////////////////////////////////////////////// // Not good to use globals in .h, but quite handy this time! char *SUITS[] = {"empty","heart","club","spade","diamond"}; char *RANKS[] = {"empty","A","2","3","4","5","6","7","8","9","10","J","Q","K"}; int COLORS[] = {0,0,1,1,0}; // 0 = red, 1 = black const CardWidth = 75; const CardHeight = 100; class card : public rect { // Inherit card from rect and int suit_v; // add suit int rank_v; // rank int faceUp_v; // and state void setSRF(int s, int r, int f) { // set suit, rank and faceUp suit_v = s; rank_v = r; faceUp_v = f; if ( faceUp_v < 0 || 1 < faceUp_v ) faceUp_v = 1; if ( suit_v < 0 || 4 < suit_v ) suit_v = 0; if ( rank_v < 0 || 13 < rank_v ) rank_v = 0; } public: card(POINTT u, int s=0, int r=0) : rect(u,u+POINTT(CardWidth,CardHeight)) { setSRF(s,r,0); } // card(int x, int y, int s, int r) { card(POINTT(x,y), s, r); } card(int px, int py, int s=0, int r=0) : rect(px,py,px+CardWidth,py+CardHeight) { setSRF(s,r,0); } virtual void draw(window *w) { rect::draw(w); if ( faceUp() ) { w->setBrush((color() ? black : red)); w->circle(XY_(upper,CardWidth/2,CardHeight-15),10); w->wout << setpos(XY_(upper,5,5)) << SUITS[suit()] << setpos(XY_(upper,25,30)) << RANKS[rank()]; } else { w->setPen(red,solidLine,4); w->line(XY_(upper,4,4),XY_(lower,-4,-4)); w->line(XY_(upper,CardWidth-4,4),XY_(upper,4,CardHeight-4)); } } card *flip() { faceUp_v = 1- faceUp_v; return this;} void moveTo(int x,int y) { rect::moveTo(x,y,x+CardWidth,y+CardHeight);} void moveTo(POINTT p) { moveTo(XY(p)); } int rank() { return rank_v; } int suit() { return suit_v; } int faceUp(){ return faceUp_v; } int color() { return COLORS[suit()]; } }; // card #endif