diff --git a/Common/Common.h b/Common/Common.h index 549f60f..86f04f3 100644 --- a/Common/Common.h +++ b/Common/Common.h @@ -7,9 +7,9 @@ using namespace std; const string consoleSep = "-------------------------------"; const string consoleLine = "* * * * * *"; // game selection -const int NUM_VALID_GAMES = 1; +const int NUM_VALID_GAMES = 2; const int MAX_GAME_NAME = 50; -string validGameChoices[][MAX_GAME_NAME] = {{"1", "Go Fish"}}; +string validGameChoices[][MAX_GAME_NAME] = {{"1", "Go Fish"}, {"2", "Crazy Eights"}}; const string QUIT_PROG_CMD = "Q"; // game play const string COMP_NAME = "Computer"; @@ -70,6 +70,14 @@ void free2DArray(char** arr, int* size) { free2DArray(arr, *size); free(size); } +char* stringToCharArray(string s) { + int n = s.length(); + char* charArr = (char*)malloc(n * sizeof(char)); + for (int i = 0; i < n; i++) { + charArr[i] = s[i]; + } + return charArr; +} // PLAYING GAMES @@ -167,24 +175,6 @@ char** copyDeck(char** deck, int n) { printArray(copyDeck, n); return copyDeck; } -char*** dealCards(char** deck, int deckSize, int numToDeal) { - char*** dealtCards = (char***)malloc(2 * sizeof(char**)); - for (int i = 0; i < 2; i++) { - dealtCards[i] = (char**)malloc(CARD_SIZE * numToDeal * sizeof(char*)); - } - int p1 = 0, p2 = 0; - for (int i = 0; i < 2*numToDeal; i++) { - if (i%2 == 0) { - dealtCards[0][p1] = copyCard(deck[i]); - p1++; - } - else { - dealtCards[1][p2] = copyCard(deck[i]); - p2++; - } - } - return dealtCards; -} void addToCards(char** cards, int* n, char* card) { *n = *n + 1; cards = (char**)realloc(cards, (*n) * sizeof(char*)); @@ -207,8 +197,11 @@ bool checkValidCardRank(string r) { bool cardRanksEq(char* c1, char* c2) { return c1[0] == c2[0] && c1[1] == c2[1]; } +bool cardSuitsEq(char* c1, char* c2) { + return c1[2] == c2[2]; +} bool cardsEq(char* c1, char* c2) { - return cardRanksEq(c1, c2) && c1[2] == c2[2]; + return cardRanksEq(c1, c2) && cardSuitsEq(c1, c2); } void removeFromCards(char** cards, int* n, char** toRemove, int numToRemove) { int origN = *n; @@ -235,14 +228,37 @@ void removeFromCards(char** cards, int* n, char** toRemove, int numToRemove) { // cout << "just removed from cards" << endl; // printArray(cards, *n); } -char* removeTopFromCards(char** cards, int* n) { - char* toRemove = copyCard(cards[0]); +void removeSingleCard(char** cards, int* n, char* cardToRemove) { char** temp = (char**)malloc(sizeof(char*)); - temp[0] = copyCard(toRemove); - removeFromCards(cards, n, temp, 1); + temp[0] = copyCard(cardToRemove); + removeFromCards(cards, n , temp, 1); free2DArray(temp, 1); +} +char* removeTopFromCards(char** cards, int* n) { + char* toRemove = copyCard(cards[0]); + removeSingleCard(cards, n, toRemove); return toRemove; } +char*** dealCards(char** deck, int* deckSize, int numToDeal) { + char*** dealtCards = (char***)malloc(2 * sizeof(char**)); + for (int i = 0; i < 2; i++) { + dealtCards[i] = (char**)malloc(CARD_SIZE * numToDeal * sizeof(char*)); + } + int p1 = 0, p2 = 0; + for (int i = 0; i < 2*numToDeal; i++) { + if (i%2 == 0) { + dealtCards[0][p1] = copyCard(deck[i]); + p1++; + } + else { + dealtCards[1][p2] = copyCard(deck[i]); + p2++; + } + } + removeFromCards(deck, deckSize, copyDeck(dealtCards[0], numToDeal), numToDeal); + removeFromCards(deck, deckSize, copyDeck(dealtCards[1], numToDeal), numToDeal); + return dealtCards; +} int countCardsWithRank(char** cards, int n, char* cardRank) { int numCards = 0; for (int i = 0; i < n; i++) { @@ -262,4 +278,23 @@ bool rankInCards(char** cards, int n, char* cardRank) { } // printf("%c%c not in cards\n", cardRank[0], cardRank[1]); return false; +} +bool cardInCards(char** cards, int n, char* card) { + for (int i = 0; i < n; i++) { + if (cardsEq(cards[i], card)) { + return true; + } + } + return false; +} +char* chooseCardFromHand(char** userCards, int* numUserCards) { + string userAsk; + char* cardChosen; + do { + cout << "Please choose a card to play" << endl; + printArray(userCards, *numUserCards); + cin >> userAsk; + cardChosen = stringToCharArray(userAsk); + } while (!cardInCards(userCards, *numUserCards, cardChosen)); + return cardChosen; } \ No newline at end of file diff --git a/CrazyEights/CrazyEights.h b/CrazyEights/CrazyEights.h new file mode 100644 index 0000000..1f71dd1 --- /dev/null +++ b/CrazyEights/CrazyEights.h @@ -0,0 +1,42 @@ +#pragma once +#include "../precompiled_headers/pch.h" +#include "../Common/Common.h" + +using namespace std; + +// ---- GAME FUNCTIONS ---- +bool canPlayCard(char* lastCard, char* newCard) { + return cardRanksEq(lastCard, newCard) || cardSuitsEq(lastCard, newCard); +} + + +// ---- PLAYING CRAZY EIGHTS ---- +void playCrazyEights(char** stock) { + bool gameOver = false; + + const int DEAL_SIZE = 5; + int* stockSize = new int(DECK_SIZE); + + char*** dealtCards = dealCards(stock, stockSize, DEAL_SIZE); + char** userCards = dealtCards[0]; + char** compCards = dealtCards[1]; + int* numUserCards = new int(DEAL_SIZE); + int* numCompCards = new int(DEAL_SIZE); + + char* lastCardPlayed = new char[CARD_SIZE]; + + while (!gameOver) { + char* starterCard = removeTopFromCards(stock, stockSize); + lastCardPlayed = starterCard; + + printf("starter card is %c%c%c\n", starterCard[0], starterCard[1], starterCard[2]); + + // user's turn + char* userPlay; + do { + userPlay = chooseCardFromHand(userCards, numUserCards); + } while (!canPlayCard(lastCardPlayed, userPlay)); + removeSingleCard(userCards, numUserCards, userPlay); + printf("you chose to play %c%c%c\n", userPlay[0], userPlay[1], userPlay[2]); + } +} \ No newline at end of file diff --git a/GoFish/GoFish.h b/GoFish/GoFish.h index 63e0773..32c05fe 100644 --- a/GoFish/GoFish.h +++ b/GoFish/GoFish.h @@ -52,6 +52,7 @@ int removeCardsWithRank(char** cards, int* n, char* cardRank) { } } removeFromCards(cards, n, toRemove, numToRemove); + free2DArray(toRemove, numToRemove); return numToRemove; } void giveCardsWithRank(char** pCards, int* nP, char** opCards, int* opN, char* cardRank) { @@ -112,9 +113,7 @@ bool checkWinConditionGoFish(int numUserBooks, int numCompBooks, int stockSize) // ---- PLAYING GO FISH ---- -void playGoFish(char** origCardDeck, int origDeckSize) { - char** stock = copyDeck(origCardDeck, origDeckSize); - cout << "just made stock" << endl; +void playGoFish(char** stock) { int* stockSize = (int*)malloc(sizeof(int)); *stockSize = DECK_SIZE; bool gameOver = false; @@ -137,17 +136,13 @@ void playGoFish(char** origCardDeck, int origDeckSize) { string userName = "player"; // deal cards - char*** dealtCards = dealCards(stock, origDeckSize, DEAL_SIZE); + char*** dealtCards = dealCards(stock, stockSize, DEAL_SIZE); char** userCards = dealtCards[0]; char** compCards = dealtCards[1]; cout << "user's cards: " << endl; printArray(userCards, *numUserCards); cout << "computer's cards: " << endl; printArray(compCards, *numCompCards); - removeFromCards(stock, stockSize, userCards, *numUserCards); - removeFromCards(stock, stockSize, compCards, *numCompCards); - cout << "stock: " << endl; - printArray(stock, *stockSize); handleBookInCards(userCards, numUserCards, userBooks, numUserBooks, NULL_RANK); handleBookInCards(compCards, numCompCards, compBooks, numCompBooks, NULL_RANK); diff --git a/main.cpp b/main.cpp index 7392b63..a4a7c70 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include "precompiled_headers/pch.h" #include "Common/Common.h" #include "GoFish/GoFish.h" +#include "CrazyEights/CrazyEights.h" /* TO DO TESTING @@ -72,19 +73,20 @@ int main() cout << "\nshuffled deck" << endl; printArray(cardDeck,DECK_SIZE); - // // select game to play - // userChoice = selectGame(); - // while (!checkValidGameChoice(userChoice)) { - // cout << "Invalid choice" << endl; - // userChoice = selectGame(); - // } - // showGameChoice(userChoice); - /* TESTING PURPOSES */ - userChoice = "1"; + // select game to play + userChoice = selectGame(); + while (!checkValidGameChoice(userChoice)) { + cout << "Invalid choice" << endl; + userChoice = selectGame(); + } + showGameChoice(userChoice); // play game if (userChoice == "1") { - playGoFish(cardDeck, DECK_SIZE); + playGoFish(copyDeck(cardDeck, DECK_SIZE)); + } + else if (userChoice == "2") { + playCrazyEights(copyDeck(cardDeck, DECK_SIZE)); } else if (userChoice == QUIT_PROG_CMD) { cout << "Goodbye!" << endl; diff --git a/main.exe b/main.exe index ba66a92..bae02cd 100644 Binary files a/main.exe and b/main.exe differ