-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_cricket.java
223 lines (217 loc) · 5.59 KB
/
hand_cricket.java
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//!/bin/java
import java.util.*;
public class hand_cricket
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
//Switch declaration
int sh=0;
System.out.println("Enter 1 for single player");
System.out.println("Enter 2 for two players");
sh=in.nextInt();
switch(sh)
{
//For single player
case 1:
{
int toss, u_toss, comp_runs, comp_ball=0, u_ball, t_runs=0, c_run=0, tar, c_ball;
toss=(int)(Math.random()*(3-0)+0);
System.out.println("Toss");
System.out.println("Enter 0 for HEADS and 1 for TAILS");
u_toss=in.nextInt();
if(u_toss==toss)
{
System.out.println("You will bat");
comp_runs=(int)(Math.random()*(50-15+1)+15);
tar=comp_runs+1;
System.out.println("Keep scoring runs until you get out!\nComputer scored: "+comp_runs);
System.out.println("Score "+tar+" to win!");
for(int i=1; i>0; i++)
{
c_run= in.nextInt();
if(c_run >=0 && c_run <7)
{
comp_ball= (int)(Math.random()*7);
if(comp_ball==c_run)
{
System.out.println();
System.out.println("You're OUT");
System.out.println("Your score: "+t_runs);
break;
}
else
{
t_runs=t_runs+c_run;
System.out.println("current score: "+t_runs);
if(t_runs>comp_runs)
{
break;
}
}
}
}
if(t_runs>comp_runs)
{
System.out.println("You won!");
}
if(t_runs==comp_runs)
{
System.out.println("The match tied :/");
}
if(t_runs<comp_runs)
{
System.out.println("You lost :(");
}
}
if(u_toss!=toss)
{
comp_runs=0;
System.out.println("You will bowl");
t_runs=(int)(Math.random()*(65-25+1)+25);
tar=t_runs+1;
System.out.println("You scored "+t_runs+" runs");
System.out.println("Out computer before he scores "+tar+" runs");
System.out.println();
System.out.println("Now bowl!");
for(int i=1; i>0; i++)
{
c_ball=in.nextInt();
c_run=(int)(Math.random()*(6-0+1)+0);
if(c_ball==c_run)
{
System.out.println("You bowled the computer");
System.out.println("You won");
break;
}
else
{
System.out.println("Computer scored: "+c_run);
comp_runs=comp_runs+c_run;
System.out.println("Current run: "+comp_runs);
if(comp_runs>t_runs)
{
System.out.println("You lost as you failed to out the computer");
break;
}
}
}
if(comp_runs==t_runs)
{
System.out.println("The match was tied");
}
}
break;
}
//For 2 players
case 2:
{
Scanner s = new Scanner(System.in);
String name1, name2;
System.out.println("Enter the name of Player 1");
name1=s.nextLine();
System.out.println("Enter the name of Player 2");
name2=s.nextLine();
System.out.println();
int p1_runs=0, p2_runs=0, p1_crun, p2_crun, comp_ball, toss, comp_toss, tar;
comp_toss=(int)(Math.random()*(3-0)+0);
System.out.println("Toss\nEnter 0 for HEADS and 1 for TAILS");
toss=in.nextInt();
if(comp_toss==toss)
{
System.out.println(name1+" will bat");
System.out.println("Score as many runs as you can");
for(int i=1; i>0; i++)
{
comp_ball=(int)(Math.random()*7);
p1_crun=in.nextInt();
if(p1_crun >=0 && p1_crun <7 && p1_crun!=comp_ball)
{
p1_runs=p1_runs+p1_crun;
System.out.println("Current run: "+p1_runs);
}
if(p1_crun==comp_ball)
{
System.out.println("You got OUT!\nYou scored "+p1_runs+" runs");
break;
}
}
System.out.println();
tar=p1_runs+1;
System.out.println(name2+" needs "+tar+" runs to win\nNow score runs!");
for(int j=1; j>0; j++)
{
comp_ball=(int)(Math.random()*7);
p2_crun=in.nextInt();
if(p2_crun >=0 && p2_crun <7 && p2_crun!=comp_ball)
{
p2_runs=p2_runs+p2_crun;
System.out.println("Current run: "+p2_runs);
if(p2_runs>=tar)
{
System.out.println(name2+" wins!");
break;
}
}
if(p2_crun==comp_ball)
{
System.out.println("You got OUT!\n"+name1+" wins\n"+name2+" loses");
break;
}
}
if(p1_runs==p2_runs)
{
System.out.println("The match tied!");
}
}
else
{
System.out.println(name2+" will bat");
System.out.println("Score as many runs as you can");
for(int i=1; i>0; i++)
{
comp_ball=(int)(Math.random()*7);
p2_crun=in.nextInt();
if(p2_crun >=0 && p2_crun <7 && p2_crun!=comp_ball)
{
p2_runs=p2_runs+p2_crun;
System.out.println("Current run: "+p2_runs);
}
if(p2_crun==comp_ball)
{
System.out.println("You got OUT!\nYou scored "+p2_runs+" runs");
break;
}
}
System.out.println();
tar=p2_runs+1;
System.out.println(name1+" needs "+tar+" runs to win\nNow score runs!");
for(int j=1; j>0; j++)
{
comp_ball=(int)(Math.random()*7);
p1_crun=in.nextInt();
if(p1_crun >=0 && p1_crun <7 && p1_crun!=comp_ball)
{
p1_runs=p1_runs+p1_crun;
System.out.println("Current run: "+p1_runs);
if(p1_runs>=tar)
{
System.out.println(name1+" wins!");
break;
}
}
if(p1_crun==comp_ball)
{
System.out.println("You failed to score "+tar+", you lost");
break;
}
if(p1_runs==p2_runs)
{
System.out.println("The match tied!");
}
}
}
}
}
}
}