C++ Array Help [Archive] - SpeedGuide.net Broadband Community

View Full Version : C++ Array Help


trent
03-16-05, 11:31 AM
Hi, I am in a beginning c++ class and I am trying to figure out some array issues that are bugging the crap out of me.

I have to write a program that simulates the card game war. I will read in a .txt file with all the cards in it (all represented as integers 2-14) and then distribute them to two players and then simulate the game. nothing is printed to screen except how many hands were used and who won.

My problem is this: I have been able to bring in the .txt file and assign each player his cards (26 to each). I created a function to simulate the actual game part of this and I am can not get the array to work right. I am supposed to compare each player's card in order and then whichever player wins I put the winner's card along with the loser's card at the end of the array. Is there a way to declare the array dynamically and just update the size of it as I go? Then I can just check for a !=array.eof or something like that to figure out who won and i can just have a regular counter to figure out how many hands went into it right?

Right now i have a p1[26] and p2[26]. Can i just declare them as arrays and update their size as needed? I have visited several c++ help sites, tutorials, and even spoken to a few friends but haven't been able to find anything that has helped.

Thanks for your time and I appreciate any help that you can offer.

Trent

Paft
03-16-05, 08:47 PM
Why not use an std::string instead of an array? Those automatically update the size.

However, if you need a resizeable array and don't like std::string, look into the std::vector class. You could declare something like std::vector <char> p1; std::vector <char> p2; and then use them exactly like char arrays.

I can't help more without seeing the code you currently have.

trent
03-16-05, 10:54 PM
Paft -

First off, thanks for your response. The information you provided is a bit more advanced then then where I'm at. I am posting the function that I am trying to get working below. I am only going to include 1 of my if statements because the second will be the exact same except switched around for player 2.

void gameStart(int p1[52], int p2[52]){
//52 because that is max amount of cards either player will ever possibly have

int a=0, b=26, c=26;
//used these to try and get my counts right when adding cards to the array

while ((p1[0] || p2[0]) != 0) {
if ((p1[a]) > (p2[a])){
p1[b]=p1[a];
p1[b+1]=p2[a];
for (int d=0; d<b; d++){
p1[d]=p1[d+1];
}
d=0;
b++;
}
if ((p2[a]) > (p1[a])){
//this is where the same code above
//would go to do the same thing for player 2
}
a++;
if (a == 51)
a=0;
}
}

With this I am trying to figure out who has the higher number (like the card game war) and then whoever wins I put the winner's card at the end of the array followed by the loser's card. I then shrink the loser's array by one number by adding a zero to the end of it and shifting everthing else one slot over. This part isn't working very well right now.

Again, Thanks

Trent

Paft
03-17-05, 02:57 PM
Basically, you're going to want code like this. This code currently doesn't work, but it will get you where you need to go with a little work. ;)

The only reason I give partially broken code is so you can learn. If you're really, truly stuck after this, I'll give you more complete code.

int gameStart(int p1[52], int p2[52])
{
int p1p = 26, p2p = 26; // Player 1 position, Player 2 position.

while ((p1[0] != 0) && (p2[0] != 0))
{
if (p1[0] > p2[0]) // You want to add the value of the won card to the end..
{
p1[p1p + 1] = p2[0];
p1p++;

for (int x = 0; x < p1p; x++) // And rotate the cards.
{
int temp;

temp = p1[x];
p1[x] = p1[x+1];
p1[x+1] = temp;
}
}

else if (p2[0] > p1[0])
{
p2[p2p + 1] = p1[0];
p2p++;

for (int x = 0; x < p2p; x++)
{
int temp;

temp = p2[x];
p2[x] = p2[x+1];
p2[x+1] = temp;
}
}

else if (p2[0] == p1[0]) // Rotate both because of the tie.
{
for (int x = 0; x < p1p; x++)
{
int temp;

temp = p1[x];
p1[x] = p1[x+1];
p1[x+1] = temp;
}

for (int x = 0; x < p2p; x++)
{
int temp;

temp = p2[x];
p2[x] = p2[x+1];
p2[x+1] = temp;
}
}
}

if (p2[0] == 0)
{
return(1);
}

else if (p1[0] == 0)
{
return(2);
}

else
{
return(0);
}
}