forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangMan.java
More file actions
141 lines (121 loc) · 4.2 KB
/
HangMan.java
File metadata and controls
141 lines (121 loc) · 4.2 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
package com.company;
import java.util.Random;
import java.util.Scanner;
public class HangMan {
private String[] wordCollection = {"banana", "apple", "mango", "pinapple", "grapefruit", "kiwi", "custardapple", "dragonfruit", "coconut", "strawberry", "blueberry", "melon", "tangerine", "apricot", "peach", "orange", "cherry", "grape", "watermelon", "pear"};
private String compPick = null;
private int guesses;
private int warning = 4;
private String alphabets = "abcdefghijklmnopqrstuvwxyz";
private char[] guessedWord = null;
private int[] indices;
public char inputChar(){
System.out.print("Enter Your Guess: ");
Scanner num = new Scanner(System.in);
return num.next().charAt(0);
}
public void pickWord(){
int rng = wordCollection.length;
Random num = new Random();
int i = num.nextInt(rng);
this.compPick = wordCollection[i];
}
public void setGuessedWord(){
int length = this.compPick.length();
this.guessedWord = new char[length];
for(int i = 0; i < length; i++){
this.guessedWord[i] = '_';
}
}
public void setGuess(String word){
this.guesses = (int) (word.length()*1.5);
}
public Boolean isInPick(char letter) {
return this.compPick.indexOf(letter) != -1;
}
public void setIndices(char letter){
int length = this.compPick.length();
int ind = 0;
char[] comPickArray = this.compPick.toCharArray();
this.indices = new int[length];
for(int i = 0; i < length; i++){
if(letter == comPickArray[i]){
this.indices[ind] = i;
ind++;
}
}
}
public void updateGuessed(char letter, int[] loc) {
int length = this.guessedWord.length;
int ind = 0;
for(int i = 0; i < length; i++){
if(i == loc[ind]){
ind++;
this.guessedWord[i] = letter;
}
}
}
public void removeAlphabet(char letter){
this.alphabets = this.alphabets.replace(letter, ' ');
}
public boolean alreadyGuessed(char letter){
if(this.alphabets.indexOf(letter) == -1){
this.warning--;
return true;
}
else{
return false;
}
}
public boolean wordGuessed(){
for(int i = 0; i < this.guessedWord.length; i++){
if(guessedWord[i] == '_'){
return false;
}
}
return true;
}
public void play() {
pickWord();
setGuessedWord();
setGuess(this.compPick);
System.out.println("I am thinking of a " + this.compPick.length() + " letter word.");
boolean flag = true;
do {
if ((this.warning == 0 || this.guesses == 0) || wordGuessed()) {
if(wordGuessed()){
System.out.println(this.guessedWord);
System.out.println("Congrats!");
}else{
System.out.println("You failed. The word was: " + this.compPick);
}
flag = false;
} else
{
System.out.println("You have " + this.guesses + " guesses.");
System.out.println("You have " + this.warning + " warnings.");
System.out.println("Alphabets remaining: " + this.alphabets);
System.out.println(this.guessedWord);
char guessLetter = inputChar();
if (isInPick(guessLetter)) {
if (alreadyGuessed(guessLetter)) {
System.out.println("You have already guessed this letter.");
}
else{
setIndices(guessLetter);
updateGuessed(guessLetter, this.indices);
removeAlphabet(guessLetter);
}
}
else{
System.out.println("Wrong Guess!");
this.guesses--;
}
}
}while (flag) ;
}
public static void main(String args[]) {
HangMan h = new HangMan();
h.play();
}
}