-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdventureLab.java
More file actions
109 lines (99 loc) · 3.73 KB
/
Copy pathAdventureLab.java
File metadata and controls
109 lines (99 loc) · 3.73 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
import java.util.*;
public class AdventureLab
{
/*
* Directions: You are tasked with making a short “Choose Your
Own Adventure” story. You should decide on a plot and use a scanner
for the user to make a decision to further the story. You may write
all your code in one main method, or if you would like to reuse code,
you can write the code for each situation in a separate method and
then call the methods.
You should use the following four methods
at least once in deciding the next step of your story-
.equals(), .length(), .substring(int) or .substring (int, int),
and .indexOf(String). Be creative and have fun!
*/
public static void main (String [] args){
Scanner scan = new Scanner(System.in);
System.out.println("Let's play hangman! Person one, please enter the word (no spaces, no repeat letters)");
String word = scan.nextLine();
System.out.println(word);
//enter down several lines so the second person can't see it
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Person 2 can come to the computer now");
System.out.println("You have 10 guesses");
int l = word.length();
int x = 0;
String store = ("");
while (x < l){
store = store.concat("_");
x += 1;
}
boolean win = false;
int lives = 10;
String guess = "";
while (win == false && lives > 0){
System.out.println(store);
System.out.println(" ");
System.out.println("Enter a guess-- letter, part of a word, or whole word");
guess = scan.nextLine();
int len = guess.length();
if (guess.equals(word)){
win = true;
}
else if (word.indexOf(guess) != -1){
if (len > 1){
int s = word.indexOf(guess);
int e = (word.indexOf(guess) + len);
store = store.substring(0, s) + word.substring (s, e)
+ store.substring(e);
}
else {
if (word.length() == word.indexOf(guess) + 1){
int s = word.indexOf(guess);
store = store.substring(0, s) + word.substring(s);
}
else {
int s = word.indexOf(guess);
int e = s+1;
store = store.substring(0, s) + word.charAt(s)
+ store.substring(e);
}
}
}
else {
lives -= 1;
System.out.println("Nope. Lives: " + lives);
}
if (store.equals(word)){
win = true;
}
if (win == true){
System.out.println("Congratulations! " + word + " is the word!");
}
if (lives == 0){
System.out.println("I'm sorry-- you ran out of lives");
}
}
}
}