In: Computer Science
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).
#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