1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
using namespace std;
VOID WINAPI Sleep(DWORD dwMilliseconds);
#define TableSize 5
#define HandSize 2
char SuitNameOptions[] = { 'H', 'D', 'C', 'S' };
char ValueNameOptions[] = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K' };
string CardName1Options[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string CardName2Options[] = { " of Hearts", " of Diamonds", " of Clubs", " of Spades" };
int ValueOptions[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int SuitOptions[] = { 1, 2, 3, 4 };
class card {
public:
char ValueName, SuitName;
//eg A, 2, 3... eg H, C, D
int Value, Suit;
//eg 1, 2, 3... eg 1, 2, 3
string CardName;
//eg "Ace of Hearts"
int PosInPack;
};
class hand {
public:
card CardsInHand[HandSize];
};
class player {
public:
int pos;
hand PlayerHand;
};
card Pack[4][13];
void create_pack() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
Pack[i][j].SuitName = SuitNameOptions[i];
Pack[i][j].Suit = SuitOptions[i];
Pack[i][j].ValueName = ValueNameOptions[j];
Pack[i][j].Value = ValueOptions[j];
Pack[i][j].CardName = CardName1Options[Pack[i][j].Value-1] + CardName2Options[Pack[i][j].Suit-1];
Pack[i][j].PosInPack = (j+1)+13*(i);
}
}
}
card PackList[52];
void create_packlist() {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 13; k++) {
PackList[(j*13)+k] = Pack[j][k];
}
}
}
player Table[TableSize-1];
void create_table() {
for (int a = 0; a < TableSize; a++) {
Table[a].pos = a;
}
}
void Deal() {
for (int a = 0; a < HandSize; a++) {
for (int b = 0; b < TableSize; b++) {
int c = b+1+a*3;
Table[b].PlayerHand.CardsInHand[a] = PackList[c];
}
}
}
void OutputCardArray(card a[], int len) {
for (int i = 0; i < len; i++) {
cout << a[i].CardName;
cout << endl;
}
}
void OutputPackList() {
OutputCardArray(PackList, 52);
}
void shuffleElements(card theArr[], int length) {
for (int last = length; last > 1; last--) {
int randomNum = rand()%last;
card temp = theArr[randomNum];
theArr[randomNum] = theArr[last-1];
theArr[last-1] = temp;
// Chooses a random card in the array and swaps it with the last, then the next one with the second-last, etc.
}
}
void shuffleCards(card arrPack[]) {
for (int a = 0; a < 52; a++) {
int randNum = rand()%52;
card Temp = arrPack[a];
arrPack[a] = arrPack[randNum];
arrPack[randNum] = Temp;
/* Swaps each card with a random one - each card could be swapped with itself;
or twice, back to its original position, but this can happen in real life. */
}
}
void ShufflePack() {
shuffleElements(PackList, 52);
shuffleCards(PackList);
shuffleElements(PackList, 52);
shuffleCards(PackList);
shuffleElements(PackList, 52);
shuffleCards(PackList);
}
void init() {
create_pack();
create_packlist();
create_table();
}
int main() {
init();
ShufflePack();
cout << endl;
Deal();
OutputPackList();
getch();
return 0;
}
| |