+ 1
Basic c++ game HELP pls
Hi community. Im trying to make a basic game. The main idea is "PC" vs "Player" bet game with this structure: Menu (start game, exit) game idea--> pc and player got 100 coins each, player make a bet "1-100 coins". Game end when player or pc got 0 coins. game system--> we got 3 boxes ( red, orange, green ) and pc ramdom choose 1 box, if player choose same box then pc = win,, if no =lose. 1. ¿ How make this random choice for pc between 3 boxes ? "pc_choice" I will put all new doubts in commends and code when finish.
11 Antworten
+ 10
// Geez, I did it again, didn't I?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int PC_coin = 100;
int user_coin = 100;
int menu_selection;
int bet_amount;
string box_choice;
string boxes[3] = {"red", "orange", "green"};
cout << "MENU" << endl;
cout << " - Input 1 to start" << endl;
cout << " - Input any other key to quit" << endl << endl;
cin >> menu_selection;
cin.clear(); cin.ignore(512, '\n');
if (menu_selection != 1)
{
return 0;
}
while (PC_coin > 0 && user_coin > 0)
{
cout << "Please input box choice : ";
cin >> box_choice;
cout << "Please input bet amount : ";
cin >> bet_amount;
cin.clear(); cin.ignore(512, '\n');
if (box_choice == boxes[rand()%3])
{
user_coin -= bet_amount;
PC_coin += bet_amount;
cout << "You lost." << endl;
}
else
if (box_choice == boxes[0] || box_choice == boxes[1] || box_choice == boxes[2])
{
user_coin += bet_amount;
PC_coin -= bet_amount;
cout << "You win." << endl;
}
else
{
cout << "Invalid box type." << endl << endl;
}
}
return 0;
}
+ 8
@Ace
One day people will be doing srand(no_repeat.Ace()) where the seed will be read from a huge library of oscillating random numbers written by you. :P
+ 7
@Yuriy az
For now just know that srand(time(0)) randomises rand() so that rand() can "truly" output a random number. Or else, rand() is actually predictable after a few runs.
boxes[rand()%3] has possible results for boxes[0], boxes[1] and boxes[2], which corresponds to the three different boxes. The modulus operator "%" is important to control the range of numbers which you get. For this case, the array size is 3 (indexes 0, 1 and 2), hence, by doing rand()%3, possible results are 0, 1 and 2.
+ 5
@rei what is cin.clear(); cin.ignore(512, '\n'); Since you r using two cin , so you have to put this cin.clear(); cin.ignore(512, '\n'); after the cin definition???
+ 1
Rei you are a cheater! <3
in your code pc always win player hahahaha
if you want player win
if (box_choice != boxes[rand()%3])
-------------------------------------------------------
Game is here ( https://code.sololearn.com/c7K05irWcVVO/#cpp ) run it in codeblocks or others.
game still got some defects.
0
Rei why we use time in that code? " srand(time(0)) "
Also i can use [rand()] for anything to make random choice between different options ?
p.d: Big Rei <3
0
any sugestion whats wrong here ?
!if i bet more then i got first and later more then 300 i dont got the cout " you dont follow my rules! ? "
do
{
if ((betv<300) && (betv>hum_coin)) {{cout<<" YOU FORGET THE RULES?"<<endl; cin>>betv; cin.clear();cin.ignore(512, '\n');}}
else if (betv<=10) cout<<endl<<" !!PUSSY¡¡"<<endl;
else if (betv<=hum_coin) cout<<endl<<" great"<<endl;
else if (betv>=300) cout<<endl<<" YOU DONT FOLLOW MY GAME RULES !!!!? ok, is fine <3"<<endl;
} while((betv<300) && (betv>hum_coin)); // little chaotic code maybe