Question

In: Computer Science

This program needs to be in C++. I have barely gotten anywhere but am already stuck....

This program needs to be in C++. I have barely gotten anywhere but am already stuck. Any help with this one would be greatly appreciated.

Write a program that simulates playing a simplified version of the game "Candy Land." In "Candy Land" players take turns drawing cards that have a colored square on them. The player then travels on the board according to the color on the card. The first person to reach the end of the board wins.

In this version we will practice using arrays and enums. We will use an array of ints to represent the players of the game. You should have an int array of size 4, then you will prompt the user for the number of players from 2 - 4 and this will determine how much of the array you will use. You will create an enum type CARD. The values of the enum will be the possible color of the card. Value 0 should represent an empty deck, and you should have 6 colors (RED, PINK, ...).

To draw a card you will read a number from cards.txt. Write a function that takes an ifstream as an input parameter and returns a CARD. Read a number from the file and convert the number to a variable of the CARD type.

After prompting for the number of players use a loop to draw cards for each player. After drawing a card, move the player by adding the card value to the player total. The game will end either when the first player reaches 20 or there are no more cards (input file reaches the end). The winner is the first player to have a score of 20 or more, or the player with the most points after all the cards have been drawn.

The program should output for each player's turn the current player, the card they drew, how many spaces they move, and their location after moving (total score).

Solutions

Expert Solution

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// CARD enum
enum CARD {EMPTY, RED, PINK, BLUE, GREEN, YELLOW, BLACK};

// function prototype
CARD getCard(ifstream& infile);

int main()
{
   int players[4] = {0}; // array of players (stores players total)

   int number_of_players;
   // getting number of players
   cout << "How many players(2-4)?: ";
   cin >> number_of_players;

   // input file
   ifstream infile;
   infile.open("cards.txt");

   int player_turn = 1; // first player
   int winner = 0;
   while (1)
   {
       if (infile.eof()) // if end of file reached
       {
           break;
       }

       cout << "\nPlayer " << player_turn << "'s turn!" << endl;
       // getting card
       enum CARD card = getCard(infile);

       // getting cards color
       string color;
       switch(card)
       {
           case 1:
               color = "RED";
               break;
           case 2:
               color = "PINK";
               break;
           case 3:
               color = "BLUE";
               break;
           case 4:
               color = "GREEN";
               break;
           case 5:
               color = "YELLOW";
               break;
           case 6:
               color = "BLACK";
               break;
       }

       cout << "Player " << player_turn << " drew a " << color << " card." << endl;
       cout <<"Player " << player_turn << " moves " << card << " spaces." << endl;

       players[player_turn-1] += card;
       cout << "Player " << player_turn << " is at " << players[player_turn-1] << " position." << endl;

       if (players[player_turn-1] >= 20)
       {
           winner = player_turn-1;
           break;
       }

       if (player_turn == number_of_players)
       {
           player_turn = 1;
       }
       else
       {
           player_turn++;
       }
   }

   // priting winner
   if (winner > 0) // winner who got 20 or more
   {
       cout << "\n\nPlayer " << winner << " wins!" << endl;
   }
   else
   {
       // finding the index of maximum scorer
       int max = 0;
       for (int i = 1; i < number_of_players; i++)
       {
           if (players[i] > players[max])
           {
               max = i;
           }
       }

       cout << "\n\nPlayer " << (max + 1) << " wins!" << endl;
   }

   return 0;
}

CARD getCard(ifstream& infile)
{
   int i;
   infile >> i;
   enum CARD c = static_cast<CARD>(i); // casting integer to enum
   return c;
}

// cards.txt

// OUTPUT

// skipping to the end


Related Solutions

I am stuck on this problem and I am not sure what the solution is. In...
I am stuck on this problem and I am not sure what the solution is. In C Write item.h and item.c. In item.h, typedef a struct (of type t_item) which contains the following information: t_item: char name[MAX_ITEM_NAME_STRING]; char description[MAX_ITEM_DESCRIPTION_STRING]; Make sure that MAX_ITEM_NAME_STRING and MAX_ITEM_DESCRIPTION_STRING are defined with suitable sizes in your item.h. Typical values are, 25 and 80, respectively. Add the following interface definition to item.h: int item_load_items(t_item items[], int max_items, char *filename); Returns the number of objects loaded...
Hello, I stuck doing c++ program The program will be recieved from the user as input...
Hello, I stuck doing c++ program The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to...
using C , comments will be appreciated. I already posted this before and I am looking...
using C , comments will be appreciated. I already posted this before and I am looking for different answer. please answer both part of the question. A) Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating point number number1is less than, equal to or greater than another floating point number number2, by simply comparing their floating point representations bitwise from left to right, stopping as soon as the first differingbit is encountered. The fact that...
Hello this is for C++ language. I am currently stuck on creating my api for Day...
Hello this is for C++ language. I am currently stuck on creating my api for Day Trading Stocks. as follows I need an api for *//Function Signature * * parameter: * * Return Value: ** *// Write the following function taking in an integer vector (vector &prices) consisting of all prices, in chronological order, for an hypothetical instrument. Your function recommends the maximum profit an investor can make by placing AT MOST one buy and one sell order in the...
Hi! I am in an intro level Finance course and I am stuck on this problem....
Hi! I am in an intro level Finance course and I am stuck on this problem. Any help would be greatly appreciated. I am deciding on opening a restaurant. I was able to scrape together some capital from friends and family, but I must pay them back in 4 years at 12% per annum. I figure that it will cost me $165,000 to start up with rent, deposits, equipment, salaries, chicken, basil, rice, etc. for the first year, but I...
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose...
C# Yahtzee Program I am creating a Yahtzee program where you have the option to choose 5 or more dice that will be rolled. I just need help trying to create a random dice roll and seeding it. Could you give me some code examples of getting a dice roll of a six sided die that won't display similar results.
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
I am stuck on approaching the answer to this question, and most of the answer that...
I am stuck on approaching the answer to this question, and most of the answer that I have found online and on Chegg are do not make any sense regarding this question. What would be a good interpretation of what this question is asking regarding the GDP? "Think about the increases since 2001 in spending for the Department of Homeland Security and the wars in Afghanistan and Iraq. These increases represent government expenditures that have increased GDP. Explain whether you...
Kind of stuck on this one with the JE. Need to know if I am on...
Kind of stuck on this one with the JE. Need to know if I am on the right track with this problem. Please help Thomas sells products that carry a 6-month manufacturer’s warranty. Customers have the opportunity at the time of purchase to also buy a 3 year extended warranty for an additional charge of $100. Thomas sold 1,000 units this year for $2,000 each. For this year’s sales, Thomas estimates the 6-month warranty costs to be 10% of sales....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT