-2

我正在学习 C++,我正在尝试找出一种方法,该方法涉及在下面的列表中切换一张卡。

例如:

  1. 黑桃王牌
  2. 红心之王
  3. 四个俱乐部
  4. 两颗心
  5. 两个俱乐部

我将如何将 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++;
    }

这就是我遇到麻烦的地方,我只能在列表中获得第一名进行更改。当只有用户输入数字时应该改变。如何更改代码以实现此目的?

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

0

2个问题:

1)你没有初始化user_input. 做:

int user_input[6] = {};

将所有元素设置为0.

2)您在中的条件都是分配而不是比较......

if(user_input[i] =  1)

应该:

if(user_input[i] == 1)
//                ^ - missing equal sign
于 2013-09-16T00:52:04.857 回答