-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp.save-failed
153 lines (141 loc) · 4.16 KB
/
main.cpp.save-failed
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
#include <iostream>
#include <time.h>
#include <string>
#include <stdlib.h>
using namespace std;
// ---- GLOBAL VARIABLES ----
// console display
const string consoleSep = "-------------------------------";
const string consoleLine = "* * * * * *";
// game choices
const int NUM_VALID_GAMES = 1;
const int MAX_GAME_NAME = 100;
string validGameChoices[][MAX_GAME_NAME] = {{"1", "Go Fish"}};
// playing games
string selectGame() {
string input;
cout << "\n" << consoleLine << endl;
cout << "Please select a game to play: " << endl;
for (int i = 1; i < NUM_VALID_GAMES + 1; i++) {
cout << i << ". " << validGameChoices[i-1][1] << endl;
}
cin >> input;
cout << consoleLine << "\n" << endl;
return input;
}
void showGameChoice(string userChoice) {
cout << "You chose to play " << validGameChoices[stoi(userChoice)-1][1] << "!\n";
cout << "\n" << consoleSep << endl;
}
void playGoFish() {
}
// game functionalities
void shuffleDeck(string deck[], int n) {
for (int i = 0; i < n; i++) {
int random = rand() % n;
string temp = deck[random];
deck[random] = deck[i];
deck[i] = temp;
}
}
void shuffleDeck(char** deck, int n) {
for (int i = 0; i < n; i++) {
int random = rand() % n;
char* temp = deck[random];
deck[random] = deck[i];
deck[i] = temp;
}
}
bool checkValidGameChoice(string userChoice) {
for (int i = 0; i < NUM_VALID_GAMES; i++) {
if (validGameChoices[i][0] == userChoice) {
return true;
}
}
return false;
}
// functional utils
void fillCard(char* card, char val1, char val2, char suit) {
card[0] = val1, card[1] = val2, card[2] = suit;
// cout << card[0] << card[1] << card[2] << " ";
}
bool strArrayContains(string arr[], int n, string elem) {
for (int i = 0; i < n; i++) {
if (arr[i] == elem) {
return true;
}
}
return false;
}
// testing utils
void printStrArray(string arr[], int n) {
for (int i = 0; i < n; i++) {
cout << "\"" << arr[i] << "\" ";
// cout << arr[i] << " ";
}
printf("\n");
}
void printArray(char** ptr, int n) {
for (int i = 0; i < n; i++) {
cout << *(ptr+i) << " ";
}
printf("\n");
}
int main()
{
// ---- CONSTANTS ----
const int NUM_SUITS = 4;
const int NUM_NUM_CARDS = 9;
const int NUM_FACE_CARDS = 4;
const int NUM_VALS = NUM_NUM_CARDS + NUM_FACE_CARDS;
const int DECK_SIZE = NUM_SUITS * NUM_VALS;
// ---- CREATING CARD DECK ----
// card suits and face cards
char **cardSuits = (char**)malloc(NUM_SUITS * sizeof(char*));
cardSuits[0] = "S", cardSuits[1] = "H", cardSuits[2] = "D", cardSuits[3] = "C";
char **faceCards = (char**)malloc(NUM_FACE_CARDS * sizeof(char*));
faceCards[0] = "J", faceCards[1] = "Q", faceCards[2] = "K", faceCards[3] = "A";
// creating card deck
char **cardDeck = (char**)malloc(DECK_SIZE * sizeof(char*));
if (cardDeck == NULL) {
cout << "no more space" << endl;
return -1;
}
for (int i = 0; i < DECK_SIZE; i++) {
cardDeck[i] = (char*)malloc(3 * sizeof(char));
}
int cdCount = 0;
for (int i = 0; i < NUM_SUITS; i++) {
char currSuit = cardSuits[i][0];
for (int j = 2; j < 10; j++) {
fillCard(cardDeck[cdCount], '0', to_string(j)[0], currSuit);
cdCount++;
}
fillCard(cardDeck[cdCount], '1', '0', currSuit);
cdCount++;
for (int j = 0; j < NUM_FACE_CARDS; j++) {
fillCard(cardDeck[cdCount], '0', faceCards[j][0], currSuit);
cdCount++;
}
}
cout << "\ncardDeck" << endl;
printArray(cardDeck, DECK_SIZE);
// ---- PREP FOR GAME ----
// shuffle deck
srand(time(0));
shuffleDeck(cardDeck, DECK_SIZE);
cout << "\nshuffled deck" << endl;
printArray(cardDeck,DECK_SIZE);
// select game to play
string userChoice = selectGame();
while (!checkValidGameChoice(userChoice)) {
cout << "Invalid choice" << endl;
userChoice = selectGame();
}
showGameChoice(userChoice);
// deallocating memory
free(cardSuits);
free(faceCards);
free(cardDeck);
return 0;
}