-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.java
135 lines (120 loc) · 3.69 KB
/
hangman.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
import java.util.*;
import java.io.*;
public class hangman1{
public static void main(String[] args) throws FileNotFoundException{
System.out.println("Welcome to the Hangman.");
int lives =8;
String word = getWord();
String dashed = slashed(word);
System.out.println("Your word is " + dashed);
while(lives !=0&&!(found(dashed))) {
Scanner console = new Scanner(System.in);
System.out.print("Your guess: ");
String guess = console.nextLine();
while(!(guess.toUpperCase().charAt(0)>=65 && guess.toUpperCase().charAt(0)<=95)) {
System.out.print("Invalid input, try again: ");
guess = console.next();
}
guess = guess.toUpperCase();
lives=game(lives,word,dashed,guess);
if(guessCheck(word,guess)) {
dashed=changeIndex(word,dashed,guess);
}
// dashed=play1(word,dashed,lives);
// lives=lives -1;
}
}
public static String slashed(String str) {
int len = str.length();
String w1="";
int length = str.length();
for(int a=1;a<=length;a++) {
w1 += "-";}
return w1;
}
public static int game(int lives,String word,String dashed,String guess) {
// System.out.println("You have "+lives +" guesses left.");
// Scanner console = new Scanner(System.in);
// System.out.print("Your guess: ");
// String guess = console.nextLine();
// while(!(guess.toUpperCase().charAt(0)>=65 && guess.toUpperCase().charAt(0)<=95)) {
// System.out.print("Invalid input, try again: ");
// guess = console.next();
// }
// guess = guess.toUpperCase();
if(guessCheck(word,guess)) {
System.out.println("Now your word looks like this: "+changeIndex(word,dashed,guess));
return lives;
}else {
System.out.println("Wrong guess.");
lives--;
return lives;
}
}
public static boolean found(String dashed) {
if(-1==dashed.indexOf("-")) {
System.out.println("Congratulations!");
return true;
}else {
return false;
}
}
public static Boolean guessCheck(String word, String guess) {
if(word.indexOf(guess)==-1) {
System.out.println("There are no " + guess + "'s in the word.");
return false;
}else {
System.out.println("Good guess");
return true;
}
}
// public static String slashy (String word,String dashed, String g,int l) {
// if(word.indexOf(g)==-1) {
// System.out.println("There are no " + g + "'s in the word.");
// System.out.println("You have "+ (l-1) + " lives left");
// return dashed;
// }else {
// System.out.println("Good guess");
//
// System.out.println("The word looks like this: "+ changeIndex(word,dashed,g));
//
// return changeIndex(word,dashed,g);
// }
//
// }
public static String changeIndex(String word,String dashed,String g) {
int a = word.indexOf(g);
return dashed.substring(0,a)+g + dashed.substring(a+1);
}
// public static String play1(String word,String dashed, int lives) throws FileNotFoundException {
//
// Scanner console = new Scanner(System.in);
// System.out.print("Your guess: ");
// String guess = console.nextLine();
// while(!(guess.toUpperCase().charAt(0)>=65 && guess.toUpperCase().charAt(0)<=95)) {
// System.out.print("Invalid input, try again: ");
// guess = console.next();
// }
// guess = guess.toUpperCase();
//
// dashed=slashy(word,dashed,guess,lives);
// return dashed;
// }
public static String getWord() throws FileNotFoundException {
Scanner file = new Scanner(new File("word.txt"));
Random rand = new Random();
int countLine = 0;
while(file.hasNextLine()) {
file.nextLine();
countLine++;
}
file.close();
Scanner file2 = new Scanner(new File("word.txt"));
int a = rand.nextInt(countLine);
for(int b = 1; b<=a; b++) {
file2.nextLine();
}
String word=file2.nextLine();
return word;
}
}