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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include "stdafx.h"
// Inventory
// Manages a player's inventory
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
void Initialize(string cards[], const string * ps, const char * pc);
void Display(string cards[], int size);
void Shuffle(string cards[]);
void Transfer(string cards[], string hnd[], int size, int fun);
void Add(string cards[], string hnd[], int size);
void Remove(string cards[], string hnd[], int size);
int main()
{
cout << "\tWelcome to Inventory!" << endl;
srand(time(NULL));
//items
const int NUM_CARDS = 52;
const int NUM_RANK = 13;
const int NUM_HAND = 5;
const int NUM_SUIT = 4;
const string rank[NUM_RANK] = {"2", "3", "4",
"5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
const char suit[NUM_SUIT] = { 'c', 'h', 's', 'd' };
string deck[NUM_CARDS];
string hand[NUM_HAND] = { "" };
int choice; //menu choice
int input;
Initialize(deck, rank, suit);
do
{
cout << endl << "Inventory" << endl;
cout << "---------" << endl << endl;
cout << "0 - Quit" << endl;
cout << "1 - Shuffle deck";
cout << endl;
cout << "2 - Display deck";
cout << endl;
cout << "3 - Display hand";
cout << endl;
cout << "4 - Deal cards from deck to hand";
cout << endl;
cout << "5 - Return cards from hand to deck";
cout << endl;
cout << endl << "Choice: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 0:
cout << "Good-bye." << endl;
break;
case 1:
Shuffle(deck);
cout << "Shuffled" << endl;
break;
case 2:
cout << "Deck: " << endl;
Display(deck, 52);
break;
case 3:
cout << "Hand: " << endl;
Display(hand, 5);
break;
case 4:
do
{
cout << "Enter number of cards to add to hand: ";
cin >> input;
} while (input < 0 || input > NUM_HAND);
cout << "Dealing cards from deck..." << endl;
Transfer(deck, hand,input, 1);
break;
case 5:
do
{
cout << "Enter number of cards to remove from hand: ";
cin >> input;
} while (input < 0 || input > NUM_HAND);
cout << "Removing cars from hand..." << endl;
Transfer(deck, hand, input, 2);
break;
default:
cout << "Sorry, " << choice;
cout << " isn't a valid choice." << endl;
}
} while (choice != 0);
return 0;
}
void Initialize(string cards[], const string * ps, const char * pc)
{
for (int i = 0; i < 52; i++)
{
cards[i] = *(ps + (i % 13)) + *(pc + (i % 4));
}
}
void Display(string cards[], int size)
{
for (int i = 0; i < size; i++)
{
cout << cards[i] << " ";
if ((i+1) % 4 == 0)
cout << endl;
}
}
void Shuffle(string cards[])
{
for (int i = 0; i < 52; i++)
{
int rnd = rand() % 51;
string temp = cards[i];
cards[i] = cards[rnd];
cards[rnd] = temp;
}
}
void Transfer(string cards[], string hnd[], int size, int fun)
{
//adding
if (fun == 1)
{
Add(cards, hnd, size);
}
//removing
else if (fun == 2)
{
Remove(cards, hnd, size);
}
}
void Add(string cards[], string hnd[], int size)
{
int count1 = 0;
for (int i = 0; i < 5; i++)
if (!hnd[i].empty())
count1++;
cout << count1 << endl;
if (size + count1 > 5)
{
cout << "Too many cards. Please try again." << endl;
return;
}
for (int i = 51, j = 0; i > (51 - size); i--)
{
hnd[j++] = cards[i];
cards[i] = "";
}
}
void Remove(string cards[], string hnd[], int size)
{
int count1 = 0;
for (int i = 0; i < 5; i++)
if (!hnd[i].empty())
count1++;
cout << count1 << endl;
int count2 = 0;
for (int i = 0; i < 52; i++)
if (!cards[i].empty())
count2++;
cout << count2 << endl;
if (size > count1)
{
cout << "Hand is too small. Please try again." << endl;
return;
}
for (int i = (count2), j = (count1-1); i < (count2+size); i++, j--)
{
cards[i] = hnd[j];
hnd[j] = "";
}
}
|