The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I'm not sure if this is even the right place to post this, please move if it should go somewhere else.
I'm in a beginning C++ course online and I need some desperate help (ASAP) on this week's assignment. I've gotten my code all the way to the last section of the assignment and am totally stuck.
I'm looking for anyone with some awesome C++ knowledge, that is confident in structs, arrays, and creating functions, to take a look at my code and help me at least get started on this last part.
It's the game, Spades, in text format, of course.
I guess respond here or something if you think you can help!
Unfortunately we can't look at your code unless you post it, although if you want us to hack into your computer in order to prove our 1337 skillz then I guess we could try...
The game is Spades (most Windows PCs have this game)
We have to use structs and arrays.
The part I'm stuck on right now is being able to deal the top card of the shuffled deck.
The player needs to be asked whether they will keep it or discard it. If they keep it, I have to add it to their hand. If they discard it, I have to add it back to the deck. And if they discard it, then I have to deal the next card and add it automatically to the player's hand.
Deck.aiDeck is an array, so those assignments will either do bad things or not compile at all. I'm not sure if that shuffle algorithm is proper or not off the top of my head, I would do it a bit differently but it might work.
The part I'm stuck on right now is being able to deal the top card of the shuffled deck.
So if you want to deal the top card off the shuffled deck, you will need something to keep track of the number of cards in the deck, or where the top of the deck is. Perhaps some form of index *hint*hint*?
As you deal off the top, you'll probably want to change that index to represent the deck changing in size.
A tricky part for you, because of the data structure that you have chosen (are being asked to use?), is adding a card back to the deck.
Do you have to add it to the bottom of the deck, or do you add it back to the deck and then reshuffle it?
for( int i = 0; i < 52; i++ )
{
int iRandomNumber = ( rand() % 51) + 1;
SOneCard card = Deck.aiDeck; // <- Where'd the [ i ] go?!
Deck.aiDeck = Deck.aiDeck[iRandomNumber];
Deck.aiDeck[iRandomNumber] = card;
//cout << Deck.aiDeck.iRank << " " << Deck.aiDeck.Suits << endl;
showCard(Deck.aiDeck);
//showCard(i);
}
Deck.aiDeck is an array, so those assignments will either do bad things or not compile at all. I'm not sure if that shuffle algorithm is proper or not off the top of my head, I would do it a bit differently but it might work.
I hit quote on the OP, and got Deck.aiDeck[ i ], but Savant, you didn't because you copied/pasted straight from the post... because BBCode interpreted the [ i ] as the italics tag (see how the OP code starts out normal, and then italics after that line?).
Anyway, I think that that is a valid way of shuffling. I think it ends up giving each card a equal probability of being shuffled to every spot.
Ecco already pointed it out, but you have an off by one error in your shuffling algorithm. Also your current algorithm will not produce a equal distribution of shuffles and there is going to be a problem when you figure out your question as well.
Your currently doing:
for(i = 0 to n)
Swap i with random position between 0 and n
Example with 3 cards and shows uneven distribution:
You should be doing:
for(i = 0 to n)
Swap i with random position between i and n
Example with 3 cards and shows even distribution:
I also think chances are pretty good that your seed and thus the randomization of the deck is not going to be completely random, but I'm unsure how to fix it and if it even really matters. You can read more about this here: http://www.cigital.com/papers/download/developer_gambling.php.
I'm also confused by your question since the Spades that I've played deals the entire deck to the players without any choices. Anyway I think the easiest way to figure out what you need to do is think about what actions a person would take. Ecco already pointed out that you need an index or a size, though I think keeping the card in the deck, and thus the array, is easier than he implies as you can just not remove it unless the player accepts. If you have to reshuffle, then you should probably create a shuffle function that takes the deck and the size and shuffles it.
Hey guys! Yeah, I know that there seems to be a lot of easier ways to do all of this, but this is sort of the way the professor wants us to do it for this part of the class (as we haven't learned about classes or anything like that yet). I made some comments on what needs to be done at a certain section (given by the professor). Right now my code is just giving me the same card even if I decide to keep it. Any ideas?
if(sDecision == "y" || sDecision == "Y")
{
sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12,13] = Deck.aiDeck[Deck.iCardIndex];
Deck.iCardIndex++;
cout << "You discarded ";
showCard(Deck.aiDeck[Deck.iCardIndex]);
Deck.iCardIndex++;
} else if (sDecision == "n" || sDecision == "N")
{
sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12,13] = Deck.aiDeck[Deck.iCardIndex];
showCard(Deck.aiDeck[Deck.iCardIndex]);
// add second card to hand instead of first
//display second card that was added to hand
Deck.iCardIndex++;
showCard(Deck.aiDeck[Deck.iCardIndex]);
Deck.iCardIndex++;
sComputersHand.aiHand[0] = Deck.aiDeck[Deck.iCardIndex];
Deck.iCardIndex++;
// then draw one for computer
// discard the next
} else {
cout << "That input is invalid. Try again." << endl;
}
}while (Deck.iCardIndex < 52);
std::cin.sync();
std::cin.get();
return 0;
}
void showCard(SOneCard card)
{
if( card.iRank >= 2 && card.iRank <=9 )
cout << card.iRank;
switch( card.iRank )
{
case 10:
cout << "T";
break;
case 11:
cout << "J";
break;
case 12:
cout << "Q";
break;
case 13:
cout << "K";
break;
case 14:
cout << "A";
break;
}
Hmm there's a very distinct mix of C in that C++ there, is that intentional (from the instructor)? I can see a few things that might not compile at least on my compiler.
Double Edit: Removed code problem, that was happening because of the quote feature of the forum.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
Use ["code"] with no parenthesis. I'm pretty sure the error Bowen pointed out is the same one that Ecco pointed out as well where i in brackets was changed to italics by the forum.
That said now, the problem really is a super common one among new programmers. In a loop, variables get reset, when you define them in a loop.
So, defining an index inside the loop at a certain point at the begging would keep its position static as it never changes, and you always reset it back to the original position. There are 3 lines I would move out of the "do." And that should fix some of your problems.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
Sorry that the quotes thing on here keeps messing up the code! Wasn't sure that there was a good way to put it on here.
Do I figured out how to get the cards to be different for each time (thanks for that, Bowen!). I'm not sure that they are going into the player's hand, though. I want to be able to display the player's hand after they have 13 cards.
And I still need to be sure that the computer is getting cards. This is the instruction for that bit:
Then, the computer opponent should “draw” the next two cards. For simplicity, let‘s have the computer always keep the first card, so all we have to do is add the top card of the deck to the computer‘s hand and ignore the next card. The computer‘s cards should not be displayed to the player.
I guess if someone could explain the proper syntax to display the player's hand after they have kept 13 cards, that would be super helpful!
/*
Gabrielle
This program is the first part of the card game, Spades. This part, creates the deck, shuffles it,
and then deals to the player and computer. The player will be asked if they want the first card.
If yes, that card gets put into the player's hand and the second card will be discarded. If no,
the first card will go back into the deck and the second card will go into the player's hand.
The computer will keep the first card and discard the second.
*/
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
enum eSuit {C=0, D, S, H};
struct SOneCard
{
int iRank;
eSuit Suits;
};
struct SCardDeck
{
SOneCard aiDeck[52];
int iCardIndex;
};
struct SPlayerHand
{
SOneCard aiHand[14];
int iTotalHand;
};
struct SCardDisplay
{
char cSuit;
char cRank;
};
void showCard(SOneCard);
int main(int argc, char* argv[])
{
SCardDeck Deck;
// Creates the card deck
Deck.iCardIndex = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 13; j++)
{
Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
Deck.iCardIndex++;
}
}
// Shuffles the card deck
srand( time( NULL ) );
for( int i = 0; i < 52; i++ )
{
int iRandomNumber = ( rand() % 51) + 1;
SOneCard card = Deck.aiDeck[i];
Deck.aiDeck[i] = Deck.aiDeck[iRandomNumber];
Deck.aiDeck[iRandomNumber] = card;
}
// This section deals and handles the player's response for each card
Deck.iCardIndex = 0;
do
{
SPlayerHand sPlayersHand;
SPlayerHand sComputersHand;
showCard(Deck.aiDeck[Deck.iCardIndex]);
cout << "Keep this one? (y/n)";
string sDecision;
cin >> sDecision;
if(sDecision == "y" || sDecision == "Y")
{
sPlayersHand.aiHand[0] = Deck.aiDeck[Deck.iCardIndex];
//Deck.iCardIndex++;
cout << "You discarded ";
Deck.iCardIndex++;
showCard(Deck.aiDeck[Deck.iCardIndex]);
//Deck.iCardIndex++;
} else if (sDecision == "n" || sDecision == "N")
{
sPlayersHand.aiHand[0] = Deck.aiDeck[Deck.iCardIndex];
cout << "You discarded ";
showCard(Deck.aiDeck[Deck.iCardIndex]);
Deck.iCardIndex++;
cout << "You kept ";
showCard(Deck.aiDeck[Deck.iCardIndex]);
Deck.iCardIndex++;
sComputersHand.aiHand[0] = Deck.aiDeck[Deck.iCardIndex];
Deck.iCardIndex++;
// then draw one for computer
// discard the next
} else {
cout << "That input is invalid. Try again." << endl;
}
}while (Deck.iCardIndex < 52);
std::cin.sync();
std::cin.get();
return 0;
}
// Function to display the card
void showCard(SOneCard card)
{
if( card.iRank >= 2 && card.iRank <=9 )
cout << card.iRank;
switch( card.iRank )
{
case 10:
cout << "T";
break;
case 11:
cout << "J";
break;
case 12:
cout << "Q";
break;
case 13:
cout << "K";
break;
case 14:
cout << "A";
break;
}
if( card.Suits >= 0 && card.Suits <= 4 )
switch( card.Suits )
{
case 0:
cout << 'C' << endl;
break;
case 1:
cout << 'D' << endl;
break;
case 2:
cout << 'S' << endl;
break;
case 3:
cout << 'H' << endl;
break;
}
}
if(condition)
statement;
switch(value)
case 0:
....
etc.
...You will run statement if condition resolves to true, and you will always run the switch. Look in your code for when you are trying to print the card rank. You're saying if it's 2-9, print the rank. Then, also, switch off of the rank. This will work since the card rank will never be, say, 5 and 12 at the same time, however, it would probably be better organized with an else clause before the switch, in order to only enter that switch statement if needed.
As for displaying your cards, you already have the code to display one card.
If you have an array of 13 elements (index 0 through index 12) you could loop through them all using another for loop in much the same way as you have already proved you know how to, and for each element in the array that contains the player's hand, you can call your function.
You're already using for loops, and you're already using your card printing function to print cards. You also already have the player's hand (though you probably want that to be [13] instead of [14] in the array), so all of the data is available to execute this process.
This is more generic programming advice than a solution to your problem until I look over your code.
You should consider commenting your code with lots of information to help you visualize your problem. For instance, your first loop looks like it generates the deck of cards (before the shuffle).
You really should consider using classes for this, as it will help you with visualizing code, is there any reason why the course hasn't done that but has skipped to a pretty difficult problem? I mean you're already using structs, this course seems to be all over the place, classes are basically structs with built in functions.
You seem to be doing remarkably well considering the pitfalls of the course (online courses ).
So that all said my logic would be this:
Generate the cards
Deal the cards
Show the hands (show the computer hands for debugging purposes)
It doesn't look like there's any game playing yet, so, looks like it may not be up there. Has the course not gotten to classes and object oriented design in C++ yet?
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
First semester C++, at least in my experiences, delves very little if at all into object oriented concepts. Back when I took intro java (~5 years ago?) classes were second semester material for some reason. We covered classes in c++, but not very heavily, I assume because c++ didn't have a second semester sequence.
That is crazy! She is practically 2 inches from classes by using structs. Might as well be an intro to C course. Hell we could throw some functions in her structs and still be kosher there if we wanted to piss some teachers off.
Seems like she understands encapsulate at the least.
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
We haven't learned about classes or objects yet. This project right now is focused on teaching us structs and arrays. Last week, was functions. I think we learn classes in a week or so.
Every where I've seen card programs for C++, they use classes. Unfortunately, we aren't allowed to use them yet. So that also adds to why I'm struggling with this very difficult program b/c I can't find any examples of this type of struct use anywhere, ha.
This is Part 1 of the project that basically just creates, shuffles, deals the deck by having the player decide if they want to keep the card drawn or discard it and take the second. Then the computer does the same.
I'll see about commenting on my code more, if only for my own sanity with this!
This line seems odd to have, you might consider rewriting your while loop there. Looks like you've got deck creation done. I'm concerned about the second loop (shuffle?), as it doesn't look like you're checking to see if you've got a duplicate card (you could possibly get 52 Ace of spades.
Moving your index definition out was a good idea. I think the problem is solely in your decisions part.
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
That is crazy! She is practically 2 inches from classes by using structs. Might as well be an intro to C course. Hell we could throw some functions in her structs and still be kosher there if we wanted to piss some teachers off.
Seems like she understands encapsulate at the least.
Yes. It is. But the reason is because some non-computer majors have to take a programming class (like business), so they cant just get down and dirty from the beginning. On the bright side, encapsulation is probably the most important concept to really fundamentally grasp out of object oriented programming, since the rest of them really flow from it intuitively, so at least there's that.
We haven't learned about classes or objects yet. This project right now is focused on teaching us structs and arrays. Last week, was functions. I think we learn classes in a week or so.
Every where I've seen card programs for C++, they use classes. Unfortunately, we aren't allowed to use them yet. So that also adds to why I'm struggling with this very difficult program b/c I can't find any examples of this type of struct use anywhere, ha.
This is Part 1 of the project that basically just creates, shuffles, deals the deck by having the player decide if they want to keep the card drawn or discard it and take the second. Then the computer does the same.
I'll see about commenting on my code more, if only for my own sanity with this!
Beginning programmers never plan, and they never document. That is why advanced programmers find they have the same habit. Then you come back to week-old code and can't remember what anything does. It happens, and it happens literally to everyone. Don't let it happen to you! Document your code as you write it, you will will will thank yourself for it later. Promise.
Second, I may just not be seeing it, but where in your code (if anywhere) are you checking to see if the player has 13 cards? That is/will be an important part of your program flow. Think to yourself "when should I check how big the player's hand is?" and "what should I do if it's 13 or more cards?" That will help you a lot when you get your program flowing. Keep in mind that the break statement can be used not just inside of a switch, but also inside of any control structure (like a for loop, or a do/while loop, etc) in order to immediately break out of it from the middle.
Seems to be enforced in the loops. The if statements in the showCard function are improper, since you know the bounds of your program you probably don't need to check the lower limit and only the upper limit
So I'd change:
if( card.iRank >= 2 && card.iRank <=9 )
to
if(card.iRank <= 9)
And also, the switch statement will run regardless, but you don't want it to, so you'll need if/else there (if the card rank is lower than 9, print it, else, use the switch).
The if statement on the suits is unneeded.
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
0
Inquisitor772 x Penny Arcade Fight Club ChampionA fixed point in space and timeRegistered Userregular
That's true, but it's possible the professor wanted them to "validate" first.
Big quotes.
Shit like this pulls my hair out as a programmer. The bounds were defined by the program themself, there's no user input, there's no reason to assume it's out of bounds. Augh!
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
That's true, but it's possible the professor wanted them to "validate" first.
Big quotes.
Shit like this pulls my hair out as a programmer. The bounds were defined by the program themself, there's no user input, there's no reason to assume it's out of bounds. Augh!
What if a hacker tries to coredump her spades game
She could end up inadvertently granting root access to this guy
This crap needs to be tight
Posts
Origin: DustBunny777
3DS: 2836-0103-2102
We have to use structs and arrays.
The part I'm stuck on right now is being able to deal the top card of the shuffled deck.
The player needs to be asked whether they will keep it or discard it. If they keep it, I have to add it to their hand. If they discard it, I have to add it back to the deck. And if they discard it, then I have to deal the next card and add it automatically to the player's hand.
I'm just completely lost on this part.
Origin: DustBunny777
3DS: 2836-0103-2102
Your shuffle routine looks wrong at first glance.
Deck.aiDeck is an array, so those assignments will either do bad things or not compile at all. I'm not sure if that shuffle algorithm is proper or not off the top of my head, I would do it a bit differently but it might work.
Did you mean to generate a random number from 1 to 51, or 0 to 51?
So if you want to deal the top card off the shuffled deck, you will need something to keep track of the number of cards in the deck, or where the top of the deck is. Perhaps some form of index *hint*hint*?
As you deal off the top, you'll probably want to change that index to represent the deck changing in size.
A tricky part for you, because of the data structure that you have chosen (are being asked to use?), is adding a card back to the deck.
Do you have to add it to the bottom of the deck, or do you add it back to the deck and then reshuffle it?
Aside:
Oh haha - I see what happened here.
I hit quote on the OP, and got Deck.aiDeck[ i ], but Savant, you didn't because you copied/pasted straight from the post... because BBCode interpreted the [ i ] as the italics tag (see how the OP code starts out normal, and then italics after that line?).
Anyway, I think that that is a valid way of shuffling. I think it ends up giving each card a equal probability of being shuffled to every spot.
Your currently doing:
for(i = 0 to n)
Swap i with random position between 0 and n
Example with 3 cards and shows uneven distribution:
You should be doing:
for(i = 0 to n)
Swap i with random position between i and n
Example with 3 cards and shows even distribution:
I also think chances are pretty good that your seed and thus the randomization of the deck is not going to be completely random, but I'm unsure how to fix it and if it even really matters. You can read more about this here: http://www.cigital.com/papers/download/developer_gambling.php.
I'm also confused by your question since the Spades that I've played deals the entire deck to the players without any choices. Anyway I think the easiest way to figure out what you need to do is think about what actions a person would take. Ecco already pointed out that you need an index or a size, though I think keeping the card in the deck, and thus the array, is easier than he implies as you can just not remove it unless the player accepts. If you have to reshuffle, then you should probably create a shuffle function that takes the deck and the size and shuffles it.
Origin: DustBunny777
3DS: 2836-0103-2102
Double Edit: Removed code problem, that was happening because of the quote feature of the forum.
You should double check each place where the iCardIndex variable is being assigned a value or incremented inside the "do while" loop.
And a couple of things, this looks wrong, I don't know what this will do when run:
When you're assigning a value to a slot in an array you only use the index of the slot you're assigning to. e.g. . And remember an array declared as will have indexes from 0 to 20 not 0 to 21.
So, defining an index inside the loop at a certain point at the begging would keep its position static as it never changes, and you always reset it back to the original position. There are 3 lines I would move out of the "do." And that should fix some of your problems.
Do I figured out how to get the cards to be different for each time (thanks for that, Bowen!). I'm not sure that they are going into the player's hand, though. I want to be able to display the player's hand after they have 13 cards.
And I still need to be sure that the computer is getting cards. This is the instruction for that bit:
Then, the computer opponent should “draw” the next two cards. For simplicity, let‘s have the computer always keep the first card, so all we have to do is add the top card of the deck to the computer‘s hand and ignore the next card. The computer‘s cards should not be displayed to the player.
I guess if someone could explain the proper syntax to display the player's hand after they have kept 13 cards, that would be super helpful!
Origin: DustBunny777
3DS: 2836-0103-2102
...You will run statement if condition resolves to true, and you will always run the switch. Look in your code for when you are trying to print the card rank. You're saying if it's 2-9, print the rank. Then, also, switch off of the rank. This will work since the card rank will never be, say, 5 and 12 at the same time, however, it would probably be better organized with an else clause before the switch, in order to only enter that switch statement if needed.
As for displaying your cards, you already have the code to display one card.
If you have an array of 13 elements (index 0 through index 12) you could loop through them all using another for loop in much the same way as you have already proved you know how to, and for each element in the array that contains the player's hand, you can call your function.
You're already using for loops, and you're already using your card printing function to print cards. You also already have the player's hand (though you probably want that to be [13] instead of [14] in the array), so all of the data is available to execute this process.
Does that make sense?
You should consider commenting your code with lots of information to help you visualize your problem. For instance, your first loop looks like it generates the deck of cards (before the shuffle).
You really should consider using classes for this, as it will help you with visualizing code, is there any reason why the course hasn't done that but has skipped to a pretty difficult problem? I mean you're already using structs, this course seems to be all over the place, classes are basically structs with built in functions.
You seem to be doing remarkably well considering the pitfalls of the course (online courses ).
So that all said my logic would be this:
Generate the cards
Deal the cards
Show the hands (show the computer hands for debugging purposes)
It doesn't look like there's any game playing yet, so, looks like it may not be up there. Has the course not gotten to classes and object oriented design in C++ yet?
Seems like she understands encapsulate at the least.
Every where I've seen card programs for C++, they use classes. Unfortunately, we aren't allowed to use them yet. So that also adds to why I'm struggling with this very difficult program b/c I can't find any examples of this type of struct use anywhere, ha.
This is Part 1 of the project that basically just creates, shuffles, deals the deck by having the player decide if they want to keep the card drawn or discard it and take the second. Then the computer does the same.
I'll see about commenting on my code more, if only for my own sanity with this!
Origin: DustBunny777
3DS: 2836-0103-2102
I figured that was the case too.
This line seems odd to have, you might consider rewriting your while loop there. Looks like you've got deck creation done. I'm concerned about the second loop (shuffle?), as it doesn't look like you're checking to see if you've got a duplicate card (you could possibly get 52 Ace of spades.
Moving your index definition out was a good idea. I think the problem is solely in your decisions part.
Yes. It is. But the reason is because some non-computer majors have to take a programming class (like business), so they cant just get down and dirty from the beginning. On the bright side, encapsulation is probably the most important concept to really fundamentally grasp out of object oriented programming, since the rest of them really flow from it intuitively, so at least there's that.
Beginning programmers never plan, and they never document. That is why advanced programmers find they have the same habit. Then you come back to week-old code and can't remember what anything does. It happens, and it happens literally to everyone. Don't let it happen to you! Document your code as you write it, you will will will thank yourself for it later. Promise.
Second, I may just not be seeing it, but where in your code (if anywhere) are you checking to see if the player has 13 cards? That is/will be an important part of your program flow. Think to yourself "when should I check how big the player's hand is?" and "what should I do if it's 13 or more cards?" That will help you a lot when you get your program flowing. Keep in mind that the break statement can be used not just inside of a switch, but also inside of any control structure (like a for loop, or a do/while loop, etc) in order to immediately break out of it from the middle.
So I'd change:
to
And also, the switch statement will run regardless, but you don't want it to, so you'll need if/else there (if the card rank is lower than 9, print it, else, use the switch).
The if statement on the suits is unneeded.
I was going to say the exact same thing. :P
Big quotes.
Origin: DustBunny777
3DS: 2836-0103-2102
Shit like this pulls my hair out as a programmer. The bounds were defined by the program themself, there's no user input, there's no reason to assume it's out of bounds. Augh!
What if a hacker tries to coredump her spades game
She could end up inadvertently granting root access to this guy
This crap needs to be tight