How do I get this to loop? It only gives the user, computer, and barbarian chara
ID: 3768697 • Letter: H
Question
How do I get this to loop? It only gives the user, computer, and barbarian character one chance to play, and it's over.
Thank you!
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
struct Card {
string rank;
string suit;
};
class Deck {
public:
void makeDeck(int deck[], int& size) {
deck[size];
for (int i = 0; i < size; i++) {
deck[i] = 2 + (i % 8);
}
}
void shuffle(int deck[], int& size) {
int i;
int k;
int temp;
for (i = 0; i < size; i++) {
k = 1 + (rand() % 32);
temp = deck[i];
deck[i] = deck[k];
deck[k] = temp;
}
}
};
class Hand {
public:
void createHand(int hand[], int deck[], int& deck_size, int& hand_size) {
int temp;
int counter = deck_size - 1;
for (int i = 0; i < hand_size; i++) {
temp = deck[counter];
hand[i] = temp;
counter--;
}
deck_size = counter;
}
void addCard(int hand[], int& hand_size, int card) {
hand[hand_size] = card;
hand_size = hand_size + 1;
}
void drawCard(int deck[], int hand[], int& hand_size, int& deck_size,
int& drawn) {
drawn = deck[deck_size - 1];
addCard(hand, hand_size, drawn);
deck_size = deck_size - 1;
}
void sortHand(int hand[], int hand_size) {
for (int i = 0; i < hand_size; i++) {
for (int j = i + 1; j < hand_size; j++) {
if (hand[i] > hand[j]) {
swap(hand[i], hand[j]);
}
}
}
}
void searchHand(int hand[], int& hand_size, int card, int& counter) {
for (int i = 0; i < hand_size; i++) {
if (hand[i] == card) {
counter++;
}
}
}
void deleteCard(int hand[], int& hand_size, int card) {
for (int i = 0; i < hand_size; i++) {
if (hand[i] == card) {
for (int j = i + 1; j < hand_size; j++) {
int temp = hand[j];
hand[i] = temp;
i++;
}
}
}
hand_size--;
}
};
class Player {
Hand hand;
public:
void userTurn(int deck[], int barbarianHand[], int computerHand[],
int userHand[], int& deckSize, int& barbarianHandSize,
int& computerHandSize, int& userHandSize, int& barbarian_score,
int& computer_score, int& user_score) {
hand.sortHand(userHand, userHandSize);
cout << "You have:";
for (int i = 0; i < userHandSize; i++)
cout << ' ' << userHand[i];
cout << ' ';
int times = 1;
int guess;
while (times > 0 && (computerHandSize > 0)) {
times = 0;
cout << "You ask if I have a .... " << endl;
cin >> guess;
int counter = 0;
hand.searchHand(computerHand, computerHandSize, guess, times);
if (times > 0) {
cout << "Yes I do. I have " << times << endl;
for (int k = 0; k < times; k++) {
hand.addCard(userHand, userHandSize, guess);
hand.deleteCard(computerHand, computerHandSize, guess);
}
hand.sortHand(userHand, userHandSize);
cout << "You have: ";
for (int i = 0; i < userHandSize; i++)
cout << userHand[i] << " ";
cout << ' ';
for (int j = 0; j < userHandSize; j++) {
if (userHand[j] == guess)
counter++;
}
if (counter == 4) {
cout << "You completed a set!" << endl;
for (int i = 0; i < 4; i++) {
hand.deleteCard(userHand, userHandSize, guess);
}
user_score++;
}
}
}
if (times == 0) {
int drawn_card;
cout << "No I don't! Go Fish!" << endl;
hand.drawCard(deck, userHand, userHandSize, deckSize, drawn_card);
cout << "You draw a " << drawn_card << endl;
int counter = 0;
for (int j = 0; j < userHandSize; j++) {
if (userHand[j] == drawn_card)
counter++;
}
if (counter == 4) {
cout << "You completed a set!" << endl;
for (int i = 0; i < 4; i++) {
hand.deleteCard(userHand, userHandSize, drawn_card);
}
user_score++;
}
cout << "My Turn!" << endl;
}
if (computerHandSize == 0)
cout << "Computer has no more cards!" << endl;
}
void computerTurn(int deck[], int barbarianHand[], int computerHand[],
int userHand[], int& deckSize, int& barbarianHandSize,
int& computerHandSize, int& userHandSize, int& barbarian_score,
int& computer_score, int& user_score) {
int times = 1;
while (times > 0) {
times = 0;
int guess = rand() % 7 + 2;
int counter = 0;
cout << "I ask: Do you have any ... " << guess << "'s?" << endl;
hand.searchHand(barbarianHand, barbarianHandSize, guess, times);
if (times > 0) {
cout << "You have " << times << endl;
for (int k = 0; k < times; k++) {
hand.addCard(computerHand, computerHandSize, guess);
hand.deleteCard(barbarianHand, barbarianHandSize, guess);
}
hand.sortHand(barbarianHand, barbarianHandSize);
cout << "You have: ";
for (int i = 0; i < barbarianHandSize; i++)
cout << barbarianHand[i] << " ";
cout << ' ';
for (int j = 0; j < computerHandSize; j++) {
if (computerHand[j] == guess)
counter++;
}
if (counter == 4) {
cout << "I completed a set!" << endl;
for (int i = 0; i < 4; i++) {
hand.deleteCard(computerHand, computerHandSize, guess);
}
computer_score++;
}
}
if (times == 0) {
int card;
cout << "You don't have any!" << endl;
hand.drawCard(deck, computerHand, computerHandSize, deckSize,
card);
cout << "I draw a card" << endl;
counter = 0;
for (int j = 0; j < computerHandSize; j++) {
if (computerHand[j] == card)
counter++;
}
if (counter == 4) {
cout << "I completed a set!" << endl;
for (int i = 0; i < 4; i++) {
hand.deleteCard(computerHand, computerHandSize, card);
}
computer_score++;
}
cout << "Barbarian turn!" << endl;
}
}
}
void barbarian(int deck[], int barbarianHand[], int computerHand[],
int userHand[], int& deckSize, int&barbarianHandSize,
int& computerHandSize, int& userHandSize, int& barbarian_score,
int& computer_score, int& user_score) {
int times = 1;
while (times > 0) {
times = 0;
int guess = rand() % 7 + 2;
int counter = 0;
cout << "Barbarian ask: Do you have any ... " << guess << "'s?" << endl;
hand.searchHand(userHand, userHandSize, guess, times);
if (times > 0) {
cout << "You have " << times << endl;
for (int k = 0; k < times; k++) {
hand.addCard(barbarianHand, barbarianHandSize, guess);
hand.deleteCard(userHand, userHandSize, guess);
}
hand.sortHand(userHand, userHandSize);
cout << "You have: ";
for (int i = 0; i < userHandSize; i++)
cout << userHand[i] << " ";
cout << ' ';
for (int j = 0; j < barbarianHandSize; j++) {
if (barbarianHand[j] == guess)
counter++;
}
if (counter == 4) {
cout << "Barbarian completed a set!" << endl;
for (int i = 0; i < 4; i++) {
hand.deleteCard(barbarianHand, barbarianHandSize, guess);
}
barbarian_score++;
}
}
if (times == 0) {
int card;
cout << "You don't have any!" << endl;
hand.drawCard(deck, computerHand, computerHandSize, deckSize,
card);
cout << "Barbarian draw a card" << endl;
counter = 0;
for (int j = 0; j < computerHandSize; j++) {
if (computerHand[j] == card)
counter++;
}
if (counter == 4) {
cout << "Barbarian completed a set!" << endl;
for (int i = 0; i < 4; i++) {
hand.deleteCard(computerHand, computerHandSize, card);
}
computer_score++;
}
cout << "Your turn!" << endl;
}
}
}
};
class Game {
public:
void playGoFish() {
Player player;
Deck decK;
Hand hand;
string play_again = "yes";
while (play_again == "yes") {
int deck[52];
int deckSize = 52;
int userHand[52];
int userHandSize = 7;
int computerHand[52];
int computerHandSize = 7;
int barbarianHand[52];
int barbarianHandSize = 7;
int user_score = 0;
int computer_score = 0;
int barbarian_score = 0;
decK.makeDeck(deck, deckSize);
decK.shuffle(deck, deckSize);
hand.createHand(userHand, deck, deckSize, userHandSize);
hand.createHand(computerHand, deck, deckSize, computerHandSize);
hand.createHand(barbarianHand, deck, deckSize, computerHandSize);
hand.sortHand(userHand, userHandSize);
hand.sortHand(computerHand, computerHandSize);
hand.sortHand(barbarianHand, barbarianHandSize);
cout << "Lets play a game of go fish" << endl;
while (deckSize != 0 && computerHandSize != 0 && userHandSize != 0 && barbarianHandSize != 0) {
player.userTurn(deck, barbarianHand, computerHand, userHand,
deckSize, barbarianHandSize, computerHandSize,
userHandSize, barbarian_score, computer_score, user_score);
if (computerHandSize == 0 || deckSize == 0) {
if (computerHandSize == 0)
cout << "Game Over! The Computer ran out of cards!" << endl;
if (deckSize == 0)
cout << "Game Over! The deck ran out of cards!" << endl;
} else
player.computerTurn(deck, barbarianHand, computerHand,
userHand, deckSize, barbarianHandSize,
computerHandSize, barbarian_score, userHandSize, computer_score,
user_score);
if (barbarianHandSize == 0 || deckSize == 0) {
if (barbarianHandSize == 0)
cout << "Game Over! Barbarian ran out of cards!" << endl;
if (deckSize == 0)
cout << "Game Over! The deck ran out of cards!" << endl;
} else
player.barbarian(deck, barbarianHand, computerHand,
userHand, deckSize, barbarianHandSize,
computerHandSize, userHandSize, barbarian_score,
computer_score, user_score);
if (userHandSize == 0 || deckSize == 0) {
if (userHandSize == 0)
cout << "Game Over! You ran out of cards!" << endl;
if (deckSize == 0)
cout << "Game Over! The deck ran out of cards!" << endl;
}
cout << "Final Score: " << endl;
cout << "Computer - " << computer_score << endl;
cout << "Barbarian - " << barbarian_score << endl;
cout << "You - " << user_score << endl;
cout << "Would you like to play again? Enter yes or no" << endl;
cin >> play_again;
}
}
}
};
int main() {
Game game;
game.playGoFish();
cout << ' ';
}
Explanation / Answer
In player class... you declared times =1 and in while loop you are using this
variable and inside this loop you are making times = 0. So, it will always executes
one time.
int times = 1;
int guess;
while (times > 0 && (computerHandSize > 0)) {
times = 0;
cout << "You ask if I have a .... " << endl;
cin >> guess;
int counter = 0;
hand.searchHand(computerHand, computerHandSize, guess, times);
.....
.....
.....
**************** If we initialize times variable, thst is how many times you want to repeat*****************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.