Hello, when I run my code I have errors and I need help fixing them here is my code:

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Hello, when I run my code I have errors and I need help fixing them here is my code:

Post by answerhappygod »

Hello, when I run my code I have errors and I need help fixing
them here is my code:
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 1
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 1 (16.55 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 2
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 2 (21.79 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 3
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 3 (18.93 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 4
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 4 (21.61 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 5
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 5 (20.29 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 6
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 6 (21.37 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 7
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 7 (24.87 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 8
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 8 (21.31 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 9
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 9 (18.94 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 10
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 10 (18.04 KiB) Viewed 57 times
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 11
Hello When I Run My Code I Have Errors And I Need Help Fixing Them Here Is My Code 11 (1.92 KiB) Viewed 57 times
#include <iostream- #include <vector> #include <string> #include <cstdlib> using namespace std; string playerName() // Definition of the function that takes player name as an input { string name; cout << "Welcome to the game B.S.! Please enter your name:"; // isking user to enter their name getline (cin, nare); // Storing "name" in a variable return name; // Returning the name of the player enu Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING }; enun Suit { SPADES, HEARTS, DIAMOND, CLUBS }; struct Card { int num ranks = 13; int num_suits = 4; Rank rank; Suit suit; struct Deck // Defines what a deck is K vector<Card> cards; int max_deck_size = 52; struct Player // Defines what a player is { string nane; vector<Card> hand; vector<int> cards_to_place;

bool BS = false; int score = 0; struct Gare // Defines what a game is { vector<Player> players; Deck deck; int num_players = 4; int num_cards_per_player = 13; void initialize(Decke); void print_deck(const Decke); void print_cardí const Card); void shuffle (Decke); bool deal_cards (Gerec); void print_hand (const vector<Card>$) ; void initialize(Games, string*, int); void add_players (Game &, string*, int); void print_hands (const Games) ; void play(Game&, string*, int); Rank get_rank(const vector<Card>&); void display_state (const Game); void get_cards (Games) ; void place_cards (Game); void get_score (Garne); int main() { srand(time(NULL)); string Pname [4] ; // Initializing an array that stores names for 4 players for (int i = 0; i < 4; it) { Prame[1] playerName(); // Getting the names of each player by calling the "playerName" function } int ran = rand() * 4; // Generating a random integer from 0 to 3 cout < "\n"; cout << Pname[ran] «" you are going first. Good luck!" < "\n" < endl; // Printing the players name with the index of the random integer

Game game: play (game, Pname, ran); } void initialize (Decke deck)// Creates a deck of cards as a vector Card card; for (int suit = 0; suit < card.num_suits; suit++) { for (int rank 0; rank < card.num_ranks; rank++) { card.suit - Suit (suit); card.rank = Rank(rank) ; deck.cards. push_back(card); } void print_deckiconst Decke deck)// Prints out a whole deck of cards { for (Card c : deck.cards) { print_cardic); } } void print_card(const Card& card)// Prints a card's suit and value IF PP cout << "Rank = << card.rank << " " << "Suit = < card.suit < '\n'; } void shuffle (Decke deck)// Shuffles the deck of cards to deal to the players { Deck shuffled; while (!deck.cards. empty()) { size_t rand_index - rand() * deck.cards.size(); shuffled.cards.push_backideck.cards[rand_index] ) ; deck.cards.erase (deck.cards.begin() + rand_index); } deck = shuffled; }

bool deal_cards (Gamec gane) // Deals the cards to the 4 players size_t cards Delt = game.nun_players * game.num_cards_per_player; // makes sure to only deal 13 cards to each player if (game.deck.cards.size() < cardsDelt) { return false; } for (int card = 0; card < game.nur_cards_per_player; card++; //the cards being delt mst not go over the amount of cards each player can have if it does the loop ends { for (int player = 0; player < game.num_players; playerh} { game .players[player] .hand.push_back(game.deck.cards[0]); game.deck.cards.erase (game.deck.cards.begin()); return true; } void print_hand (const vector<Card>& hand// Prints the hand of a player { int x = 1; for (Card c : hand) { cout << "Card " < x < print_cardic): x+; '* Takes in the names as an array along with the position from where to add the player names The position is determined randomly during the start of the game. "game" Game object representing the current games "names" pointer to string array representing the names of the players "startPos" position to start in the names array to add the players in the players vector in the game object */

void initialize(Gemee game, string* names, int startPos) // Initializes the game by initializing the deck, shuflling the deck, and adding players { initialize (genre.deck) ; shuffle (game.deck); add_players ( game, names, startPos); } void add_players (Game & game, stringt names, int startPos) // Adds players to the game and has the players are stored in the vector { int idx = 0; for (int player = 0; player < game.num_players; player+) { Player new_player; new_player.name = names [ (startPos + idx) game. num_players] ; idx ++; game.players. push_back(new_player); } } void print_hands (const Gamec game) { for (int player = 0; player < game.num_players; playerH} { print_hand (game. players [player] .hand) ; cout << endl; } } void get_score (Gaec game, size_t player) { for (int i = 0; i < 4; if} { game.players[ i].score = game .players.hand.size(); } } void display_state(const Gamec game, size_t player) { for (int i = 0; i < 4; i++)| { cout << game. players .nare «"'s hand: \n"; print_hand (game.players .hand) : = =

Ils score:" « game.players.score cout << game. players .name << endl; } } void get_cards (Game & gare, size_t player) // this function allows a player to select which cards they would like to place from their hand { bool done = false; = bool alreadyPlaced = false; size t x = 0; char y = 'a'; size_t handSize = game .players(player) .hand.size(); game.players [player] .cards_to_place.clear(); // clears out any previous values enetered in the vector while (!done) { size_t cardsPSize = game.players[player].cards_to_place.size(); alreadyPlaced = false; cout << "Enter what card you would like to place: "; cin >> X; for (size_t i = 0; i < cardsPSize; i++) { size_t CardPlaced = gare .players [player].cards_to_place.at(i); if (x CardPlaced) { alreadyPlaced = true; } } if (x > handSize || X <= 0 || alreadyPlaced true) // Checks to see if the give card is a valid option { cout << "That card is not in your hand, please select a card that is in your hand \n"; } else if (x < handSize ee X0 && alreadyPlaced false) // executes when the card is valid { gare .players[player].cards_to_place.push_backix}; //enters in the card position into a vector ==

cout << "\n" << "If you are done selecting cards type 'd' to continue type any other letter: \n"; cin >> y; if (Y 'd' || y == 'D') // once the player is done selecting cards the loop ends { done = true; } } } void place_cards (Gares game, size_t player) //searches through the players hand to find the cards that need to be placed and places them into the deck size_t cardIndex = 0; Card current Card; vector<Card> temp Vector = game.players[player] . hand; // makes a copy of the player's hand for (size_t i = 0; i < gare .players[player].cards_to_place.size(); it) { cardIndex = game.players[player].cards_to_place. at(i); // retrieves the value of the vector which gives a card position currentCard = tempVector.at (cardIndex); //this finds the card that needs to be placed into the deck game.deck.cards.push_back(current Card) ; // places the card into the deck for (size_t j = 0; j < game. players[player] .hand.size(); j++} // searches the players hand to find the card that is being placed { if (currentCard.rank == game. players(player) .hand.at (j).rank ce currentCard.suit game.players[player] . hand. at (j).suit) //If the card being placed currently is equal to the card in the player's hand { = . gane.players[player] .hand. erase (game.players[player] .hand.begin() +j); // erases the card from the player's hand at the given index } }

} void call_BS (Gemee game, size_t player) // isks the player if they would like to call the bluff char bsCall = 'a'; cout < game.players[player].cards_to_place.size() << " cardis) were placed.\n"; //outputs how many cards were placed for (size_t i = 0; i < 4; i+} //takes inputs from all four players { cout << game. players .name « " would you like to call a bluff enter 'y' for yes or 'n' for no \n"; cin >> bsCall; if (bsCall 'y' || bsCall == 'Y') { game.players.BS = true; } == else { game .players.BS = false; } bool check_bluff (Gerne & game, size_t player, int currentRank) // checks to see if the bluff call was true or false { size_t findCards = game.players[player] .cards_to_place.size(); //gets the amount of cards placed down previously bool bluff = false; vector<Card> tempVector; tempVector.clear(); for (size_t i = 0; i < findCards; i++) { tempVector.at (i) game.deck.cards.at(i); //populating the vector with the cards placed } for (size_t i = 0; i < temp Vector.size(); i++) { if(tempVector.at(i).rank == currentRank) // checks each card to see if it matches the current rank { = =

bluff = false; } else // if the rank does not match then the bluff is true and the loor breaks bluff = true; break; } return bluff; } bool wasBluffCalled (Gane game) // if a bluff was called a true value is returned and if it wasn't a false value is returned { bool wasCalled = false; //initializes the call to false for (size_t i = 0; i < 4; i++) { if (game.players .BS) //check each player to see if "BS" is true if it is the loop breaks { wasCalled = true; break; } return wasCalled; } void give_deck (Game game, size_t PlayerWhoCalled} // gives the deck to either the player calling the bluff or the player who bluffed { vector<Card> NewHand (game.players[Player WhoCalled) .hand) ; NewHand. insert (NewHand.end(), game.deck.cards.begin(), game.deck.cards.end()); NewHand = game.players [PlayerWhoCalled] . hand; game.deck.cards.clear(); NewHand.clear(); } void play(Game & game, string* names, int pos) { initialize (genre, names, pos); deal_cards (game);

bool game_over = false; size_t player = 0; int currentRank = 0; while (!game_over) { bool bluff = false; size_t player_who_called = 0; if (player == 4) { player = 0; } if (currentRank 13) { == currentRank = 0; } get_score(game, player); cout < game. players[player] .name << " it is your turn!" << endl; cout << "The current Rank is: << current Rank << endl; display_state (game, player); get_cards (game, player) ; place_cards (game, player); get_score(game, player) ; display_state (game, player); call BS ( game, player); was Bluff Called(game); //checks to see if a bluff was called or not if (wasBluffCalled (game) true) { for (size_t i = 0; i < 4; i++) { if igane.players . BS == true) { bluff = check_bluff (game, player_who_called, currentRank) : == if (bluff == false) { give_deck (game, player_who_called) ; } else if (bluff == true) { give_deckigame, player);

} currentRank current Rank + 1; player player + 1; = =
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply