-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hangman.java
164 lines (162 loc) · 4.42 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
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
/*
Sai Avula
1.24.24
Hangman.java
Create A game of Hangman
*/
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Hangman
{
Scanner input;
PrintWriter pw;
String newwd;
public static void main(String[]args)
{
Hangman sai = new Hangman();
sai.runner();
}
public void runner()
{
Scanner scan = new Scanner(System.in);
System.out.println("\n\n\n");
System.out.print("Would you like to add to the dictionary, yes(1), no(2)? ");
int ans = scan.nextInt();
if(ans == 1)
{
System.out.print("What is your new word? ");
String ans2 = scan.next();
append(ans2);
}
int counter = 0;
tryCatchIt();
while(input.hasNext())
{
counter ++;
String temp = input.next();
}
input.close();
String [] words = new String[counter];
int num = (int) (Math.random() * counter)-1;
counter = 0;
tryCatchIt();
while(input.hasNext())
{
counter++;
words[ counter-1 ] = input.next();
}
input.close();
String wd = words[ num ];
int numoflives = 5;
newwd = "";
wd = wd.toUpperCase();
System.out.println("Here is your random word: \n ");
hang(wd);
for(int i = 0; i < wd.length(); i++)
{
newwd += "-";
}
while(numoflives > 0)
{
String [] ar = new String[wd.length()];
System.out.print("Please guess a letter: ");
String let = scan.next();
let = let.toUpperCase();
for(int i = 0; i < newwd.length(); i++)
{
if(newwd.charAt(i) == let.charAt(0))
{
numoflives = numoflives-1;
System.out.println("That is incorrect. You have " + numoflives + " guess(es) left.");
break;
}
}
int [] index = new int[wd.length()];
int temp = 0;
index[0] = -5;
for(int i = 0; i < wd.length(); i++)
{
if(wd.substring(i,i+1).equalsIgnoreCase(let))
{
temp ++;
index[temp-1] = i;
man(index[temp-1], newwd, let);
}
}
System.out.println(newwd + "\n");
if(index[0] == -5)
{
numoflives = numoflives-1;
System.out.println("That is incorrect. You have " + numoflives + " guess(es) left.");
}
if(wd.equalsIgnoreCase(newwd))
{
System.out.println("You Won! Congratulations!");
numoflives = 0;
}
else if(numoflives == 0)
{
System.out.println("You Lost! Try Again:(");
System.out.println("The word was " + wd);
}
}
System.out.println("\n\n");
}
public void tryCatchIt()
{
File inFile = new File ("dictionary.txt");
String inFileName = "dictionary.txt";
//value = "";
input = null;
try
{
input = new Scanner ( inFile );
}
catch ( FileNotFoundException e )
{
System.err.println("Cannot find " + inFileName + " file.");
System.exit(1);
}
}
public void append(String wd)
{
pw = null;
File outFile = new File("dictionary.txt");
try
{
pw = new PrintWriter( new FileWriter(outFile, true) );
}
catch (IOException e)
{
System.err.println("Cannot append to " + " dictionary.txt");
System.exit(1);
}
pw.println( wd );
pw.close();
}
public void hang(String wd)
{
for(int i = 0; i < wd.length(); i++)
{
System.out.print(" - ");
}
System.out.println(" \n ");
}
public void man(int index, String wd, String let)
{
String temp = newwd;
newwd = "";
for(int i = 0; i < wd.length(); i ++)
{
if(index== i)
{
newwd += let;
}
else newwd += temp.charAt( i );
}
}
}