Display command-line HangmanHangman game codeCommand line Contact ManagementHangman on the C++ommand...
If I sold a PS4 game I owned the disc for, can I reinstall it digitally?
How to tag distinct options/entities without giving any an implicit priority or suggested order?
Eww, those bytes are gross
Difference between thick vs thin front suspension?
What makes the Forgotten Realms "forgotten"?
Book where aliens are selecting humans for food consumption
What does Cypher mean when he says Neo is "gonna pop"?
Difference between two quite-similar Terminal commands
Why does a metal block make a shrill sound but not a wooden block upon hammering?
Should I write a companion book/blog?
The effects of magnetism in radio transmissions
Are there neural networks with very few nodes that decently solve non-trivial problems?
Word or phrase for showing great skill at something without formal training in it
Does fast page mode apply to ROM?
Can I become debt free or should I file for bankruptcy? How do I manage my debt and finances?
How to explain planetary rings pulsating?
Quenching swords in dragon blood; why?
Contest math problem about crossing out numbers in the table
Why does String.replaceAll() work differently in Java 8 from Java 9?
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
Is there some relative to Dutch word "kijken" in German?
Why doesn't "auto ch = unsigned char{'p'}" compile under C++ 17?
Solving Fredholm Equation of the second kind
Pre-1980's science fiction short story: alien disguised as a woman shot by a gangster, has tentacles coming out of her breasts when remaking her body
Display command-line Hangman
Hangman game codeCommand line Contact ManagementHangman on the C++ommand lineMulti-player game server accessing and querying MongoDBDice-throwing gameBeginnings of a Chess gameCommand line Hangman gameHangman game class in PythonJava Hangman implementationHangman v2 written in C
$begingroup$
This code is part of my Hangman game. One of my minor issues is how to properly print out the hangman.
Could you please give me hints on how to make the code less hardcoded? This code is so redundant, shoot me...
Any comments are welcome.
class HangmanState
static void show(int count, PrintStream out) {
switch(count){
case 1: {
showOne(out);
break;
}
case 2: {
showTwo(out);
break;
}
case 3: {
showThree(out);
break;
}
case 4: {
showFour(out);
break;
}
case 5: {
showFive(out);
break;
}case 6: {
showSix(out);
break;
}
case 7: {
showSeven(out);
break;
}
case 8: {
showEight(out);
break;
}
case 9: {
showNine(out);
break;
}case 10: {
showTen(out);
break;
}default: {
showZero(out);
break;
}
}
}
private static void showZero(PrintStream out) {
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" _______________");
out.println(" | 0/10 |");
out.println(" | |");
}
private static void showOne(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
}
private static void showTwo(PrintStream out) {
out.println(" ________");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 2/10 |");
out.println(" | |");
}
private static void showThree(PrintStream out) {
out.println(" ________");
out.println(" \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 3/10 |");
out.println(" | |");
}
private static void showFour(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 4/10 |");
out.println(" | |");
}
private static void showFive(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 5/10 |");
out.println(" | |");
}
private static void showSix(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" | |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 6/10 |");
out.println(" | |");
}
private static void showSeven(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 7/10 |");
out.println(" | |");
}
private static void showEight(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 8/10 |");
out.println(" | |");
}
private static void showNine(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / |");
out.println(" ___________|___");
out.println(" | 9/10 |");
out.println(" | |");
}
private static void showTen(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / \ |");
out.println(" ___________|___");
out.println(" | 10/10 |");
out.println(" | R.I.P |");
}
java ascii-art hangman
$endgroup$
add a comment |
$begingroup$
This code is part of my Hangman game. One of my minor issues is how to properly print out the hangman.
Could you please give me hints on how to make the code less hardcoded? This code is so redundant, shoot me...
Any comments are welcome.
class HangmanState
static void show(int count, PrintStream out) {
switch(count){
case 1: {
showOne(out);
break;
}
case 2: {
showTwo(out);
break;
}
case 3: {
showThree(out);
break;
}
case 4: {
showFour(out);
break;
}
case 5: {
showFive(out);
break;
}case 6: {
showSix(out);
break;
}
case 7: {
showSeven(out);
break;
}
case 8: {
showEight(out);
break;
}
case 9: {
showNine(out);
break;
}case 10: {
showTen(out);
break;
}default: {
showZero(out);
break;
}
}
}
private static void showZero(PrintStream out) {
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" _______________");
out.println(" | 0/10 |");
out.println(" | |");
}
private static void showOne(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
}
private static void showTwo(PrintStream out) {
out.println(" ________");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 2/10 |");
out.println(" | |");
}
private static void showThree(PrintStream out) {
out.println(" ________");
out.println(" \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 3/10 |");
out.println(" | |");
}
private static void showFour(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 4/10 |");
out.println(" | |");
}
private static void showFive(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 5/10 |");
out.println(" | |");
}
private static void showSix(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" | |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 6/10 |");
out.println(" | |");
}
private static void showSeven(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 7/10 |");
out.println(" | |");
}
private static void showEight(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 8/10 |");
out.println(" | |");
}
private static void showNine(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / |");
out.println(" ___________|___");
out.println(" | 9/10 |");
out.println(" | |");
}
private static void showTen(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / \ |");
out.println(" ___________|___");
out.println(" | 10/10 |");
out.println(" | R.I.P |");
}
java ascii-art hangman
$endgroup$
add a comment |
$begingroup$
This code is part of my Hangman game. One of my minor issues is how to properly print out the hangman.
Could you please give me hints on how to make the code less hardcoded? This code is so redundant, shoot me...
Any comments are welcome.
class HangmanState
static void show(int count, PrintStream out) {
switch(count){
case 1: {
showOne(out);
break;
}
case 2: {
showTwo(out);
break;
}
case 3: {
showThree(out);
break;
}
case 4: {
showFour(out);
break;
}
case 5: {
showFive(out);
break;
}case 6: {
showSix(out);
break;
}
case 7: {
showSeven(out);
break;
}
case 8: {
showEight(out);
break;
}
case 9: {
showNine(out);
break;
}case 10: {
showTen(out);
break;
}default: {
showZero(out);
break;
}
}
}
private static void showZero(PrintStream out) {
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" _______________");
out.println(" | 0/10 |");
out.println(" | |");
}
private static void showOne(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
}
private static void showTwo(PrintStream out) {
out.println(" ________");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 2/10 |");
out.println(" | |");
}
private static void showThree(PrintStream out) {
out.println(" ________");
out.println(" \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 3/10 |");
out.println(" | |");
}
private static void showFour(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 4/10 |");
out.println(" | |");
}
private static void showFive(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 5/10 |");
out.println(" | |");
}
private static void showSix(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" | |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 6/10 |");
out.println(" | |");
}
private static void showSeven(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 7/10 |");
out.println(" | |");
}
private static void showEight(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 8/10 |");
out.println(" | |");
}
private static void showNine(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / |");
out.println(" ___________|___");
out.println(" | 9/10 |");
out.println(" | |");
}
private static void showTen(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / \ |");
out.println(" ___________|___");
out.println(" | 10/10 |");
out.println(" | R.I.P |");
}
java ascii-art hangman
$endgroup$
This code is part of my Hangman game. One of my minor issues is how to properly print out the hangman.
Could you please give me hints on how to make the code less hardcoded? This code is so redundant, shoot me...
Any comments are welcome.
class HangmanState
static void show(int count, PrintStream out) {
switch(count){
case 1: {
showOne(out);
break;
}
case 2: {
showTwo(out);
break;
}
case 3: {
showThree(out);
break;
}
case 4: {
showFour(out);
break;
}
case 5: {
showFive(out);
break;
}case 6: {
showSix(out);
break;
}
case 7: {
showSeven(out);
break;
}
case 8: {
showEight(out);
break;
}
case 9: {
showNine(out);
break;
}case 10: {
showTen(out);
break;
}default: {
showZero(out);
break;
}
}
}
private static void showZero(PrintStream out) {
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" _______________");
out.println(" | 0/10 |");
out.println(" | |");
}
private static void showOne(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
}
private static void showTwo(PrintStream out) {
out.println(" ________");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 2/10 |");
out.println(" | |");
}
private static void showThree(PrintStream out) {
out.println(" ________");
out.println(" \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 3/10 |");
out.println(" | |");
}
private static void showFour(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 4/10 |");
out.println(" | |");
}
private static void showFive(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 5/10 |");
out.println(" | |");
}
private static void showSix(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" | |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 6/10 |");
out.println(" | |");
}
private static void showSeven(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" |\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 7/10 |");
out.println(" | |");
}
private static void showEight(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 8/10 |");
out.println(" | |");
}
private static void showNine(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / |");
out.println(" ___________|___");
out.println(" | 9/10 |");
out.println(" | |");
}
private static void showTen(PrintStream out) {
out.println(" ________");
out.println(" | \|");
out.println(" o |");
out.println(" /|\ |");
out.println(" | |");
out.println(" / \ |");
out.println(" ___________|___");
out.println(" | 10/10 |");
out.println(" | R.I.P |");
}
java ascii-art hangman
java ascii-art hangman
edited 15 hours ago
200_success
130k16153417
130k16153417
asked yesterday
Martin FrankMartin Frank
649319
649319
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Use a Map
Since Java 8 it is possible to treat methods as higher order functions, what makes it possible to store them as a value inside a Map
Map<Integer, Consume<OutputStream> countByConsumer = new HashMap<>();
countByConsumer.put(1, BaseTest::showOne);
countByConsumer.put(2, BaseTest::showTwo);
Than you can simply use get
inside show
static void show(int count, PrintStream out) {
Consumer<PrintStream> printStreamConsumer = countByConsumer.get(count);
printStreamConsumer.accept(out);
}
The advantage of this method is that you get ride of the huge switch
Use OOP
Use the State-Pattern
If you want a oop solution, i think the state-pattern would be the way to go. There for you would have the class Hangman
which have multiple HealthState
s
class Hangman {
private HealthState healthState;
// constructor
void setHealthState(HealthState healthState) {
this.healthState = healthState;
}
void display(HealthState healthState, PrintStream out) {
healthState.display(out)
}
}
class OneHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
hangman.setHealthState(new TwoHealth());
}
}
class TwoHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
/*...*/
hangman.setHealthState(new ThreeHealth());
}
}
$endgroup$
$begingroup$
i don't think it's a good idea to set the state duringdisplay
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)
$endgroup$
– Martin Frank
18 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return aList
or a custom object
$endgroup$
– Roman
17 hours ago
add a comment |
$begingroup$
You can effectively compress the data by defining two strings, representing a format and a mask.
private static final String IMG_FMT =
" ________%n" +
" | \|%n" +
" o |%n" +
" /|\ |%n" +
" | |%n" +
" / \ |%n" +
" ___________|___%n" +
" | %2d/10 |%n" +
" | R.I.P |%n";
private static final String IMG_FMT_MASK =
" 2222222200" +
" 4 3100" +
" 5 100" +
" 867 100" +
" 6 100" +
" 9 a 100" +
" 00000000000100000" +
" 0 000000 000" +
" 0 aaaaa 000";
static { assert(IMG_FMT.length() == IMG_FMT_MASK.length()); }
public static void show(int stage, PrintStream out) {
char m = Character.forDigit(stage, 36);
StringBuilder s = new StringBuilder(IMG_FMT.length());
for (int i = 0; i < IMG_FMT.length(); i++) {
s.append((IMG_FMT_MASK.charAt(i) <= m) ? IMG_FMT.charAt(i) : ' ');
}
out.printf(s.toString(), stage);
}
A caveat, though, is that the animation must be additive. Specifically, you want the base of the vertical post to change from -
initially to a |
character, and that cannot be accomplished using this technique without a nasty hack.
$endgroup$
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214471%2fdisplay-command-line-hangman%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Use a Map
Since Java 8 it is possible to treat methods as higher order functions, what makes it possible to store them as a value inside a Map
Map<Integer, Consume<OutputStream> countByConsumer = new HashMap<>();
countByConsumer.put(1, BaseTest::showOne);
countByConsumer.put(2, BaseTest::showTwo);
Than you can simply use get
inside show
static void show(int count, PrintStream out) {
Consumer<PrintStream> printStreamConsumer = countByConsumer.get(count);
printStreamConsumer.accept(out);
}
The advantage of this method is that you get ride of the huge switch
Use OOP
Use the State-Pattern
If you want a oop solution, i think the state-pattern would be the way to go. There for you would have the class Hangman
which have multiple HealthState
s
class Hangman {
private HealthState healthState;
// constructor
void setHealthState(HealthState healthState) {
this.healthState = healthState;
}
void display(HealthState healthState, PrintStream out) {
healthState.display(out)
}
}
class OneHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
hangman.setHealthState(new TwoHealth());
}
}
class TwoHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
/*...*/
hangman.setHealthState(new ThreeHealth());
}
}
$endgroup$
$begingroup$
i don't think it's a good idea to set the state duringdisplay
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)
$endgroup$
– Martin Frank
18 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return aList
or a custom object
$endgroup$
– Roman
17 hours ago
add a comment |
$begingroup$
Use a Map
Since Java 8 it is possible to treat methods as higher order functions, what makes it possible to store them as a value inside a Map
Map<Integer, Consume<OutputStream> countByConsumer = new HashMap<>();
countByConsumer.put(1, BaseTest::showOne);
countByConsumer.put(2, BaseTest::showTwo);
Than you can simply use get
inside show
static void show(int count, PrintStream out) {
Consumer<PrintStream> printStreamConsumer = countByConsumer.get(count);
printStreamConsumer.accept(out);
}
The advantage of this method is that you get ride of the huge switch
Use OOP
Use the State-Pattern
If you want a oop solution, i think the state-pattern would be the way to go. There for you would have the class Hangman
which have multiple HealthState
s
class Hangman {
private HealthState healthState;
// constructor
void setHealthState(HealthState healthState) {
this.healthState = healthState;
}
void display(HealthState healthState, PrintStream out) {
healthState.display(out)
}
}
class OneHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
hangman.setHealthState(new TwoHealth());
}
}
class TwoHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
/*...*/
hangman.setHealthState(new ThreeHealth());
}
}
$endgroup$
$begingroup$
i don't think it's a good idea to set the state duringdisplay
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)
$endgroup$
– Martin Frank
18 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return aList
or a custom object
$endgroup$
– Roman
17 hours ago
add a comment |
$begingroup$
Use a Map
Since Java 8 it is possible to treat methods as higher order functions, what makes it possible to store them as a value inside a Map
Map<Integer, Consume<OutputStream> countByConsumer = new HashMap<>();
countByConsumer.put(1, BaseTest::showOne);
countByConsumer.put(2, BaseTest::showTwo);
Than you can simply use get
inside show
static void show(int count, PrintStream out) {
Consumer<PrintStream> printStreamConsumer = countByConsumer.get(count);
printStreamConsumer.accept(out);
}
The advantage of this method is that you get ride of the huge switch
Use OOP
Use the State-Pattern
If you want a oop solution, i think the state-pattern would be the way to go. There for you would have the class Hangman
which have multiple HealthState
s
class Hangman {
private HealthState healthState;
// constructor
void setHealthState(HealthState healthState) {
this.healthState = healthState;
}
void display(HealthState healthState, PrintStream out) {
healthState.display(out)
}
}
class OneHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
hangman.setHealthState(new TwoHealth());
}
}
class TwoHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
/*...*/
hangman.setHealthState(new ThreeHealth());
}
}
$endgroup$
Use a Map
Since Java 8 it is possible to treat methods as higher order functions, what makes it possible to store them as a value inside a Map
Map<Integer, Consume<OutputStream> countByConsumer = new HashMap<>();
countByConsumer.put(1, BaseTest::showOne);
countByConsumer.put(2, BaseTest::showTwo);
Than you can simply use get
inside show
static void show(int count, PrintStream out) {
Consumer<PrintStream> printStreamConsumer = countByConsumer.get(count);
printStreamConsumer.accept(out);
}
The advantage of this method is that you get ride of the huge switch
Use OOP
Use the State-Pattern
If you want a oop solution, i think the state-pattern would be the way to go. There for you would have the class Hangman
which have multiple HealthState
s
class Hangman {
private HealthState healthState;
// constructor
void setHealthState(HealthState healthState) {
this.healthState = healthState;
}
void display(HealthState healthState, PrintStream out) {
healthState.display(out)
}
}
class OneHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
out.println(" ");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" |");
out.println(" ___________|___");
out.println(" | 1/10 |");
out.println(" | |");
hangman.setHealthState(new TwoHealth());
}
}
class TwoHealth implements HealthState {
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out) {
/*...*/
hangman.setHealthState(new ThreeHealth());
}
}
edited yesterday
answered yesterday
RomanRoman
867214
867214
$begingroup$
i don't think it's a good idea to set the state duringdisplay
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)
$endgroup$
– Martin Frank
18 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return aList
or a custom object
$endgroup$
– Roman
17 hours ago
add a comment |
$begingroup$
i don't think it's a good idea to set the state duringdisplay
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)
$endgroup$
– Martin Frank
18 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return aList
or a custom object
$endgroup$
– Roman
17 hours ago
$begingroup$
i don't think it's a good idea to set the state during
display
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)$endgroup$
– Martin Frank
18 hours ago
$begingroup$
i don't think it's a good idea to set the state during
display
... but really: using methods as higher order functions is a real bumper - thats a very good hint on how to handle that (since this is not the first time i ran into a problem like this)$endgroup$
– Martin Frank
18 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return a
List
or a custom object$endgroup$
– Roman
17 hours ago
$begingroup$
yes, you are right. Maybe it would be better to return a
List
or a custom object$endgroup$
– Roman
17 hours ago
add a comment |
$begingroup$
You can effectively compress the data by defining two strings, representing a format and a mask.
private static final String IMG_FMT =
" ________%n" +
" | \|%n" +
" o |%n" +
" /|\ |%n" +
" | |%n" +
" / \ |%n" +
" ___________|___%n" +
" | %2d/10 |%n" +
" | R.I.P |%n";
private static final String IMG_FMT_MASK =
" 2222222200" +
" 4 3100" +
" 5 100" +
" 867 100" +
" 6 100" +
" 9 a 100" +
" 00000000000100000" +
" 0 000000 000" +
" 0 aaaaa 000";
static { assert(IMG_FMT.length() == IMG_FMT_MASK.length()); }
public static void show(int stage, PrintStream out) {
char m = Character.forDigit(stage, 36);
StringBuilder s = new StringBuilder(IMG_FMT.length());
for (int i = 0; i < IMG_FMT.length(); i++) {
s.append((IMG_FMT_MASK.charAt(i) <= m) ? IMG_FMT.charAt(i) : ' ');
}
out.printf(s.toString(), stage);
}
A caveat, though, is that the animation must be additive. Specifically, you want the base of the vertical post to change from -
initially to a |
character, and that cannot be accomplished using this technique without a nasty hack.
$endgroup$
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
add a comment |
$begingroup$
You can effectively compress the data by defining two strings, representing a format and a mask.
private static final String IMG_FMT =
" ________%n" +
" | \|%n" +
" o |%n" +
" /|\ |%n" +
" | |%n" +
" / \ |%n" +
" ___________|___%n" +
" | %2d/10 |%n" +
" | R.I.P |%n";
private static final String IMG_FMT_MASK =
" 2222222200" +
" 4 3100" +
" 5 100" +
" 867 100" +
" 6 100" +
" 9 a 100" +
" 00000000000100000" +
" 0 000000 000" +
" 0 aaaaa 000";
static { assert(IMG_FMT.length() == IMG_FMT_MASK.length()); }
public static void show(int stage, PrintStream out) {
char m = Character.forDigit(stage, 36);
StringBuilder s = new StringBuilder(IMG_FMT.length());
for (int i = 0; i < IMG_FMT.length(); i++) {
s.append((IMG_FMT_MASK.charAt(i) <= m) ? IMG_FMT.charAt(i) : ' ');
}
out.printf(s.toString(), stage);
}
A caveat, though, is that the animation must be additive. Specifically, you want the base of the vertical post to change from -
initially to a |
character, and that cannot be accomplished using this technique without a nasty hack.
$endgroup$
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
add a comment |
$begingroup$
You can effectively compress the data by defining two strings, representing a format and a mask.
private static final String IMG_FMT =
" ________%n" +
" | \|%n" +
" o |%n" +
" /|\ |%n" +
" | |%n" +
" / \ |%n" +
" ___________|___%n" +
" | %2d/10 |%n" +
" | R.I.P |%n";
private static final String IMG_FMT_MASK =
" 2222222200" +
" 4 3100" +
" 5 100" +
" 867 100" +
" 6 100" +
" 9 a 100" +
" 00000000000100000" +
" 0 000000 000" +
" 0 aaaaa 000";
static { assert(IMG_FMT.length() == IMG_FMT_MASK.length()); }
public static void show(int stage, PrintStream out) {
char m = Character.forDigit(stage, 36);
StringBuilder s = new StringBuilder(IMG_FMT.length());
for (int i = 0; i < IMG_FMT.length(); i++) {
s.append((IMG_FMT_MASK.charAt(i) <= m) ? IMG_FMT.charAt(i) : ' ');
}
out.printf(s.toString(), stage);
}
A caveat, though, is that the animation must be additive. Specifically, you want the base of the vertical post to change from -
initially to a |
character, and that cannot be accomplished using this technique without a nasty hack.
$endgroup$
You can effectively compress the data by defining two strings, representing a format and a mask.
private static final String IMG_FMT =
" ________%n" +
" | \|%n" +
" o |%n" +
" /|\ |%n" +
" | |%n" +
" / \ |%n" +
" ___________|___%n" +
" | %2d/10 |%n" +
" | R.I.P |%n";
private static final String IMG_FMT_MASK =
" 2222222200" +
" 4 3100" +
" 5 100" +
" 867 100" +
" 6 100" +
" 9 a 100" +
" 00000000000100000" +
" 0 000000 000" +
" 0 aaaaa 000";
static { assert(IMG_FMT.length() == IMG_FMT_MASK.length()); }
public static void show(int stage, PrintStream out) {
char m = Character.forDigit(stage, 36);
StringBuilder s = new StringBuilder(IMG_FMT.length());
for (int i = 0; i < IMG_FMT.length(); i++) {
s.append((IMG_FMT_MASK.charAt(i) <= m) ? IMG_FMT.charAt(i) : ' ');
}
out.printf(s.toString(), stage);
}
A caveat, though, is that the animation must be additive. Specifically, you want the base of the vertical post to change from -
initially to a |
character, and that cannot be accomplished using this technique without a nasty hack.
answered 15 hours ago
200_success200_success
130k16153417
130k16153417
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
add a comment |
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
14 hours ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214471%2fdisplay-command-line-hangman%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown