diff --git a/Solitaire/main.cpp b/Solitaire/main.cpp index 6b3cc7a..bfa7999 100644 --- a/Solitaire/main.cpp +++ b/Solitaire/main.cpp @@ -118,22 +118,22 @@ class Solitaire : public olc::PixelGameEngine }break; case HOME: { if (faceUpCards.size() > 2) { - Card& c = faceUpCards[faceUpCards.size() - 1]; + Card c = faceUpCards[faceUpCards.size() - 3]; game->RenderCard(pos+vi2d{-2,-2}, c.s, c.number); c = faceUpCards[faceUpCards.size() - 2]; game->RenderCard(pos + vi2d{ -1,-1 }, c.s, c.number); - c = faceUpCards[faceUpCards.size() - 3]; + c = faceUpCards[faceUpCards.size() - 1]; game->RenderCard(pos, c.s, c.number); } else if (faceUpCards.size() > 1) { - Card& c = faceUpCards[faceUpCards.size() - 1]; + Card c = faceUpCards[faceUpCards.size() - 2]; game->RenderCard(pos + vi2d{ -1,-1 }, c.s, c.number); - c = faceUpCards[faceUpCards.size() - 2]; + c = faceUpCards[faceUpCards.size() - 1]; game->RenderCard(pos, c.s, c.number); } else if (faceUpCards.size() == 1) { - Card& c = faceUpCards[faceUpCards.size() - 1]; + Card c = faceUpCards[faceUpCards.size() - 1]; game->RenderCard(pos, c.s, c.number); } }break; @@ -180,6 +180,38 @@ class Solitaire : public olc::PixelGameEngine } return false; } + + void ResetDraggedCard() { + for (int i = 0; i < previousCardInteraction.size(); i++) { + previousCardInteraction[i]->number = draggedCard[i].number; + previousCardInteraction[i]->s = draggedCard[i].s; + } + draggedCard.clear(); + previousCardInteraction.clear(); + } + + void DeleteDraggedCard() { + switch (previousCardSlotInteraction->type) { + case ACTIVE_PLAYDECK: { + //Erases top-most card. + previousCardSlotInteraction->faceUpCards.erase(previousCardSlotInteraction->faceUpCards.end() - 1); + }break; + case HOME: { + //Erases top-most card. + previousCardSlotInteraction->faceUpCards.erase(previousCardSlotInteraction->faceUpCards.end() - 1); + }break; + case FIELD: { + //Erases top-most card. + for (int i = 0; i < draggedCard.size(); i++) { + previousCardSlotInteraction->faceUpCards.erase(previousCardSlotInteraction->faceUpCards.end() - 1); + } + }break; + } + previousCardSlotInteraction = NULL; + draggedCard.clear(); + previousCardInteraction.clear(); + } + void UpdateCardSlots() { if (GetMouse(0).bPressed&&MouseOver(PlayDeck)) { if (PlayDeck.faceDownCards.size() > 0) { @@ -202,8 +234,9 @@ class Solitaire : public olc::PixelGameEngine if (GetMouse(0).bPressed && MouseOver(ActivePlayDeck)) { if (ActivePlayDeck.faceUpCards.size() > 0) { Card& card = ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1]; - draggedCard = { card.s,card.number }; - previousCardInteraction = &ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1]; + draggedCard.push_back({ card.s,card.number }); + previousCardInteraction.push_back(& ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1]); + previousCardSlotInteraction = &ActivePlayDeck; ActivePlayDeck.faceUpCards[ActivePlayDeck.faceUpCards.size() - 1].number = -1; } } @@ -211,8 +244,9 @@ class Solitaire : public olc::PixelGameEngine if (GetMouse(0).bPressed && MouseOver(Home[i])) { if (Home[i].faceUpCards.size() > 0) { Card& card = Home[i].faceUpCards[Home[i].faceUpCards.size() - 1]; - draggedCard = { card.s,card.number }; - previousCardInteraction = &Home[i].faceUpCards[Home[i].faceUpCards.size() - 1]; + draggedCard.push_back({ card.s,card.number }); + previousCardInteraction.push_back(& Home[i].faceUpCards[Home[i].faceUpCards.size() - 1]); + previousCardSlotInteraction = &Home[i]; Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].number = -1; } } @@ -220,10 +254,28 @@ class Solitaire : public olc::PixelGameEngine for (int i = 0; i < Field.size(); i++) { if (GetMouse(0).bPressed && MouseOver(Field[i])) { if (Field[i].faceUpCards.size() > 0) { - Card& card = Field[i].faceUpCards[Field[i].faceUpCards.size() - 1]; - draggedCard = { card.s,card.number }; - previousCardInteraction = &Field[i].faceUpCards[Field[i].faceUpCards.size() - 1]; - Field[i].faceUpCards[Field[i].faceUpCards.size() - 1].number = -1; + if (GetMouseY() < Field[i].pos.y + Field[i].faceDownCards.size() * Field[i].cardSpacing) { + //Grabbing everything because we clicked above the stack. + for (int j=0;j 0) { Card card = Field[i].faceDownCards[Field[i].faceDownCards.size() - 1]; @@ -234,10 +286,32 @@ class Solitaire : public olc::PixelGameEngine } } - if (GetMouse(0).bReleased && draggedCard.number !=-1) { - previousCardInteraction->number = draggedCard.number; - previousCardInteraction->s = draggedCard.s; - draggedCard.number = -1; + if (GetMouse(0).bReleased && draggedCard.size()>0) { + for (int i = 0; i < Home.size(); i++) { + if (MouseOver(Home[i])) { + if (draggedCard.size()==1&&draggedCard[0].number == 1 && Home[i].faceUpCards.size() == 0) { + //If the slot has no cards in it and it's an Ace, accept. + Home[i].faceUpCards.push_back(draggedCard[0]); + DeleteDraggedCard(); + goto draggedCardSuccess; + } else + if (Home[i].faceUpCards.size() > 0 && draggedCard.size() == 1 && draggedCard[0].number == Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].number + 1 && + draggedCard[0].s == Home[i].faceUpCards[Home[i].faceUpCards.size() - 1].s) { + //If the card we are holding is one number higher than the top card on the slot & the suit is matching. + Home[i].faceUpCards.push_back(draggedCard[0]); + DeleteDraggedCard(); + goto draggedCardSuccess; + } + } + } + for (int i = 0; i < Field.size(); i++) { + if (MouseOver(Field[i])) { + + } + } + ResetDraggedCard(); + draggedCardSuccess: + int a; } } @@ -255,8 +329,9 @@ class Solitaire : public olc::PixelGameEngine CardSlot ActivePlayDeck{ {56,8},ACTIVE_PLAYDECK }; std::array Home; std::array Field; - Card draggedCard = {HEART,-1}; - Card*previousCardInteraction=NULL; + std::vector draggedCard = {}; + std::vectorpreviousCardInteraction = {}; + CardSlot*previousCardSlotInteraction=NULL; Card DrawCard() { std::uniform_int_distribution distribution(0, cardDeck.size()-1); @@ -279,6 +354,10 @@ class Solitaire : public olc::PixelGameEngine diamond_spr.Load("assets/diamond.png"); heart_spr.Load("assets/heart.png"); spade_spr.Load("assets/spade.png"); + Home[0].faceUpCards.push_back({ SPADE,1 }); + Home[0].faceUpCards.push_back({ SPADE,2 }); + Field[3].faceUpCards.push_back({ SPADE,3 }); + Field[3].faceUpCards.push_back({ SPADE,4 }); for (int i = 0; i < Field.size(); i++) { for (int j = 0; j < i; j++) { Field[i].faceDownCards.push_back(DrawCard()); @@ -316,7 +395,9 @@ class Solitaire : public olc::PixelGameEngine for (CardSlot& c : Field) { c.RenderCards(); } - RenderCard(GetMousePos() - CARD_SIZE/2, draggedCard.s, draggedCard.number); + for (int i = 0; i < draggedCard.size(); i++) { + RenderCard(GetMousePos() - CARD_SIZE / 2 + vi2d{0,i*16}, draggedCard[i].s, draggedCard[i].number); + } return true; } };