How do i make my function in my player class for game of hearts work without being a pointer?
May 4, 2019 at 10:43pm UTC
I was assigned to make a game of hearts where there is an array of 4 people and each people has a hand of 13 cards. Currently, when dealing trying to deal cards to each person deck, I am only able to accomplish this using pointers. However, this has posed problems in getting other functions like sort and such to work. I know that it's possible to do it without pointers I'm just not sure how and was wondering if anybody could assist.
In the psudocode, my teacher gave us, the hand[13] array which holds the cards is not a pointer, but I made it a pointer in order to points the cards to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#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
May 5, 2019 at 12:27am UTC
you can copy cards, of course.
card hand[13];
hand[x] = somecard; //overloaded or default created for you assignment operator for your class... this is slow for large classes but you should not have anything of the size where its an issue.
void addcard(card &in)
hand[x] = in; //at least you can avoid the copy on the parameter via reference.
Topic archived. No new replies allowed.