// pileapp.cpp #include "card.h" class cardLink { public: card *value; cardLink *next; cardLink(card *nvalue, cardLink *link) { value = nvalue; next=link; } }; // cardLink class cardPile { protected: POINTT upper; cardLink *firstLink; public: cardPile(int x,int y) { upper=POINTT(x,y); firstLink = NULL; } virtual card *topCard() { if ( !firstLink ) return NULL; cardLink *p = firstLink; card *q = p->value; firstLink = firstLink->next; delete p; return q; } virtual void addCard(card *newCard) { firstLink = new cardLink(newCard, firstLink); newCard->moveTo(upper); } int includes(int x, int y); virtual void display(window *w) { if ( !firstLink ) return; (firstLink->value)->draw(w); } virtual void select() {}; }; // cardPile int cardPile::includes(int x, int y) { for (cardLink *p = firstLink; p != NULL; p = p-> next ) if ( (p->value)->includes(x,y) ) return 1; return 0; } class deckPil : public cardPile { int count; public: deckPil(int x,int y) : cardPile(x,y) { count = 0; } virtual card *topCard() { if (!count) return NULL; count--; return cardPile::topCard(); } virtual void addCard(card *newCard); }; // deckPil void deckPil::addCard(card *newCard) { int n = randomInt(0,count); if ( n == 0 ) cardPile::addCard(newCard); else { newCard->moveTo(upper); cardLink *p = firstLink; while ( n > 0 ) { if ( p->next ) p = p->next; n--; } p->next = new cardLink(newCard,p->next); } count++; } class pileApplication : public application { public: pileApplication(char *apName, char *apTitle, HANDLE thisInst, HANDLE prevInst): application(apName, apTitle, thisInst, prevInst){}; void mouseDown(int,int); void paint(); }; // pileApplication deckPil theDeck(100,100); cardPile discardPile(100,200); void pileApplication::mouseDown(int x,int y) { if ( theDeck.includes(x,y) ) { card *c = theDeck.topCard(); if ( !c ) return; discardPile.addCard(c); } if ( discardPile.includes(x,y) ) { card *c = discardPile.topCard(); if ( !c ) return; c->flip(); discardPile.addCard(c); } clearAndUpdate(); } void pileApplication::paint() { theDeck.display(this); discardPile.display(this); } void initDeck() { for (int s=1; s <= 4; s++) for (int r=1; r<=13; r++) theDeck.addCard(new card(0,0,s,r)); } int PASCAL WinMain(HANDLE thisInst, HANDLE prevInst, LPSTR, int) { pileApplication theApp("PENS","Pile Application",thisInst,prevInst); initDeck(); return theApp.run(); }