Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Card class
  • Loading branch information
Derlin8 authored Jan 6, 2021
0 parents commit 0d6d123
Show file tree
Hide file tree
Showing 14 changed files with 1,082 additions and 0 deletions.
190 changes: 190 additions & 0 deletions Card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
//
// Created by mfbut on 10/26/2019.
//

#include <sstream>
#include <algorithm>
#include "Card.h"
#include "InputValidation.h"
#include "NumberCard.h"
#include "ReverseCard.h"
#include "WildCard.h"
#include "WildCardDrawNCard.h"
#include "DrawNCard.h"

Uno::Card *Uno::Card::toCard(const std::string &stringMove) {
if (RepresentsNumberCard(stringMove)) {
return getNumberCard(stringMove);
} else if (RepresentsReverseCard(stringMove)) {
return getReverseCard(stringMove);
} else if (RepresentsSkipCard(stringMove)) {
return getSkipCard(stringMove);
} else if (RepresentsWildCard(stringMove)) {
return getWildCard(stringMove);
} else if (RepresentsWildCardDrawNCard(stringMove)) {
return getWildCardDrawNCard(stringMove);
} else if (RepresentsDrawNCard(stringMove)) {
return getDrawNCard(stringMove);
} else {
return NULL;
}

return NULL;
}

bool Uno::Card::RepresentsNumberCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
int number;
parser >> keyword;
parser >> color;
parser >> number;
return InputValidation::StreamOnlyContainsWhiteSpace(parser);
}

Uno::Card* Uno::Card::getNumberCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
int number;
parser >> keyword;
parser >> color;
parser >> number;
return new NumberCard("NumberCard", color, number);
}

bool Uno::Card::RepresentsReverseCard(const std::string &line) {
const std::string neededKeyWord("reverse");
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string reverse;
parser >> keyword;
parser >> color;
parser >> reverse;
return InputValidation::StreamOnlyContainsWhiteSpace(parser) && StringMethods::isPrefix(reverse, neededKeyWord);
}


Uno::Card* Uno::Card::getReverseCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string reverse;
parser >> keyword;
parser >> color;
parser >> reverse;
//TODO
return new ReverseCard("ReverseCard", color, -1);
}


bool Uno::Card::RepresentsSkipCard(const std::string &line) {
const std::string neededKeyWord("skip");
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string skip;
parser >> keyword;
parser >> color;
parser >> skip;
return InputValidation::StreamOnlyContainsWhiteSpace(parser) && StringMethods::isPrefix(skip, neededKeyWord);
}

Uno::Card* Uno::Card::getSkipCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string skip;
parser >> keyword;
parser >> color;
parser >> skip;
// TODO
return new SkipCard("SkipCard", color, -1);
}



bool Uno::Card::RepresentsWildCard(const std::string &line) {
const std::string neededKeyWord("wild");
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string wild;
parser >> keyword;
parser >> wild;
parser >> color;
return InputValidation::StreamOnlyContainsWhiteSpace(parser) && StringMethods::isPrefix(wild, neededKeyWord);
}

Uno::Card* Uno::Card::getWildCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string wild;
parser >> keyword;
parser >> wild;
parser >> color;
return new WildCard("WildCard", color,-1);
}

bool Uno::Card::RepresentsWildCardDrawNCard(const std::string &line) {
const std::string neededKeyWordWild("wild");
const std::string neededKeyWordDraw("draw");
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string wild;
std::string draw;
int number;
parser >> keyword;
parser >> wild;
parser >> draw;
parser >> number;
parser >> color;
return InputValidation::StreamOnlyContainsWhiteSpace(parser) && StringMethods::isPrefix(wild, neededKeyWordWild) && StringMethods::isPrefix(draw, neededKeyWordDraw);
}

Uno::Card* Uno::Card::getWildCardDrawNCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string wild;
std::string draw;
int number;
parser >> keyword;
parser >> wild;
parser >> draw;
parser >> number;
parser >> color;
return new WildDrawNCard("WildDrawNCard" , color, -1, number);
}


bool Uno::Card::RepresentsDrawNCard(const std::string &line) {
const std::string neededKeyWord("draw");
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string draw;
int number;
parser >> keyword;
parser >> color;
parser >> draw;
parser >> number;
return InputValidation::StreamOnlyContainsWhiteSpace(parser) && StringMethods::isPrefix(draw, neededKeyWord);
}

Uno::Card* Uno::Card::getDrawNCard(const std::string &line) {
std::stringstream parser(line);
std::string keyword;
std::string color;
std::string draw;
int number;
parser >> keyword;
parser >> color;
parser >> draw;
parser >> number;
return new DrawNCard("DrawNCard", color, -1, number);
}
76 changes: 76 additions & 0 deletions Card.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Created by mfbut on 10/26/2019.
//

