Hello, I hope someone can help me out. I am going to be taking the
third step in a programming class soon(I took the previous two a while
ago at a different school) and in an effort to get back up to speed, I
bought the Dietel book How To Program 5th ed. I am going through some
examples trying to work them out, and have been doing pretty good, but
came across one exercise that has me stumped. It's a card shuffling
program that deals 5 cards, and stores the face value and suit in
arrays respectively. I have pasted the code which I hope shows that.
// to deal only 5 cards for a poker hand
for ( int card = 1; card <= 5; card++ )
{
// loop through rows of deck
for ( int row = 0; row <= 3; row++ )
{
// loop through columns of deck for current row
for ( int column = 0; column <= 12; column++ )
{
// if slot contains current card, display card
if ( deck[ row ][ column ] == card )
{
cout << setw( 5 ) << right << face[ column ]
<< " of " << setw( 8 ) << left << suit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
} // end if
} // end innermost for
} // end inner for
} // end outer for
What this example asks to do is to modify the program so that it can
determine if the hand has a pair. (and three of a kind and so on)
Considering the "cards" are stored in arrays, how would you compare all
5 face values for example and be able to tell their are two of a kind?
Thanks for taking the time to look at this and any help you can
provide.
Chaz 2 3644
On 14 Jun 2005 18:49:13 -0700, "Chaz" <ch********@aol .com> wrote in
comp.lang.c++: Hello, I hope someone can help me out. I am going to be taking the third step in a programming class soon(I took the previous two a while ago at a different school) and in an effort to get back up to speed, I bought the Dietel book How To Program 5th ed. I am going through some examples trying to work them out, and have been doing pretty good, but came across one exercise that has me stumped. It's a card shuffling program that deals 5 cards, and stores the face value and suit in arrays respectively. I have pasted the code which I hope shows that.
// to deal only 5 cards for a poker hand for ( int card = 1; card <= 5; card++ ) { // loop through rows of deck for ( int row = 0; row <= 3; row++ ) { // loop through columns of deck for current row for ( int column = 0; column <= 12; column++ ) { // if slot contains current card, display card if ( deck[ row ][ column ] == card ) { cout << setw( 5 ) << right << face[ column ] << " of " << setw( 8 ) << left << suit[ row ] << ( card % 2 == 0 ? '\n' : '\t' ); } // end if } // end innermost for } // end inner for } // end outer for
What this example asks to do is to modify the program so that it can determine if the hand has a pair. (and three of a kind and so on) Considering the "cards" are stored in arrays, how would you compare all 5 face values for example and be able to tell their are two of a kind?
Thanks for taking the time to look at this and any help you can provide.
Chaz
I'll give you a suggestion, not the code.
Create an array of 13 ints, one for each face value, and initialize
all the elements of this array to 0. Decide on a mapping between the
13 different card faces, if the program does not provide one already.
Now look at each of the 5 cards in the hand. For each card, increment
the element of the array that corresponds to its face value.
Final step, loop through the 13 elements of the array. If any element
has the value 2, you have a pair of the corresponding card face. If
any element has 3, you have three of a kind, a 4 means four of a kind,
and a 5 means you are playing with a crooked deck.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jack,
Thanks, I think I follow.
The program maps the cards in a 2-d array, 4x13. Normally, it shuffles
all 52 cards, and randomly selects a number between 1-52 and puts it
into an element on the array. I changed it so that instead of it
randomly selecting 52 cards, it only selects 5 as to select 5 cards(I
would post the code but I am at work and don't have it with me).
I will work on it tonight and see what I come up with.
Thanks again.
Chaz This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: twdo |
last post by:
Let me see if I can explain my situation clearly.
I have a table with the columns:
answer_id, question_id, member_id, answer
- answer_id is the primary key for the table.
- question_id relates to another table with questions for a user. The
table holds the question and the possible choices in a varchar field
separated by a delimiter.
|
by: Odd-R. |
last post by:
I have to lists, A and B, that may, or may not be equal. If they are not
identical, I want the output to be three new lists, X,Y and Z where X has
all the elements that are in A, but not in B, and Y contains all the
elements that are B but not in A. Z will then have the elements that are
in both A and B.
One way of doing this is of course to iterate throug the lists and compare
each of the element, but is there a more efficient way?
...
|
by: Manny Chohan |
last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30
AM. How can i compare dates .net c# or if there is any other way such as
Javascript.
Thanks
Manny
|
by: Dennis |
last post by:
I have a public variable in a class of type color declared as follows:
public mycolor as color = color.Empty
I want to check to see if the user has specified a color like;
if mycolor = Color.Empty then.....
or
if mycolor is Color.Empty then .......
|
by: Frank |
last post by:
Hello All,
I am working on a vb.net app where I need to compare to 2 datatables
and determine if a string exists in one or both. The first dt is
filled from the db. A form is loaded and the appropriate items in a
checkedlist box are selected based on the dt. So far, no problem. Then
user can then edit the values in the checkedlist box and choose to
save changes. When they save changes, I throw the new values of the
checked items from...
| |
by: Kermit Piper |
last post by:
Hello,
I am comparing two date values, one from a database and one that has
been converted from a hard-coded string into an actual Date type. So
far so good. The problem I'm having is that one of the values comes
from the database, and for existing values it works fine, but if the
date doesn't exist (which will always be the condition when the user
first enters into the form) I am adding logic to my javascript like: if
(dbDate <...
|
by: galapogos |
last post by:
Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array and I want to compare it with the constant
0x00010203040506070809 where array == 0x00, array == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other...
|
by: Pugi! |
last post by:
hi,
I am using this code for checking wether a value (form input) is an
integer and wether it is smaller than a given maximum and greater then
a given minimum value:
function checkInteger(&$value, $checks) {
$err = '';
if (!is_numeric($value) || (floatval($value) != intval($value))) {
$err .= 'Input must be an integer. ';
|
by: Yash |
last post by:
Hi,
We are in the process of tuning the performance of our stored procs in
SQL 2000 and are looking for a tool that would help us in comparing
the result sets of an old SP and a modified SP. The tool should take
as input the names of the 2 SPs, a set of parameter values and invoke
them. Internally it should compare the result sets returned and log/
display the difference in result sets if any.
We do not want to compare response time,...
|
by: DomiNeug |
last post by:
Hello,
Since a while i have to find a way of comparing "Sets" (multiple int Values) and so to find equal sets. There simply 3 tables
ValueList with: ID int
ValueListHasValue: ID int, ValueListID int, ValueID int
Value: ID int, Description varchar(max)
The sets are defined in the ValueList has ValueTable. Any combination and order is possible as there is also no restriction on the amount of Values in a ValueList. My first approach was to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |