-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hangman.java
200 lines (141 loc) · 5.03 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.kct.fifthsem.cseb.assignment.hangman;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Hangman {
private Player player = null;
private Word currentWordPlayerPlaying = null;
private static final int BONUS_LIFE_FOR_GUESSING_WORD = 2;
private Scanner input = new Scanner(System.in);
private char[] correctlyGuessedLetters = null;
private Hangman(String playerName) {
player = new Player(playerName);
System.out.println("Hi "+player.getPlayerName()+" Welcome to Hangman Game !! ");
System.out.println("Your game Starts Now");
startNewGame();
}
public static void startGame(final String playerName)
{
new Hangman(playerName);
}
private void startNewGame()
{
System.out.println("Please find below new word to guess ");
currentWordPlayerPlaying = this.getWordToPlayforPlayer();
System.out.println(currentWordPlayerPlaying.wordInGameFormat());
System.out.println("Number of lifes left for "+player.getPlayerName()+" to find this word is "+player.getNumberofLifes());
this.guessGameStarts();
}
private void guessGameStarts()
{
while(player.getNumberofLifes()>0
&& !(currentWordPlayerPlaying.getNumberofLettersFoundinWord() == currentWordPlayerPlaying.wordLength()))
{
char guessedLetter = this.validCharfromPlayer();
if(this.isLetterinCorrectlyGuessedLetterList(guessedLetter))
{
System.out.println("Sorry "+player.getPlayerName()+ " You have already guessed this letter ("+guessedLetter+") correctly");
continue;
}
int[] indexcesforguessedLetter = currentWordPlayerPlaying.arrayofLetterIndexcesinWord(guessedLetter);
if(indexcesforguessedLetter==null)
{
System.out.println("Sorry letter ("+ guessedLetter +") is not in the word ");
player.reduceLifeofUserByOne();
System.out.println("Life Left "+player.getNumberofLifes());
continue;
}
else
{
currentWordPlayerPlaying.updateDisplayWordFormat(indexcesforguessedLetter, guessedLetter);
this.updateListofCorrectlyGuessedLetter(guessedLetter);
System.out.println("Word :"+currentWordPlayerPlaying.wordInGameFormat());
}
}
if(player.getNumberofLifes()<=0)
{
System.out.println("Sorry ! " +player.getPlayerName()+" have lost all your lifes , Better Luck next time");
player = null;
}
else if(player.getNumberofLifes()>0
&& currentWordPlayerPlaying.getNumberofLettersFoundinWord() == currentWordPlayerPlaying.wordLength())
{
System.out.println("Congrats "+ player.getPlayerName() +" You found the Word ("+ this.currentWordPlayerPlaying.wordInGameFormat() +") Your Life would be increased by 2");
player.addLifetoUser(BONUS_LIFE_FOR_GUESSING_WORD);
System.out.println("Number of lives left "+player.getNumberofLifes());
System.out.println("Cont for next word");
player.updateWordIndentifiedByPlayerInList(currentWordPlayerPlaying);
currentWordPlayerPlaying = null;
this.clearCorrectlyGuessedLettersList();
this.startNewGame();
}
}
private char validCharfromPlayer()
{
Pattern pattern = Pattern.compile("[a-z]");
Matcher matcher = null;
Character guessedLetter = null;
do
{
System.out.println("Guess a letter (pls enter a valid character): ");
guessedLetter = input.next().charAt(0);
matcher = pattern.matcher(guessedLetter.toString());
}while(!(matcher.find()));
return guessedLetter;
}
private Word getWordToPlayforPlayer()
{
int numberoftries = 0;
Word wordToreturn = null;
if(!(WordsContainer.isPlayerPlayedAlltheWords(player)))
{
Word word = WordsContainer.wordToPlay();
while(this.player.isPlayerIdentifiedThisWord(word)
&& numberoftries<6)
{
word = WordsContainer.wordToPlay();
numberoftries++;
}
wordToreturn = word;
}
return wordToreturn;
}
private boolean isLetterinCorrectlyGuessedLetterList(final char letter)
{
for(int index=0;index<this.getCorrectlyGuessedLettersList().length;index++)
{
if(this.getCorrectlyGuessedLettersList()[index]==letter)
{
return true;
}
}
return false;
}
private char[] getCorrectlyGuessedLettersList()
{
if(this.correctlyGuessedLetters == null)
{
correctlyGuessedLetters = new char[currentWordPlayerPlaying.wordLength()];
}
return this.correctlyGuessedLetters;
}
private void clearCorrectlyGuessedLettersList()
{
this.correctlyGuessedLetters = null;
}
private void updateListofCorrectlyGuessedLetter(final char letter)
{
for(int index=0;index<this.getCorrectlyGuessedLettersList().length;index++)
{
if(this.getCorrectlyGuessedLettersList()[index] == letter)
{
break;
}
if(this.getCorrectlyGuessedLettersList()[index]=='\u0000')
{
this.getCorrectlyGuessedLettersList()[index] = letter;
break;
}
}
}
}