|
 |
|
 |
04-02-2005, 06:28 PM
|
#1 (permalink)
|
|
Hottie
Join Date: Apr 2005
Posts: 5
|
NEED HELP on card assignment
I'm just a beginner  in C++ and need some help  . My assignment is the following:
You must implement a function that initializes the deck of cards. The internal storage of a single card is defined below:
enum SuiteType {SPADE, CLUB, HEART, DIAMOND};
struct Card
{
int value;
SuiteType suite;
};
Your function should be called generate_deck() and must initialize an array of 52 cards to the appropriate values using loops. Do not initialize this array by hand! Once you are finished with this function please test your results.
Deck array element: 0 ...1...2...3...4...5...6...7...8....9...10...11... 12
SPADE .................A....2...3...4...5...6...7...8... 9...10...J.....Q.....K
Deck array element: 13..14..15..16..17..18..19..20...21..22..23..24..2 5
CLUB .....................A....2...3....4....5...6....7 ...8....9...10....J...Q....K
Deck array element: 26..27..28..29..30..31..32..33..34...35..36..37..3 8
HEART ...................A....2...3....4....5....6...7.. ..8 ...9...10...J....Q....K
Deck array element: 39..40..41..42..43..44..45..46..47..48..49..50..51
DIAMOND ...............A....2...3 ...4 ...5...6....7 ...8 ...9..10...J ..Q ..K
I know there is a nested loop I should use because the card value changes while the suit stays the same for 13 times then it goes to the next suit. I think using a for loop and case.
 Please help if you may!
