我如何使我的玩家类中的功能发挥作用而不需要指针?

时间:2019-05-04 21:52:25

标签: c++ pointers

我被分配去做一个由四个人组成的游戏,每个人有13张牌。目前,在尝试向每个人的牌堆发牌时,我只能使用指针来完成。但是,这在使其他功能(例如sort等)正常工作时带来了问题。我知道有可能在没有指针的情况下做到这一点,我只是不确定如何,并且想知道是否有人可以提供帮助。

在伪代码中,我的老师给了我们,持有卡片的hand [13]数组不是指针,但我将其设为指针以将卡片指向它。

  #include <iostream>
  #include <iomanip>
  #include "Card.h"
  #include "Player.h"
  #include <cstdlib>
  #include <random>
  using namespace std;

  //Prototype

   void shuffleDeck(Card deck1[])
      {

          int ranNum;
          Card temp;
          int count = 0;

    for(int i = 0; i < 52; i++)
    {
          count++;
          ranNum = (rand() % 52); 
          temp = deck1[ranNum];
          deck1[ranNum] = deck1[i];
          deck1[i] = temp;

    }

     }



  int main()
  {

     //Declarations
      int nextPlayer;
      int firstPlayer;
      Card deck2[4][13];
      Suit temp;
      Card deck1[52];
      int count = 0;
      int check = 0;
      int round = 0;
      int points = 0;
      int numCards = 0;

 //Part 1: The set-up

//Create player array of [4] players
Player player[4] = {Player("Me"),
                    Player("Elton John"),
                    Player("Snoop Dog"),
                    Player("Lady Gaga")
                   };

//create and initialize deck;
    for (int r = 0; r < 4; r++)
    { 

       for (int c = 0; c < 13; c++)
        {

        deck2[r][c].setNumber(c + 1);
        temp = ((Suit)r);
        deck2[r][c].setSuit(temp);

        deck2[r][c].setDescription();

        }

    }


  //Convert to one dimensional array
  for(int i = 0; i < 4; i++)
  {
      for(int j = 0; j < 13; j++)
      {
          deck1[count] = deck2[i][j];
          count++;

      }
  }
         shuffleDeck(deck1); //call shuffle deck function



//Deal deck to players // have add card in player class that sticks a card in players deck
/*
for(int i = 0; i < 52; i++)
{
    int playerIndex = i % 4;
    player[playerIndex].addCard(&deck1[i]); //HERE IS WHERE I CALL IT IN THE MAIN
}

//Sort each players hand
for (int i = 0; i < 4; i++)
{
    player[i].sort();
}
//Find player who goes first (the person holding the 2 of clubs) //firstPlayer

for(int j = 0; j < 4; j ++)
{
    for(int i = 0; i < 13; i++)
    {   


        if(player[j].getCard(i) == 20)
        {
            firstPlayer = j;
        }
    }
}
*/


 Card openCard[4]; //keeps track of all played cards

//  //Part 2: The play
 for(int i = 0 ;  i < 13; i++) //gonna play 13 rounds, same thing 13 times
 {  numCards = 0;
     round++;

//     Print points of players
cout << "\nPlayer     Round " << round << "  Total\n"; 
cout << "-------    --------  -----\n";
cout << "\nMe "; 
cout << "\nSnoop Dogg "; 
cout << "\nLady Gaga  ";
cout << "\nElton John ";

//     Print hand
cout << "\n\nMy Hand: \n" << "-------------\n\n";


   for(int i = 0; i < 13; i++)
   {
//   cout << player[0].getCard(i) << "\n";
   numCards++;
   check++;
   cout << numCards << ".) " <<  deck1[i].getDescription() << "\n";

   }

     for (int j = 0; j < 3; j++)
     {
     nextPlayer = (firstPlayer + j) % 4;

     //Play a card
     //Compare card to other card


    //  openCard[nextPlayer] = player[nextPlayer].playCard(deck1[i], firstPlayer);

     //inside playCard() remove card from hand after it's played
    //  hand[cardPlayed] = null //null means played already// (I think this is another method of doing it)

      }
     //end for j

   //count points per player

   //firstPlayer = winner of round //implement this in
//   if(firstPlayer) //something like this
//   {
//       points++;
//   }
   //add points to first player

 }
//end for i




return 0;
}

//PLAYER CLASS

 #ifndef PLAYER_H
 #define PLAYER_H
 #include "Card.h"
 #include <iostream>
 #include <iomanip>
 using namespace std;



 class Player 
 {
  //Member variables
private:
string name;

public:
static int count;
Card *hand[13]; //TRYING TO MAKE THIS NOT A POINTER
int number;
bool played;


//Constructors

Player()
{

    name = "";

}

Player(string n)
{
    name = n;


}

string getName()
{
    return name;
}

int getCard(int index)
{
    return hand[index]->getNumber();
}

void setName(string n)
{
    name = n;
}

 //     void addCard(Card *card) //THIS IS WHERE CARDS ARE DELT (HOW DO I 
 MAKE IT NOT A POINTER?)
 //     {

 //          if (number < 13) 
 //          {
//              hand[number] = card;
//              number++;
 //          }
 //     }




  //    void sort() 
 //     {
 //         for (int i = 1; i < 13; i++) 
 //         {

 //     Card *temp; 
 //     bool swap = false;
 //     int size = 52;
 //     do
 //     {
 //         swap = false;
 //         for (int count = 0; count < (size - 1); count++)
//         {
//             if (hand[count + 1] < hand[count])
//             {
//                 temp = hand[count];
//                 hand[count] = hand[count + 1];
//                 hand[count + 1] = temp;
//                 swap = true;
//             }
//         }
//     } 
//     while (swap == true);
//      }
//      }




//      Card playCard(Suit lead, bool leader) 
//      {
//          int cardPlayed;

//      hand[cardPlayed] = -1; //null means played already// (I think this 
is another method of doing it)
//      }

};

#endif

0 个答案:

没有答案