-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
121 lines (115 loc) · 2.88 KB
/
Game.java
File metadata and controls
121 lines (115 loc) · 2.88 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
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class Game
{
public static void main(String args[])
{
int exit = 1;
do
{
Scanner scan = new Scanner(System.in);
int count = 10;
String m = "";
//String movie = m.toUpperCase();
//int len = movie.length();
//char film[] = new char[len];
try
{
Random rand = new Random();
int number = rand.nextInt(1285);
File f = new File("D:/java_project/films.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Reading file using Buffered Reader");
int i = 0;
//System.out.println(number);
while ((readLine = b.readLine()) != null)
{
if (number == i)
{
//System.out.println(readLine);
m = readLine;
break;
}
else
{
i++;
}
}
}catch (IOException e)
{
e.printStackTrace();
}
String movie = m.toUpperCase();
int len = movie.length();
char film[] = new char[len];
for(int i=0;i<len;i++)
{
if(movie.charAt(i) == ' ')
{
System.out.print(" / ");
film[i] = '/';
}
else
{
System.out.print("_");
film[i] = '_';
}
}
System.out.println();
int flag = 0;
ArrayList<Character> al = new ArrayList<Character>();
while(count!=0)
{
System.out.println("You have " + count + " chances remaining");
System.out.print("wrong letters" + al + "\n");
System.out.print("Guess a letter: ");
char c = scan.next().charAt(0);
c = Character.toUpperCase(c);
int find = 0;
for(int i = 0;i<len;i++)
{
if(movie.charAt(i) == c)
{
film[i] = c;
find++;
}
}
if(find==0)
{
if(al.indexOf(c)>=0)
System.out.println(c + " already guessed.");
else
{
al.add(c);
count--;
}
}
int num = 0;
for(int i=0;i<len;i++)
{
if(film[i] == '_')
num++;
}
if (num == 0)
{
System.out.println("movie is: " + movie);
System.out.println("Congratulations! You have won this game.");
break;
}
System.out.println("--------------------------------------------------------");
System.out.println(film);
}
if (count == 0)
{
System.out.println("You lost this game. Better Luck Next Time.");
System.out.println("movie is: " + movie);
}
System.out.println("Enter any number to continue or 0 to exit.");
exit = scan.nextInt();
}while(exit!=0);
}
}