-1

So I'm trying to create a simple ROCK-PAPER-SCISSORS game wherein I want to display ASCII artwork for the individual elements. It would be great if I could store them into variables in some way for the further logic to follow up & use those variables.

   ....
   .....
     .......
      ........
         ........
            ........
               ........           .........
                 ..................       ..
                   ................       ..
                     .........   ....  ....
                      ..     ....
                      ..        ...
                       ...         ...
                       ...         ..
                          ...........

This is the piece of art & when I try to store it into a variable like:

set /p scissors = " the above art "

I get errors.

Does someone know how to resolve this?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 1
    Which Windows version is this meant to run on? – Gerhard Dec 16 '21 at 13:36
  • 1
    Here's an idea, call your game R-P-S, and guess what, you can output the options much easier, `%SystemRoot%\System32\choice.exe /C RPS`. Or if you really wanted to use representaions instead of alphabet letters, `@ _ X` each of which would take up the same mimimal amount of space. – Compo Dec 16 '21 at 13:49
  • 2
    `set /P` is for prompting the user for a value, and `set /P VAR = …` will not set `VAR` but `VAR`+ _space_. Anyway, do you really need the text in a variable? Take a look at this post for alternatives: [Windows batch: Can't echo ASCII art with ._|_](https://stackoverflow.com/a/35855559)… – aschipfl Dec 16 '21 at 14:00
  • 1
    This line: `set /p scissors = " the above art "` say nothing to us about your code. What do you put in the `set /p` command line? _The first line_ of the "above art"? And the rest of the lines below? If so, then it should be obvious why do you get errors... – Aacini Dec 16 '21 at 20:49

4 Answers4

3

Another of may ways to approach the goal is to use findstr to read data embedded in your file. The findstr command may be assigned to a variable allowing it to be used repeatedly for different elements:

@echo off

Setlocal EnableDelayedExpansion

 Mode 1000
 Set "Scissors="
 Setlocal EnableDelayedExpansion
 Set "RPS=For /f "tokens=2 Delims=RPS123" %%G in ('%SystemRoot%\System32\Findstr.exe /bl ":RPS#" "%~f0"')Do Echo(%%G"
 Choice /C:RPS
 %RPS:#=!Errorlevel!%

:RPS1             
:RPS1                
:RPS1                .. ... .. .. . 
:RPS1            . ... ... .. . .. .
:RPS1          . .. .  ... ... ......
:RPS1         .........   ..... ......
:RPS1        . ......   ..... ........ .
:RPS1         .........   ..... ......
:RPS1        .. ..  ................ .        
:RPS1          .. .....  .. .. .......
:RPS1            . . . ... . .. ... .
:RPS1             . ... .. . . . .
:RPS1              ...   ...   ..
:RPS1       
:RPS1          


:RPS2      . 
:RPS2     .  .
:RPS2    .     .
:RPS2   .        .
:RPS2  .           .
:RPS2 .              .
:RPS2.                 .
:RPS2  .                 .
:RPS2    .                 . 
:RPS2      .              .
:RPS2        .           .
:RPS2          .        .
:RPS2            .     .
:RPS2              .  .
:RPS2                .

:RPS3  ....
:RPS3   .....
:RPS3     .......
:RPS3      ........
:RPS3         ........
:RPS3            ........
:RPS3               ........           .........
:RPS3                 ..................       ..
:RPS3                   ................       ..
:RPS3                     .........   ....  ....
:RPS3                      ..     ....
:RPS3                      ..        ...
:RPS3                       ...         ...
:RPS3                       ...         ..
:RPS3                          ...........
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
1

You could define your variable like this:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set Scissors=^

....^

  .....^

    .......^

      ........^

         ........^

            ........^

               ........           .........^

                 ..................       ..^

                   ................       ..^

                     .........   ....  ....^

                      ..     ....^

                      ..        ...^

                       ...         ...^

                       ...         ..^

                          ...........

Then to use it you could enable delayed expansion.

SetLocal EnableDelayedExpansion
Echo(!Scissors!

Then later, revert to the previous delayed expansion state:

EndLocal

If you did not want to enable delayed expansion, it would take a little more work to output your image:

For /F "Tokens=1,* Delims==" %%G In ('"(Set Scissors) 2>NUL"') Do Echo(%%G
Compo
  • 36,585
  • 5
  • 27
  • 39
1

Another way:

@echo off
setlocal EnableDelayedExpansion

REM Define variables:

(for %%a in (
"      .. ... .. .. ."
"    . ... ... .. . .. ."
"  . .. .  ... ... ......"
" .........   ..... ......"
". ......   ..... ........ ."
" .........   ..... ......"
".. ..  ................ ."
"  .. .....  .. .. ......."
"    . . . ... . .. ... ."
"     . ... .. . . . ."
"      ...   ...   ..") do set "rock=!rock!$%%~a") & set "rock=!rock:~1!"

(for %%a in (
"      ."
"     .  ."
"    .     ."
"   .        ."
"  .           ."
" .              ."
".                 ."
"  .                 ."
"    .                 ."
"      .              ."
"        .           ."
"          .        ."
"            .     ."
"              .  ."
"                .") do set "paper=!paper!$%%~a") & set "paper=!paper:~1!"

(for %%a in (
"...."
" ....."
"   ......."
"    ........"
"       ........"
"          ........"
"             ........           ........."
"               ..................       .."
"                 ................       .."
"                   .........   ....  ...."
"                    ..     ...."
"                    ..        ..."
"                     ...         ..."
"                     ...         .."
"                        ...........") do set "scissors=!scissors!$%%~a") & set "scissors=!scissors:~1!"

REM Display variables:

cls

echo rock: & echo/& echo %rock:$=& echo %& echo/& echo/

echo paper: & echo/& echo %paper:$=& echo %& echo/& echo/

echo scissors: & echo/& echo %scissors:$=& echo %& echo/& echo/
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Just putting an option out there, if you are running Windows 10, which supports VT100 escape codes, you can do something like:

@echo off
for /F %%i in ('echo prompt $E ^| cmd') do set "n=%%iE"
set "scissors=%n%   ....%n%   .....%n%     .......%n%      ........%n%         ........%n%            ........%n%               ........           .........%n%                 ..................       ..%n%                   ................       ..%n%                     .........   ....  ....%n%                      ..     ....%n%                      ..        ...%n%                       ...         ...%n%                       ...         ..%n%                          ..........."
echo %scissors%
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • You are missing the `[` charcter in the Linefeed control sequence. `for /F %%i in ('echo(prompt $E^|cmd') do set "n=%%i[E"` – T3RR0R Dec 16 '21 at 14:13
  • @T3RR0R not relevant here, test it for yourself. :) – Gerhard Dec 16 '21 at 14:17
  • I already had, and it fails as you've scripted it, it fails in cmd.exe and powershell.exe - It does however output correctly in windows terminal. – T3RR0R Dec 16 '21 at 14:23
  • conversely, the reverse is true of `ESC[E`, it outputs correctly in Cmd.exe and powershell, but not in windows terminal unless explicitly in a cmd tab – T3RR0R Dec 16 '21 at 14:25
  • Which version of Windows are you running, @T3RR0R? – Gerhard Dec 16 '21 at 14:26
  • Microsoft Windows [Version 10.0.19043.1348] – T3RR0R Dec 16 '21 at 14:27
  • I am running `Microsoft Windows [Version 10.0.22000.318]` i.e Windows 11. . here is [proof](https://i.stack.imgur.com/nDshu.png) it works as is for me. – Gerhard Dec 16 '21 at 14:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240207/discussion-between-t3rr0r-and-gerhard). – T3RR0R Dec 16 '21 at 14:31