Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming - Game of UNO I need to code the card game UNO in C. I don\'t know

ID: 3710133 • Letter: C

Question

C Programming - Game of UNO

I need to code the card game UNO in C. I don't know how to set up the deck for the game. I am told in my project rubric to have a "an UNO deck of cards being implemented as a linked list". Linked lists confuse me and I don't know what to do. If you could code a deck of UNO cards using a linked list, that would be awesome! You can see below what I am being asked of regarding on how to setup the deck.

2.1 Setup 1. UNO cards are represented as variables of the following type typedef atzuct carda t char awst [7) char action [15) struct card-S ept: card You are allowed to add attributes to this definition, but not to remove any. You can represent colors hy using card suits Red: hearts; Yellow: diamonds Green: clubs; Blue: spades. The action field is used to denote the function of action cards 2. The game is played using the following deck of cards Figure 1: The deck of UNO cards. The following action cards are included Reverse If going clockwise, switch to counterclockwise or vice versa . Skip - When a player places this card, the next player has to skip their turn. If turned up at the beginning. the first player loeses his/her turn Draw Two When a person places this card, the next player will have to pick up two cards and forfeit his/her turn by Dmitry Fomin btt/co.iedia.crg/w/Lndez.papeurd-2362748. Wild - This card represents all four colors, and can be placed on any card. The player has to state which color it will reprsent for the next player. It can be played regardless of whether another card is available Wild Draw Four - This acts just like the wild card except that the next player also has to draw four cards as well as forfeit his/her turn. With this card,ymust have no other alternative cards to play that matches the color of the card previously played. If you play this card illegally, you may be challenged by the ather player to show your hand to him/her. If guilty, yaa need to draw cards. If not, the challenger eeds to draw 6 cards instead 3. At the beginning, the user can choose to shuffle the deck or load a predefined sequence of cards fron a ile (or testing) 4. The deck is implemented by a dynamic list of cards. The cards drawnm from the deck are deeted froan the ist 5. Each player's hand is implemented by a dynamic list ocards. The lEst i initially populated with the cards dealt to each player. The card drawn (played) by each player is added to (deleted from) the respective list. 6. The discard pile is implemented by a dynamic list. The discard pile is shuied if the draw pale is exhausted and the game has not ended. Only the top ive cards of the discard pile are shown on scre

Explanation / Answer

ANS:-

#ifndef Card_h
#define Card_h
#include <string>
#include <ctime>
#include <sstream>
#include <vector>
using namespace std;
class Card {
private:
string m_value;
string m_type;
int m_score;
int m_dealerScore;
bool m_inHand;
public:
Card(string value, string type, int score, int dealerScore);
~Card();
string getValue();
string getType();
int getScore();
int getDealerScore();
void setValue(string username);
void setType(string password);
void setScore(int score);
void setDealerScore(int dealerScore);
void setInHand(bool isInHand);
bool getInHand();
};
#endif Card_h

#include "Card.h"
#include <iostream> // using IO functions
#include <string> // using string
using namespace std;
Card::Card(string value, string type, int score, int dealerScore) {
m_value = value;
m_type = type;
m_score = score;
m_dealerScore = dealerScore;
m_inHand = false;
}
string Card::getValue() { // Member function (Getter)
return m_value;
}
string Card::getType() { // Member function (Getter)
return m_type;
}
int Card::getScore(){
return m_score;
}
int Card::getDealerScore(){
return m_dealerScore;
}
void Card::setValue(string value){
m_value = value;
}
void Card::setType(string type){
m_type = type;
}
void Card::setScore(int score){
m_score = score;
}
void Card::setDealerScore(int dealerScore){
m_dealerScore = dealerScore;
};
void Card::setInHand(bool isInHand){
m_inHand = isInHand;
}
bool Card::getInHand(){
return m_inHand;
}
// need to end the class declaration with a semi-colon


#ifndef Game_h
#define Game_h
#include "Player.h"
#include "Card.h"
#include <iostream>
using namespace std;
class Game{
private:
vector<Player*> m_gamePlayers;
vector<Card*> m_discardPile;
vector<Card*> m_drawPile;
public:
Game();
~Game();
void addCardFromDrawPile(Card* card);
void discardCardToDiscardPile(Card* card);
void shuffleCards();
Player* getPlayer(int playerIndex);
void displayPlayerHand();
void displayTopCardInDiscardPile();
void displayTopCardInDrawPile();
void processLoadCards();
void setupPlayers();
void processMenu();
void processGamePlay();
};
#endif Game_h
Game.cpp

