Search code examples
c++poker

Poker game: Swap method in C++


I'm learning C++ and I'm trying to figure out a method that involves switching out a card in the list below.

for example:

  1. ace of spades
  2. king of hearts
  3. four of clubs
  4. two of hearts
  5. two of clubs

How would I go about exchanging 2, 3, and 5 each for a new card.

Ok so here is my code, i have other header files that it also uses but I think that you guys should be able to understand stand where I going with it.

#ifndef POKERGAME_H
#define POKERGAME_H//guard code 

#include <iostream>
#include <iomanip>

//don't need to add .cpp files  
#include "Card.h"
#include "Deck.h"
#include "Hand.h"


class PokerGame 
{
public:
  void playGame()
  {
    PokerGame play;
    Deck myCard;
    Hand hand;
    Hand list;

    cout << "***Simple 5-card Poker***" << endl;

    //get a brand new card and places it in position 
    hand.setCard(0, myCard.getNextCard());
    hand.setCard(1, myCard.getNextCard());
    hand.setCard(2, myCard.getNextCard());
    hand.setCard(3, myCard.getNextCard());
    hand.setCard(4, myCard.getNextCard());

    cout << "The cards have been shuffled and you are dealt " << endl
      <<"1."<< hand.getCard(0).printName() << endl//then i ask what is the name of the card in that position
      <<"2."<< hand.getCard(1).printName() << endl
      <<"3."<< hand.getCard(2).printName() << endl
      <<"4."<< hand.getCard(3).printName() << endl
      <<"5."<< hand.getCard(4).printName() << endl;

    //ask for users input and store them in an array 
    int stop = 0;
    int user_input[6];
    int counter = 0;

    while((stop != -1) && counter < 6 )
    {

      cout << "Indicate the cards that you would like to exchange (-1 to end): ";
      cin >> user_input[counter];


      if(user_input[counter] > 5 || user_input[counter] < 0 || user_input[counter - 1] == user_input[counter])
      {
        cout << "Invalid input" << endl; 
        if(user_input[counter] == -1) 
        {
          stop = -1;
          cout << "...Oh nevermind...ended" << endl;               
        }                
      }
      counter++;
    }

This is where I'm having trouble, I'm only getting #1 on the list to change. When only the user inputs numbers should be changed. How do I change the code to make this happen?

    //now remove the desired card from the player's hand     
      for(int i = 0; i < sizeof(user_input); i++ )
      {
        if(user_input[i] =  1)
        {
          hand.setCard(0, myCard.getNextCard());//change #1 on the list
        }else if(user_input[i] =  2)
        {
          hand.setCard(1, myCard.getNextCard());//#2
        }
        else if(user_input[i] =  3)
        {
          hand.setCard(2, myCard.getNextCard());//#3
        }
        else if(user_input[i] =  4)
        {
          hand.setCard(3, myCard.getNextCard());//#4
        }
        else if(user_input[i] =  5)
        {
          hand.setCard(4, myCard.getNextCard());//#5
        }

      }

    cout << "You new hand is: " << endl
         <<"1."<< hand.getCard(0).printName() << endl//then i ask what is the name of //the card in that position
         <<"2."<< hand.getCard(1).printName() << endl
         <<"3."<< hand.getCard(2).printName() << endl
         <<"4."<< hand.getCard(3).printName() << endl
         <<"5."<< hand.getCard(4).printName() << endl;

Solution

  • 2 problems:

    1) You don't initialize user_input. Do:

    int user_input[6] = {};
    

    to set all elements to 0.

    2) Your conditions in the are all assigning instead of comparing...

    if(user_input[i] =  1)
    

    should be:

    if(user_input[i] == 1)
    //                ^ - missing equal sign