Can someone please add a computer player in here somehow? From there, I can prob
ID: 3768415 • Letter: C
Question
Can someone please add a computer player in here somehow?
From there, I can probably just add that to the go fish game part.
Thank you so much!
#include<string>
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
struct Card {
int number;
string suit;
};
class Deck {
public:
Deck();
~Deck();
void initializeDeck(Deck *ocean);
void shuffle(Deck *ocean);
Card getTopCard(vector*);
vector cards;
int cardsInPlay;
vector discard;
};
class Player {
public:
Player();
~Player();
vector hand;
Card goFish();
Card discard();
Card getCard(Deck);
int number;
};
class Game {
public:
Game();
~Game();
vector p;
int ask(Player *, Player *, int);
void playGame(Game *, vector*, Deck *);
void computer(Game *, vector*, Deck *);
void deal(vector*, Deck*);
char win;
int getP();
void setP(int);
private:
int players;
};
int errorCheck(int);
int getPlayers();
void printHand(Player);
int check1(int, int, int);
int check2(int);
void checkForSet(Player *, Deck *);
void checkForWin(Player *);
Game::Game() {
}
Game::~Game() {
}
Deck::Deck() {
}
Deck::~Deck() {
}
Player::Player() {
}
Player::~Player() {
}
int errorCheck(int a) {
int valid = 1;
if (cin.fail()) {
cin.clear();
cin.ignore(1000, ' ');
valid = 0;
}
return valid;
}
void printHand(Player p) {
cout << endl;
cout << "Player " << p.number << ", this is what you have: " << endl;
for (int i = 0; i < p.hand.size(); i++)
cout << p.hand.at(i).number << " of " << p.hand.at(i).suit << endl;
cout << endl;
}
int getPlayers() {
int players, valid = 0;
cout << "How many people will be playing this round of Go Fish? ";
while (valid == 0) {
cin >> players;
valid = errorCheck(players);
if (valid == 0 || players < 2 || players > 8) {
cout << "Please enter a number between 2 and 7. ";
valid = 0;
}
}
return players;
}
int Game::getP() {
return this->players;
}
void Game::setP(int players) {
this->players = players;
}
void Deck::initializeDeck(Deck *ocean) {
ocean->cardsInPlay = 52;
string suitstring;
int i = 0;
for (int suit = 1; suit <= 4; suit++) {
if (suit == 1)
suitstring = "Hearts";
else if (suit == 2)
suitstring = "Spades";
else if (suit == 3)
suitstring = "Diamonds";
else
suitstring = "Clubs";
for (int card = 1; card <= 13; card++) {
ocean->cards.at(i).suit = suitstring;
ocean->cards.at(i).number = card;
i++;
}
}
}
void Deck::shuffle(Deck *ocean) {
int i, j, a;
Card temp;
srand(time(NULL));
a = ocean->cardsInPlay;
for (int b = 0; b < 4; b++) {
for (i = 0; i < a; i++) {
j = rand() % a;
temp = ocean->cards.at(i);
ocean->cards.at(i) = ocean->cards.at(j);
ocean->cards.at(j) = temp;
}
}
}
Card Deck::getTopCard(vector *cards) {
Card a = cards->at(0);
cards->erase(cards->begin());
if (cards->size() == 0) {
cout << "No more cards. Game over!" << endl;
exit(1);
}
return a;
}
void Game::deal(vector *p, Deck *ocean) {
Card a;
for (int i = 0; i < p->size(); i++) {
for (int j = 0; j < 7; j++) {
a = ocean->getTopCard(&ocean->cards);
p->at(i).hand.push_back(a);
}
}
}
int check1(int valid, int i, int players) {
int pBeingAsked;
while (valid == 0) {
cin >> pBeingAsked;
valid = errorCheck(pBeingAsked);
if (pBeingAsked <= 0 || pBeingAsked > players || pBeingAsked == i + 1) {
valid = 0;
cout << "Please enter a valid player!" << endl;
}
}
return pBeingAsked;
}
int check2(int valid) {
int cardAskedFor;
while (valid == 0) {
cin >> cardAskedFor;
valid = errorCheck(cardAskedFor);
if (cardAskedFor < 1 || cardAskedFor > 13) {
valid = 0;
cout << "You didn't enter a valid card number! Please enter a number between 1 and 13. " << endl;
}
}
return cardAskedFor;
}
void Game::playGame(Game *goFish, vector *p, Deck *ocean) {
int pBeingAsked, cardAskedFor, cardsBack;
while (1) {
int i = 0;
for (i = 0; i < goFish->players; i++) {
printHand(p->at(i));
cout << "Player " << i + 1
<< ", which player would you like to ask for their cards? ";
pBeingAsked = check1(0, i, goFish->players);
cout << "Player " << i + 1
<< ", what number do you want to ask for? ";
cardAskedFor = check2(0);
cardsBack = ask(&p->at(i), &p->at(pBeingAsked - 1), cardAskedFor);
if (cardsBack == 0)
p->at(i).hand.push_back(ocean->getTopCard(&ocean->cards));
checkForSet(&p->at(i), ocean);
checkForWin(&p->at(i));
}
}
}
void checkForSet(Player *p, Deck *ocean) {
int count = 0;
for (int j = 0; j <= 13; j++) {
count = 0;
for (int i = 0; i < p->hand.size(); i++) {
if (p->hand.at(i).number == j) {
count++;
}
if (count == 4) {
for (int k = 0; k < p->hand.size(); k++) {
if (p->hand.at(k).number == j) {
ocean->discard.push_back(p->hand.at(k));
p->hand.erase(p->hand.begin() + k);
k--;
i--;
}
}
cout << "You got a set! Congratulations!" << endl;
}
}
}
}
int Game::ask(Player *asking, Player *beingAsked, int cardAskedFor) {
int num = 0;
for (int i = 0; i < beingAsked->hand.size(); i++) {
if (beingAsked->hand.at(i).number == cardAskedFor) {
asking->hand.push_back(beingAsked->hand.at(i));
beingAsked->hand.erase(beingAsked->hand.begin() + i);
i--;
num++;
}
}
cout << "Player " << beingAsked->number << " had " << num << " "
<< cardAskedFor << "'s." << endl << endl;
return num;
}
void checkForWin(Player *p) {
if (p->hand.size() == 0) {
cout << "Player " << p->number << " wins!! Congratulations!" << endl;
exit(1);
}
}
void Game::computer(Game *goFish, vector *p, Deck *ocean) {
int pBeingAsked, cardAskedFor, cardsBack;
while (1) {
int i = 0;
for (i = 0; i < goFish->players; i++) {
printHand(p->at(i));
cout << "Player " << i + 1
<< ", which player would you like to ask for their cards? ";
pBeingAsked = check1(0, i, goFish->players);
cout << "Player " << i + 1
<< ", what number do you want to ask for? ";
cardAskedFor = check2(0);
cardsBack = ask(&p->at(i), &p->at(pBeingAsked - 1), cardAskedFor);
if (cardsBack == 0)
p->at(i).hand.push_back(ocean->getTopCard(&ocean->cards));
checkForSet(&p->at(i), ocean);
checkForWin(&p->at(i));
}
}
}
int main() {
Game goFish = Game();
Deck ocean = Deck();
ocean.cards.resize(52);
ocean.initializeDeck(&ocean);
ocean.shuffle(&ocean);
int a = getPlayers();
goFish.p.resize(a);
for (int i = 0; i < a; i++)
goFish.p.at(i).number = i + 1;
int players = goFish.getP();
goFish.setP(a);
goFish.deal(&goFish.p, &ocean);
goFish.playGame(&goFish, &goFish.p, &ocean);
return 0;
}
Explanation / Answer
Iam giving full code with description to the program by comment line includes on the program check it
#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; const int SIZE = 32; int ocean[SIZE]; int oceanPos; void shuffle( int deck[], int& position ); void initialize( int deck[] ); void drawCard ( int deck[], int& position, int hand[], int& handSize, int &drawn ); int countRank( int rank, int hand[], int handSize ); void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize ); void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] ); void humanTurn ( int deck[], int humanHand[], int computerHand[], int& position, int& humanHandSize, int& computerHandSize, int& score ); void computerTurn ( int deck[], int computerHand[], int humanHand[], int& position, int& computerHandSize, int& humanHandSize, int& score ); int main() { char yesOrNo = 'y'; while ( yesOrNo == 'y' || yesOrNo == 'Y' ) { // initialize deck initialize ( ocean ); // shuffle deck shuffle( ocean, oceanPos ); // initial deal int humanHand[50]; int humanHandSize = 0; int computerHand[50]; int computerHandSize = 0; int drawn; int humanScore = 0; int computerScore = 0; int i; for ( i = 0; i < 5; i++ ) { drawCard( ocean, oceanPos, humanHand, humanHandSize, drawn ); } for ( i = 0; i < 5; i++ ) { drawCard( ocean, oceanPos, computerHand, computerHandSize, drawn ); } for ( i = 0; i < humanHandSize; i++ ) { cout << humanHand[i] << " "; } for ( i = 0; i < computerHandSize; i++ ) { cout << computerHand[i] << " "; } bool breaking = false; while ( oceanPos != 3 && humanHandSize != 0 && computerHandSize != 0 && !breaking ) { // human turn humanTurn ( ocean, humanHand, computerHand, oceanPos, humanHandSize, computerHandSize, humanScore ); if ( computerHandSize == 0 || oceanPos == 31 ) { breaking = true; cout << "Game Over." << endl; } else { //computer turn computerTurn ( ocean, computerHand, humanHand, oceanPos,computerHandSize, humanHandSize, computerScore ); } if ( humanHandSize == 0 || oceanPos == 31 ) { breaking = true; cout << "Game Over." << endl; } } cout << "*Final Score*" << endl; cout << "You: " << humanScore << endl; cout << "Computer: " << computerScore << endl; cout << "Would you like to play again? (y/n) "; cin >> yesOrNo; } } void initialize ( int deck[] ) { int i; for ( i = 0; i < SIZE; i++ ) { deck[i] = 2 + ( i % 8 ); } } void shuffle ( int deck[], int& position ) { int i; int j; int temp; for ( i = 0; i < SIZE; i++ ) { j = 1 + rand() % 32; temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; } position = 0; } void drawCard ( int deck[], int& position, int hand[], int& handSize, int &drawn ) { handSize++; hand[handSize-1] = deck[position]; drawn = hand[handSize-1]; position++; } int countRank ( int rank, int hand[], int handSize ) { int i; int count = 0; for ( i = 0; i < handSize; i++ ) { if ( hand[i] == rank ) { count++; } } return count; } void stealCards ( int rank, int from[], int& fromSize, int to[], int& toSize ) { int i; for ( i = 0; i < fromSize; i++ ) { if ( from[i] == rank ) { toSize++; to[toSize-1] = rank; from[i] = from[fromSize-1]; fromSize--; } } } void checkSet ( int rank, int hand[], int& handSize, int& score, char pronoun[] ) { int count = countRank( rank, hand, handSize ); int i; if ( count == 4 ) { for ( i = 0; i < handSize; i++ ) { if ( hand[i] == rank ) { hand[i] = hand[handSize - 1]; handSize--; } } score++; cout << pronoun << " scored a set!" << endl; } } void humanTurn ( int deck[], int humanHand[], int computerHand[], int& position, int& humanHandSize, int& computerHandSize, int& score ) { int count = 1; char noun[] = "You"; bool breaking = false; while ( count > 0 && !breaking ) { int i; cout << "You have: "; for ( i = 0; i < humanHandSize; i++ ) { cout << humanHand[i] << " "; } cout << endl; int guess; cout << "You ask if I have any "; cin >> guess; cout << endl; count = countRank ( guess, computerHand, computerHandSize ); stealCards ( guess, computerHand, computerHandSize, humanHand, humanHandSize ); if ( count > 0 ) { cout << "Yes I do. I have " << count << endl; } checkSet ( guess, humanHand, humanHandSize, score, noun ); if ( computerHandSize == 0 ) { breaking = true; } } if ( count == 0 ) { cout << "No I don't. Go Fish!" << endl; int drawn; drawCard ( deck, position, humanHand, humanHandSize, drawn ); cout << "You draw a " << drawn << "." << endl; cout << endl; checkSet ( drawn, humanHand, humanHandSize, score, noun ); if ( position != 0 ) { cout << "My turn!" << endl; cout << endl; } } } void computerTurn ( int deck[], int computerHand[], int humanHand[], int& position, int& computerHandSize, int& humanHandSize, int& score ) { int count = 1; char noun[] = "I"; bool breaking = false; while ( count > 0 && !breaking ) { int i; cout << "You have: "; for ( i = 0; i < humanHandSize; i++ ) { cout << humanHand[i] << " "; } cout << endl; int guess = 2 + rand() % 8; cout << "I ask: Do you have any " << guess << "'s?" << endl; cout << endl; count = countRank ( guess, humanHand, humanHandSize ); stealCards ( guess, humanHand, humanHandSize, computerHand, computerHandSize ); if ( count > 0 ) { cout << "Yes you do. You have " << count << endl; } checkSet ( guess, computerHand, computerHandSize, score, noun ); if ( humanHandSize == 0 ) { breaking = true; } } if ( count == 0 ) { cout << "No you don't. I will Go Fish." << endl; int drawn; drawCard ( deck, position, computerHand, computerHandSize, drawn ); checkSet ( drawn, computerHand, computerHandSize, score, noun ); if ( position != 0 ) { cout << "Your turn" << endl; cout << endl; } } }