My code any solution

My code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

int main()
{

string lol[53]={0,"1heart","2heart","3heart","4heart","5heart","6heart","7heart","8heart","9heart","10heart","Jheart","Qheart","Kheart",
"1diamond","2diamond","3diamond','4diamond","5diamond","6diamond","7diamond","8diamond","9diamond","10diamond","Jdiamond","Qdiamond","Kdiamond",
"1spade","2spade","3spade","4spade","5spade","6spade","7spade","8spade","9spade","10spade","Jspade","Qspade","Kspade",
"1season","2season","3season","4season","5season","6season","7season","8season","9season","10season","Jseason","Qseason","Kseason"};
srand(time(0));

string I1,I2,I3,I4;

I1=lol[1+rand()%53];
I2=lol[1+rand()%53];
I3=lol[1+rand()%53];
I4=lol[1+rand()%53];

cout<<"      Paper That have been Choseen are :     "<<endl;
cout<<"Paper1 : "<<I1<<endl;
cout<<"Paper2 : "<<I2<<endl;
cout<<"Paper3 : "<<I3<<endl;
cout<<"Paper4 : "<<I4<<endl;

if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Jheart")
cout<<"You Win"<<endl;
if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Jdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Jspade")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Jseason")
cout<<"You Win"<<endl;
if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Qheart")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Qdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Qspade")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Qseason")
cout<<"You Win"<<endl;
if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Kheart")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Kdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Kspade")
cout<<"You Win"<<endl;
else if(I1=="Jheart"&&I2=="Qheart"&&I3=="Kheart"&&I4=="Kseason")
cout<<"You Win"<<endl;

else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Jheart")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Jdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Jspade")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Jseason")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Jheart")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Qdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Qspade")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Qseason")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Jheart")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Kdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Kspade")
cout<<"You Win"<<endl;
else if(I1=="Jdiamond"&&I2=="Qdiamond"&&I3=="Kdiamond"&&I4=="Kseason")
cout<<"You Win"<<endl;

else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Jheart")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Jdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Jspade")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Jseason")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Qheart")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Qdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Qspade")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Qseason")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Kheart")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Kdiamond")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Kspade")
cout<<"You Win"<<endl;
else if(I1=="Jspade"&&I2=="Qspade"&&I3=="Kspade"&&I4=="Kseason")
cout<<"You Win"<<endl;




    return 0;
}


i need Change These if in my code With any idea help me .



1- if there are 4 Picture (k,Q,J...) any 4 from them >> player win.
2- if there are 3 or 2 picture From (k,Q,J...0)>>> Player play again
3- if there are 4 numbers or 4 picture are same like(1spade ,1diamond,1heart,1season) and pic like it >> Player Win
4- if there are 4 serial numbers like (1spade,2spade,3spade,4spade)like that >> Player Win.
5- if there are 4 same kind like (2sapde.5spade.8spade.1spade) like that but not serial >>> player play again.

I hope any one help me :)
Last edited on
So many if, else if statements is generally bad and makes it very hard to go through your code.

Practice on different approaches to each problem and you will become much better at working out solutions.

Last edited on
To avoid so many if-else constructions, try making two strings out of one representing name of the card (for example "5spade" becoming "5" and "spade") and then compare just parts you need (for example if first part of all strings is the same, let's say "5", case 3 is true).

Also, all cases with same outcome can go into one branch:
1 or 3 or 4 => player wins
2 or 5 => player plays again

Another thing, variables of char type you can compare by the fact if they belong to the range which contains numbers or letters. So, you may want to keep that first part of the string in char variable and check if it is in range between 49 and 57 for numbers or in range 65-90 (upper case letters) or 97-122 (lower case letters).
Last edited on
OK, I had some spare time, so I made rough code idea. You will probably need to smooth it out. Also, I haven't tested it, so there might be some errors on the way.

To break each of I1-I4 strings you can do the following:
1
2
3
char i1=I1[0];
string II1;
for(int i=1; i<I1.length(); i++)II1[i-1]=I1[i];


To avoid multiple testing in if-else part you may want to make two user functions, one for detecting scales and one for counting number of pictures drawn.
For finding number of pictures user function may look something like this:
1
2
3
4
5
6
7
8
int pictures(char n1, char n2, char n3, char n4){
  int Pnumber(0);
  if(n1>='J' && n1<='Q')Pnumber++;
  if(n2>='J' && n2<='Q')Pnumber++;
  if(n3>='J' && n3<='Q')Pnumber++;
  if(n4>='J' && n4<='Q')Pnumber++;
  return Pnumber;
}

Function which detects scales is just a bit more complex, but nothing too complicated. Also, one needs to predict scales of type 8,9,10,J; 9,10,J,Q and 10,J,Q,K. I used sorting function built in algorithm library. If you want to avoid it you can make your own sorting function. There are multiple choices of algorithms on the internet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
bool scale(char n1, char n2, char n3, char n4){
  int Pnumb=pictures(n1,n2,n3,n4);
  switch(Pnumb){
  case 4: return false;
  case 3:
    int C[4];
    C[0]=(int)n1;
    C[1]=(int)n2;
    C[2]=(int)n3;
    C[3]=(int)n4;
    sort(C,C+4);
    if((char)(C[0])!='10')return false;
    else{
      if((char)(C[1])=='J' && (char)(C[2])=='Q' && (char)(C[3])=='K')return true;
      else return false;
    }
  case 2:
    int C[4];
    C[0]=(int)n1;
    C[1]=(int)n2;
    C[2]=(int)n3;
    C[3]=(int)n4;
    sort(C,C+4);
    if((char)(C[0])=='9' && (char)(C[1])=='10' && (char)(C[2])=='J' && (char)(C[3])=='Q')return true;
    else return false;
  case 1:
    int C[4];
    C[0]=(int)n1;
    C[1]=(int)n2;
    C[2]=(int)n3;
    C[3]=(int)n4;
    sort(C,C+4);
    if((char)(C[0])=='8' && (char)(C[1])=='9' && (char)(C[2])=='10' && (char)(C[3])=='J')return true;
    else return false;
  case 0:
    int C[4];
    C[0]=(int)n1;
    C[1]=(int)n2;
    C[2]=(int)n3;
    C[3]=(int)n4;
    sort(C,C+4);
    if(C[1]==C[0]+1 && C[2]==C[1]+1 && C[3]==C[2]+1 && C[4]==C[3]+1)return true;
    else return false;
  }
} 


So, with these two function, all your cases come down to the following if-elseif construction:
1
2
3
4
5
6
7
8
int N=pictures(i1,i2,i3,i4);
bool S=scale(i1,i2,i3,i4);
if(N==4 || (i1==i2 && i2==i3 && i3==i4 && i4==i1) ||
   (S && !strcmp(II1,II2) && !strcmp(II2,II3) && !strcmp(II3,II4) && !strcmp(II4,II1))
  cout<<"You win!";
else if(N==3 || N==2 || !strcmp(II1,II2) && !strcmp(II2,II3) && !strcmp(II3,II4) && !strcmp(II4,II1))
  cout<<"Play again!";
else cout<<"You lose!";


Hope this was helpful :)
Last edited on
I Hope So :) i see it now :) Thanks
Topic archived. No new replies allowed.