Create assignment four, commit the cards library that is currently a side project.
This commit is contained in:
parent
56e27589c0
commit
94ac292f32
5 changed files with 342 additions and 0 deletions
0
assignmentFour/medicalRecords.cpp
Normal file
0
assignmentFour/medicalRecords.cpp
Normal file
0
assignmentFour/videogamer.cpp
Normal file
0
assignmentFour/videogamer.cpp
Normal file
BIN
cards/cards
Executable file
BIN
cards/cards
Executable file
Binary file not shown.
304
cards/cards.cpp
Normal file
304
cards/cards.cpp
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
/*
|
||||
Program Name: Cards
|
||||
Process: This is primarily a definition of a card class, for some terminal card games.
|
||||
Function Input/Parameters: ???
|
||||
Function Output/Parameters: ???
|
||||
Function Output/Returned: ???
|
||||
Device Input/Device: ???
|
||||
Device Output/Monitor: ???
|
||||
Dependencies: ???
|
||||
*/
|
||||
|
||||
#include "cards.h"
|
||||
|
||||
using namespace std;
|
||||
// using namespace Cards;
|
||||
|
||||
// class definitions
|
||||
class Card
|
||||
{
|
||||
public:
|
||||
Card(char value, char suit)
|
||||
{
|
||||
// store the value and suit. (possible values 2-9, T, J, Q, K, A)
|
||||
cardValue = value;
|
||||
cardSuit = suit;
|
||||
// get the numerical value of the card, casting from string to num where possible. (2-9)
|
||||
if ((cardValue >= 2) || (cardValue <= 9))
|
||||
{
|
||||
// cast the character to number.
|
||||
cardNumericalValue = (int)cardValue - 48; // numerical values 2-9 only
|
||||
}
|
||||
// face values here.
|
||||
if (cardValue == 'T')
|
||||
{
|
||||
// 'ten' card
|
||||
cardNumericalValue = 10;
|
||||
}
|
||||
if (cardValue == 'J')
|
||||
{
|
||||
// jack
|
||||
cardNumericalValue = 11;
|
||||
}
|
||||
if (cardValue == 'Q')
|
||||
{
|
||||
// queen
|
||||
cardNumericalValue = 12;
|
||||
}
|
||||
if (cardValue == 'K')
|
||||
{
|
||||
// king
|
||||
cardNumericalValue = 13;
|
||||
}
|
||||
// special 1/Ace card here
|
||||
if (cardValue == 'A')
|
||||
{
|
||||
cardNumericalValue = 1;
|
||||
}
|
||||
// add the cardValue and cardSuit to the string
|
||||
fullCard = fullCard + cardValue;
|
||||
fullCard = fullCard + cardSuit;
|
||||
}
|
||||
int getCardValue()
|
||||
{
|
||||
return cardNumericalValue;
|
||||
}
|
||||
char getCardRawValue()
|
||||
{
|
||||
return cardValue;
|
||||
}
|
||||
char getCardSuit()
|
||||
{
|
||||
return cardSuit;
|
||||
}
|
||||
string getFullCard()
|
||||
{
|
||||
return fullCard;
|
||||
}
|
||||
void switchComparison(CardSwitch switchComp = VALUE)
|
||||
{
|
||||
//
|
||||
if(switchComp == VALUE)
|
||||
{
|
||||
//
|
||||
Card::compareValue = true;
|
||||
Card::compareSuit = false;
|
||||
Card::compareCard = false;
|
||||
}
|
||||
else if(switchComp == SUIT)
|
||||
{
|
||||
Card::compareValue = false;
|
||||
Card::compareSuit = true;
|
||||
Card::compareCard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Card::compareValue = false;
|
||||
Card::compareSuit = false;
|
||||
Card::compareCard = true;
|
||||
}
|
||||
}
|
||||
void switchInput(CardSwitch switchComp = FULLCARD)
|
||||
{
|
||||
//
|
||||
if (switchComp == VALUE)
|
||||
{
|
||||
//
|
||||
Card::inputValue = true;
|
||||
Card::inputSuit = false;
|
||||
Card::inputCard = false;
|
||||
}
|
||||
else if (switchComp == SUIT)
|
||||
{
|
||||
Card::inputValue = false;
|
||||
Card::inputSuit = true;
|
||||
Card::inputCard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Card::inputValue = false;
|
||||
Card::inputSuit = false;
|
||||
Card::inputCard = true;
|
||||
}
|
||||
}
|
||||
void switchOutput(CardSwitch switchComp = FULLCARD)
|
||||
{
|
||||
//
|
||||
if (switchComp == VALUE)
|
||||
{
|
||||
//
|
||||
Card::outputValue = true;
|
||||
Card::outputSuit = false;
|
||||
Card::outputCard = false;
|
||||
}
|
||||
else if (switchComp == SUIT)
|
||||
{
|
||||
Card::outputValue = false;
|
||||
Card::outputSuit = true;
|
||||
Card::outputCard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Card::outputValue = false;
|
||||
Card::outputSuit = false;
|
||||
Card::outputCard = true;
|
||||
}
|
||||
}
|
||||
friend bool operator==(const Card &cardOne, const Card &cardTwo)
|
||||
{
|
||||
// operator== can decide whether or not it will compare the
|
||||
// values of cards, suits of cards, or complete cards, defaults to
|
||||
// comparing values of cards only.
|
||||
// to switch the comparison operator, use the switchComparison function
|
||||
// to switch how the comparison operator works.
|
||||
if(compareSuit)
|
||||
{
|
||||
return (cardOne.cardSuit == cardTwo.cardSuit);
|
||||
}
|
||||
else if(compareCard)
|
||||
{
|
||||
return ((cardOne.cardNumericalValue == cardTwo.cardNumericalValue)
|
||||
&& (cardOne.cardSuit == cardTwo.cardSuit));
|
||||
}
|
||||
else
|
||||
{
|
||||
return (cardOne.cardNumericalValue == cardTwo.cardNumericalValue);
|
||||
}
|
||||
}
|
||||
friend bool operator<(const Card &cardOne, const Card &cardTwo)
|
||||
{
|
||||
// compare cardOne's value to cardTwo's value with less than function.
|
||||
// THIS WILL ONLY CHECK NUMERICAL VALUE
|
||||
return (cardOne.cardNumericalValue < cardTwo.cardNumericalValue);
|
||||
}
|
||||
friend ostream &operator<<(ostream &output, const Card &cardOut)
|
||||
{
|
||||
// operator<< can decide whether or not it will output the
|
||||
// values of cards, suits of cards, or complete cards, defaults to
|
||||
// printing full card value.
|
||||
// to switch the output operator, use the switchOutput function
|
||||
// to switch how the output operator works.
|
||||
if (outputValue)
|
||||
{
|
||||
return output << cardOut.cardValue;
|
||||
}
|
||||
else if (outputSuit)
|
||||
{
|
||||
return output << cardOut.cardSuit;
|
||||
}
|
||||
else
|
||||
{
|
||||
return output << cardOut.fullCard;
|
||||
}
|
||||
}
|
||||
friend istream &operator>>(istream &input, Card &cardIn)
|
||||
{
|
||||
// operator>> can decide whether or not it will input the
|
||||
// values of cards, suits of cards, or complete cards, defaults to
|
||||
// getting input of full card value in format NumberSuit (aka 2D for 2 of Diamonds).
|
||||
// to switch the input operator, use the switchInput function
|
||||
// to switch how the input operator works.
|
||||
|
||||
// get a temporary string in.
|
||||
string inStr;
|
||||
// read in the input into the temp string.
|
||||
input >> inStr;
|
||||
// grab the input based on what is currently set for the class.
|
||||
if (outputValue)
|
||||
{
|
||||
// assume the input is a single value, cast from string to char
|
||||
cardIn.cardNumericalValue = atoi(inStr.c_str());
|
||||
cardIn.cardValue = (char)cardIn.cardNumericalValue + 48; // needs testing.
|
||||
return input;
|
||||
}
|
||||
else if (outputSuit)
|
||||
{
|
||||
// assume the input is a single value, cast from string to char
|
||||
cardIn.cardSuit = inStr.c_str()[0]; // this may not work.
|
||||
return input;
|
||||
}
|
||||
else
|
||||
{
|
||||
cardIn.cardNumericalValue = atoi(inStr.c_str());
|
||||
cardIn.cardValue = (char)cardIn.cardNumericalValue + 48;
|
||||
cardIn.fullCard = inStr;
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
char cardValue;
|
||||
char cardSuit;
|
||||
int cardNumericalValue;
|
||||
string fullCard;
|
||||
// comparison defaults to value
|
||||
static bool compareValue; // unused?
|
||||
static bool compareSuit;
|
||||
static bool compareCard;
|
||||
// output defaults to full card value
|
||||
static bool outputValue;
|
||||
static bool outputSuit;
|
||||
static bool outputCard;
|
||||
// input defaults to full card value
|
||||
static bool inputValue;
|
||||
static bool inputSuit;
|
||||
static bool inputCard;
|
||||
};
|
||||
|
||||
// function prototypes
|
||||
|
||||
bool compareValue = true;
|
||||
bool compareSuit = false;
|
||||
bool compareCard = false;
|
||||
bool outputValue = false;
|
||||
bool outputSuit = false;
|
||||
bool outputCard = true;
|
||||
bool inputValue = false;
|
||||
bool inputSuit = false;
|
||||
bool inputCard = true;
|
||||
|
||||
// main program
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
// create array of cards called deck.
|
||||
vector<Card> deck;
|
||||
// have two hands for cards
|
||||
vector<Card> humanHand;
|
||||
vector<Card> computerHand;
|
||||
// char array for the setups
|
||||
char suits[] = {'C', 'D', 'H', 'S'};
|
||||
int suitsLen = sizeof(suits) / sizeof(suits[0]);
|
||||
// char values[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
|
||||
char values[] = {'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'A'};
|
||||
int valuesLen = (sizeof(values) / sizeof(values[0]));
|
||||
// game setup
|
||||
// create deck by loop for 1 - 13 with each suit {(H)earts, S(pades), C(lubs), D(iamonds)}.
|
||||
// use double for loop, outer for suits, inner for values.
|
||||
for (int deckCounter = 0; deckCounter < suitsLen; deckCounter++)
|
||||
{
|
||||
// creates each suit
|
||||
for (int suitCounter = 0; suitCounter < valuesLen; suitCounter++)
|
||||
{
|
||||
// creates each card/value
|
||||
deck.push_back(Card(values[suitCounter], suits[deckCounter]));
|
||||
}
|
||||
}
|
||||
// shuffle deck randomly? (use shuffle function from algorithm library)
|
||||
// generate a random number (which is so much harder in CPP because they 'frown upon' using the C STL rand stuff)
|
||||
default_random_engine randNumGen(time(NULL));
|
||||
// shuffle the deck using that rng engine.
|
||||
shuffle(deck.begin(), deck.end(), randNumGen);
|
||||
// return 0
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* THIS BELOW CODE IS FOR CHECKING THE DECK
|
||||
for (int tempIterator = 0; tempIterator < 52; tempIterator++)
|
||||
{
|
||||
// print off each card to see if it was correctly made.
|
||||
cout << tempIterator << ": " << deck[tempIterator].getFullCard() << " | ";
|
||||
}
|
||||
cout << endl;
|
||||
*/
|
||||
|
||||
// compiles using "g++ -Wall -pedantic cards.cpp -o cards" - note to programmer
|
||||
38
cards/cards.h
Normal file
38
cards/cards.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef CARDS_UTIL
|
||||
#define CARDS_UTIL
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
#include <time.h>
|
||||
|
||||
// enumerated definitions
|
||||
typedef enum
|
||||
{
|
||||
FULLCARD = 0,
|
||||
VALUE = 1,
|
||||
SUIT = 2
|
||||
} CardSwitch;
|
||||
|
||||
namespace Cards
|
||||
{
|
||||
//
|
||||
class Card
|
||||
{
|
||||
public:
|
||||
Card(char value, char suit);
|
||||
int getCardValue();
|
||||
char getCardRawValue();
|
||||
char getCardSuit();
|
||||
std::string getFullCard();
|
||||
void switchComparison(CardSwitch switchComp = VALUE);
|
||||
void switchInput(CardSwitch switchComp = FULLCARD);
|
||||
void switchOutput(CardSwitch switchComp = FULLCARD);
|
||||
friend bool operator==(const Card &cardOne, const Card &cardTwo);
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue