Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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*));
Expand All @@ -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;
Expand All @@ -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++) {
Expand All @@ -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;
}
42 changes: 42 additions & 0 deletions CrazyEights/CrazyEights.h
Original file line number Diff line number Diff line change
@@ -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]);
}
}
11 changes: 3 additions & 8 deletions GoFish/GoFish.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down
22 changes: 12 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "precompiled_headers/pch.h"
#include "Common/Common.h"
#include "GoFish/GoFish.h"
#include "CrazyEights/CrazyEights.h"

/*
TO DO TESTING
Expand Down Expand Up @@ -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;
Expand Down
Binary file modified main.exe
Binary file not shown.