I have been working on this mini game of a 'high or low' spin off where you have to shuffle the deck, distribute cards to each player (26 each) randomly . Then the game is whoever has a higher card value at the top of their deck wins. Then you add both cards that were won to their winning pile(to whichever player won), this continues for 26 rounds.
NOTE: I haven't gotten into classes just yet so that's the reason I am using structs.
I have each card(struct) in an array of 52 elements.
here is what i have so far.
#include <iostream>
#include <iomanip>
#include <time.h>
#include <random>
using namespace std;
const int NUM_CARDS = 52;
const int PLAYER_HAND = 26;
struct Card {
int val;
string suit;
};
void shuffleDeck(Card[]);
void displayDeck(Card[]);
void displayHand(Card[]);
void getSuit(Card[]);
void playGame(Card[], Card[], int &, int &);
void tieGame(Card [], Card []);
int main(){
Card myDeck[NUM_CARDS];
Card p1Hand[PLAYER_HAND];
Card p2Hand[PLAYER_HAND];
int player1Winnings = 0;
int player2Winnings = 0;
getSuit(myDeck);
shuffleDeck(myDeck);
getSuit(p1Hand);
getSuit(p2Hand);
shuffleDeck(p1Hand);
shuffleDeck(p2Hand);
cout << "-----------------------------------------\n" << "PLAYER 1 HAND: \n";
displayHand(p1Hand);
cout << "\n\n\n\n\n-----------------------------------------\n" << "PLAYER 2 HAND: \n";
displayHand(p2Hand);
cout << "\n\n\n\n\n";
playGame(p1Hand, p2Hand, player1Winnings, player2Winnings);
cout << endl << player1Winnings << " vs. " << player2Winnings;
}
void shuffleDeck(Card cardArray[])
{
srand(time(0));
for (int i = 0; i < NUM_CARDS; i++){
// Random for remaining positions.
int r = i + (rand() % (NUM_CARDS -i));
swap(cardArray[i], cardArray[r]);
}
}
void displayDeck(Card cardArray[]) {
for (int i = 0; i < NUM_CARDS; i++) {
cout << "\n" << cardArray[i].val << " of " << cardArray[i].suit;
}
}
void getSuit(Card cardArray[]){
for (int i = 0; i < NUM_CARDS; i++) {
if (i%4==0) {
cardArray[i].suit = "Spades";
}
else if (i%4==1) {
cardArray[i].suit = "Clubs";
}
else if (i%4==2) {
cardArray[i].suit = "Hearts";
}
else {
cardArray[i].suit = "Diamonds";
}
cardArray[i].val = i/4+1;
}
}
void displayHand(Card cardArray[]){
for(int i = 0; i < PLAYER_HAND; i++){
cout << "\n" << cardArray[i].val << " of " << cardArray[i].suit;
}
}
void playGame(Card p1Hand[], Card p2Hand[], int &p1Winnings, int &p2Winnings){
int player1Winnings, player2Winnings;
bool flagWin;
for(int i = PLAYER_HAND; i > 0; i--){
flagWin == true ? p1Winnings += 2 : p2Winnings -= 1;
flagWin == false ? p2Winnings += 2 : p1Winnings -= 1;
cout << "\nPlayer 1's top card is: " << p1Hand[i].val << " of " << p1Hand[i].suit;
cout << "\nPlayer 2's top card is: " << p2Hand[i].val << " of " << p2Hand[i].suit;
if(p1Hand[i].val > p2Hand[i].val){
cout << "\n\nPlayer 1 has won this hand with: " << p1Hand[i].val << " of " << p1Hand[i].suit;
flagWin = true;
}
else if(p1Hand[i].val == p2Hand[i].val){
if((p1Hand[i].suit == "Spades") && (p2Hand[i].suit == "Clubs") || (p2Hand[i].suit == "Diamonds") || (p2Hand[i].suit == "Hearts")){
cout << "\nPlayer 1 has won this hand with: " << p1Hand[i].val << " of " << p1Hand[i].suit;
flagWin = true;
}
else if((p1Hand[i].suit == "Clubs") && (p2Hand[i].suit == "Diamonds") || (p2Hand[i].suit == "Hearts")){
cout << "\nPlayer 1 has won this hand with: " << p1Hand[i].val << " of " << p1Hand[i].suit;
flagWin = true;
}
else if((p1Hand[i].suit == "Diamonds") && (p2Hand[i].suit == "Hearts")){
cout << "\nPlayer 1 has won this hand with: " << p1Hand[i].val << " of " << p1Hand[i].suit;
flagWin = true;
}
else {
cout << "\nPlayer 2 has won this hand with: " << p2Hand[i].val << " of " << p2Hand[i].suit;
flagWin = false;
}
}
else{
cout << "\nPlayer 2 has won this hand with: " << p2Hand[i].val << " of " << p2Hand[i].suit;
flagWin = false;
}
}
}
Aucun commentaire:
Enregistrer un commentaire