0

我刚刚开始学习编写 C++ 代码,所以请多多包涵,我的知识非常有限。这是我从头开始编写的第一个程序。我正在编写一个程序,它查看一手五张牌,然后告诉用户它的类型,例如皇家同花顺、顺子等。

首先,我让用户输入他们的手,然后将该手存储到一个数组中。

然后我需要做的是在该数组中搜索重复项(例如数组中有两个 3),计算重复项的数量(在本例中为 2 个),然后将该数字 2 放入对应位置的另一个数组中的卡片。

例如:用户有一手牌 S11H11D11C11S10(S - 黑桃,H - 红心,D - 菱形和 C - 梅花) 这里的数字 11 代表 J,A 是 1 级,K 是 13 级。

因此,用户手中有 4 个千斤顶和 10 个黑桃。

我需要程序来计算这四个千斤顶,然后在第 11 个位置放入另一个数组。这将告诉我用户有四张牌,它们是 Jacks。

你可以在这里查看我的代码:

标题:

#ifndef SUIT_H_INCLUDED
#define SUIT_H_INCLUDED
#include <string>

using namespace std;


struct Card {
int rank1;
int rank2;
int rank3;
int rank4;
int rank5;

char suit1;
char suit2;
char suit3;
char suit4;
char suit5;


};

#endif // SUIT_H_INCLUDED

主功能:

#include <iostream>
#include <string>
#include "suit.h"

using namespace std;



int main()
{

int usrhand[10];


char a; //letters for declaring cards in hand
char b;
char c;
char d;
char e;

int f; //numbers for declaring rank of cards
int g;
int h;
int i;
int j;

Card hand; //initiates a structure for the players hand

    hand.suit1 = a;
    hand.suit2 = b;
    hand.suit3 = c;
    hand.suit4 = d;
    hand.suit5 = e;

    hand.rank1 = f;
    hand.rank2 = g;
    hand.rank3 = h;
    hand.rank4 = i;
    hand.rank5 = j;



cout << "Welcome to Card Sorter (TM) designed by ----" << endl;
cout << " " <<endl;
cout << "You will be prompted to enter your 5 card hand." <<endl;
cout << "I will prompt you for each card, one by one." <<endl;
cout << "I will ask you for the suit and then the rank of the card." <<endl;
cout << "Keep in mind: Spades=S, Hearts=H, Diamonds=D, Clubs=C" <<endl;
cout << "Also keep in mind the value of Ace is low, and is rank 1" <<endl;
cout << "While the value of a King is high and is rank 13." <<endl;
cout << "Let's begin." <<endl;
cout << " " << endl;
cout << "What is the suit of your first card (S,H,D,C)?" <<endl;
cin >> a;
cout << "What is the rank of your first card (1-13)" <<endl;
cin >> f;
cout << "What is the suit of your second card? (S,H,D,C)?" <<endl;
cin >> b;
cout << "What is the rank of your second card (1-13)" <<endl;
cin >> g;
cout << "What is the suit of your third card? (S,H,D,C)?" <<endl;
cin >> c;
cout << "What is the rank of your third card (1-13)" <<endl;
cin >> h;
cout << "What is the suit of your fourth card? (S,H,D,C)?" <<endl;
cin >> d;
cout << "What is the rank of your fourth card (1-13)" <<endl;
cin >> i;
cout << "What is the suit of your final card? (S,H,D,C)?" <<endl;
cin >> e;
cout << "What is the rank of your final card (1-13)" <<endl;
cin >> j;
usrhand[0] = a;
usrhand[1] = f;
usrhand[2] = b;
usrhand[3] = g;
usrhand[4] = c;
usrhand[5] = h;
usrhand[6] = d;
usrhand[7] = i;
usrhand[8] = e;
usrhand[9] = j;

cout <<" Lets take a look at your current hand. " <<endl;
printf("%c", usrhand[0]);
printf("%d", usrhand[1]);
printf("%c", usrhand[2]);
printf("%d", usrhand[3]);
printf("%c", usrhand[4]);
printf("%d", usrhand[5]);
printf("%c", usrhand[6]);
printf("%d", usrhand[7]);
printf("%c", usrhand[8]);
printf("%d", usrhand[9]);

//need to count duplicate ranks in this array (ie. player has 4 cards of the same value, let's say value is 5)
// and then place the counted value in another array at the
// correct position, the position that matches the value in this case (so array2[4] = 4)
// Then I will program something to examine the second array for its number values and print out the players hand
// (ie. array2[4] = 4, player has four 5's this is a Four of a Kind)

    return 0;
}
4

1 回答 1

0

由于您只需要找到重复的排名,您可以这样做:

int dupCounter[13];
for (int i=0; i<13; i++)
{
    dupCounter[i] = 0;
}
for (int i=0; i<5; i++)
{
    dupCounter[usrhand[i*2+1]-1] ++;
}
于 2013-05-23T03:55:05.370 回答