patternjavaMinor
Console based Hangman in Java
Viewed 0 times
hangmanconsolebasedjava
Problem
I just created a Hangman app (same game mechanics mostly) using Java. I did this mainly to test my knowledge in OOP design/coding. I'd like to know your thoughts on my code
The whole project is on Github
I would really appreciate the comments and criticism!
```
public class Launcher {
private static BufferedReader consoleReader;
public static void main(String[] args) throws IOException {
consoleReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("LET'S PLAY HANGMAN (w/o the actual \"man\")");
System.out.print("Enter name:");
String username = consoleReader.readLine();
Player P1 = new Player(username);
String plyrName = P1.getUsername();
System.out.println("Welcome " + plyrName + "!");
int choice = 0;
while (choice != 4) {
try {
System.out.println();
System.out.println("1. Start game");
System.out.println("2. Help");
System.out.println("3. About this game");
System.out.println("4. Quit");
choice = Integer.parseInt(consoleReader.readLine());
if (choice == 1) {
AI a1 = new AI(P1);
String answer = a1.getAnswer();
P1.setGuess(P1.initGuess(a1));
System.out.println();
System.out.println("Guess this " + answer.length() + " letter word!");
while (P1.getTries() != 0) {
try {
for (int x = 0; x < answer.length(); x++) {
System.out.print(P1.getGuess()[x] + " ");
}
System.out.println();
System.out.println("1. Guess a letter");
System.out.println("2. Guess the answer");
System.out.println("3. Concede");
System.out.println();
System.out.println("No. of tries remaining: " + "*
The whole project is on Github
I would really appreciate the comments and criticism!
```
public class Launcher {
private static BufferedReader consoleReader;
public static void main(String[] args) throws IOException {
consoleReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("LET'S PLAY HANGMAN (w/o the actual \"man\")");
System.out.print("Enter name:");
String username = consoleReader.readLine();
Player P1 = new Player(username);
String plyrName = P1.getUsername();
System.out.println("Welcome " + plyrName + "!");
int choice = 0;
while (choice != 4) {
try {
System.out.println();
System.out.println("1. Start game");
System.out.println("2. Help");
System.out.println("3. About this game");
System.out.println("4. Quit");
choice = Integer.parseInt(consoleReader.readLine());
if (choice == 1) {
AI a1 = new AI(P1);
String answer = a1.getAnswer();
P1.setGuess(P1.initGuess(a1));
System.out.println();
System.out.println("Guess this " + answer.length() + " letter word!");
while (P1.getTries() != 0) {
try {
for (int x = 0; x < answer.length(); x++) {
System.out.print(P1.getGuess()[x] + " ");
}
System.out.println();
System.out.println("1. Guess a letter");
System.out.println("2. Guess the answer");
System.out.println("3. Concede");
System.out.println();
System.out.println("No. of tries remaining: " + "*
Solution
Please split your code into different smaller functions.
This is just impossible to understand. Here's my attempt, it probably doesn't compile but it does look much easier to understand :
```
public class Launcher {
private static BufferedReader consoleReader;
public static void main(String[] args) throws IOException {
consoleReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("LET'S PLAY HANGMAN (w/o the actual \"man\")");
System.out.print("Enter name:");
String username = consoleReader.readLine();
Player P1 = new Player(username);
String plyrName = P1.getUsername();
System.out.println("Welcome " + plyrName + "!");
while (true) {
System.out.println();
System.out.println("1. Start game");
System.out.println("2. Help");
System.out.println("3. About this game");
System.out.println("4. Quit");
switch (askUserInteger(0)) {
case 1: game(P1); break;
case 2: help(); break;
case 3: about(); break;
case 4:
if (userWantsToQuit()) {
System.out.println("Goodbye, " + plyrName + "");
return;
}
break;
default: System.out.println("ERROR: Invalid input!");
}
}
}
public static void game (Player P1)
{
AI a1 = new AI(P1);
String answer = a1.getAnswer();
P1.setGuess(P1.initGuess(a1));
System.out.println();
System.out.println("Guess this " + answer.length() + " letter word!");
while (P1.getTries() != 0) {
for (int x = 0; x < answer.length(); x++) {
System.out.print(P1.getGuess()[x] + " ");
}
System.out.println();
System.out.println("1. Guess a letter");
System.out.println("2. Guess the answer");
System.out.println("3. Concede");
System.out.println();
System.out.println("No. of tries remaining: " + "" + P1.getTries() + "");
switch (askUserInteger(0)) {
case 1: guessLetter(P1); break;
case 2: guessAnswer(P1); break;
case 3 : if (userWantsToConcede()) P1.setTries(0); break;
default : System.out.println("ERROR: Invalid input!");
}
}
if (P1.getTries() == 0 && !String.valueOf(P1.getGuess()).equals(answer)) {
System.out.println("GAME OVER, " + plyrName + "!");
System.out.println("The answer is " + "\"" + answer + "\"");
}
}
public static void guessLetter(Player P1)
{
System.out.print("Input letter: ");
String ltr = consoleReader.readLine();
if (a1.isLetterInWord(P1, answer, ltr.toLowerCase())) {
if (String.valueOf(P1.getGuess()).equals(answer)) {
System.out.println("\"" + String.valueOf(P1.getGuess()) + "\"" + " is correct!");
System.out.print("You have beaten the game, " + plyrName + "!");
P1.setTries(0);
}
System.out.println();
} else {
P1.setTries(P1.getTries() - 1);
if (P1.getTries() != 0) {
System.out.println("Sorry try again!");
}
}
}
public static void guessAnswer(Player P1)
{
System.out.print("Input guess: ");
String word = consoleReader.readLine();
if (a1.isGuessCorrect(answer, word.toLowerCase())) {
P1.setGuess(word.toCharArray());
System.out.println("\"" + word + "\"" + " is correct!");
System.out.println("You have beaten the game, " + plyrName + "!");
P1.setTries(0);
} else {
System.out.println("Sorry try again!");
P1.setTries(P1.getTries() - 1);
}
}
public static void help ()
{
System.out.println(" Help");
System.out.println("You will be given a word to try and guess.");
System.out.println("Your number of trials will depend on the length of the word.");
System.out.println("You can guess it by letter or guess it directly.");
System.out.println("Goodluck; Have fun!");
System.out.println(" Help");
}
public static void about()
{
System.out.println(" About this game");
System.out.println("This game was developed to check and exercise");
System.out.println("zurcnay4's OOP knowledge using java. \n");
System.out.println("Comments & suggestions on: ");
System.out.println("- the application's overall design/code");
System.out.println("- how to improve this game");
System.out.pri
This is just impossible to understand. Here's my attempt, it probably doesn't compile but it does look much easier to understand :
```
public class Launcher {
private static BufferedReader consoleReader;
public static void main(String[] args) throws IOException {
consoleReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("LET'S PLAY HANGMAN (w/o the actual \"man\")");
System.out.print("Enter name:");
String username = consoleReader.readLine();
Player P1 = new Player(username);
String plyrName = P1.getUsername();
System.out.println("Welcome " + plyrName + "!");
while (true) {
System.out.println();
System.out.println("1. Start game");
System.out.println("2. Help");
System.out.println("3. About this game");
System.out.println("4. Quit");
switch (askUserInteger(0)) {
case 1: game(P1); break;
case 2: help(); break;
case 3: about(); break;
case 4:
if (userWantsToQuit()) {
System.out.println("Goodbye, " + plyrName + "");
return;
}
break;
default: System.out.println("ERROR: Invalid input!");
}
}
}
public static void game (Player P1)
{
AI a1 = new AI(P1);
String answer = a1.getAnswer();
P1.setGuess(P1.initGuess(a1));
System.out.println();
System.out.println("Guess this " + answer.length() + " letter word!");
while (P1.getTries() != 0) {
for (int x = 0; x < answer.length(); x++) {
System.out.print(P1.getGuess()[x] + " ");
}
System.out.println();
System.out.println("1. Guess a letter");
System.out.println("2. Guess the answer");
System.out.println("3. Concede");
System.out.println();
System.out.println("No. of tries remaining: " + "" + P1.getTries() + "");
switch (askUserInteger(0)) {
case 1: guessLetter(P1); break;
case 2: guessAnswer(P1); break;
case 3 : if (userWantsToConcede()) P1.setTries(0); break;
default : System.out.println("ERROR: Invalid input!");
}
}
if (P1.getTries() == 0 && !String.valueOf(P1.getGuess()).equals(answer)) {
System.out.println("GAME OVER, " + plyrName + "!");
System.out.println("The answer is " + "\"" + answer + "\"");
}
}
public static void guessLetter(Player P1)
{
System.out.print("Input letter: ");
String ltr = consoleReader.readLine();
if (a1.isLetterInWord(P1, answer, ltr.toLowerCase())) {
if (String.valueOf(P1.getGuess()).equals(answer)) {
System.out.println("\"" + String.valueOf(P1.getGuess()) + "\"" + " is correct!");
System.out.print("You have beaten the game, " + plyrName + "!");
P1.setTries(0);
}
System.out.println();
} else {
P1.setTries(P1.getTries() - 1);
if (P1.getTries() != 0) {
System.out.println("Sorry try again!");
}
}
}
public static void guessAnswer(Player P1)
{
System.out.print("Input guess: ");
String word = consoleReader.readLine();
if (a1.isGuessCorrect(answer, word.toLowerCase())) {
P1.setGuess(word.toCharArray());
System.out.println("\"" + word + "\"" + " is correct!");
System.out.println("You have beaten the game, " + plyrName + "!");
P1.setTries(0);
} else {
System.out.println("Sorry try again!");
P1.setTries(P1.getTries() - 1);
}
}
public static void help ()
{
System.out.println(" Help");
System.out.println("You will be given a word to try and guess.");
System.out.println("Your number of trials will depend on the length of the word.");
System.out.println("You can guess it by letter or guess it directly.");
System.out.println("Goodluck; Have fun!");
System.out.println(" Help");
}
public static void about()
{
System.out.println(" About this game");
System.out.println("This game was developed to check and exercise");
System.out.println("zurcnay4's OOP knowledge using java. \n");
System.out.println("Comments & suggestions on: ");
System.out.println("- the application's overall design/code");
System.out.println("- how to improve this game");
System.out.pri
Code Snippets
public class Launcher {
private static BufferedReader consoleReader;
public static void main(String[] args) throws IOException {
consoleReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("***LET'S PLAY HANGMAN (w/o the actual \"man\")***");
System.out.print("Enter name:");
String username = consoleReader.readLine();
Player P1 = new Player(username);
String plyrName = P1.getUsername();
System.out.println("Welcome " + plyrName + "!");
while (true) {
System.out.println();
System.out.println("1. Start game");
System.out.println("2. Help");
System.out.println("3. About this game");
System.out.println("4. Quit");
switch (askUserInteger(0)) {
case 1: game(P1); break;
case 2: help(); break;
case 3: about(); break;
case 4:
if (userWantsToQuit()) {
System.out.println("***Goodbye, " + plyrName + "***");
return;
}
break;
default: System.out.println("***ERROR: Invalid input!***");
}
}
}
public static void game (Player P1)
{
AI a1 = new AI(P1);
String answer = a1.getAnswer();
P1.setGuess(P1.initGuess(a1));
System.out.println();
System.out.println("***Guess this " + answer.length() + " letter word!***");
while (P1.getTries() != 0) {
for (int x = 0; x < answer.length(); x++) {
System.out.print(P1.getGuess()[x] + " ");
}
System.out.println();
System.out.println("1. Guess a letter");
System.out.println("2. Guess the answer");
System.out.println("3. Concede");
System.out.println();
System.out.println("No. of tries remaining: " + "*" + P1.getTries() + "*");
switch (askUserInteger(0)) {
case 1: guessLetter(P1); break;
case 2: guessAnswer(P1); break;
case 3 : if (userWantsToConcede()) P1.setTries(0); break;
default : System.out.println("***ERROR: Invalid input!***");
}
}
if (P1.getTries() == 0 && !String.valueOf(P1.getGuess()).equals(answer)) {
System.out.println("***GAME OVER, " + plyrName + "!***");
System.out.println("***The answer is " + "\"" + answer + "\"***");
}
}
public static void guessLetter(Player P1)
{
System.out.print("Input letter: ");
String ltr = consoleReader.readLine();
if (a1.isLetterInWord(P1, answer, ltr.toLowerCase())) {
if (String.valueOf(P1.getGuess()).equals(answer)) {
System.out.println("***\"" + String.valueOf(P1.getGuess()) + "\"" + " is correct!***");
System.out.print("*Context
StackExchange Code Review Q#31751, answer score: 5
Revisions (0)
No revisions yet.