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













2












$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("









share|improve this question











$endgroup$
















    2












    $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("









    share|improve this question











    $endgroup$














      2












      2








      2


      1



      $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("









      share|improve this question











      $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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 1 at 8:20









      200_success

      130k17153419




      130k17153419










      asked Feb 28 at 14:12









      Martin FrankMartin Frank

      659319




      659319




















          3 Answers
          3






          active

          oldest

          votes


















          1












          $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());







          share|improve this answer











          $endgroup$












          • $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


















          1












          $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.






          share|improve this answer









          $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


















          0












          $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.





          share









          $endgroup$












            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
            );



            );













            draft saved

            draft discarded


















            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









            1












            $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());







            share|improve this answer











            $endgroup$












            • $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















            1












            $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());







            share|improve this answer











            $endgroup$












            • $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













            1












            1








            1





            $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());







            share|improve this answer











            $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());








            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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$
              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$
            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













            1












            $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.






            share|improve this answer









            $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















            1












            $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.






            share|improve this answer









            $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













            1












            1








            1





            $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.






            share|improve this answer









            $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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • $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











            0












            $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.





            share









            $endgroup$

















              0












              $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.





              share









              $endgroup$















                0












                0








                0





                $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.





                share









                $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.






                share











                share


                share










                answered 6 mins ago









                Austin HastingsAustin Hastings

                7,2421233




                7,2421233



























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    बाताम इन्हें भी देखें सन्दर्भ दिक्चालन सूची1°05′00″N 104°02′0″E / 1.08333°N 104.03333°E / 1.08333; 104.033331°05′00″N 104°02′0″E / 1.08333°N 104.03333°E / 1.08333; 104.03333

                    Why is the 'in' operator throwing an error with a string literal instead of logging false?Why can't I use switch statement on a String?Python join: why is it string.join(list) instead of list.join(string)?Multiline String Literal in C#Why does comparing strings using either '==' or 'is' sometimes produce a different result?How to initialize an array's length in javascript?How can I print literal curly-brace characters in python string and also use .format on it?Why does ++[[]][+[]]+[+[]] return the string “10”?Why is char[] preferred over String for passwords?Why does this code using random strings print “hello world”?jQuery.inArray(), how to use it right?

                    How can we generalize the fact of finite dimensional vector space to an infinte dimensional case?$k[x]$-module and cyclic module over a finite dimensional vector spaceSubspace of a finite dimensional space is finite dimensionalIf V is an infinite-dimensional vector space, and S is an infinite-dimensional subspace of V, must the dimension of V/S be finite? ExplainWhy is an infinite dimensional space so different than a finite dimensional one?base for finite dimensional vector space is not infinite dimensional vector space?Any finite-dimensional vector space is the dual space of anotherHaving Trouble Understanding Meaning Of A Finite-Dimensional Vector SpaceProve that “Every subspaces of a finite-dimensional vector space is finite-dimensional”Ring as a finite dimensional Vector space over a field KQuestion regarding basis and dimension