0

I decided to make a really simple batch file game, yet when it gets to the IFs, where I try to compare ERRORLEVEL to a character it simply quits. I even tried comparing it to an ASCII code.

Here is my spaghetti code, (I know I shouldn't use ::, and I should use rem instead, but a batch this size wouldn't be affected at all).

@echo off

::Declaracion_Variables

set /a dinero = 0
set /a clasificador
set /a picoa = true
set /a picob = false

::---------------------

:main

echo bienvenido a un juego hecho con archivos con lote
echo porque? tenia ganas
set /p clasificador="dale a algo para empezar"
cls
:Visualizar
echo tienes: %dinero%$
echo dale a g para minar
echo dale a s para ir al shop
choice /c gs
IF ERRORLEVEL == g (
    IF "%picoa%" == "true" (
        set /a dinero = dinero + 1
        goto Visualizar
    )
    IF "%picob%" == "true" (
        set /a dinero = dinero + 2
        goto Visualizar
    )

) 

IF ERRORLEVEL == s (
    IF "%picoa%" == "true" (
        echo pico * 2 // 200$ // Producto A
        echo terminar el juego // 600$ // Producto B
        set /p clasificador="porfavor pulse la tecla corespondiente al producto o pulse K para salir"
            IF "%clasificador%" == "a"(
                IF "%dinero%" GTR 200 (
                    set /a picoa = false
                    set /a picob = true
                    cls
                    :Visualizar
                )
                IF "%dinero%" LSS 200 (
                    echo no tienes bastante dinero
                    pause
                    cls
                    :Visualizar
                )
            )
                IF "%clasificador%" == "b"(
                IF "%dinero%" GTR 600 (
                    cls
                    echo gracias por haber jugado
                    pause
                    exit
                    :Visualizar
                )
                IF "%dinero%" LSS 600 (
                    echo no tienes bastante dinero
                    pause
                    cls
                    :Visualizar
                )
            )

    )
        IF "%picob%" == "true" (
        echo terminar el juego // 600$
        set /p clasificador="porfavor pulse la tecla corespondiente al producto o pulse K para salir"
        IF "%clasificador%" == "a"(
                IF "%dinero%" GTR 600 (
                    cls
                    echo gracias por haber jugado
                    pause
                    exit
                    :Visualizar
                )
                IF "%dinero%" LSS 600 (
                    echo no tienes bastante dinero
                    pause
                    cls
                    :Visualizar
                )
            )
    )
)
Compo
  • 30,301
  • 4
  • 20
  • 32
  • 1
    Start by typing `IF /?` at the command prompt and read the output. Then don't do anything with the information, just type `SET /?` at the command prompt and do some more reading. After that try to rewrite your code and come back here with a specific question if the code still exhibits issues. It may sound like one of those comments you don't want to receive but believe me, your issues can be solved by reading how to use the commands your using. – Compo Jun 04 '18 at 22:33
  • `if errorlevel == s` is incorrect syntax. Anyway, `choice` sets `%ERRORLEVEL%` to the *index* of the answer, so you want `if errorlevel == 1` instead of `== g`. – AlexP Jun 04 '18 at 22:44
  • Read the help files for the commands you are trying to use before you ask a question. The help file for the choice command is pretty clear on its usage for the IF command. – Squashman Jun 05 '18 at 00:55
  • Read answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) where I have explained usage of __CHOICE__ very detailed with examples including how to correct process the exit codes of __CHOICE__. And do not use `set /a` to assign a value to an environment variable. Everything after `/a` is interpreted as __arithmetic expression__. See [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) for details on `set`. – Mofi Jun 05 '18 at 06:23
  • ok i checked the cmd guide for choice,set and IF, this is waaaay more complicated than what i thought, i'm converting the code in c++ (at least i know how things works there) and doing another easier to make batch program (it is for school the prof said that if someone wants a positive they must bring something to impress him so yeah i'm basically the only one in my classroom that can code) thanks for the help anyways, i could probably code a calculator with what i learned from the cmd guide – Massimiliano Jun 05 '18 at 20:16

0 Answers0