I'm working on the following C++ application:
Description:
The user enters a number n and the program takes a random collection of cards that are declared at the top of main in the global arrays. It will output a random number of cards each time.
Issue:
The number, e.g. '3 Hearts', appears more than one time. I made a function to correct that, however it didn't solve the issue.
Reference code provided below:
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
string type[4] = {"Hearts" , "Diamonds" , "Spades" , "Clubs"};
string num[13] = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"};
int random(int x)
{
return rand() %x;
}
bool isDrawn(int);
void DrawCard();
int card_remaining = 52;
bool card_is_drawn[52] = {false};
int main()
{
while(1)
{
cout<<"\n Enter A Card Number : ";
int n;
cin>>n;
if(card_remaining <= 0)
{
card_remaining = 52;
cout<<" No More Cards , Refreshing ...\n";
cout<<" Refresh Done ! Try Again if you Want \n";
for(int i=0;i<52;i++)
card_is_drawn[i] = false;
}
else
{
for(int i=0;i<n;i++)
{
DrawCard();
}
}
}
cout<<endl;
system("PAUSE");
return 0;
}
void DrawCard()
{
bool check_1 = false;
int card;
while(!check_1)
{
card = random(card_remaining);
if(!isDrawn(card))
check_1 = true;
}
if(check_1)
cout << num[card%13]<<" OF " << type[card/13] << endl ;
card_remaining--;
}
bool isDrawn(int x)
{
if (card_is_drawn[x] == false)
{
card_is_drawn[x] = true;
return true;
}
return false;
}
Check the function
bool isDrawn(int x){
if(card_is_drawn[x] == false){
card_is_drawn[x] = true;
return true;
}
return false;
}
You might want to exchange the both return values. That means:
if(card_is_drawn[x] == false) {
...
return false; //since the card was NOT drawn;
And at the end of the function:
return true; //since the if-clause evaluated as false what means that the card was drawn;
By the way:
You get your random card by rand()%cards_remaining. That means if you draw ANY card and therefore reduce cards_remaining by one you won't be able to draw the King of Clubs anymore. And going on like this you will loose cards from the 'end' of your deck.
should be:
void DrawCard(){
if (card_remaining <= 0) // no cards left? can't draw
return;
bool check_1 = false;
int card;
while(!check_1){
card = random(52); // here 52
if (isDrawn(card)) // here, if the card HAS been drawn
check_1 = true;
}
if(check_1)
cout << num[card%13]<<" OF " << type[card/13] << endl ;
card_remaining--;
}
Related
#include <iostream>
#include <string>
using namespace std;
#define Max 100000
class Stack {
private:
int top =-1;
char letters[Max];
public:
void setTop(int t) {
top = t;
}
int getTop() {
return top;
}
bool isEmptyStack() {
if (top == -1) {
return true;
}
else{ return false;
}
}
char push(char x,int s) {
if (top != s - 1){
top++;
x = letters[top];
return x;
}
}
char pop() {
if ((isEmptyStack())==false){
cout << "the deleted value is: " << l[top]<<endl;
top--;
return l[top];
}
}
};
void reverse(char letters[], char temp[], int size, Stack stack) {
int i=0;
for (i = 0; i < size; i++) {
stack.push(letters[i],size);
}
i = 0;
cout << temp<<endl;
while (stack.isEmptyStack() == false)
{
letters[-1] = stack.getTop();
stack.pop();
stack.push(letters[i],size);
i++;
}
/* for (int i = 0; i < size; i++) {
cout << temp[i];
}*/
}
int myStringLength(const char* letter)
{
for (int i = 0, c = 0; letter[i] != '\0'; i++, c++) {
if (letter[i] != '\0')
for (; letter[i] != '\0'; i++, c++)
if (letter[i] == '\0') break;
return c;
}
}
int main()
//initializes the main function
{
Stack stack;
string w;
std::cout << "Enter a Word: ";
getline(cin,w);
char* letters = &w[0];
// sets the character text array to set the number of characters equal to the size of the string
//calls the processData function
std::cout << letters<<endl;
int size = myStringLength(letters);
reverse(letters, letters, size, stack);
return 0;//returns the function at 0.
}
I set out to create a program that will check if a word is a palindrome(meaning it is spelled the same normally and if the word is reversed.) I am not yet at that point that is just the final objective. In my code, I have created a stack class because I wanted to feel the satisfaction of getting the same result using my own code. My problem is the stack is not reversing it is returning some weird characters that I don't have the keys on my keyboard to replicate.
The desired outcome should be word's reversed characters.
if the word is food the function should be returning doof. I have already compared the reversed stack to the original and printed the final statement. I fixed the char letters[];
If you're open to using a simple function instead of a Stack then you could use the following program since it is much more simple than your Stack version and hence less-error prone.
#include <iostream>
#include <string>
bool checkIfPalindroom(const std::string &str)
{
for(int i=0;i<(str.size()/2);i++)
{
if (str[i] != str[str.size() - i - 1])
{
return false;//if this if is satisfied even once, return false
}
}
return true;//if the control flow reaches here this will mean that no if was satisfied and hence return true
}
int main()
{
std::string myString = "Somearbitrarystring";
if(checkIfPalindroom(myString))//call the function
{
std::cout<<"The given string: "<<myString <<" is a palindrome"<<std::endl;
}
else
{
std::cout<<"The given string: "<<myString<<" is not a palindrome"<<std::endl;
}
return 0;
}
My professor has asked us to make a program that will take a user's input and continue reading until the end of input. Only then, can the program output what the user has typed.
Input should be based on video title, it's url, comments made on the video, length (in minutes), and rating (in *).
For example:
United Break Guitars, https://www.youtube.com/watch?v+5YGc4zOqozo, Great example of one person getting a giant company to listen, 4.5, ***, Space Versus Tabs, https://www.youtube.com/watch?v=SsoOG6ZeyUl, Decide for yourself: spaces or tabs?, 2.83, ****
Before inputting any video description, the user needs to specify a sorting method of three choices, Rating, Length, or title. I have completed most of the code and sort method asked by my professor (bubble sort), however when I ask the program to sort by title (which is the only one of the three options that is a string), it does not output correctly.
Here is my code:
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
#include "video.h"
int main()
{
string user, url, comment, title;
int rating;
double length;
int i = 0, last = 0;
Video *videoObj[100];
// specifies how the videos should be sorted
cin >> user;
cin.ignore();
while (getline(cin,title) ) {
getline(cin, url);
getline(cin, comment);
cin >> length;
cin >> rating;
cin.ignore();
videoObj[i] = new Video(title, url, comment, length, rating);
i++;
last++;
}
//------------------------------------------------------------------------
//--------------- Sorts the list based on rating (*) ---------------------
//------------------------------------------------------------------------
if(user=="rating"){
for(int i = 0; i < last - 1; i++){
for(int j = 0; j< last - i -1; j++){
if(videoObj[j +1]->Rating(videoObj[j])){
swap(videoObj[j], videoObj[j+1]);
}
}
}
}
//------------------------------------------------------------------------
//--------------- Sorts the list based on length -------------------------
//------------------------------------------------------------------------
if(user=="length"){
for(int i = 0; i < last - 1; i++){
for(int j = 0; j< last - i -1; j++){
if(videoObj[j +1]->Length(videoObj[j])){
swap(videoObj[j], videoObj[j+1]);
}
}
}
}
//------------------------------------------------------------------------
//--------------- Sorts the list based on title --------------------------
//------------------------------------------------------------------------
if(user=="title"){
for(int i = 0; i < last - 1; i++){
for(int j = 0; j< last - i -1; j++){
if(videoObj[j +1]->Title(videoObj[j])){
swap(videoObj[j], videoObj[j+1]);
}
}
}
}
for(int i= 0; i < last; i++){
videoObj[i]->print();
}
//delete[] videoObj;
return 0;
}
video.cpp:
#include <iostream>
#include <algorithm>
using namespace std;
#include "video.h"
Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
: title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
m_title = title;
m_link = link;
m_comment = comment;
m_length = length;
m_rating = rating;
}
bool Video::Rating(Video *other)
{
if(m_rating > other-> m_rating){
return true;
}
else
{
return false;
}
}
bool Video::Length(Video *other2)
{
if(m_length > other2-> m_length){
return true;
}
else
{
return false;
}
}
bool Video::Title(Video *other3)
{
if(m_length > other3-> m_length){
return true;
}
else
{
return false;
}
}
void Video::print(){
string star;
switch(rating){
case 1:
star = "*";
break;
case 2:
star = "**";
break;
case 3:
star = "***";
break;
case 4:
star = "****";
break;
case 5:
star = "*****";
break;
}
cout << title << ", " << link << ", " << comment << ", " << length << ", " << star << endl;
}
video.h:
#ifndef VIDEO_H
#define VIDEO_H
using namespace std;
class Video {
public:
Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
void print();
bool Rating(Video *other);
bool Length(Video *other2);
bool Title(Video *other3);
private:
string m_title;
string m_link;
string m_comment;
double m_length;
int m_rating;
string title;
string link;
string comment;
double length;
int rating;
};
#endif
I'm not exactly sure what I need to do to title to make it function correctly. I was thinking of comparing by strings, but again, not sure where to start.
Also, another question, how do I use delete[] videoObj;without getting an error?
Well this is wrong, just a typo probably
bool Video::Title(Video *other3)
{
if(m_length > other3-> m_length){
return true;
}
else
{
return false;
}
}
It should be m_title not m_length (probably)
bool Video::Title(Video *other3)
{
if(m_title > other3-> m_title){
return true;
}
else
{
return false;
}
}
Also this code can be simplified, the above can be written in one line
bool Video::Title(Video *other3)
{
return m_title > other3-> m_title;
}
if (xxx) return true; else return false; is exactly the same as return xxx;. Beginners often don't realise you can calculate with booleans in this is way.
Using the BBC Micro bit.
There are two buttons on the micro bit and in my code button A is for even numbers and button B is for negative numbers. The game runs fine if the numbers are only a 1 or a 0 anything else it ends the game and i'm not sure why anymore.
Tried using just 1 number in the array and it works, or even changing the order of the array the first number in the array is the only one which will be accepted.
#include "MicroBit.h"
MicroBit uBit;
bool game = true;
bool press = false;
int i;
int score;
int number;
int nextNumber;
int odd[5] = {1, 3, 5, 7, 9};
int even[5] = {0, 2, 4, 6, 8};
void endGame()
{
game = false;
}
void evenNumber()
{
if(number == even[i])
{
score++;
//number = nextNumber;
}
else
{
endGame();
}
}
void oddNumber()
{
if(number == odd[i])
{
score++;
//number = nextNumber;
}
else
{
endGame();
}
}
void onButtonA(MicroBitEvent)
{
evenNumber();
press = true;
}
void onButtonB(MicroBitEvent)
{
oddNumber();
press = true;
}
void reset(MicroBitEvent)
{
game = true;
}
int main()
{
// Initialise the micro:bit runtime.
uBit.init();
// Random number between 0 - 10
uBit.seedRandom(uBit.random(10));
number = uBit.random(10);
nextNumber = number;
while (game == true)
{
press = false;
number = nextNumber;
nextNumber = uBit.random(10);
// If numbers are the same, randomise again.
while(nextNumber == number)
{
nextNumber = uBit.random(10);
}
uBit.display.print(number);
// Button press and shake
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
uBit.messageBus.listen(MICROBIT_ID_ACCELEROMETER, MICROBIT_ACCELEROMETER_EVT_SHAKE, reset);
while (press == false){
uBit.sleep(100);
}
}
// Make sure we don't exit main
uBit.sleep(1000);
// Shows score
uBit.display.scroll("YOUR SCORE IS:", 80);
uBit.display.scroll(score, 80);
release_fiber();
}
All even numbers up to 8 should work on button click A and odd numbers on button click B up to 9.
Welcome to Stackoverflow. I replicated your problem. Here's some code that works for me. I removed some lines that aren't needed any more. As #RetiredNinja said, you need to change how you check for odd and even numbers.
#include "MicroBit.h"
MicroBit uBit;
bool game = true;
bool press = false;
int score;
int number;
int nextNumber;
void endGame()
{
game = false;
}
void evenNumber()
{
if(number%2 == 0)
{
score++;
//number = nextNumber;
}
else
{
endGame();
}
}
void oddNumber()
{
if(number%2 == 1)
{
score++;
//number = nextNumber;
}
else
{
endGame();
}
}
void onButtonA(MicroBitEvent)
{
evenNumber();
press = true;
}
void onButtonB(MicroBitEvent)
{
oddNumber();
press = true;
}
void reset(MicroBitEvent)
{
game = true;
}
int main()
{
// Initialise the micro:bit runtime.
uBit.init();
// Random number between 0 - 10
uBit.seedRandom(uBit.random(10));
number = uBit.random(10);
nextNumber = number;
while (game == true)
{
press = false;
number = nextNumber;
nextNumber = uBit.random(10);
// If numbers are the same, randomise again.
while(nextNumber == number)
{
nextNumber = uBit.random(10);
}
uBit.display.print(number);
// Button press and shake
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
uBit.messageBus.listen(MICROBIT_ID_ACCELEROMETER, MICROBIT_ACCELEROMETER_EVT_SHAKE, reset);
while (press == false){
uBit.sleep(100);
}
}
// Make sure we don't exit main
uBit.sleep(1000);
// Shows score
uBit.display.scroll("YOUR SCORE IS:", 80);
uBit.display.scroll(score, 80);
release_fiber();
}
I want to sort a vector of strings into order alphabetically. I have coded thus far and I can not resolve the error for strcoll. Also, I am not allowed to use algorithm library. The error can be seen in the bubbub function where I am trying to bubble sort.
I have a few functions that should explain themselves with their names
#include <iostream>
#include <string.h>
#include <vector>
#include <stdio.h>
using namespace std;
inline void swap(string & a, string & b)
{
string c = b;
b = a;
a = c;
return;
}
void input_name(string&);
void sort_names(string&);
void repeat_pro(int&);
void sortArray(string, int);
void print_names(vector<string>& b_list);
void bubbub(vector<string> & b_list);
int main() {
vector<string> b_list;
string name;
int choice;
int count=0;
cout << "Welcome to the Business Sorting Program!" << endl;
do{
input_name(name);
b_list.push_back(name);
count++;
repeat_pro(choice);
bubbub(b_list);
cout<<"\n \n Your Businesses are:"<<endl;
for(int i=0; i < b_list.size() ; i++){
cout<<b_list[i]<<"\n";
}
cout << "\n\n";
}while(choice == 0);
cout << "Thanks for using this program"<<endl;
return 0;
}
void input_name(string &name){
cout << "Enter in the name of the business: ";
getline(cin, name);
}
void sort_names(string &name){
}
void repeat_pro(int &choice){
cout << "Do you want to enter in more names: ";
string answ;
cin>>answ;
cin.ignore(1000,'\n');
for (int x=0; x<answ.size(); x++){
answ[x] = tolower(answ[x]);
}
if (answ == "yes" || answ == "y"){
choice = 0;
}
else {
choice = 1;
}
}
void bubbub(vector<string> & b_list)
{
vector<string>::size_type loop = 0;
bool done = false;
while ((loop+1 < b_list.size()) && ! done)
{
done = true;
for (vector<string>::size_type count = 0;
count+1 != b_list.size(); count++)
{
string x;
string z;
x = b_list[count];
z= b_list[count+1];
if ( strcoll (x,z) < 0 )
{
swap( b_list[count], b_list[count+1] ); // swap
done = false;
}
}
loop++;
}
return;
}
I fixed it by converting my string into a list of chars. then compared them and swapped the vector based on the results. Thanks for the help guys
void bubbub(vector<string> & b_list)
{
vector<string>::size_type loop = 0;
bool done = false;
while ((loop+1 < b_list.size()) && ! done)
{
done = true;
for (vector<string>::size_type count = 0;
count+1 != b_list.size(); count++)
{
string x;
string z;
char array[50];
char array2[50];
x = b_list[count];
z= b_list[count+1];
strncpy(array, x.c_str(), sizeof(x));
strncpy(array2, z.c_str(), sizeof(z));
if ( strcoll (array,array2) > 0 )
{
swap(b_list[count+1], b_list[count] ); // swap
done = false;
}
}
loop++;
}
return;
}
You can use std::string::compare instead of strcoll
#include <iostream>
#include <vector>
#include <stdio.h>
#include <cstring>
using namespace std;
inline void swap(string & a, string & b)
{
string c = b;
b = a;
a = c;
return;
}
void input_name(string&);
void sort_names(string&);
void repeat_pro(int&);
void sortArray(string, int);
void print_names(vector<string>& b_list);
void bubbub(vector<string> & b_list);
int main() {
vector<string> b_list;
string name;
int choice;
int count=0;
cout << "Welcome to the Business Sorting Program!" << endl;
do{
input_name(name);
b_list.push_back(name);
count++;
repeat_pro(choice);
bubbub(b_list);
cout<<"\n \n Your Businesses are:"<<endl;
for(int i=0; i < b_list.size() ; i++){
cout<<b_list[i]<<"\n";
}
cout << "\n\n";
}while(choice == 0);
cout << "Thanks for using this program"<<endl;
return 0;
}
void input_name(string &name){
cout << "Enter in the name of the business: ";
getline(cin, name);
}
void sort_names(string &name){
}
void repeat_pro(int &choice){
cout << "Do you want to enter in more names: ";
string answ;
cin>>answ;
cin.ignore(1000,'\n');
for (int x=0; x<answ.size(); x++){
answ[x] = tolower(answ[x]);
}
if (answ == "yes" || answ == "y"){
choice = 0;
}
else {
choice = 1;
}
}
void bubbub(vector<string> & b_list)
{
vector<string>::size_type loop = 0;
bool done = false;
while ((loop+1 < b_list.size()) && ! done)
{
done = true;
for (vector<string>::size_type count = 0;
count+1 != b_list.size(); count++)
{
string x;
string z;
x = b_list[count];
z = b_list[count+1];
if (z.compare(x) != 0 )
{
swap( b_list[count], b_list[count+1] ); // swap
done = false;
}
}
loop++;
}
return;
}
Output
Welcome to the Business Sorting Program!
Enter in the name of the business: hello
Do you want to enter in more names: yes
Your Businesses are:
hello
Enter in the name of the business: apple
Do you want to enter in more names: no
Your Businesses are:
apple
hello
Thanks for using this program
Program ended with exit code: 0
I am making a card game, Crazy Eights, I have everything done except I am having a problem with this AI. The AI needs to delete the card it just played from its card options. (Each player is dealt 5 cards, and if the card matches the suit, value, or an 8, it can play). SO far, the computer does play the correct card, it just doesn't remove the card so it can just keep playing and playing.
Main Function
#include <iostream>
#include <time.h>
#include "CCrazyEight.h"
#include "stdafx.h"
using namespace std;
void main()
{
// Calls Crazy eight and declares a variable, then runs the game
CCrazyEight E1;
E1.Menu();
}
CCard Header File
#pragma once
#include <iostream>
#include <time.h>
#include "stdafx.h"
using namespace std;
enum CardSuit { Hearts, Diamonds, Spades, Clubs };
class CCard
{
friend ostream & operator << (ostream &left, CCard right);
private:
int value;
CardSuit suit;
public:
// Constructor, sets default values to suit and value
CCard()
{
value = 1;
suit = Hearts;
}
// return thes value of the card
int GetValue()
{
return value;
}
// returns the suit of the card
CardSuit GetSuit()
{
return suit;
}
// Passes values to CDeck constructor, which then randomizes values for the cards
void InitCard(CardSuit s, int v)
{
suit = s;
if (v > 0 && v < 14)
{
value = v;
}
}
};
// outputs value and suit of the card
ostream & operator << (ostream &left, CCard right)
{
if (right.value < 11 && right.value > 1)
{
left << right.value;
}
else if (right.value == 1)
{
left << "Ace";
}
else if (right.value == 11)
{
left << "Jack";
}
else if (right.value == 12)
{
left << "Queen";
}
else if (right.value == 13)
{
left << "King";
}
left << " of ";
if (right.suit == Hearts)
{
left << "Hearts";
}
else if (right.suit == Diamonds)
{
left << "Diamonds";
}
else if (right.suit == Spades)
{
left << "Spades";
}
else if (right.suit == Clubs)
{
left << "Clubs";
}
return left;
}
CDeck Header File
#pragma once
#include <iostream>
#include "CCard.h"
#include <time.h>
using namespace std;
class CDeck
{
private:
CCard Deck[52];
int TopCard;
public:
// constructor
// randomizes the card numbers for suit and value
CDeck()
{
srand(time(NULL));
TopCard = 0;
for (int suit = 0; suit < 4; suit++)
{
for (int value = 1; value <= 13; value++)
{
Deck[((suit * 13) + value) - 1].InitCard((CardSuit)suit, value);
}
}
}
// shuffles the deck, completely randomizes the cards
void Shuffle()
{
TopCard = 0;
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 52; x++)
{
int SwapPosition = rand() % 52;
CCard temp;
temp = Deck[x];
Deck[x] = Deck[SwapPosition];
Deck[SwapPosition] = temp;
}
}
}
// Draw a card function that gives out a card when called for in crazy eight
CCard DrawCard()
{
if (TopCard > 51)
{
cout << "Deck is Empty." << endl;
}
else
{
return Deck[TopCard++];
}
}
};
CrazyEight header file
#pragma once
#include <iostream>
#include <time.h>
#include "CCard.h"
#include "CDeck.h"
#include "CPlayer.h"
using namespace std;
// Having an issue trying to get the computer to play something different
class CCrazyEight
{
private:
CDeck d1;
CCard c1;
int index;
CCard Topcard;
CPlayer Computer;
CPlayer Human;
public:
// constructor
// shuffles the deck to the played, and assigns the topcard to start the game
CCrazyEight()
{
d1.Shuffle();
c1 = d1.DrawCard();
Topcard = c1;
}
// plays the game, dealing cards to computer and human
void PlayGame()
{
cout << "Welcome to Crazy Eight!" << endl;
cout << "You get to go first!" << endl;
cout << endl;
for (int k = 0; k < 5; k++)
{
Computer.GetCard(d1.DrawCard());
Human.GetCard(d1.DrawCard());
}
}
// displays the topmost card
void TopCard()
{
cout << Topcard << endl;
}
// displays the cards for the human, and the options they can chose, and anything else they need about the game
void Menu()
{
PlayGame();
while (Human.NumCards() != 0 || Computer.NumCards() != 0)
{
cout << endl;
cout << "Top Card is: "; TopCard();
cout << endl;
cout << "You Have " << Human.NumCards() << " Cards Left." << endl;
cout << "Your play: " << endl;
cout << "0) Draw Card" << endl;
cout << Human << endl;
cin >> index;
if (index == 0)
{
Human.GetCard(d1.DrawCard());
Topcard = Topcard;
}
else if (index > 0 && index <= Human.NumCards())
{
Topcard = Human.CardPlayed(index, Topcard);
}
cout << endl;
Topcard = Computer.ComputerAI(Topcard);
cout << "Computer Played: " << Topcard << endl;
if (Computer.NumCards() > 0)
{
cout << "Computer has " << Computer.NumCards() << " Cards Left." << endl;
}
else
cout << "You Lose!" << endl;
}
}
};
CPlayer Header File
#pragma once
#include "CCard.h"
#include <time.h>
#include <iostream>
using namespace std;
class CPlayer
{
friend ostream & operator << (ostream &left, CPlayer right);
private:
CCard Hand[25];
CDeck d1;
int NumberOfCard;
bool Computer;
const int MaxHand = 26;
bool TopCard;
public:
// Constructor for the computer, sets computer to true
CPlayer(bool computer = true)
{
Computer = computer;
NumberOfCard = 0;
}
// Computers AI, checks and sees if they can play, if not then they draw until they can play, or they play a card that is playable******Easy mode**** Lol
CCard ComputerAI(CCard Topcard)
{
for (int i = 0; i < NumberOfCard; i++)
{
if (Hand[i].GetSuit() == Topcard.GetSuit() || Hand[i].GetValue() == 8 || Hand[i].GetValue() == Topcard.GetValue())
{
for (int i = 0; i < NumberOfCard; i++)
{
if (Topcard.GetSuit() == Hand[i].GetSuit() && Hand[i].GetValue() != 8)
{
NumberOfCard--;
return Hand[i];
}
}
for (int i = 0; i < NumberOfCard; i++)
{
if (Topcard.GetValue() == Hand[i].GetValue() && Hand[i].GetValue() != 8)
{
NumberOfCard--;
return Hand[i];
}
}
for (int i = 0; i < NumberOfCard; i++)
{
if (Hand[i].GetValue() == 8)
{
NumberOfCard--;
return Hand[i];
}
}
}
else
{
GetCard(d1.DrawCard());
}
}
}
// Is not the computer, sets computer to fasle. i.e. its the human
void Human()
{
Computer = false;
}
int NumCards()
{
return NumberOfCard;
}
// will hand out a card
bool GetCard(CCard NewCard)
{
if (NumberOfCard < MaxHand)
{
// Now is taking the card
Hand[NumberOfCard] = NewCard;
NumberOfCard++;
return true;
}
else
return false;
}
// subtracts the card that was played from the deck of the human user
CCard CardPlayed(int index, CCard Topcard)
{
if (CheckCard(index, Topcard) == true)
{
CCard TempTopcard = Hand[index - 1];
for (int i = index - 1; i <= NumberOfCard - 1; i++)
{
Hand[i] = Hand[i + 1];
}
NumberOfCard--;
return TempTopcard;
}
else
cout << "Not a valid Card." << endl;
return Topcard;
}
// checks the card and see's if it is playable or not
bool CheckCard(int index, CCard Topcard)
{
if (Hand[index - 1].GetSuit() == Topcard.GetSuit() || Hand[index - 1].GetValue() == 8 || Hand[index - 1].GetValue() == Topcard.GetValue())
{
return true;
}
else
return false;
}
};
// overloads the output to cout the card
ostream & operator << (ostream &left, CPlayer right)
{
for (int i = 0; i < right.NumberOfCard; i++)
{
left << i + 1 << ") " << right.Hand[i] << endl;
}
return left;
}