#include "Game.h"
#include "Player.h"
#include "Card.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>  
#include <vector>
using namespace std;
void processLoadCards();
void processGamePlay();
void setupPlayers();
void processMenu();
string displayPlayerHand();
vector<Card*> cards;
vector<Card*> drawPile;
string value;
string type;
int score;
int dealerScore;
vector<Player*> players;
Game::Game(){
}
Game::~Game(){
}
void Game::shuffleCards(){
}
void Game::addCardFromDrawPile(Card* card){
m_discardPile.push_back(card);
}
void Game::discardCardToDiscardPile(Card* card){
m_drawPile.push_back(card);
}
Player* Game::getPlayer(int playerIndex){
if (playerIndex >= 0 && playerIndex < m_gamePlayers.size()){
return m_gamePlayers[playerIndex];
}
else{
return nullptr;
}
}
void Game::displayPlayerHand(){
for (auto it = m_gamePlayers.begin(); it != m_gamePlayers.end(); ++it){
Player* player = *it;
//cout << player->displayPlayerHand() << endl; //deference the pointer address to grab the function
}
}
void Game::displayTopCardInDiscardPile(){
while (m_discardPile.back() != 0)
{
m_discardPile.push_back(m_discardPile.back() - 1);
}
cout << "Top Card in Discard Pile:";
for (unsigned i = 0; i < m_discardPile.size(); i++){
cout << ' ' << m_discardPile[0];
}
}
void Game::displayTopCardInDrawPile(){
while (m_drawPile.back() != 0)
{
m_drawPile.push_back(m_drawPile.back() - 1);
}
cout << "Top Card in Draw Pile:";
for (unsigned i = 0; i < m_drawPile.size(); i++){
cout << ' ' << m_drawPile[0];
}
}
void Game::processLoadCards(){
ifstream file("UnoDeck.txt");
cout << " -- LET'S PLAY UNO --" << endl << " ";
if (file.is_open()){
string line;
while (getline(file, line)){
file >> ws;
stringstream ss;
ss << line;
Card* card = nullptr;
ss >> value;
ss >> type;
ss >> score;
ss >> dealerScore;
srand(time(NULL));
// Make the new card and put it in the cards vector
card = new Card(value, type, score, dealerScore);
cards.push_back(card);
}
// Scramble the vector
random_shuffle(cards.begin(), cards.end());
}
setupPlayers();
}
void Game::setupPlayers() {
int numPlayers = 4;
Player* player1 = new Player("Mary", "password", 0);
Player* player2 = new Player("John", "passwordForJohn", 0);
Player* player3 = new Player("Ben", "ben", 0);
Player* player4 = new Player("Junior", "junior", 0);
players.push_back(player1);
players.push_back(player2);
players.push_back(player3);
players.push_back(player4);
int playerIndex = 0;
for (int i = 1; i <= 7 * numPlayers; ++i){
// If the number we look at is divisible by 7, then it means we just hit the first 7 items
if (i < cards.size()) {
// Put first element into the cards pile
players[playerIndex]->addCardFromDrawPile(cards[i - 1]);
/* Say that the card is in a hand (so when you go to draw a new card from the pile, you'll interate through till you find a card
that has getInHand() == false and then you set it to true because it gets put in the user's hand. When you want to put a card back
in the pile, you would search for the card in the cards vector and then when you find that card go cards[i]->setInHand(false)
because it is no longer in someone's hand */
cards[i]->setInHand(true);
}
if (i % 7 == 0){
// Move to putting cards in the hand of the next player
playerIndex++;
}
Card* remainingCards = cards[cards.size() - (player1->m_playerHand.size() * 4)];
drawPile.push_back(remainingCards);
}
string displayPlayer1Cards = player1->displayPlayerHand();
string displayPlayer2Cards = player2->displayPlayerHand();
string displayPlayer3Cards = player3->displayPlayerHand();
string displayPlayer4Cards = player4->displayPlayerHand();
for (int i = 0; i < 2; --i){
cout << displayPlayer4Cards << endl;
player4->playGame();
cout << displayPlayer1Cards << endl;
player1->computerPlayerGame();
cout << displayPlayer2Cards << endl;
player2->computerPlayerGame();
cout << displayPlayer3Cards << endl;
player3->computerPlayerGame();
}
}
void Game::processMenu(){
// Declare other variables
int nMenuChoice = 0;
cout << "--- Welcome to the UNO Game! --- ";
// Keep the program running until they choose to exit
while (nMenuChoice != 4) {
cout << "---Main Menu--- ";
cout << "1. Start Game ";
cout << "2. Tutorial ";
cout << "3. View Leadership Board ";
cout << "4. Quit ";
cout << " Pick a choice: ";
cin >> nMenuChoice;
// Switch based on user input
switch (nMenuChoice) {
// Case of starting the game play
case 1:
processLoadCards();
break;
// Case of UNO Tutorial
case 2:
cout << "--- HOW TO PLAY UNO ---" << endl;
cout << "Rules:" << endl
<< "1. Play a card by entering the number on the left of the card." << endl
<< "2. A card can only be played if the colour or the title of the card is same " << endl
<< " as the card on the pile." << endl
<< "3. Draw a card if you cannot play any card, or you want to have more cards just" << endl
<< " for fun." << endl
<< "4. The last card in your hand can be a power card." << endl
<< "5. Draw Two and Draw Four will cause the next user to be skipped." << endl
<< "6. Wild card can be played at any time without restriction, but for Draw Four," << endl
<< " the computer players might challenge you. Draw Four can only be played when" << endl
<< " you have no same colour/ title card in your hand. If you are found guilty," << endl
<< " you will draw 4 cards as punishment, but you still can choose your colour." << endl
<< " If challenge failed, the challenger will draw 6 cards instead of 4." << endl
<< "7. A game will end, when one of the player had finished all the cards in his " << endl
<< " hand, or when the cards had ran out of stock." << endl << endl;
break;
// Case of Displaying Leadership Board  
case 3:
//processLeadershipBoard();
break;
// Quit condition
case 4:
break;
// Invalid selection   
default:
cout << "Invalid Menu Choice! ";
break;
}
}
};


