-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhollywood.java
More file actions
182 lines (152 loc) · 5.26 KB
/
hollywood.java
File metadata and controls
182 lines (152 loc) · 5.26 KB
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
/*Multiplayer Hollywood game with 2 classes */
package hollywood;
import java.util.*;
class challenger
{
Scanner s=new Scanner(System.in);
String select;//question
String guess;//answer
int y;//no. of consonants
int x;//index of movie in array
char[] already=new char[26];//to store already guessed alphabets
challenger()
{
select="";
guess="";
for(int i=0;i<26;i++)
{
already[i]=0;//frequency of every alphabet is zero now
}
}
//or you can have a large database of movies.
String name[]= {"lagaan","sing street","lion","la la land"};
void getmovie()//either serially assign a movie to every player. Or ask for a number and select movie at that index for the player
{
System.out.println("Select a random number between 0-3");
x=s.nextInt();
select=name[x];
for(int i=0;i<select.length();i++)
{
char ch=select.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
System.out.print(ch);
guess=guess+ch;
}
else if(ch==' ')
{
System.out.print("/");
guess=guess+'/';
}
else
{
System.out.print("-");
guess+='-';
y++;//count of consonants/no. of occurrences of consonants
}
}
}
void hint()
{
switch(x)
{
case 0:System.out.println("British, cricket");
break;
case 1:System.out.println("Music, school band");
break;
case 2:System.out.println("Australia, lost kid");
break;
case 3:System.out.println("Wannabe musician, actress, fools who dream");
break;
}
}
void display(char ch, int i)
{
guess=guess.substring(0, (i))+ch+guess.substring(i+1);
System.out.println(guess);
y--;//game should run till y=0 i.e. all consonants are solved
}
void play()
{
int chances=9;//game should run till all chances are lost
int hints=0;//to keep record of hints taken
int value=0;//to store ascii value of char
do
{
//hints
System.out.println("\nDo you want a hint? Press Y for yes");
char yes=s.next().charAt(0);
if((yes=='y'||yes=='Y')&&hints==0)
{
hint();
hints++;
}
else if(hints>0&&(yes=='y'||yes=='Y'))
System.out.println("Sorry hints exhausted");
//guess
System.out.println("\nEnter your guess (all vowels displayed already)");//assuming user always enters in lowercase, otherwise use tolowercase
char ch=s.next().charAt(0);
value=(int)ch;
value=value-97;//to correlate with index of array
already[value]++;
if(already[value]==1||already[value]==0)//i.e. char guessed once or never guessed
{
int flag=0;//initializes to zero for every character
if((ch!='a'&&ch!='e'&&ch!='i'&&ch!='o'&&ch!='u'))
{
for(int i=0;i<select.length();i++)
{
char c=select.charAt(i);
//System.out.println(c);
if((ch==c))
{
display(ch,i);
flag=1;
}
}
}
else
{
System.out.println("Vowels already displayed!");
flag=2;//so that it doesn't go into wrong guess
}
//wrong guess
if(flag==0)
{
System.out.println("Wrong guess :(");
chances--;
System.out.println("No. of guesses left="+chances);
}
}
else
System.out.println("Already guessed the alphabet");
}while(chances!=0&&y!=0);
//checking if won or not
if(chances==0&&y!=0)
{
System.out.println("You lost");
}
else if(chances!=0&&y==0)
{
System.out.println("You won");
}
}
}
class hollywood
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("HOLLYWOOD\nHow to play? A movie name will be selected and only vowels will be displayed(- for consonants and / for spaces)\nYou have to guess the missing characters to find out the movie name");
System.out.println("How many players?");
int players=sc.nextInt();
challenger c[]=new challenger[players];//array of challenger class array
for(int i=0;i<players;i++)
{
System.out.println("Player "+(i+1));
c[i]=new challenger();
c[i].getmovie();//movie is assigned to player and displayed on screen
c[i].play();
}
}
}