#include <iostream>
#include <string.h>
#include <iomanip>
#include <cstring>
#include <cassert>
using namespace std;
#ifndef _TYPE_H
#define _TYPE_H
enum CardSuitType {DIAMOND,HEART,CLUB,SPADE};//enum for the suit type
struct card // creates the structure for cards
{
CardSuitType suit; //declares suit type
int numValue; // numerical value of the card
int pointValue; // point value of the card
};
const int DECK_SIZE = 52;//constant for the max size of the deck
const int HAND_SIZE = 13;//constant for the max size of hand
#endif /* _TYPE_H */
#include“type.h”
#ifndef _CARDCLASS_H
#define _CARDCLASS_H
class CardClass {
public:
CardClass();//default constructor
void ShuffleCards();//shuffles the deck
card DealCards();//deals one card to whoever calls the function
void Print();//Prints the deck of cards
private:
card deck[DECK_SIZE]; //initial deck
int cardsLeft; //cards left in deck
};
#endif /* _CARDCLASS_H */
//////start CardClass.cpp
#include "CardClass.h"
#include <stdio.h>
#include <stdlib.h>
//Default Constructor for the Card Class
//Creates a deck of 52 cards 13 of each suit
CardClass::CardClass()
{
cardsLeft = 52;
for(int i = 0; i <= 12; i++)//sets all the diamond cards
{
deck[i].numValue = i+1;
deck[i].pointValue = 0;
deck[i].suit = DIAMOND;
}
for(int i = 13; i <=25; i++)//sets the club cards
{
deck[i].numValue = i-12;
if(i-12 == 10)//checks for jack of clubs
deck[i].pointValue = -100;
else//for all other cards
deck[i].pointValue = 0;
deck[i].suit = CLUB;
}
for(int i = 26; i <= 38; i++)//sets all the heart cards
{
deck[i].numValue = i-25;
if(i-25 < 10)//checks for card value less than 10
deck[i].pointValue = 5;
else//checks for 10, jack, queen, king
deck[i].pointValue = 10;
deck[i].suit = HEART;
}
for(int i = 39; i <= 51; i++)//sets the spade cards
{
deck[i].numValue = i-38;
if(i-38 == 11)//checks for jack of spades
deck[i].pointValue = 100;
else//for all other cards
deck[i].pointValue = 0;
deck[i].suit = SPADE;
}
}
/*
* ShuffleCards: Shuffles the deck of cards in a random order.
* Precondition: An object of CardClass has been constructed
* Postcondition: The deck of cards is shuffled.
*
*/
void CardClass::ShuffleCards()
{
card temp;
for(int i = 0; i < DECK_SIZE; i++)//goes through the deck and swaps with a random card
{
int random = rand() %DECK_SIZE;
temp = deck[i];
deck[i] = deck[random];
deck[random] = temp;
}
}
/*
* DealCards: Deals a card to a user
* Precondition: deck has been initialized
* Postcondition: a card is "dealt" (returned) to the caller
*/
card CardClass::DealCards()
{
cardsLeft--;
return deck[cardsLeft];
}
/*
* Print: Prints the deck in a tabled format
* Precondition: deck has been initialized
* Postcondition: the contents of the deck are outputted
* on the screen
*/
void CardClass::Print()
{
//output the table head
cout << setw(20) << left << "Suit Type"
<< setw(20) << right << "Number Value"
<< setw(20) << right << "Point Value"
<< endl;
//output the cards
for(int i = 0; i < DECK_SIZE; i++)//moves through the hand
{
string value = " ";//string for Face cards
string suit[4] = {"Diamond", "Heart","Club", "Spades"};//String for suit values
string num[13] = {"A","2","3","4","5","6","7","8","9", "10", "Jack","Queen","King"};
//output deck in tabular format.
cout << setw(20) << fixed << left << suit[deck[i].suit]
<< setw(20) << fixed << right << num[deck[i].numValue]
<< setw(20) << fixed << right << deck[i].pointValue
<< endl;
}
}
///////start main.cpp
#include <stdlib.h>
#include <cassert>
#include <fstream>
#include <cstring>
#include <time.h>
#include "CardClass.h"
#include "PlayerClass.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
srand(time(0));//primes random
CardClass deck;//local deck of cards
PlayerClass player[4];//array or players to be dealt
deck.ShuffleCards();//shuffles the deck
deck.Print();//prints the deck
for(int i = 0; i < DECK_SIZE; i++)//goes through deck
{
player[i%4].AddACard(deck.DealCards());//adds cards to a player's hand
}
for(int i = 0; i < 4; i++)//goes through array of players
{
player[i].SortCard();//sorts the hand of the player
}
for(int i = 0; i < 4; i++)//goes through array of players
{
cout << endl << endl//prints player hand information.
<< "Player "<< i+1 << endl
<< player[i];
}
return (0);
}
它在Windows和我需要用于学校的Debian客户端中编译和输出完全正常,但我更喜欢在ubuntu中开发。
输出
Suit Type Number Value Point Value
Heart 6 5
Spades King 0
Spades 7 0
> Club 2 0 Heart
> 8 5 Spades 6
> 0 Spades 10 0 Diamond
> Jack 0 Club 5
> 0 Club King 0 Diamond
> King 0 Club 6
> 0 Heart Jack 10 Diamond
> 5 0 Spades Queen
> 100 Diamond Queen 0
> Spades 8 0 Heart
> 10 5 Club 7
> 0 Diamond 6 0 Diamond
> 10 0 Spades Jack
> 0 Spades 2 0 Club
> 8 0 ��5���d �=; 6t�� ��5��HcD$
> H���H�=��5H�����:
> H�Ā�4���H�=j�5H���;
> H�Ā�G���H�=K�5H�����:
> H�Ā�g���H�=,�5H�����: �5H�����:�Ā�����H�=
> H�Ā�����H�=��5H�����:
> H�Ā�����H�=��5H����C:
> H�Ā�����H�=��5H����T:
> H�Ā�����zLc҅�H�O�VF�D�F��D�FD�V
> t/�?����A����)�Hc�H��H�B�����A��Hc�H��H�Mc�H�NJ��H�F(1�DH�H)�H������V���H���5d�������fff.�H��t{H��tv�WH�O��tKH�L�L�O(�A�H����I9щI�@vI9�H�HF�H�1��H��H�1�H��i�mN�A90%�����1��H�A�5d�������fff.�USH��H��H����V���������L�F���E�1���A���nHc�H��~M�A�_
> N���A���������)�i�����i��)�iɧAȍ�����I�A�
> �H��H9�|�HcC �l�L��l-�I��H�1���x@H�t$
> H�߃���������u�1�H��[]ø�����H�\$�H�l$�H��L�d$�H��(H��!H�AH��t�q����@
当我使用调试器时,它会在deck.Print();
期间全部放到屏幕上答案 0 :(得分:3)
你的牌组号码从1开始,你从0引用num
数组。所以i
中的12个
for(int i = 0; i <= 12; i++)//sets all the diamond cards
{
deck[i].numValue = i+1;
deck[i].pointValue = 0;
deck[i].suit = DIAMOND;
}
给出numValue为13且num
数组仅转到索引12. num[deck[i].numValue]
应为num[deck[i].numValue - 1]
。