6 Replies - 3419 Views - Last Post: 10 December 2010 - 05:54 AM Rate Topic: -----

#1 CodeMachine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 10

C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 09 December 2010 - 02:29 PM

The Instructions
Due tonight at 11:59pm Eastern Time in the US.

1. Write a program that sorts and evaluates bridge hands according to the rules outlined below.

2. For the purposes of this assignment, the ranks and the suits will be given as

A Ace S Spades
2 two H Hearts
3 three C Clubs
4 four D Diamonds
5 five
6 six
7 seven
8 eight
9 nine
T ten
J Jack
Q Queen
K King

3. The input will consist of zero or more lines of input, where each line represents a single bridge hand. Each line will contain a stream of 13 character pairs as shown in this example.

2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH

The input will be error free and there will always be 13 cards available in every input line. There will be no duplicate cards in any hand. Your program should process each line, in turn, producing the output described below for each hand until the end-of-file.

4. The output will be a display of the hand in a more “readable” format, followed by a numerical evaluation of the hand using the standard bridge values (given below). The hand should be displayed by rank with suit in the following fashion, with the evaluation point count following it:

Spades A
Hearts K J 7 5 3
Diamonds A Q 10 3
Clubs 10 6 2
---------
Points = 16

Notice the conversion of TC (ten of Clubs) into ‘‘Clubs 10’’.

5. The rules for evaluating bridge hands are:
Aces count as 4 points
Kings count as 3 points
Queens count as 2 points
Jacks count as 1 points
Voids (no cards in a suit) count as 3 points
Singletons (one card in a suit) count as 2 points
Doubletons (two cards in a suit) count as 1 points
Long suits (more than 5cards in a suit) count as 1 point for each card over 5
(a void means that you have a hand of cards, say, clubs, diamonds, spades, and no hearts --- a suit is missing. if you had one heart you would have a singleton, two hearts are a doubleton, etc.)

6. Your program should accept its input from stdin and print its output to stdout. A sample input is:
2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH
3D QC TS 9S 4H 6D 6H 8S 2S 4D 7H 6C 9C
6D 9C 2S 2D TS 4H TD 7S 4C 3C 8H 8D 6S
AS AD AH AC KS KH KD KC JC JH JD QH QC
7D TS 8S TC 2C 2H 7C 8C QS QD JS 9D 4C
You can test your program thoroughly using your own test data, as well.

