patternjavaModerate
"Hangman" game follow-on
Viewed 0 times
gamefollowhangman
Problem
Last week I asked to review my "Guess a number" game in Java here
I learned so much that I decided to try a little harder game and put into practice all the amazing feedback I received there.
Would you please review and give me some feedback?
My main concern is if i'm doing ok using so many methods instead of a big block of code in the main function. I've found it was nice to create several methods in the previous project, but here it was difficult. Perhaps I'm having a serious design issue?
If the game looks fine, what would be the next simple game challenge I should attempt?
Is it a good idea to try to port this to swing? Or would be better to port it to a web application? or android? I want to keep learning while challenging myself to something new.
I know I need to learn how to handle exceptions better or how to cast / downcast into subclasses (where can I learn this or how can I practice this in a project? a card game?)
I'm new to GitHub, but I created my second repository today to upload this code, perhaps its easier to look at the code here
This is the main class:
```
package game;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class HangmanGame {
private Scanner input;
private String playerName;
private String wordToGuess;
private boolean gameIsRunning;
private int lives;
private char letter;
private Set lettersToGuess;
private Set lettersGuessed;
private Set lettersWrong;
public static void main(String[] args) {
HangmanGame game = new HangmanGame();
game.run();
}
private void run() {
input = new Scanner(System.in);
playerName = getPlayerName(input);
wordToGuess = getRandomWordToGuess();
gameIsRunning = true;
lives = 5;
lettersToGuess = new HashSet();
lettersGuessed = new HashSet();
lettersWrong = new HashSet();
fillLettersToGuess();
// Put first and last letter into lett
I learned so much that I decided to try a little harder game and put into practice all the amazing feedback I received there.
Would you please review and give me some feedback?
My main concern is if i'm doing ok using so many methods instead of a big block of code in the main function. I've found it was nice to create several methods in the previous project, but here it was difficult. Perhaps I'm having a serious design issue?
If the game looks fine, what would be the next simple game challenge I should attempt?
Is it a good idea to try to port this to swing? Or would be better to port it to a web application? or android? I want to keep learning while challenging myself to something new.
I know I need to learn how to handle exceptions better or how to cast / downcast into subclasses (where can I learn this or how can I practice this in a project? a card game?)
I'm new to GitHub, but I created my second repository today to upload this code, perhaps its easier to look at the code here
This is the main class:
```
package game;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class HangmanGame {
private Scanner input;
private String playerName;
private String wordToGuess;
private boolean gameIsRunning;
private int lives;
private char letter;
private Set lettersToGuess;
private Set lettersGuessed;
private Set lettersWrong;
public static void main(String[] args) {
HangmanGame game = new HangmanGame();
game.run();
}
private void run() {
input = new Scanner(System.in);
playerName = getPlayerName(input);
wordToGuess = getRandomWordToGuess();
gameIsRunning = true;
lives = 5;
lettersToGuess = new HashSet();
lettersGuessed = new HashSet();
lettersWrong = new HashSet();
fillLettersToGuess();
// Put first and last letter into lett
Solution
Nice improvement from the initial version. Good job! Here are a couple of brief notes.
To address your specific questions:
Is it a good idea to try to port this to swing? Or would be better to port it to a web application? or android?
You should definitely go with Swing before trying to integrate Java code into a web-app. If you're interested in making Android apps, though, you can download the Android SDK and start toying around with that API. I'm sure there are tutorials out there for that. Setting up your environment might be a pain, though (your programs have to run in an emulator or be deployed to an actual phone).
I know I need to learn how to handle exceptions better or how to cast / downcast into subclasses (where can I learn this or how can I practice this in a project? a card game?)
Casting is actually a kind of "all else failed" tactic in Java programming. Ideally, you should never have to explicitly cast anything. I think you're referring to the concept of polymorphism rather than just casting. A good way to learn about polymorphism is with some game or project involving different kinds of animals. You would have an abstract class or interface named
Exceptions you learn naturally when you start actually trying to deal with errors rather than allowing your application to crash when it doesn't go down the "happy path". I think most beginners first experience this when they're trying to get a user to input integers and they have to deal with a user maliciously typing in
For your next iteration...
I challenge you to do some file handling! Download this file to your workspace and figure out how to read the words into your program and have it "think up" a random word from it. :)
- I'm very wary of having any method named
run()outside of classes which implement theRunnableinterface. This isn't something you'd know about as a beginner, but basicallyrun()has a very specific connotation which regards using multiple threads. I might rename this method toplay()or something of the sort.
- It looks like you have a lot of functionality tacked on that is just needless overhead. For example, you have a method
addLetterToLettersGuessed()which only has one line of code:lettersGuessed.add(letter). There's no need for you to have a wrapper method here. The name of your variable and the method you invoke from it state what you're doing very succinctly. Because you've chosen your variable names well, the code is somewhat self-documenting. Over-architecting your solution can be as big of a problem as having all of your code just slapped in themain()method. (This comment applies to a chunk of your methods belowshowWordToGuess()).
- If you're going to have big block comments over every method, consider formatting them with JavaDoc. It's never too early to learn how to properly document your code.
To address your specific questions:
Is it a good idea to try to port this to swing? Or would be better to port it to a web application? or android?
You should definitely go with Swing before trying to integrate Java code into a web-app. If you're interested in making Android apps, though, you can download the Android SDK and start toying around with that API. I'm sure there are tutorials out there for that. Setting up your environment might be a pain, though (your programs have to run in an emulator or be deployed to an actual phone).
I know I need to learn how to handle exceptions better or how to cast / downcast into subclasses (where can I learn this or how can I practice this in a project? a card game?)
Casting is actually a kind of "all else failed" tactic in Java programming. Ideally, you should never have to explicitly cast anything. I think you're referring to the concept of polymorphism rather than just casting. A good way to learn about polymorphism is with some game or project involving different kinds of animals. You would have an abstract class or interface named
Animal, and below this you might have mammals and birds and all sorts of things. Turns out the world of biology is naturally suited towards hierarchy.Exceptions you learn naturally when you start actually trying to deal with errors rather than allowing your application to crash when it doesn't go down the "happy path". I think most beginners first experience this when they're trying to get a user to input integers and they have to deal with a user maliciously typing in
sdfksdjf. As you try to make your applications more robust, Exception-handling skills will develop.For your next iteration...
I challenge you to do some file handling! Download this file to your workspace and figure out how to read the words into your program and have it "think up" a random word from it. :)
Context
StackExchange Code Review Q#47504, answer score: 10
Revisions (0)
No revisions yet.