Randomize and setup playing field, all rendering of cards and card slots completed.

master
sigonasr2 2 years ago
parent 7531981c08
commit 9cccbec9b7
  1. 185
      Solitaire/main.cpp

@ -1,91 +1,153 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include <random>
using namespace olc; using namespace olc;
const float PI = 3.14159f; const float PI = 3.14159f;
const vi2d CARD_SIZE = vi2d{32,48}; const vi2d CARD_SIZE = vi2d{32,48};
PixelGameEngine*game; class Solitaire;
Solitaire*game;
class Example : public olc::PixelGameEngine class Solitaire : public olc::PixelGameEngine
{ {
std::default_random_engine generator;
Renderable club_spr, spade_spr, diamond_spr, heart_spr;
enum Suit {
CLUB,
DIAMOND,
HEART,
SPADE,
};
enum Suit { enum SlotType {
SPADE, PLAYDECK,
CLUB, ACTIVE_PLAYDECK,
HEART, HOME,
DIAMOND FIELD
}; };
enum SlotType {
PLAYDECK,
ACTIVE_PLAYDECK,
HOME,
FIELD
};
struct Card { struct Card {
Suit s; Suit s;
int number; int number;
}; };
struct CardSlot { Decal*GetSprite(Suit s) {
vf2d pos; switch (s) {
SlotType type; case CLUB: {
std::vector<Card> faceDownCards; return club_spr.Decal();
std::vector<Card> faceUpCards; }break;
std::vector<Card> flippedOverCards; //Just for active play deck. Face up cards will contain the rest of the pile. case DIAMOND: {
void RenderCard(vf2d pos, Suit s, int number) { return diamond_spr.Decal();
game->FillRectDecal(pos, CARD_SIZE, WHITE); }break;
game->DrawStringDecal(pos + vf2d{ 2,2 }, number==1?"A":number == 13 ? "K" : number == 12 ? "Q" : number == 11 ? "J" : std::to_string(number), s <= CLUB ? BLACK : DARK_RED, number == 10 ? vf2d{0.6,1} : vf2d{1,1}); case HEART: {
game->DrawRotatedStringDecal(pos + CARD_SIZE - vf2d{ 2,2 }, number == 1 ? "A" : number == 13 ? "K" : number == 12 ? "Q" : number == 11 ? "J" : std::to_string(number), PI, { 0,0 }, s <= CLUB ? BLACK : DARK_RED, number == 10 ? vf2d{ 0.6,1 } : vf2d{ 1,1 }); return heart_spr.Decal();
game->DrawRectDecal(pos, CARD_SIZE, BLACK);
}
void RenderCards() {
switch (type) {
case PLAYDECK: {
if (faceDownCards.size() > 0) {
game->FillRectDecal(pos, CARD_SIZE, DARK_CYAN);
}
}break; }break;
case ACTIVE_PLAYDECK: { case SPADE: {
if (flippedOverCards.size() > 0) { return spade_spr.Decal();
for (int i = 0; i < flippedOverCards.size(); i++) { }break;
Card& c = flippedOverCards[i]; }
RenderCard(pos + vi2d{ i * 12,0 }, c.s, c.number); }
struct CardSlot {
vf2d pos;
SlotType type;
std::vector<Card> faceDownCards;
std::vector<Card> faceUpCards;
std::vector<Card> flippedOverCards; //Just for active play deck. Face up cards will contain the rest of the pile.
void RenderCard(vf2d pos, Suit s, int number) {
game->FillRectDecal(pos, CARD_SIZE, WHITE);
game->DrawStringDecal(pos + vf2d{ 2,2 }, number==1?"A":number == 13 ? "K" : number == 12 ? "Q" : number == 11 ? "J" : std::to_string(number), s == CLUB || s == SPADE ? BLACK : DARK_RED, number == 10 ? vf2d{0.6,1} : vf2d{1,1});
game->DrawDecal(pos + vf2d{ 2,12 }, game->GetSprite(s), { 0.25,0.25 });
game->DrawRotatedStringDecal(pos + CARD_SIZE - vf2d{ 2,2 }, number == 1 ? "A" : number == 13 ? "K" : number == 12 ? "Q" : number == 11 ? "J" : std::to_string(number), PI, { 0,0 }, s == CLUB||s==SPADE ? BLACK : DARK_RED, number == 10 ? vf2d{ 0.6,1 } : vf2d{ 1,1 });
game->DrawRotatedDecal(pos + CARD_SIZE - vf2d{ 2,12 }, game->GetSprite(s), PI, { 0,0 }, { 0.25, 0.25 });
game->DrawRectDecal(pos, CARD_SIZE, BLACK);
}
void RenderCards() {
game->DrawRectDecal(pos, CARD_SIZE, BLACK);
switch (type) {
case PLAYDECK: {
if (faceDownCards.size() > 0) {
game->FillRectDecal(pos, CARD_SIZE, DARK_CYAN);
}
}break;
case ACTIVE_PLAYDECK: {
if (flippedOverCards.size() > 0) {
for (int i = 0; i < flippedOverCards.size(); i++) {
Card& c = flippedOverCards[i];
RenderCard(pos + vi2d{ i * 12,0 }, c.s, c.number);
}
} else {
if (faceUpCards.size() > 0) {
Card& c = faceUpCards[faceUpCards.size() - 1];
RenderCard(pos, c.s, c.number);
}
} }
} else { }break;
case HOME: {
if (faceUpCards.size() > 0) { if (faceUpCards.size() > 0) {
Card& c = faceUpCards[faceUpCards.size() - 1]; Card& c = faceUpCards[faceUpCards.size() - 1];
RenderCard(pos, c.s, c.number); RenderCard(pos, c.s, c.number);
} }
} }break;
}break; case FIELD: {
if (faceUpCards.size() > 0) {
for (int i = 0; i < faceUpCards.size(); i++) {
Card& c = faceUpCards[i];
RenderCard(pos + vi2d{ 0,i * 16 }, c.s, c.number);
}
}
else
if (faceDownCards.size()>0){
game->FillRectDecal(pos, CARD_SIZE, DARK_CYAN);
}
}break;
}
} }
} };
};
public: public:
Example() Solitaire()
{ {
sAppName = "Solitaire"; sAppName = "Solitaire";
game = this; game = this;
} }
public: public:
std::vector<Card> cardDeck;
CardSlot PlayDeck{ {16,8},PLAYDECK }; CardSlot PlayDeck{ {16,8},PLAYDECK };
CardSlot ActivePlayDeck{ {56,8},ACTIVE_PLAYDECK }; CardSlot ActivePlayDeck{ {56,8},ACTIVE_PLAYDECK };
std::array<CardSlot, 4> Home; std::array<CardSlot, 4> Home;
std::array<CardSlot, 7> Field; std::array<CardSlot, 7> Field;
Card DrawCard() {
std::uniform_int_distribution<int> distribution(0, cardDeck.size()-1);
int dice_roll = distribution(generator);
std::cout << dice_roll << std::endl;
Card drawnCard = cardDeck[dice_roll];
cardDeck.erase(cardDeck.begin() + dice_roll);
return drawnCard;
}
bool OnUserCreate() override bool OnUserCreate() override
{ {
PlayDeck.faceDownCards.push_back({SPADE,4}); for (int i = 0; i <= SPADE; i++) {
ActivePlayDeck.faceUpCards.push_back({ CLUB,9 }); for (int j = 1; j < 14; j++) {
ActivePlayDeck.flippedOverCards.push_back({ HEART,3 }); cardDeck.push_back({ (Suit)i,j });
ActivePlayDeck.flippedOverCards.push_back({ HEART,10 }); }
ActivePlayDeck.flippedOverCards.push_back({ SPADE,1 }); }
Clear(VERY_DARK_GREEN);
club_spr.Load("assets/club.png");
diamond_spr.Load("assets/diamond.png");
heart_spr.Load("assets/heart.png");
spade_spr.Load("assets/spade.png");
for (int i = 0; i < Field.size(); i++) {
for (int j = 0; j < i; j++) {
Field[i].faceDownCards.push_back(DrawCard());
}
Field[i].faceUpCards.push_back(DrawCard());
}
for (int i = 0; i < Home.size();i++) { for (int i = 0; i < Home.size();i++) {
CardSlot&c = Home[i]; CardSlot&c = Home[i];
c.pos = vi2d{ 136 + i * 40,8 }; c.pos = vi2d{ 136 + i * 40,8 };
@ -96,6 +158,9 @@ public:
c.pos = vi2d{ 16 + i * 40,64 }; c.pos = vi2d{ 16 + i * 40,64 };
c.type = SlotType::FIELD; c.type = SlotType::FIELD;
} }
while (cardDeck.size()>0) {
PlayDeck.faceDownCards.push_back(DrawCard());
}
return true; return true;
} }
@ -118,7 +183,7 @@ public:
int main() int main()
{ {
Example demo; Solitaire demo;
if (demo.Construct(312, 200, 4, 4)) if (demo.Construct(312, 200, 4, 4))
demo.Start(); demo.Start();

Loading…
Cancel
Save