Problem in making a game

0 Superman HC · July 4, 2015
Hey there... 
I am trying to create the most popular game of cards in India named as "Teen Patti" where i have used the concept of random generators...
Here's d code: 
#include <iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
int main(){

string arr1[9]={"1","2","3","4","5","6","King","Jack","Queen"};
string arr2[4]={"Heart","Diamond","Spade","Clubs"};
srand(time(0));

for(int i=0;i<=2;i++){
    int x = 1+(rand()%8);
    int y = 1+(rand()%3);
    cout<<"Press enter to play"<<endl;
    cin.get();
    cout<<arr1[x]<<" of "<<arr2[y]<<endl<<endl;
}

return 0;
}

Can anybody tell me the solution to the code so that the card do not repeat itself lyk dis as there is only one card of a specific type.../images/forum/upload/2015-07-04/c8fd98640d97c198b543afc990f86684.png

Post a Reply

Replies

Oldest  Newest  Rating
0 Ben Elson · July 6, 2015
Based on what I understood from the random if you press enter too fast it doesn't have time to count another second to move on to the next variable as it's not  a true random but an algorithm based off your srand time(0);

However I modified the code a bit as the arrays start on 0 so your +1 would skip 1 and heart every time.  It seemed to work fine on my computer as long as I don't hit enter too fast.

#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
int main(){

string arr1[9]={"1","2","3","4","5","6","King","Jack","Queen"};
string arr2[4]={"Heart","Diamond","Spade","Clubs"};
srand(time(0));

for(int i=0;i<=2;i++){
int x = rand()%8;
int y = rand()%3;
cout<<"Press enter to play"<<endl;
cin.get();
cout<<arr1[x]<<" of "<<arr2[y]<<endl<<endl;
}

return 0;
}
0 Gurpreet Singh · July 7, 2015
it would be better to write cin.get(); outside for loop so that this fast pressing enter problem goes off and we have to press enter only once to get the 3 cards. and why didnt you included 7,8,9,10? 
0 Jon Z. · July 14, 2015
try something like this


//...

int xx[3] = {0};
int yy[3] = {0};
int x;
int y;

for(int i=0;i<=2;i++){
    do{
        x = 1+(rand()%8);
        y = 1+(rand()%3);
    }while(check(xx, yy, x, y) != 0);

xx = x;
yy = y;

    cout<<"Press enter to play"<<endl;
    cin.get();
    cout<<arr1[x]<<" of "<<arr2[y]<<endl<<endl;
}

//...

int check(int xx[], int yy[], int x, int y){
int i;
for(i=0; i<3; i++){
if(xx == x && yy == y){
return 1;

}

return 0;
}
  • 1

C++

107,276 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator