Reputation: 268
I'm learning c++ and as an exercise with arrays and user input, I'm trying to write a simple poker application.
Since I'm at the begin of this course, all I know about the c++ language is that the execution of the code is demanded to the main()
function. I've write some lines of code that is the base of the final app, it works fine for now. I want to implement a loop to re run the app based on the user input and on a condition that for the scope of the app will be the amount of th fish variable quantity after every execution. How I can achieve this? Another question is about the use of random elements from an array. Is there any good reference where I can learn how to do this?
This is my code:
#include <iostream>
using namespace std;
int main(){
string name;
int bet;
int fish = 100;
char seed[4][10] = {"hearts","clubs","diamonds","spades"};
int cards[9] = {2,3,4,5,6,7,8,9,10};
std::cout << "Welcome in PokerBash! Please enter your name:" <<std::endl;
std::cin >> name;
std::cout << "Your name is " << name <<std::endl;
std::cout << "You have a credit of:" << fish <<std::endl;
std::cout << "Please enter your bet:" <<std::endl;
std::cin >> bet;
std::cout << "Your cards are " << seed[2] << " " << cards[3] << " " << seed[1] << " " << cards[7] <<std::endl;
std::cout << "Your credits after this bet:" << fish - bet <<std::endl;
return 0;
}
Upvotes: 0
Views: 693
Reputation: 8126
You can do a loop that stops if the user wants to or fish
is less than 0
by making a while
loop that depends on some boolean playing
that is initially true
. So if one of the two events happen, set playing
to be false
and the loop stops:
int main() {
//variables
bool playing = true;
while (playing) {
int fish = 100;
//poker game
if (fish < 0) { //no money
playing = false;
}
else {
char input;
std::cout << "would you like to play again? (y/n): ";
std::cin >> input;
if (input != 'y') {
playing = false;
}
}
}
}
as you can see, this repeats until I enter something that isn't 'y'
:
would you like to play again? (y/n): y
would you like to play again? (y/n): y
would you like to play again? (y/n): n
to choose a random element from an array you would use the utilities from <random>
like their std::mersenne_twister_engine
. To get a random element from an array you would basically just need create a random number and use that as the arrays index:
#include <iostream>
#include <random>
int main() {
std::random_device rd;
std::mt19937_64 engine(rd());
std::uniform_int_distribution<int> distribution(0, 8);
int cards[9] = { 2,3,4,5,6,7,8,9,10 };
while (true) {
std::cout << cards[distribution(engine)] << '\n';
}
}
some important things from here:
std::random_device rd;
std::mt19937_64 engine(rd());
is done only once (never in a loop). It is for initializing your pseudo random generator engine
.
std::uniform_int_distribution<int> distribution(0, 8);
adds a distribution. Note that, because your int cards[9]
has 9
elements, the range has to go from 0
to 8
as arrays start at 0
and end at their size - 1, as you might probably already know. :)
Running this you can see it randomly prints out the card numbers from 2
to 10
:
2
10
7
9
2
4
9
10
8
9
8
6
8
2
10
These are your helping points to implement further. I add some more things I noticed about your code but are not necessary to the question itself.
You should note that you should not use namespace std
- you can read here why.
Also, instead of:
char seed[4][10] = { "hearts","clubs","diamonds","spades" };
use:
std::string seed[4] = { "hearts","clubs","diamonds","spades" };
To use std::string
include the <string>
header.
you wrote std::cin >> name;
but this doesn't work for strings with spaces, like look here:
Welcome in PokerBash! Please enter your name:
Stack Danny
Your name is Stack
To get the full name, use
std::getline(std::cin, name);
Upvotes: 3
Reputation: 2866
Try this,
#include <iostream>
using namespace std;
int main()
{
string name;
int bet;
int fish = 100;
char seed[4][10] = {"hearts", "clubs", "diamonds", "spades"};
int cards[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
while (1)
{
std::cout << "Welcome in PokerBash! Please enter your name ( Enter q to quit ):" << std::endl;
std::cin >> name;
if(name == "q")
exit(0);
std::cout << "Your name is " << name << std::endl;
std::cout << "You have a credit of:" << fish << std::endl;
std::cout << "Please enter your bet:" << std::endl;
std::cin >> bet;
std::cout << "Your cards are " << seed[2] << " " << cards[3] << " " << seed[1] << " " << cards[7] << std::endl;
std::cout << "Your credits after this bet:" << fish - bet << std::endl;
}
return 0;
}
Upvotes: 1