#ifndef ECS_36B_HOMEWORK_CARD_H
#define ECS_36B_HOMEWORK_CARD_H

#include <iostream>
#include <string>
//#include "NumberCard.h"

namespace Uno {
class GameOfUno;
class Card {

public:
Card();

Card(std::string type);

virtual void play(GameOfUno *gameState) = 0;

virtual void onInitialFlip(GameOfUno *gameState) = 0;

virtual bool aboutEqual(Card *rhs) = 0;

virtual int getValue() const = 0;
virtual std::string getColor() = 0;

virtual bool canBePlayedOnTopOf(Card *card) = 0;

virtual std::string printCard() = 0;

virtual bool operator==(const Card &rhs) const = 0;

virtual bool operator<(const Card &rhs) = 0;

std::string type;

static bool RepresentsNumberCard(const std::string &line);

static bool RepresentsReverseCard(const std::string &line);

static bool RepresentsSkipCard(const std::string &line);

static bool RepresentsWildCard(const std::string &line);

static bool RepresentsWildCardDrawNCard(const std::string &line);

static bool RepresentsDrawNCard(const std::string &line);

static Card* getNumberCard(const std::string &line);

static Card* getReverseCard(const std::string &line);

static Card* getSkipCard(const std::string &line);

static Card* getWildCard(const std::string &line);

static Card* getWildCardDrawNCard(const std::string &line);

static Card* getDrawNCard(const std::string &line);

static Uno::Card* toCard(const std::string &stringMove);

};

// std::ostream &operator<<(std::ostream &out, const NumberCard &card);
//
// std::istream &operator>>(std::istream &in, NumberCard &card);



}

#endif //ECS_36B_HOMEWORK_CARD_H
104 changes: 104 additions & 0 deletions DrawNCard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// Created by Derek Lin on 12/12/19.
//

#include "DrawNCard.h"
#include "NumberCard.h"

// DRAW N
Uno::DrawNCard::DrawNCard(std::string type, const std::string &color, int value, int draw)
: Card(type), color(color), value(value), draw(draw) {
}

void Uno::DrawNCard::play(Uno::GameOfUno* gameState) {
gameState->discard.placeOnTop(this);
gameState->advanceTurn();
for (int i = 0; i < draw; i++) {
gameState->getCurPlayer().drawCard();
}
//gameState->addCardToDiscard(this);
}

bool Uno::DrawNCard::operator<(const Card &rhs) {

if (rhs.type == "NumberCard" || rhs.type == "SkipCard" || rhs.type == "ReverseCard") {
return false;
}

if (type != rhs.type) {
return true;
}

const DrawNCard& drawNCard = dynamic_cast<const DrawNCard&>(rhs);
if (color == drawNCard.color) {
return draw < drawNCard.draw;
} else {
return color < drawNCard.color;
}
}

void Uno::DrawNCard::onInitialFlip(GameOfUno* gameState) {
gameState->discard.placeOnTop(this);
for (int i = 0; i < draw; i++) {
gameState->getCurPlayer().drawCard();
}
}

std::string Uno::DrawNCard::getColor() {
return color;
}

bool Uno::DrawNCard::aboutEqual( Card* rhs) {

if (type.compare(rhs->type) != 0) {
return false;
}

DrawNCard* drawNCard = dynamic_cast<DrawNCard*>(rhs);

return draw == drawNCard->draw && StringMethods::isPrefix(color, drawNCard->color);

}

bool Uno::DrawNCard::canBePlayedOnTopOf(Uno::Card* card) {

if(card->type == "DrawNCard") {
Uno::DrawNCard numberCard = *dynamic_cast<Uno::DrawNCard*>(card);
return card->getColor() == color || numberCard.draw == draw;
}

if (card->type == "WildCard") {
return true;
}

if (card->type == "NumberCard") {
Uno::NumberCard numberCard = *dynamic_cast<Uno::NumberCard*>(card);
return color == numberCard.getColor();
}

if (card->type == "SkipCard") {
Uno::SkipCard skipCard = *dynamic_cast<Uno::SkipCard*>(card);
return color == skipCard.color;
}
return false;
}


std::string Uno::DrawNCard::printCard() {
return color + " Draw " + std::to_string(draw);
}

int Uno::DrawNCard::getValue() const {
return value;
}

bool Uno::DrawNCard::operator==(const Uno::Card &rhs) const {

if (type.compare(rhs.type) != 0) {
return false;
}

const Uno::DrawNCard& drawNCard = dynamic_cast<const Uno::DrawNCard&>(rhs);

return color == drawNCard.color && draw == drawNCard.draw;
}
Loading

0 comments on commit 0d6d123

Please sign in to comment.