7. This is not a difficult problem. (If you're a C++ pro and not a student, I would comment to my professor) Work on understanding the problem and coming up with a good design. Move slowly toward the implementation and coding.

8. This problem lends itself to object-oriented design using C-strings, multidimensional arrays, and classes. But remember that “object-oriented” is more than just a measure of how many classes and objects you can create and cram into your program.

My problem:

As simple as the problem might seem to be, I have never used strings or multidimensional arrays along with classes that converts a two character code into a card number and suit and keeps track of their points. I have set up the basic format of how the output should look, it should give you a good headstart on what the program should look like. Feel free to edit whatever bit of coding I made that obviously is messed up. If you can put in the sample input from Step 3 and get the output from Step 4, you've got it down. Thanks for the help in advance.

What I got so far:

//begin program
#include <iostream>
#include <string>
using namespace std;

//create Bridgehand class to hold types for card suits and numbers
//use a string-type for full suit names
class Bridgehand
{
public:
    char Min(char);
};

//declare main
int main()
{
    char cardNumber[13][4]={{'SA', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'ST', 'SJ', 'SQ', 'SK'}, 
    {'HA', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'HT', 'HJ', 'HQ', 'HK'},
    {'DA', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'DT', 'DJ', 'DQ', 'DK'}, 
    {'CA', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'CT', 'CJ', 'CQ', 'CK'}};
    string inputCards;
    int i = 0, points = 0;
    //accept user input for 13 cards
    cout << "Enter your Bridge Hand.";
    //store card input into an array
    for (i = 0; i < 13; i++)
    {
        char card;
        cin << inputCards;
        cout << "\t"
    }
    //output the number of Spades in the hand; use for-loop until all are displayed 
    cout.put << "\nSpades\t\t ";
    for (i = 0; i < Spades; i++)
    {
    cout.put << Spade.cardNumber[1][i];
    } 
    //output the number of Hearts in the hand; use for-loop until all are displayed 
    cout.put << "\nHearts\t\t ";
    for (i = 0; i < Hearts; i++)
    {
    cout.put << Heart.cardNumber[2][i] << "\t";
    }
    //output the number of Diamonds in the hand; use for-loop until all are displayed 
    cout << "\nDiamonds\t\t "; 
    for (i = 0; i < Diamonds; i++)
    {
    cout.put << Diamond.cardNumber[3][i] << "\t";
    }
    //output the number of Clubs in the hand; use for-loop until all are displayed 
    cout.put << "Clubs\t\t ";
    for (i = 0; i < Clubs; i++)
    {
    cout.put << Club.cardNumber[4][i];
    }
    
    if ([i] == 1)
    {
       points = points+2;
    }
    else-if (SuitName[i] == 2)
    {
       points = points+1;
    }
    else(SuitName[i] == 0)
    {
       points = points+3;
    }
      
    //follow point-table and keep score of the hand
    //output the amount of total points for the current Bridge hand
    cout.put << "---------\n";
    cout.put << "Points = " << points << endl;
        
system("pause");
return EXIT_SUCCESS;
}
//end main
//end program


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 09 December 2010 - 03:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Bridge Hand problem: Include C-strings, classes and multidimension

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 09 December 2010 - 02:38 PM

First good luck.

Second Please use code tags.

Third in your main()

char cardNumber[13][4]={{'SA', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'ST', 'SJ', 'SQ', 'SK'}, 


You can not use multiple character characters. A character is only 1 character. Example: 'S','2', ect....

Jim
Was This Post Helpful? 0
  • +
  • -

#3 CodeMachine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 10

Re: C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 09 December 2010 - 02:41 PM

I see that now, but that's the least of my problems - I don't how to use multidimensional arrays like this, I was never really taught how.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 09 December 2010 - 02:44 PM

Then you might want to take a look at this link: Arrays.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 CodeMachine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 10

Re: C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 09 December 2010 - 02:49 PM

Read the details of the problem, if you please. There is a lot more to it than that tutorial link. We're talking about comparing, finding, and converting a C-string from user input with a multidimensional array and outputting it in a sorted manner.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,868
  • Joined: 25-December 09

Re: C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 09 December 2010 - 03:21 PM

I scanned the details of the problem. I know there is a lot more to the program than that link.

However you stated:

Quote

I see that now, but that's the least of my problems - I don't how to use multidimensional arrays like this, I was never really taught how.


If you don't know how to use arrays then you will not be able to complete the program. So start there. After you understand arrays then you can try your writing program again, and then if you have specific problems come back and ask the question, post the modified code (in code tags), and any error messages you received.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,756
  • Joined: 16-October 07

Re: C++ Bridge Hand problem: Include C-strings, classes and multidimension

Posted 10 December 2010 - 05:54 AM

Quote

8. This problem lends itself to object-oriented design using C-strings, multidimensional arrays, and classes.


I woud disagree with this: there is rarely a valid reason to have multidimensional arrays. An array of structs, on the other hand...

Your data is given to you in two character pairs, yes? The pair consists of the face and the suit.

Define the nature of both.
e.g.
struct Suit {
	char token;
	char *name;
};

struct Face {
	char token;
	char *name;
	int value;
};



Your card is then a reference to each:
struct Card {
	Suit *suit;
	Face *face;
};



You can declare the entire domain of Suit like so:
const int SuitCount = 4;
const Suit Suits[SuitCount] = {
	{'S',"Spades"},
	{'H',"Hearts"},
	{'C',"Clubs"},
	{'D',"Diamonds"},
};



And you're on your way.

There are a number of ways to do this. The above is more C than C++. A Suit class would more more appriate:
class Suit {
private:
	char token;
public:
	Suit();
	Suit(char token);
	bool isValid() const; // good for parsing and empty constructors
	char getToken() const; // public accessor for token
	const char *getName() const; // return the name, NULL for invalid, or whatever
};

struct Card {
	Suit suit;
	Face face;
	Card();
	Card(char face, char suit);
	bool isValid() const;
};



Hope this helps.

This post has been edited by baavgai: 10 December 2010 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1