Display command-line HangmanHangman 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
In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf
Site Collection Administrator has left the building
Python if-else code style for reduced code
Are there verbs that are neither telic, or atelic?
How to explain that I do not want to visit a country due to personal safety concern?
Who is flying the vertibirds?
It's a yearly task, alright
Existence of subset with given Hausdorff dimension
Why doesn't using two cd commands in bash script execute the second command?
What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?
Recruiter wants very extensive technical details about all of my previous work
What's causing this power spike in STM32 low power mode
Min function accepting varying number of arguments in C++17
How does the Sleep spell interact with the Aspect of the Moon eldritch invocation?
A sequence that has integer values for prime indexes only:
What is the significance behind "40 days" that often appears in the Bible?
Gravity magic - How does it work?
Does Mathematica reuse previous computations?
How do I hide Chekhov's Gun?
Professor being mistaken for a grad student
Science-fiction short story where space navy wanted hospital ships and settlers had guns mounted everywhere
Do I need life insurance if I can cover my own funeral costs?
Employee lack of ownership
Do I need to be arrogant to get ahead?
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)
private static void showOne(PrintStream out) ");
out.println("
private static void showTwo(PrintStream out) ");
out.println(" ___________
private static void showThree(PrintStream out) ___");
out.println("
private static void showFour(PrintStream out) ");
out.println("
private static void showFive(PrintStream out) ");
out.println("
private static void showSix(PrintStream out) ");
out.println("
private static void showSeven(PrintStream out) 7/10
private static void showEight(PrintStream out) ");
private static void showNine(PrintStream out) 9/10
private static void showTen(PrintStream out) ");
out.println("
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)
private static void showOne(PrintStream out) ");
out.println("
private static void showTwo(PrintStream out) ");
out.println(" ___________
private static void showThree(PrintStream out) ___");
out.println("
private static void showFour(PrintStream out) ");
out.println("
private static void showFive(PrintStream out) ");
out.println("
private static void showSix(PrintStream out) ");
out.println("
private static void showSeven(PrintStream out) 7/10
private static void showEight(PrintStream out) ");
private static void showNine(PrintStream out) 9/10
private static void showTen(PrintStream out) ");
out.println("
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)
private static void showOne(PrintStream out) ");
out.println("
private static void showTwo(PrintStream out) ");
out.println(" ___________
private static void showThree(PrintStream out) ___");
out.println("
private static void showFour(PrintStream out) ");
out.println("
private static void showFive(PrintStream out) ");
out.println("
private static void showSix(PrintStream out) ");
out.println("
private static void showSeven(PrintStream out) 7/10
private static void showEight(PrintStream out) ");
private static void showNine(PrintStream out) 9/10
private static void showTen(PrintStream out) ");
out.println("
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)
private static void showOne(PrintStream out) ");
out.println("
private static void showTwo(PrintStream out) ");
out.println(" ___________
private static void showThree(PrintStream out) ___");
out.println("
private static void showFour(PrintStream out) ");
out.println("
private static void showFive(PrintStream out) ");
out.println("
private static void showSix(PrintStream out) ");
out.println("
private static void showSeven(PrintStream out) 7/10
private static void showEight(PrintStream out) ");
private static void showNine(PrintStream out) 9/10
private static void showTen(PrintStream out) ");
out.println("
java ascii-art hangman
java ascii-art hangman
edited Mar 1 at 8:20
200_success
130k17153419
130k17153419
asked Feb 28 at 14:12
Martin FrankMartin Frank
659319
659319
add a comment |
add a comment |
3 Answers
3
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 HealthStates
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("
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
Mar 1 at 5:23
$begingroup$
yes, you are right. Maybe it would be better to return aListor a custom object
$endgroup$
– Roman
Mar 1 at 6:12
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
Mar 1 at 9:12
add a comment |
$begingroup$
The drawings may be arbitrarily complex. As you grow the program, you might want to add color, animation, who knows what.
Thus, your drawings are resources, and should be treated as such.
This answer provides code, links, and explanations. In short:
create one or more (your call) text files
store the drawings in some format (your call) in the text files
open a reader on the appropriate resource:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));read in the data, parse the format, and render the artwork
PROFIT!!
Note: to start with, the right format is probably "here are my chars" and you just read them and echo to the screen. If you create one file per image, you can generate the file name as "imageNN.txt" and your rendering code becomes 5 or 6 lines.
$endgroup$
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
3 Answers
3
active
oldest
votes
3 Answers
3
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 HealthStates
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("
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
Mar 1 at 5:23
$begingroup$
yes, you are right. Maybe it would be better to return aListor a custom object
$endgroup$
– Roman
Mar 1 at 6:12
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 HealthStates
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("
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
Mar 1 at 5:23
$begingroup$
yes, you are right. Maybe it would be better to return aListor a custom object
$endgroup$
– Roman
Mar 1 at 6:12
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 HealthStates
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("
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 HealthStates
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("
class TwoHealth implements HealthState
private Hangman hangman;
// constructor
@Override
public void display(PrintStream out)
/*...*/
hangman.setHealthState(new ThreeHealth());
edited Feb 28 at 15:35
answered Feb 28 at 15:02
RomanRoman
1,017314
1,017314
$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
Mar 1 at 5:23
$begingroup$
yes, you are right. Maybe it would be better to return aListor a custom object
$endgroup$
– Roman
Mar 1 at 6:12
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
Mar 1 at 5:23
$begingroup$
yes, you are right. Maybe it would be better to return aListor a custom object
$endgroup$
– Roman
Mar 1 at 6:12
$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
Mar 1 at 5:23
$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
Mar 1 at 5:23
$begingroup$
yes, you are right. Maybe it would be better to return a
List or a custom object$endgroup$
– Roman
Mar 1 at 6:12
$begingroup$
yes, you are right. Maybe it would be better to return a
List or a custom object$endgroup$
– Roman
Mar 1 at 6:12
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
Mar 1 at 9:12
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
Mar 1 at 9:12
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 Mar 1 at 8:56
200_success200_success
130k17153419
130k17153419
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
Mar 1 at 9:12
add a comment |
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
Mar 1 at 9:12
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
Mar 1 at 9:12
$begingroup$
using a mask - thats a nice technique!!! i never would have had such an idea!!! great!
$endgroup$
– Martin Frank
Mar 1 at 9:12
add a comment |
$begingroup$
The drawings may be arbitrarily complex. As you grow the program, you might want to add color, animation, who knows what.
Thus, your drawings are resources, and should be treated as such.
This answer provides code, links, and explanations. In short:
create one or more (your call) text files
store the drawings in some format (your call) in the text files
open a reader on the appropriate resource:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));read in the data, parse the format, and render the artwork
PROFIT!!
Note: to start with, the right format is probably "here are my chars" and you just read them and echo to the screen. If you create one file per image, you can generate the file name as "imageNN.txt" and your rendering code becomes 5 or 6 lines.
$endgroup$
add a comment |
$begingroup$
The drawings may be arbitrarily complex. As you grow the program, you might want to add color, animation, who knows what.
Thus, your drawings are resources, and should be treated as such.
This answer provides code, links, and explanations. In short:
create one or more (your call) text files
store the drawings in some format (your call) in the text files
open a reader on the appropriate resource:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));read in the data, parse the format, and render the artwork
PROFIT!!
Note: to start with, the right format is probably "here are my chars" and you just read them and echo to the screen. If you create one file per image, you can generate the file name as "imageNN.txt" and your rendering code becomes 5 or 6 lines.
$endgroup$
add a comment |
$begingroup$
The drawings may be arbitrarily complex. As you grow the program, you might want to add color, animation, who knows what.
Thus, your drawings are resources, and should be treated as such.
This answer provides code, links, and explanations. In short:
create one or more (your call) text files
store the drawings in some format (your call) in the text files
open a reader on the appropriate resource:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));read in the data, parse the format, and render the artwork
PROFIT!!
Note: to start with, the right format is probably "here are my chars" and you just read them and echo to the screen. If you create one file per image, you can generate the file name as "imageNN.txt" and your rendering code becomes 5 or 6 lines.
$endgroup$
The drawings may be arbitrarily complex. As you grow the program, you might want to add color, animation, who knows what.
Thus, your drawings are resources, and should be treated as such.
This answer provides code, links, and explanations. In short:
create one or more (your call) text files
store the drawings in some format (your call) in the text files
open a reader on the appropriate resource:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));read in the data, parse the format, and render the artwork
PROFIT!!
Note: to start with, the right format is probably "here are my chars" and you just read them and echo to the screen. If you create one file per image, you can generate the file name as "imageNN.txt" and your rendering code becomes 5 or 6 lines.
answered 6 mins ago
Austin HastingsAustin Hastings
7,2421233
7,2421233
add a comment |
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