Last edited by marina; 04-30-2005 at 02:55 PM.
|
|
|
04-02-2005, 07:36 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
We're not here to do your homework, you need atleast to come up with some code, showing us what your thoughts on the problem is, and giving us some idear on what level your knowledge is. Or atleast showing us you've given it a try, that beeing a wholeharted one or just partly.
First off, read up on loops, try figuring out, what is needed in designing such a card program, how would you describe numbers from 0 - 51 beeing in the range of Ace to King...
Then try something and see if it gets any closer to solving the problem at hand, if it dosn't you can post your code here, and ask for help getting that bend in any way, so it will provide you with a working program, that does what you want it to do, according to your orriginaly design.
Dont just cry help, and expect us to do your homework, it is not here to teach us, it is here to teach you.
|
|
|
04-03-2005, 12:24 AM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Besides, there are a quite a few hints on this forum. Use the search button.
__________________
|
|
|
04-29-2005, 11:16 AM
|
#4 (permalink)
|
|
Hottie
Join Date: Apr 2005
Posts: 5
|
It took me quite a while but that's what I got so far for the Video poker game. Right now I'm working on the run game fuction. Any suggestions?
Code:
// I N C L U D E S ////////////////////////////////////////////////////////////////
#include <stdlib.h> // for itoa(), rand(), and srand()
#include <time.h> // tine() needed to seed srand()
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// D A T A T Y P E S /////////////////////////////////////////////////////////////
// enumerate our suits
enum Suit {SPADE, CLUB, HEART, DIAMOND};
// Basic card structure
struct Card
{
int value; // Card value
Suit suit; // Card suit
bool used; // Was the card used already?
};
// G L O B A L S //////////////////////////////////////////////////////////////////
struct Card DECK[52]; // Our deck of cards
// P R O T O T Y P E S ////////////////////////////////////////////////////////////
// Game related functions
void print_hand(int hand[]);
void generate_deck();
void deal(int hand[]);
void run_game(void);
// Utility functions
int x_of_a_kind( int hand[], int rank);
void reset_deck();
int get_card();
// Test hands
bool is_royal_flush(int hand[]);
bool is_straight_flush(int hand[]);
bool is_flush(int hand[]);
bool is_straight(int hand[]);
bool is_full_house(int hand[]);
bool is_four_of_a_kind(int hand[]);
bool is_three_of_a_kind(int hand[]);
bool is_two_pair(int hand[]);
bool is_pair(int hand[]);
// F U N C T I O N S //////////////////////////////////////////////////////////////
bool is_royal_flush(int hand[])
{
int i,
high = 0;
if( is_straight_flush(hand) )
{
// Test to see if high card is a king
for(i = 0; i < 5; i++)
if( DECK[ hand[i] ].value > high)
high = DECK[ hand[i] ].value;
if(high == 13)
return true;
} // end if
return false;
} // end is_royal_flush
///////////////////////////////////////////////////////////////////////////////////
bool is_straight_flush(int hand[])
{
if( is_flush(hand) && is_straight(hand) )
return true;
return false;
} // end is_straight_flush
///////////////////////////////////////////////////////////////////////////////////
bool is_flush(int hand[])
{
Suit s = DECK[ hand[0] ].suit;
for(int i = 1; i < 5; i++)
if( DECK[ hand[i] ].suit != s)
return false;
return true;
} // end is_flush()
///////////////////////////////////////////////////////////////////////////////////
bool is_straight(int hand[])
{
int values[5],
i,
j,
low=15;
bool king=false,
next;
// PASS 1 - stock array of values and check for a king
for(i = 0, king = false; i < 5; i++)
{
if( DECK[ hand[i] ].value == 13)
king = true;
values[i] = DECK[ hand[i] ].value;
} // end for
// PASS 2 - Find low value and convert any aces to high value if there is a king in the hand
for(i = 0; i < 5; i++)
{
if(values[i] == 1 && king == true)
values[i] = 14;
if(values[i] < low)
low = values[i];
} // end for
// PASS 3 - Check ordering
for(i = 0; i < 4; i++)
{
// Test to see if there is one card higher than low
for(j = 0, next = false; j < 5; j++)
{
if(values[j] == low + 1)
{
low = values[j];
next = true;
break;
} // end if
} // end for
// if it didn't find the next card
if(next == false)
return false;
} // end for
return true;
} // end is_straight()
///////////////////////////////////////////////////////////////////////////////////
int x_of_a_kind( int hand[], int rank)
{
// TODO: Lab
int matches[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
max_element,
max;
for(int i = 0; i < 5; i++)
matches[ DECK[ hand[i] ].value - 1]++;
for(int j = 0; j < rank; j++)
{
max = 0;
for(int i = 0; i < 13; i++)
{
if( matches[i] > max )
{
max_element = i;
max = matches[i];
} // end if
} // end for
matches[max_element] = 0;
} // end for
return max;
} // end x_of_a_kind()
///////////////////////////////////////////////////////////////////////////////////
bool is_full_house(int hand[])
{
if( x_of_a_kind(hand, 1) == 3 && x_of_a_kind(hand,2) == 2 )
return true;
else
return false;
} // end is_full_house()
///////////////////////////////////////////////////////////////////////////////////
bool is_four_of_a_kind(int hand[])
{
if( x_of_a_kind(hand, 1) == 4)
return true;
else
return false;
} // end is_four_of_a_kind()
///////////////////////////////////////////////////////////////////////////////////
bool is_three_of_a_kind(int hand[])
{
if( x_of_a_kind(hand, 1) == 3)
return true;
else
return false;
} // end is_three_of_a_kind()
///////////////////////////////////////////////////////////////////////////////////
bool is_two_pair(int hand[])
{
if( x_of_a_kind(hand, 1) == 2 && x_of_a_kind(hand,2) == 2 )
return true;
else
return false;
} // end is_two_pair()
///////////////////////////////////////////////////////////////////////////////////
bool is_pair(int hand[])
{
if( x_of_a_kind(hand, 1) == 2)
return true;
else
return false;
} // end is_pair()
///////////////////////////////////////////////////////////////////////////////////
/* Prints cards in array hand. hand[] is expected to be an array of
* 5 integers that corospond to elements in the deck. Numbers are also
* printed below each card.
*/
void print_hand(int hand[])
{
string output;
char c[5]; // buffer to hold integer conversions
// Print out the hand
for(int i = 0; i < 5; i++)
{
// Output the card value
switch( DECK[hand[i]].value )
{
case 1:
output = "A";
break;
case 11:
output = "J";
break;
case 12:
output = "Q";
break;
case 13:
output = "K";
break;
default:
// Convert integer value to ascii
itoa(DECK[hand[i]].value, c, 10);
output = c;
} // end switch
// Now output the suit
switch(DECK[hand[i]].suit)
{
case SPADE:
output = output + "S";
break;
case CLUB:
output = output + "C";
break;
case HEART:
output = output + "H";
break;
case DIAMOND:
output = output + "D";
break;
} // end switch
// Output the card
cout << setw(4) << output;
} // end for
cout << endl;
// print out card numbers below the hand
for(int i = 1; i <= 5; i++)
cout << setw(4) << i;
cout << endl;
} // end print_hand()
///////////////////////////////////////////////////////////////////////////////////
/* Sets up the deck of cards to be used for the video poker program.
* This function should be called at the beginning of the program
*/
void generate_deck()
{
// cycle through each suit...
for(Suit current_suit = SPADE; current_suit <= DIAMOND;
current_suit = static_cast<Suit>( current_suit + 1) )
{
// cycle through suit values
for(int index = 0; index < 13; index++)
{
DECK[ (current_suit*13) + index ].value = index + 1;
DECK[ (current_suit*13) + index ].suit = current_suit;
} // end suit values
} // end for suit
} // end generate_deck
///////////////////////////////////////////////////////////////////////////////////
/* Reset the deck's used status. This should be done before
* the deal()
*/
void reset_deck()
{
for(int i = 0; i < 52; i++)
DECK[i].used = false;
} // end reset_deck()
///////////////////////////////////////////////////////////////////////////////////
/* Gets a random card that is not currently used. sets used status to true
* once a card has been selected.
*/
int get_card()
{
int element;
// pick out random card
do
{
element = rand()%52;
} while( DECK[element].used == true );
// Mark the card as used
DECK[element].used = true;
return element;
} // end get_card
///////////////////////////////////////////////////////////////////////////////////
/* Deals 5 random cards and placed their index in hand[]. Since deal()
* uses get_card, the card is marked used.
*/
void deal(int hand[])
{
// Get 5 random cards
for(int i = 0; i < 5; i++)
hand[i] = get_card();
} // end deal
///////////////////////////////////////////////////////////////////////////////////
// Start playing video poker!
void run_game(void)
{
// deposit $
// deal 5 random cards
// option to throw out up to 3 cards and replace them
// with new cards drawn randomly from the deck
// winnings are calculated
// If the player has a winning hand then appropriate credits are awarded
// if credits are left -- option: a button to cash out or continue playing
} // end run_game
// M A I N ////////////////////////////////////////////////////////////////////////
int main(void)
{
// run the game!
run_game();
return 0;
} // end main
Last edited by Valmont; 04-29-2005 at 11:57 AM.
|
|
|
04-29-2005, 11:59 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Hmm, let's see.
Actually I wanted to implement a goody as promised here:
Issue implementing algorithm of Lempel Ziv
But I am so not in the mood  .
Let's see what you got.
What did you learn up to day?
Classes? What?
What you should learn on short notice ( I will review your code in a bit), is to communicate better. For example, don't tell us where the headers start, where the prototypes start, and so forth. Every coder should recognize these things. Even if one is slightly less seasoned. What you want to achive with comments, is to guide complete idiots like into your coding style or insights.
Try that first.
Also change first two headers. We *never* want to mix standard headers with non-standard headers!
Code:
#include <cstdlib> // itoa, rand, srand
#include <ctime>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
__________________
|
|
|
04-29-2005, 01:06 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Hmm. We, especially me, don't like itoa() in this part of the forum.
You see, that function is supported by many compilers, but was never part of the ISO standard. Not in C, and not in C++. It wasn't even part of the American ANSI standards.
So we need a homebuilt function to do the conversion. We can make a fast one quite easely:
Code:
//Utility. Convert int to string.
string itos(int i)
{
stringstream s;
s << i;
return s.str();
}
And this is how you use it in your print_hand() function:
Code:
// Convert integer value to ascii
output = itos(DECK[hand[i]].value);
Now you can remove that "ugly" buffer (char c[5]) as well.
See if you like it.
I'll wait until you finished the rest.
__________________
|
|
|
04-29-2005, 04:54 PM
|
#7 (permalink)
|
|
Hottie
Join Date: Apr 2005
Posts: 5
|
Quote:
|
Originally Posted by Valmont
don't tell us where the headers start, where the prototypes start, and so forth. Every coder should recognize these things. Even if one is slightly less seasoned.
|
My professor actually wants us to explain every step we do. I was told that programmers are actually required to label their work in case they have to go back and make revisions later on. Sorry, I didn't delete it before I posted it.
Last edited by marina; 04-30-2005 at 02:51 PM.
|
|
|
04-29-2005, 05:09 PM
|
#8 (permalink)
|
|
Hottie
Join Date: Apr 2005
Posts: 5
|
Quote:
|
Originally Posted by Valmont
Hmm. We, especially me, don't like itoa() in this part of the forum.
You see, that function is supported by many compilers, but was never part of the ISO standard. Not in C, and not in C++. It wasn't even part of the American ANSI standards.
So we need a homebuilt function to do the conversion.
|
Well, that's how I was taught in my C++ Computer Programming class. I guess my professor just made it easier for us (I'm just finishing my 1st C++ course, registered for my 2nd one next semester). However, I'll attempt to try the "standard" way. Thanks for your suggestion.
I have to go to work so hopefully I'll continue working on it tomorrow.
PS
Why are math majors required to take C++ courses? It doesn't make much sense to me. I don't think I'll ever use it in life anyways.
|
|
|
04-29-2005, 05:56 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Strictly, you don't need math do learn a language. Not a programming language, nor French or Chinese. However, as you move on you'll learn the ways of datastructures and algorithms. To understand the performance (=complexity) of these two subjects, you'll need the math. At one point you have to find the right way to store your data and match this way (datastructure) with the proper algorithms for maximum performance, correctness and or ease. Besides, the math major may be used to sharpen the "thinking" of the student in general.
As it comes to itoa(), you have just encountered your very first experiance on why it is bad to use non-standard implementations. You see, my compiler doesn't know what an itoa() is! Although I do have the <cstdlib> library. So your code didn't compile on my machine.
Luckily, there was only one instance of this, so I could fix it in a minute or so. But imagine one programs 80,000 lines of code, and then the project is going to be standardized... expensive isn't it
Since you need to add comments, I'll give you this comment for your teacher:
// http://www.research.att.com/~bs/bs_faq2.html
Add this comment above the itos() implementation.
Good luck and make sure we hear from you  .
- Val -
__________________
|
|
|
05-03-2005, 08:42 AM
|
#10 (permalink)
|
|
Hottie
Join Date: Apr 2005
Posts: 5
|
I didn't have a chance, until now, to work on my C++. Here is the flowchart of what I'm trying to get.
1. player deposit at least $.25
2. 5 cards are dealt
3. player is asked to throw away up to 3 cards and replaced with new cards
4. winnings are calculated, and user is asked to either cash out or play again
-if play again, go to step 2 to continue playing
5. cash out/no credits left
Here is my code for run_game function:
PHP Code:
void run_game(void)
{
// deposit $
cout << "Deposit $.25 to start the game" << endl << "$";
double money;
cin >> money;
if(money < .25)
{
cout << "You must deposit at least $.25 to play!" << endl;
cout << "Deposit $.25 to start the game" << endl << "$";
cin >> money;
}
generate_deck();
int reply,
discarded_card;
//MAIN LOOP
int respond = 1;
while(respond == 1)
{
// deal 5 random cards
int hand[5];
reset_deck();
deal(hand);
print_hand (hand);
for (int i = 0; i < 3; i++)
{
//ask user if they want to get rid of any cards
cout << "Would you like to throw away a card?" << endl;
cout << "Enter 1 for YES or 2 for NO: ";
cin >> reply;
//if YES, call get_card to pick out new card
if (reply == 1)
{
cout << "Enter one card, 1 to 5, to be discarded: ";
cin >> discarded_card;
hand[discarded_card - 1] = get_card();
// if card chosen is not 1 to 5
while (discarded_card != 1 && discarded_card != 2 && discarded_card != 3 && discarded_card != 4 && discarded_card != 5)
{
cout << "Invalid card selection" << endl;
cout << "Enter one card, 1 to 5, to be discarded: ";
cin >> discarded_card;
hand[discarded_card - 1] = get_card();
}// end while
}// end if
if (reply != 1 && reply != 2)
{
while (reply !=1 && reply != 2)
{
cout << "Invalid selection!" << endl;
cout << "Would you like to throw away a card?" << endl;
cout << "Enter 1 for yes or 2 for no: ";
cin >> reply;
if (reply == 1)
{
cout << "Enter one card, 1 to 5, to be discarded: ";
cin >> discarded_card;
hand[discarded_card - 1] = get_card();
// if card chosen is not 1 to 5
while (discarded_card != 1 && discarded_card != 2 && discarded_card != 3 && discarded_card != 4 && discarded_card != 5)
{
cout << "Invalid card selection" << endl;
cout << "Enter one card, 1 to 5, to be discarded: ";
cin >> discarded_card;
hand[discarded_card - 1] = get_card();
}// end while
}// end if
}// end while
}// end if
// if reply = NO (2): Do nothing
}//end for
print_hand(hand);
// Winnings are calculated
if (is_royal_flush(hand) == true)
{
money = money + 100.00;
cout << "You have a royal flush!" << endl << "You won $100!" << endl;
}
if (is_straight_flush(hand) == true)
{
money = money + 12.50;
cout << "You have a straight flush!" << endl << "You won $12.50!" << endl;
}
if (is_four_of_a_kind(hand) == true)
{
money = money + 6.25;
cout << "You have a a four of a kind!" << endl << "You won $6.25!" << endl;
}
if (is_full_house(hand) == true)
{
money = money + 2.00;
cout << "You have a full house!" << endl << "You won $2.00!" << endl;
}
if (is_flush(hand) == true)
{
money = money + 1.25;
cout << "You have a flush!" << endl << "You won $1.25!" << endl;
}
if (is_straight(hand) == true)
{
money = money + 1.00;
cout << "You have a straight!" << endl << "You won $1.00!" << endl;
}
if (is_three_of_a_kind(hand) == true)
{
money = money + 0.75;
cout << "You have a three of a kind!" << endl << "You won $.75!" << endl;
}
if (is_two_pair(hand) == true)
{
money = money + 0.50;
cout << "You have two pairs!" << endl << "You won $.50!" << endl;
}
if (is_pair(hand) == true)
{
money = money + 0.25;
cout << "You have a pair!" << endl << "You won $.25!" << endl;
}
// subtract $.25 for playing the game
money = money - 0.25;
//Adjust number of credits
cout << "Your current cash available is: $" << money << endl;
if (money >= 0.25)
{
//ask if user wants to play again
cout << "Would you like to play again?" << endl;
cout << "Press 1 for YES, and 2 for NO: ";
cin >> respond;
// if not, Cash out
if (respond == 2)
cout << "Your cash is: $" << money << endl;
}// end if
if (money < 0.25)
{
while(money < 0.25)
{
cout << "Not enough money to continue!" << endl;
cout << "To play again, insert more money: $";
double added_money;
cin >> added_money;
money = money + added_money;
cout << "Your current cash is now: $" << money << endl;
if(money >= 0.25)
respond = 1;
}// end while
}// end if
}// End main while loop
} // end run_game
Any suggestions?
Last edited by marina; 05-09-2005 at 08:10 AM.
|
|
|
| Thread Tools |
|
|
| Display Modes |
|
| | | |