#ifndef Player_h
#define Player_h
#include "Card.h"
#include <string>
#include <ctime>
#include <sstream>
#include <vector>
using namespace std;
class Player{
private:
string m_username;
string m_password;
int m_score;
vector<Card*> m_drawPile;
void discardCardToDiscardPile(Card* playerCard);
public:
Player(string username, string password, int score);
~Player();
string getUsername();
string getPassword();
int getScore();
void setUsername(string username);
void setPassword(string password);
void setScore(int score);
string displayPlayerHand();
void addCardFromDrawPile(Card* playerCard);
void retrieveCardsFromDrawPile(Card* playerCard);
void playGame();
void computerPlayerGame();
vector<Card*> m_playerHand;
};
#endif Player_h
Player.cpp

#include "Player.h"
#include "Card.h"
#include "Game.h"
#include <iostream> // using IO functions
#include <string> // using string
#include <algorithm>
using namespace std;
// Declare other variables
int nUserNum = 0;
int numToDelete = 0;
Player::Player(string username, string password, int score) {
m_username = username;
m_password = password;
m_score = score;
}
string Player::getUsername() { // Member function (Getter)
return m_username;
}
string Player::getPassword() { // Member function (Getter)
return m_password;
}
int Player::getScore(){
return m_score;
}
void Player::setUsername(string username){
m_username = username;
}
void Player::setPassword(string password){
m_password = password;
}
void Player::setScore(int score){
m_score = score;
}
void Player::addCardFromDrawPile(Card* card){
m_playerHand.push_back(card);
}
void Player::retrieveCardsFromDrawPile(Card* card){
m_drawPile.push_back(card);
}
void Player::discardCardToDiscardPile(Card* card){
vector<Card*>::iterator found = find(m_playerHand.begin(), m_playerHand.end(), card);
if (found != m_playerHand.end()){
m_playerHand.erase(found);
}
}
string Player::displayPlayerHand(){
stringstream sPlayerHand;
sPlayerHand << "Player's " << m_username << endl;
for (int i = 0; i < m_playerHand.size(); i++){
sPlayerHand << i + 1 << ". " << m_playerHand[i]->getValue() << " - " << m_playerHand[i]->getType() << endl;
m_playerHand[i]->setInHand(false);
}
return sPlayerHand.str();
}
void Player::playGame(){
stringstream sPlayerHand;
sPlayerHand << "Player's " << m_username << endl;
int option = 0;
for (int i = 0; i < m_playerHand.size(); i++){
m_playerHand[i]->setInHand(false);
sPlayerHand << option << ". " << m_playerHand[i]->getValue() << " - " << m_playerHand[i]->getType() << endl;
}
cout << "Enter a choice for " << m_username << ": " ;
cin >> option;
cout << "You have selected the card: (" << m_playerHand[option-1]->getValue() << " - " << m_playerHand[option-1]->getType() << "). ";
//if value and type matches with top card, then remove, else, return error message
clock_t start;
int pause = 1000;
for (int i = 0; i < 1; i++){
cout << "Loading next player's hands..." << flush << " ";
start = clock();
while (clock() < start + pause);
}
}
void Player::computerPlayerGame(){
Card* card = m_playerHand[rand() % m_playerHand.size()];
stringstream sPlayerHand;
sPlayerHand << "Player " << m_username << " ";
int option = 0;
for (int i = 0; i < m_playerHand.size(); i++){
sPlayerHand << option << ". " << m_playerHand[i]->getValue() << " - " << m_playerHand[i]->getType() << endl;
}
cout << m_username << " have selected the card: (" << card->getValue() << " - " << card->getType() << "). ";
clock_t start;
int pause = 2000;
for (int i = 0; i < 1; i++){
cout << "Loading next player's hands..." << flush << " ";
start = clock();
while (clock() < start + pause);
}
//if only pick rand card with value and type matches with top card, then remove, else, return error message
};

#include "Game.h"
#include "Player.h"
#include "Card.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>  
#include <vector>
using namespace std;
int main(){
//create instances of files we've created
Game* unoGame = new Game;
unoGame->processMenu();
delete unoGame;
system("pause");
return 0;
}