Problems in Jack n Poy Game

Hi.. so i have 4 problems in my program.
#1.Whatever i chose, the computer will always play (1st)Paper,(2nd)Paper and (3rd)Scissor.
For example:
 
(1st play)
Enter your choice:1/2/3 
You chose rock/paper/scissors
Computer got paper
Computer won
(2nd play)
Enter your choice:1/2/3 
You chose rock/paper/scissors
Computer got paper
Computer won
(3rd play)
Enter your choice:1/2/3 
You chose rock/paper/scissors
Computer got scissors
You won

Is my code correct?? comp=1+rand()%3;

#2.I want to print out the current score every time i chose.
For example:
1st play i win/lose/tie then the score will display then 2nd the score will change.. and so on..(CAN IT BE??)


#3.When i enter an invalid number the computer will play itself.
For example:

Invalid Input
Computer got rock 

How can i stop the response of the computer.


#4.It is the same with #3 when i input a non-numbers the game will immediately end.

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
109
110
111
112
113
114
115
116
117
118
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()

{
int userinput, computer, computer2, counter, score = 0, cscore = 0;

{
clrscr();
printf("\t\t\tLets play Rock, Paper, and Scissor\n");



for (counter = 1; counter <=3; counter++){
printf("\n");
printf("\t1) Rock    \t");
printf("\t2) Paper   \t");
printf("\t3) Scissors\t");
printf("\n\n");
printf("\t\t\t    Enter your choice: ");
scanf("%d",&userinput);

printf("\n");


if (userinput == 1){
printf("\t\t\t\tYou chose rock\n");
}


else if (userinput == 2){
printf("\t\t\t\tYou chose paper\n");
}

else if (userinput == 3){
printf("\t\t\t\tYou chose scissors\n");
}

else
printf("\t\t\t\tInvalid Input\n");

computer = 1+rand() % 3;


if (computer == 1){
printf("\t\t\t\tComputer got rock\n");
computer2 = 1;
}

else if (computer == 2){
printf("\t\t\t\tComputer got paper\n");
computer2 = 2;
}
else if (computer == 3){
printf("\t\t\t\tComputer got scissors\n");
computer2 = 3;
}

if (userinput == computer2){
printf("\t\t\t  You and the computer are tied\n");
}


else if (userinput == 1 && computer2 == 2){
printf("\t\t\t\tComputer won\n");
cscore++;
}


else if (userinput == 1 && computer2 == 3){
printf("\t\t\t\tYou won\n");
score++;
}

else if (userinput == 2 && computer2 == 1){
printf("\t\t\t\tYou won\n");
score++;
}
else if (userinput == 2 && computer2 == 3){
printf("\t\t\t\tComputer won\n");
cscore++;
}
else if (userinput == 3 && computer2 == 1){
printf("\t\t\t\tComputer won\n");
cscore++;
}


else if (userinput == 3 && computer2 == 2){
printf("\t\t\t\tYou won\n");
score ++;
}
}
printf("\n\n\t\t\t\t       SCORE");
printf("\n\n\t\t\t\t  Your score is %d\n", score);
printf("\t\t\t\t Computer score is %d\n", cscore);



if (score > cscore){
printf("\n\n\t\t\t\t YOU WON THE GAME!\n");
}
else if (score < cscore){
printf("\n\n\t\t\t\t YOU LOOSE THE GAME!\n");
}
else
printf("\n\n\t\t\t\t\tDRAW!\n");


printf("\n\n\t\t\t\t     NICE GAME!\n");


}

getch();

}



Thank you very much.. I'm sorry I was not able to summarize my problems.



1) You should seed your random generator with non-predictable value so it won't give you the same result each time
for std::rand() you may use std::strand(std::time(0)) (and include <cstdlib> and <ctime>)

2) Move bracked from line 94 to line 98

3) Make a loop on player input and do not let leave until number is correct

4) As above, but check for stream status/clean stream. Learn how to do it yourself.

And read this article:
http://www.gidnetwork.com/b-59.html
http://www.gidnetwork.com/b-60.html
http://www.gidnetwork.com/b-62.html
http://www.gidnetwork.com/b-63.html
http://www.gidnetwork.com/b-64.html
Sir, thank you very much for your helpful answer.. but sir can u give more clues on problem #3.. I don't know what to do...
use a loop:
1
2
3
4
5
6

do {
    
    input_the_player_turn;

}while ( player's_input_is_invalid ); // i.e ( turn != 1 || turn != 2 || turn != 3 ); 

loops:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
how about this code... it is only working in numbers.. when i enter for example a letters, the programs will infinite looping...
How can i also invalid the letters or any other keys in the keyboard (except 1,2 and 3)
1
2
3
4
5
6
7
8
9
printf("\t\t\t    Enter your choice: ");
scanf("%d",&userinput);

while(userinput<1||userinput>3)
{
printf("%d is not a valid number!\n",userinput);
printf("Please select 1 for rock, 2 for paper, and 3 for scissors:");
scanf("%d",&userinput);
}
use isalpha() after scanf() to check if the user entered a letter:
http://www.cplusplus.com/reference/cctype/isalpha/

If you mean disable a key to type, i doubt it is more advance method.
How can i also invalid the letters or any other keys in the keyboard (except 1,2 and 3)
Input parsing and validation is a pretty afvanced topic, as you could see if you read article I provided.

The best you can do is to read a line as a whole and then parse it manually character by character withous using scanf().
Topic archived. No new replies allowed.