Added AI Player menu setup.

master
sigonasr2 1 year ago
parent 3688ff574c
commit 591eaf788a
  1. 261
      CardGameRef/CardGame.cpp
  2. 3
      CardGameRef/CardGameRef.vcxproj
  3. 3
      CardGameRef/CardGameRef.vcxproj.filters
  4. 1170
      CardGameRef/olcPGEX_QuickGUI.h

@ -1,5 +1,7 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
#define OLC_PGEX_QUICKGUI
#include "olcPGEX_QuickGUI.h"
using namespace olc; using namespace olc;
@ -33,14 +35,40 @@ class CardGameRef : public olc::PixelGameEngine
DRAWFOUR, DRAWFOUR,
}; };
enum GameState{
AIPICK,
DRAW,
PICK,
RESULT,
VICTORY
};
struct Rect{ struct Rect{
vf2d pos; vf2d pos;
vf2d size; vf2d size;
}; };
struct Card{
Color col;
CardValue val;
bool faceDown=false;
Card(Color col,CardValue val,bool faceDown=false)
:col(col),val(val),faceDown(faceDown){}
};
typedef std::vector<Card> Hand;
typedef std::vector<Card> CardPile;
std::vector<Decal*>IMAGES; std::vector<Decal*>IMAGES;
Rect cardBorder,cardBack,skip,reverse,wild,drawTwo,drawFour; Rect cardBorder,cardBack,skip,reverse,wild,drawTwo,drawFour;
Decal*cardTilesheet; Decal*cardTilesheet;
Hand playerHand;
std::vector<Hand>opponentHands;
CardPile startingPile,drawPile,discardPile;
QuickGUI::Manager playerPickMenu;
QuickGUI::Button *TwoPlayerButton,*ThreePlayerButton,*FourPlayerButton;
GameState state=AIPICK;
public: public:
CardGameRef() CardGameRef()
@ -49,23 +77,25 @@ public:
sAppName = "Uno!"; sAppName = "Uno!";
} }
public: void ReshuffleDrawPile(){
bool OnUserCreate() override while(discardPile.size()>1){
{ int randomCardIndex=rand()%(discardPile.size()-1);
// Called once at the start, so create things here drawPile.push_back(discardPile[randomCardIndex]);
discardPile.erase(discardPile.begin()+randomCardIndex);
cardBorder={{0,0},{48,64}}; }
cardBack={{48,0},{48,64}}; }
skip={{0,64},{32,32}};
reverse={{32,64},{32,32}};
wild={{64,64},{32,32}};
drawTwo={{96,32},{32,32}};
drawFour={{96,64},{32,32}};
cardTilesheet = new Decal(new Sprite("CardGame.png"));
SetPixelMode(Pixel::MASK); void DrawCard(Hand&hand){
if(drawPile.size()==0){
ReshuffleDrawPile();
}
if(drawPile.size()>0){//It's possible all the cards in the game may have been drawn,
hand.push_back(drawPile.back());
drawPile.erase(drawPile.end()-1);
}
}
void GenerateCards(){
for(int color=0;color<4;color++){ for(int color=0;color<4;color++){
for(int numb=0;numb<13;numb++){ for(int numb=0;numb<13;numb++){
Decal*card=new Decal(new Sprite(48,64)); Decal*card=new Decal(new Sprite(48,64));
@ -73,43 +103,43 @@ public:
DrawPartialSprite({0,0},cardTilesheet->sprite,cardBack.pos,cardBack.size); DrawPartialSprite({0,0},cardTilesheet->sprite,cardBack.pos,cardBack.size);
Pixel targetCol; Pixel targetCol;
switch(Color(color)){ switch(Color(color)){
case Color::RED:{ case Color::RED:{
targetCol={192,0,0}; targetCol={192,0,0};
}break; }break;
case Color::GREEN:{ case Color::GREEN:{
targetCol={0,192,0}; targetCol={0,192,0};
}break; }break;
case Color::BLUE:{ case Color::BLUE:{
targetCol={0,0,192}; targetCol={0,0,192};
}break; }break;
case Color::YELLOW:{ case Color::YELLOW:{
targetCol={192,192,0}; targetCol={192,192,0};
}break; }break;
} }
for(int y=0;y<64;y++){ for(int y=0;y<64;y++){
for(int x=0;x<48;x++){ for(int x=0;x<48;x++){
if(card->sprite->GetPixel(x,y)==Pixel{235,235,235}){ if(card->sprite->GetPixel(x,y)==Pixel{235,235,235}){
card->sprite->SetPixel({x,y},targetCol); card->sprite->SetPixel({x,y},targetCol);
} else } else
if(card->sprite->GetPixel(x,y)==Pixel{103,103,103}){ if(card->sprite->GetPixel(x,y)==Pixel{103,103,103}){
card->sprite->SetPixel({x,y},targetCol/2); card->sprite->SetPixel({x,y},targetCol/2);
} }
} }
} }
DrawPartialSprite({0,0},cardTilesheet->sprite,cardBorder.pos,cardBorder.size); DrawPartialSprite({0,0},cardTilesheet->sprite,cardBorder.pos,cardBorder.size);
switch(numb){ switch(numb){
case SKIP:{ case SKIP:{
DrawPartialSprite(vi2d{24,32}-skip.size/2,cardTilesheet->sprite,skip.pos,skip.size); DrawPartialSprite(vi2d{24,32}-skip.size/2,cardTilesheet->sprite,skip.pos,skip.size);
}break; }break;
case REVERSE:{ case REVERSE:{
DrawPartialSprite(vi2d{24,32}-reverse.size/2,cardTilesheet->sprite,reverse.pos,reverse.size); DrawPartialSprite(vi2d{24,32}-reverse.size/2,cardTilesheet->sprite,reverse.pos,reverse.size);
}break; }break;
case DRAWTWO:{ case DRAWTWO:{
DrawPartialSprite(vi2d{24,32}-drawTwo.size/2,cardTilesheet->sprite,drawTwo.pos,drawTwo.size); DrawPartialSprite(vi2d{24,32}-drawTwo.size/2,cardTilesheet->sprite,drawTwo.pos,drawTwo.size);
}break; }break;
default:{ default:{
DrawString(vi2d{24,32}-GetTextSize(std::to_string(numb))/2*3,std::to_string(numb),WHITE,3); DrawString(vi2d{24,32}-GetTextSize(std::to_string(numb))/2*3,std::to_string(numb),WHITE,3);
} }
} }
card->Update(); card->Update();
IMAGES.push_back(card); IMAGES.push_back(card);
@ -120,45 +150,97 @@ public:
SetDrawTarget(card->sprite); SetDrawTarget(card->sprite);
DrawPartialSprite({0,0},cardTilesheet->sprite,cardBack.pos,cardBack.size); DrawPartialSprite({0,0},cardTilesheet->sprite,cardBack.pos,cardBack.size);
switch(i){ switch(i){
case 0:{//Wild Card case 0:{//Wild Card
DrawPartialSprite(vi2d{24,32}-wild.size/2,cardTilesheet->sprite,wild.pos,wild.size); DrawPartialSprite(vi2d{24,32}-wild.size/2,cardTilesheet->sprite,wild.pos,wild.size);
}break; }break;
case 1:{//Draw Four Card case 1:{//Draw Four Card
DrawPartialSprite(vi2d{24,32}-drawFour.size/2,cardTilesheet->sprite,drawFour.pos,drawFour.size); DrawPartialSprite(vi2d{24,32}-drawFour.size/2,cardTilesheet->sprite,drawFour.pos,drawFour.size);
}break; }break;
} }
switch(i){//Two cards have black backs. switch(i){//Two cards have black backs.
case 0: //Wild Card case 0: //Wild Card
case 1:{//Draw Four Card case 1:{//Draw Four Card
for(int y=0;y<64;y++){ for(int y=0;y<64;y++){
for(int x=0;x<48;x++){ for(int x=0;x<48;x++){
if(card->sprite->GetPixel(x,y)==Pixel{235,235,235}){ if(card->sprite->GetPixel(x,y)==Pixel{235,235,235}){
card->sprite->SetPixel({x,y},BLACK); card->sprite->SetPixel({x,y},BLACK);
} else } else
if(card->sprite->GetPixel(x,y)==Pixel{103,103,103}){ if(card->sprite->GetPixel(x,y)==Pixel{103,103,103}){
card->sprite->SetPixel({x,y},BLACK/2); card->sprite->SetPixel({x,y},BLACK/2);
} }
}
} }
}break; }
default:{//This is the face down card. }break;
for(int y=0;y<64;y++){ default:{//This is the face down card.
for(int x=0;x<48;x++){ for(int y=0;y<64;y++){
if(card->sprite->GetPixel(x,y)==Pixel{235,235,235}){ for(int x=0;x<48;x++){
card->sprite->SetPixel({x,y},BLACK); if(card->sprite->GetPixel(x,y)==Pixel{235,235,235}){
} else card->sprite->SetPixel({x,y},BLACK);
if(card->sprite->GetPixel(x,y)==Pixel{103,103,103}){ } else
card->sprite->SetPixel({x,y},{102,0,64}); if(card->sprite->GetPixel(x,y)==Pixel{103,103,103}){
} card->sprite->SetPixel({x,y},{102,0,64});
} }
} }
} }
} }
}
DrawPartialSprite({0,0},cardTilesheet->sprite,cardBorder.pos,cardBorder.size); DrawPartialSprite({0,0},cardTilesheet->sprite,cardBorder.pos,cardBorder.size);
card->Update(); card->Update();
IMAGES.push_back(card); IMAGES.push_back(card);
} }
SetDrawTarget(nullptr); SetDrawTarget(nullptr);
}
public:
bool OnUserCreate() override
{
// Called once at the start, so create things here
cardBorder={{0,0},{48,64}};
cardBack={{48,0},{48,64}};
skip={{0,64},{32,32}};
reverse={{32,64},{32,32}};
wild={{64,64},{32,32}};
drawTwo={{96,32},{32,32}};
drawFour={{96,64},{32,32}};
cardTilesheet = new Decal(new Sprite("CardGame.png"));
SetPixelMode(Pixel::MASK);
GenerateCards();
//2 of each basic card. (80)
//2 of each special card. (24)
//8 Wild Cards (8)
//4 Draw Four Cards (4)
for(int color=0;color<4;color++){
for(int value=0;value<13;value++){
startingPile.emplace_back(Color(color),CardValue(value),true);
startingPile.emplace_back(Color(color),CardValue(value),true);
}
}
for(int i=0;i<8;i++){
startingPile.emplace_back(Color::RED,WILD,true);
}
for(int i=0;i<4;i++){
startingPile.emplace_back(Color::RED,DRAWFOUR,true);
}
while(startingPile.size()>0){
int randomCardIndex=rand()%startingPile.size();
drawPile.push_back(startingPile[randomCardIndex]);
startingPile.erase(startingPile.begin()+randomCardIndex);
}
TwoPlayerButton=new QuickGUI::Button(playerPickMenu,"1 Opponent",{128-42,20},{84,40});
ThreePlayerButton=new QuickGUI::Button(playerPickMenu,"2 Opponents",{128-42,94},{84,40});
FourPlayerButton=new QuickGUI::Button(playerPickMenu,"3 Opponents",{128-42,168},{84,40});
for(int i=0;i<7;i++){
DrawCard(playerHand);
}
return true; return true;
} }
@ -175,15 +257,36 @@ public:
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
// Called once per frame, draws random coloured pixels switch(state){
float scale=256.f/(13*48); case AIPICK:{ //Player Pick Menu
for(int color=0;color<4;color++){ playerPickMenu.Update(this);
for(int value=0;value<13;value++){
DrawRotatedDecal(vf2d{value*scale*48,color*scale*64}+scale*vf2d{24,32}/2,GetCardImage(Color(color),CardValue(value)),0,scale*vf2d{24,32},{scale,scale}); bool buttonClicked=false;
} int AICount=0;
} if(TwoPlayerButton->bPressed){
for (int value=0;value<3;value++){ buttonClicked=true;
DrawRotatedDecal(vf2d{value*scale*48,5*scale*64}+scale*vf2d{24,32}/2,GetCardImage(Color::RED,CardValue(value+13),value==2?true:false),0,scale*vf2d{24,32},{scale,scale}); AICount=1;
}
if(ThreePlayerButton->bPressed){
buttonClicked=true;
AICount=2;
}
if(FourPlayerButton->bPressed){
buttonClicked=true;
AICount=3;
}
if(buttonClicked){
for(int ai=0;ai<AICount;ai++){
Hand opponent;
for (int i=0;i<7;i++){
DrawCard(opponent);
}
opponentHands.push_back(opponent);
}
state=GameState::DRAW;
}
playerPickMenu.DrawDecal(this);
}break;
} }
return true; return true;
} }

@ -76,6 +76,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -104,6 +105,7 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -127,6 +129,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="olcPGEX_QuickGUI.h" />
<ClInclude Include="olcPixelGameEngine.h" /> <ClInclude Include="olcPixelGameEngine.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -18,6 +18,9 @@
<ClInclude Include="olcPixelGameEngine.h"> <ClInclude Include="olcPixelGameEngine.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="olcPGEX_QuickGUI.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="CardGame.cpp"> <ClCompile Include="CardGame.cpp">

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save