this is c++ programming.. try each sample run and get same result please!. pleas
ID: 3840590 • Letter: T
Question
this is c++ programming..
try each sample run and get same result please!.
please, dont make code too complicated and difficult to understand.
thank you in advance.
In this project, you will create a simple card game. At the start of the game, ask the player his/her name and the amount of money tostart. Each turn of the game then proceeds as follows: The player is asked how much money he/she wants tobet. Then, the player is dealt one card face up. The player is then asked whether the next card dealt will be higher inrank or lower in rank than the first card. Ifthe player guesses correctly, he/she wins an amount equal to the bet. If not (including if the dealt card is of the same rank), the player lose his/her bet. You may ume the player always bets an integer amount that is neither negative nor greater than his/her current balance The game continues turns until the player either has a balance equal to or above 5 times their start amount, or the player's money is 0 In order to dothis assignment, you must create two user-defined classes, one called Card and one called Player. The definitions of these classes and their member functions should each be contained in separate files calledCard.h and Player.h. IMPORTANT: for this assignment, you are turning in three files: the class definitions Card.h and Player.h, as well as hw5.cpp, the source file that creates your program.Explanation / Answer
Following is the required code :
//Card.h
#include <string>
using namespace std;
class Card{
int rank;
int suit;
public:
Card(){
rank = (rand()%13) + 1;
suit = (rand()%4) + 1;
}
int get_rank(){ return rank; }
string get_rank_string(){
string names[] = {"Ace","Two","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Jack","Queen","King"};
return names[ rank - 1 ];
}
string get_suit_string(){
if( suit == 1 ){ return "Diamonds"; }
else if( suit == 2 ){ return "Hearts"; }
else if( suit == 3 ){ return "Spades"; }
else if( suit == 4 ){ return "Clubs"; }
}
};
//Player.h
class Player{
string name;
int current_balance;
int money_to_start;
public:
Player( string nameIs, int money_to_start_with ){
name = nameIs;
money_to_start = money_to_start_with;
current_balance = money_to_start_with;
}
void set_balance( int balance ){
current_balance = balance;
}
int get_balance( ){
return current_balance;
}
void update_balance( int addition ){ //addition can be negative too
current_balance = current_balance + addition;
}
string get_name(){
return name;
}
};
//hw5.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Card.h"
#include "Player.h"
using namespace std;
int main(){
srand(time(NULL));
string playerName;
int balance;
cout << "Player, what is your name? ";
cin >> playerName;
cout << "How much money would you like to start? ";
cin >> balance;
Player thePlayer( playerName, balance );
while(1){
int bet;
cout << endl << endl << thePlayer.get_name() << ", you have $";
cout << thePlayer.get_balance() << "." << endl;
bet = thePlayer.get_balance() + 100;
while( (bet<0) or (bet > thePlayer.get_balance()) ){
cout << "How much do you want to bet? ";
cin >> bet;
}
Card newCard;
cout << "You draw a " << newCard.get_rank_string() << " of ";
cout << newCard.get_suit_string() << "." << endl;
char choice = 'q';
while( !(choice=='l' or choice=='h') ){
cout << "Will the next card be higher of lower? Enter ";
cout << ""h" for higher, or "l" for lower: ";
cin >> choice;
}
Card otherCard;
cout << "You draw a " << otherCard.get_rank_string() << " of ";
cout << otherCard.get_suit_string() << "." << endl;
bool win = false;
if( choice == 'l' ){
if( otherCard.get_rank() < newCard.get_rank()){
win = true; }
}
if( choice == 'h' ){
if( otherCard.get_rank() > newCard.get_rank()){
win = true; }
}
if( win ){
cout << "WIN!!! Congratulations "<< thePlayer.get_name() << ", you win $" << bet << "!" << endl;
thePlayer.update_balance( bet );
}
else{
cout << "LOSE!!! Too bad " << thePlayer.get_name() << ", you lose $" << bet << "." << endl;
thePlayer.update_balance( -bet );
}
if( thePlayer.get_balance() <= 0 ){
cout << " Too bad " << thePlayer.get_name() << ". You are out of money! You lose." << endl;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.