-2

私は C++ を学んでおり、以下のリストのカードを切り替える方法を見つけようとしています。

例えば:

  1. スペードのエース
  2. ハートの王
  3. クラブの4つ
  4. ハートの2
  5. 2 つのクラブ

2枚、3枚、5枚をそれぞれ新しいカードに交換するにはどうすればよいでしょうか。

わかりましたので、これが私のコードです。他のヘッダーファイルも使用していますが、私がどこに行くのか理解できるはずです。

#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++;
    }

これは私が問題を抱えているところです。変更するリストの 1 位しか得られません。ユーザーが入力した数字のみを変更する必要がある場合。これを実現するためにコードを変更するにはどうすればよいですか?

    //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;
4

1 に答える 1