How can I improve my code? (C++)

  • Thread starter Thread starter Rechtfertigung
  • Start date Start date
R

Rechtfertigung

Guest
(If this type of question is not allowed please let me know and I will promptly delete it)

Im learning C++ and do not have any peers that can look over my code or show me better ways to solve problems. If anyone could look over a snippet of my code and give me any tips for best practices or any critiques on pieces of my code that would be greatly appreciated.

Context:

I have a practice program that I am working on, its a card game that that will have three players and each player draws three cards. Each suit has a different value that is added onto the players score, along with the actual card value. For example 7 of Hearts would add 7 points to the players score plus another 5 for being Hearts. there are 3 rounds and each player will draw 3 times per round. and by the end of the round the player with the highest score wins, and whoever has the highest score at the end of the 3 rounds wins the game.

using namespace std;

struct CardModel
{
string card_Suits = "";
int card_value = 0;
};

struct Players
{
string name;
CardModel player_Inventory[3];
int current_Score = 0;
};

//Funtion Prototypes
void reset_Player_Score();
void play();
void draw(Players current_draw[]);
void get_score(Players &player);

int main()
{
play();
return 0;
}

void play()
{
cout << "Welcome to card flash!" << endl;
Players players[3];

players[0].name = "Josh";
players[1].name = "Nick";
players[2].name = "Robert";

cout << endl << "*" << setfill('-') << setw(15) << "Players" << setfill('-') << setw(9) << "*";
cout << endl << "*" << "*" << setfill(' ') << setw(12) << players[0].name << setfill(' ') << setw(10)<< "*" << "*";
cout << endl << "*" << "*" << setfill(' ') << setw(12) << players[1].name << setfill(' ') << setw(10) << "*" << "*";
cout << endl << "*" << "*" << setfill(' ') << setw(13) << players[2].name << setfill(' ') << setw(9) << "*" << "*";
cout << endl << "*" << setfill('-') << setw(24) << "*" << endl;

cout << endl << "Game starting.." << endl;
for ( int i = 0 ; i < 3; i++)
{
cout << setfill(' ') << setw(55) << "Round " << i + 1 << endl;
cout << "First Draw:" << endl;
for (int k = 0; k < 3; k++)
{
draw(players);
get_score(players[k]);
cout << players[k].name << "'s cards:" << endl;
for (int j = 0; j < 3; j++)
{
cout << players[j].player_Inventory[j].card_value << " of " << players[j].player_Inventory[j].card_Suits << endl;
}
cout << "score: " << players[k].current_Score << endl << endl;
}
}
}

void draw(Players current_draw[])
{
string card_Suits[4] = {"Diamonds", "Spades", "Clubs", "Hearts"};
int card_value[10] = { 1,2,3,4,5,6,7,8,9,10 };
std::random_device SuitChoice;
std::mt19937 random(SuitChoice());
std::uniform_int_distribution<std::mt19937::result_type> suit_picker(0, 3);

std::random_device CardValue;
std::mt19937 rng(CardValue());
std::uniform_int_distribution<std::mt19937::result_type> value_Picker(0, 9);

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
current_draw.player_Inventory[j].card_Suits = card_Suits[suit_picker(random)];
current_draw.player_Inventory[j].card_value = card_value[value_Picker(rng)];
}
}
}

void get_score(Players &player)
{
for (int i = 0; i < 3; i++)
{
player.current_Score += player.player_Inventory.card_value;
if (player.player_Inventory.card_Suits == "Diamonds")
{
player.current_Score += 5;
}
else if (player.player_Inventory.card_Suits == "Hearts")
{
player.current_Score += 4;
}
else if (player.player_Inventory.card_Suits == "Clubs")
{
player.current_Score += 3;
}
else if (player.player_Inventory.card_Suits == "Spades")
{
player.current_Score += 2;
}
}
}



Recht

Continue reading...
 
Back
Top