扑克游戏:C ++中的交换方法

时间:2013-09-16 00:45:16

标签: c++ poker

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

例如:

  1. 黑桃王
  2. 心中之王
  3. 四个俱乐部
  4. 两颗心
  5. 两个俱乐部
  6. 我将如何换取新卡的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;
    

1 个答案:

答案 0 :(得分:0)

2个问题:

1)您没有初始化user_input。做:

int user_input[6] = {};

将所有元素设置为0

2)你的条件都是分配而不是比较...

if(user_input[i] =  1)

应该是:

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