//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <iostream>
usingnamespace std;
//Programmer defined data types
//playing cards
struct PlayingCard
{
int value;
int suit;
}; //
//Special compiler dependent definitions
//NONE
//global constants/variables
constint NUM_PLAYERS = 2;
//Programmer defined functions
void getCard(PlayingCard *);
void getSuited(PlayingCard *);
bool getYesorNo();
//main program
int main()
{
//Data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
PlayingCard player[NUM_PLAYERS];
int index;
int i = 0;
// introduction
cout << "Objective: This program will serve as a template for all programs\n written in this course.\n";
cout << "Programmer: Teacher\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: TDM MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
//
do
{
for (index = 1; index < NUM_PLAYERS; index++) getCard(&player[index]);
//
cout << "Computer's card is ";
getSuited(&player[1]);
cout << endl;
cout << "Human's card is ";
getSuited(&player[2]);
cout << endl <<endl;
//who won
if (player[1].value > player[2].value)
{
cout << "[Computer wins!] " <<endl;
getYesorNo();
}
elseif (player[1].value < player[2].value)
{
cout << "[Human wins!] " <<endl;
getYesorNo();
}
elseif (player[1].value == player[2].value)
{
cout << "[It's a tie...UNACCEPTABLE] " <<endl;
}
}while (getYesorNo());
}//main
//
bool getYesorNo()
{
char letsGo;
bool getYesorNo = true;
cout <<"Do you want to play again? [Y for yes/N for no] ";
cin >> letsGo;
cin.ignore(1000, 10);
cout << endl;
if (letsGo == 'n' || letsGo == 'N') returnfalse;
elseif (letsGo == 'y' || letsGo == 'Y') returntrue;
elsereturn getYesorNo();
}//playAgn
//
void getCard(PlayingCard *a)
{
//data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
int i;
int u;
for (i = 0; i < 13; i++)
{
for (u = 0; u < 4; u++)
a[i].value = 2 + (rand() % 13);
a[u].suit = rand() % 4;
}
}
//
void getSuited(PlayingCard *a)
{
//data
//
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
int i;
i = rand() % 13;
switch (a[i].value)
{
case 14:
cout << "Ace";
break;
case 11:
cout << "Jack";
break;
case 12:
cout << "Queen";
break;
case 13:
cout << "King";
break;
default:
cout << a[i].value;
}
i = rand() % 4;
switch (a[i].suit)
{
case 0:
cout << " of Spades";
break;
case 1:
cout << " of Diamonds";
break;
case 2:
cout << " of Hearts";
break;
case 3:
cout << " of Clubs";
} // switch to determine card suit
}
//libraries
#include <cstdlib> //random number usage
#include <ctime> //random number usage
#include <iostream>
usingnamespace std;
//Programmer defined data types
//playing cards
struct PlayingCard
{
int value;
int suit;
}; //
//Special compiler dependent definitions
//NONE
//global constants/variables
constint NUM_PLAYERS = 2;
//Programmer defined functions
void getCard(PlayingCard *);
void getSuited(PlayingCard *);
bool getYesorNo();
//main program
int main()
{
//Data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
PlayingCard player[NUM_PLAYERS];
int index;
int i = 0;
// introduction
cout << "Objective: This program will serve as a template for all programs\n written in this course.\n";
cout << "Programmer: Teacher\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: TDM MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
//
do
{
for (index = 1; index < NUM_PLAYERS; index++) getCard(&player[index]);
//
cout << "Computer's card is ";
getSuited(&player[1]);
cout << endl;
cout << "Human's card is ";
getSuited(&player[2]);
cout << endl <<endl;
//who won
if (player[1].value > player[2].value)
{
cout << "[Computer wins!] " <<endl;
getYesorNo();
}
elseif (player[1].value < player[2].value)
{
cout << "[Human wins!] " <<endl;
getYesorNo();
}
elseif (player[1].value == player[2].value)
{
cout << "[It's a tie...UNACCEPTABLE] " <<endl;
}
}while (getYesorNo());
}//main
//
bool getYesorNo()
{
char letsGo;
bool getYesorNo = true;
cout <<"Do you want to play again? [Y for yes/N for no] ";
cin >> letsGo;
cin.ignore(1000, 10);
cout << endl;
if (letsGo == 'n' || letsGo == 'N') returnfalse;
elseif (letsGo == 'y' || letsGo == 'Y') returntrue;
elsereturnfalse;
}//playAgn
//
void getCard(PlayingCard *a)
{
//data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
int i;
int u;
for (i = 0; i < 13; i++)
{
for (u = 0; u < 4; u++)
a[i].value = 2 + (rand() % 13);
a[u].suit = rand() % 4;
}
}
//
void getSuited(PlayingCard *a)
{
//data
//
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
int i;
i = rand() % 13;
switch (a[i].value)
{
case 14:
cout << "Ace";
break;
case 11:
cout << "Jack";
break;
case 12:
cout << "Queen";
break;
case 13:
cout << "King";
break;
default:
cout << a[i].value;
}
i = rand() % 4;
switch (a[i].suit)
{
case 0:
cout << " of Spades";
break;
case 1:
cout << " of Diamonds";
break;
case 2:
cout << " of Hearts";
break;
case 3:
cout << " of Clubs";
} // switch to determine card suit
}
That's because of line 60 and 65. You ask them if they want to play again, even though there's no reason to. The input they give in those function calls doesn't do anything. And then when the condition calls the function, it calls the function again which makes the user have to input their answer twice.
however i am still having issues getting this to run properly.
it should deal two random cards
take them out of the deck so they cant be played again
if computer value is larger computer wins
if user vaule is larger user wins
if its a tie play till there is a winner
void getCard(PlayingCard *a, PlayingCard *b)
{
//data
int d = 0;
int i;
bool iV = false;
do
{
srand(time(0)); //don't reseed the generator, line 31 in main() is enough
a[0].value = 2 + (rand() % 13); //`a' is not an array, confusing
a[0].suit = rand() % 3; //may use a->value, a->suit
for (i = 2; i < 13; i++)
{
if (a[0].value && a[0].suit == b[i].value && b[0].suit) //b is definitaly not an array, out of bounds access
{
i = 0;
d++;
}
elseif (a[0].value && a[0].suit == b[i].value && b[1].suit)
//¿want to compare both members? then compare both members
//a->value == b->value and a->suit == b->suit
{
i = 0;
d++;
}
elseif (a[0].value && a[0].suit == b[i].value && b[2].suit)
{
i = 0;
d++;
}
elseif (a[0].value && a[0].suit == b[i].value && b[3].suit)
{
i = 0;
d++;
}
else
{
b[a[0].value].value = a[0].value; //¿what?
b[a[0].suit].suit = a[0].suit;
iV = true;
break; //... ¿what's the point of iV then?
}//else
}//for
}while (iV == false); //while (not iV); learn what is a boolean
}
void getCard(PlayingCard *a, PlayingCard *b)
{
//data
int d = 0;
int i;
do
{
a[0].value = 2 + (rand() % 13); //these are arrays in the defined to work in the struct form??
a[0].suit = rand() % 3; //these are arrays in the defined to work in the struct form??
for (i = 2; i < 13; i++)
{
if (a[0].value == b[i].value && a[0].suit == b[0].suit) //same thing i had but reworded??
{
i = 0;
d++;
}
elseif (a[0].value == b[i].value && a[0].suit == b[1].suit)//thing i had but reworded??
{
i = 0;
d++;
}
elseif (a[0].value == b[i].value && a[0].suit == b[2].suit)//same thing i had but reworded??
{
i = 0;
d++;
}
elseif (a[0].value == b[i].value && a[0].suit == b[3].suit) //same thing i had but reworded??
{
i = 0;
d++;
}
else //if the user's current card does match any already played save that card
{
b[a[0].value].value = a[0].value; //save the value being output in the deck array so it can't be picked again
b[a[0].suit].suit = a[0].suit; //save the vaule being output in the deck array so it cant be picked again
break; //this has purpose now
}//else
}//for
}while (false); // better?
}