-1

I am trying to read the last line from a command prompt as I have another program running which will play a game and in the end echo if I am the winner, loser or we went to a draw. I have this sample code:

Edit: the section "------" isjust an example of what I am trying to achieve. Basically I want to read the line saying e.g. "Player: me win the game!" or "The game was a draw!" so the line was trying to indicate what I want to scan through the line and check if it contains either win, draw else it's a loss

@set win=0
@set draw=0
@set lose=0
@set loopcount=10

:loop
C:\battlesnakeCLI\battlesnake.exe play -W 11 -H 11 --name me --url http://localhost/api/gamev2 --name other --url http://localhost/api/gametest

------
if line.contains("win")
set /a win=win+1
else if (line.contains("draw")
set /a draw=draw+1
else
set /a lose=lose+1
------

set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop

@ECHO Wins: %win% & echo:Draws: %draw% & echo:Losses:%lose%
pause
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Andreasx23
  • 19
  • 6
  • Type `if /?` into a Command Prompt window and learn the correct `if`/`else` syntax. I do not understand what `line` you mean by `line.contains`… – aschipfl Jan 11 '22 at 11:51
  • @aschipfl the section "------" isjust an example of what I am trying to achieve. Basically I want to read the line saying e.g. "Player: me win the game!" or "The game was a draw!" so the line was trying to indicate what I want to scan through the line and check if it contains either win, draw else it's a loss – Andreasx23 Jan 11 '22 at 11:56
  • Yes, but where is the line stored, or where does it come from? – aschipfl Jan 11 '22 at 11:58
  • @aschipfl When calling the line "C:\battlesnakeCLI\battlesnake.exe play -W 11 -H 11 --name me --url http://localhost/api/gamev2 --name other --url http://localhost/api/gametest" it will trigger an EXE which will play a 2 player game of Snake. Eventually the game will finish and it will print inside the CMD the line I decribed before – Andreasx23 Jan 11 '22 at 12:03
  • 1
    We know absolutely noting about the inner workings of your game, what method it uses to output to the console, or in fact the method it uses to report the end result. It is unlikely that the case insensitive string `the game`, which is the only common string between your two examples, _(or `win` or `draw`)_, will never be used elsewhere during the executables run time, so it is probably not a robust string to check for. Essentially we would need far more information that you have submitted, in order to consider trying to assist you with a code task you have not yet attempted to solve yourself. – Compo Jan 11 '22 at 12:19
  • @Compo I am sorry if I haven't given enough information, because I feel like I do. I been searching for a way to extract the text from the output window of a console for about two hours now but haven't found anything that is why I am asking here. Have a great day Edit: the inner working of the game has nothing to do with the thing I am looking for. It literally just prints information to the console which is the information from that exact console I want to extract – Andreasx23 Jan 11 '22 at 12:30
  • So it is the output of `battlesnake.exe` you are interested in? Well, then take a look at the thread [How to set commands output as a variable in a batch file](https://stackoverflow.com/q/6359820)… – aschipfl Jan 11 '22 at 13:05
  • My assumption is that your game is being played in a cmd.exe window, _(it would be ridiculous for it to report only its results to a cmd.exe window)_ If its results are being output via stdout, and the game is being output to stdout too, then redirecting its stdout to a file or variable in order to search for trigger strings, you will not see the game output, and your game will be unplayable. What we need to know is exactly how your game has been designed to work with cmd, i.e. how its output is sent. If you're lucky the results message is sent via stderr, and can be captured independently. – Compo Jan 11 '22 at 13:22
  • Thank you both for your comments. After @aschipfl gave me a nice link I eventually found this: https://stackoverflow.com/a/7982841/15853751 to work :) – Andreasx23 Jan 11 '22 at 13:42
  • I reverted your last edit – if you want to provide your solution, please post it as an answer instead! – aschipfl Jan 11 '22 at 13:53

2 Answers2

0

I eventually came up with this solution:

@set win=0
@set draw=0
@set lose=0
@set loopcount=10

:loop
C:\battlesnakeCLI\battlesnake.exe play -W 11 -H 11 --name me --url http://localhost/api/gamev2 --name other --url http://localhost/api/gametest 1> out.txt 2>&1 | type out.txt
@findstr /c:"me is the winner" "out.txt" >nul 2>&1
@if %errorlevel%==0 (
    @set /a win=win+1
) 
@findstr /c:"It was a draw" "out.txt" >nul 2>&1
@if %errorlevel%==0 (
    @set /a draw=draw+1
) 
@findstr /c:"other is the winner" "out.txt" >nul 2>&1
@if %errorlevel%==0 (
    @set /a lose=lose+1
)
@ECHO Wins: %win% & echo:Draws: %draw% & echo:Losses: %lose%
@del out.txt

@set /a loopcount=loopcount-1
@if %loopcount%==0 goto exitloop
@goto loop
:exitloop

@ECHO FINISHED RUNNING! Final scores: & echo:Wins: %win% & echo:Draws: %draw% & echo:Losses: %lose%
@PAUSE
Andreasx23
  • 19
  • 6
  • Does this work? It seems to me that if you redirect all output into a file, then the user would not see "the game" in the screen... – Aacini Jan 12 '22 at 15:58
  • @Aacini it does work because I don't need to see the game itself I just need to read the result when the game is over :-) – Andreasx23 Jan 12 '22 at 18:19
0

a shortened version of your code, untested:

@echo off
set win=0 & set draw=0 & set lose=0 & set loopcount=10

:loop
"C:\battlesnakeCLI\battlesnake.exe" play -W 11 -H 11 --name me --url http://localhost/api/gamev2 --name other --url http://localhost/api/gametest 1> out.txt 2>&1
findstr /c:"me is the winner" "out.txt" >nul 2>&1 && set /a win=win+1
findstr /c:"It was a draw" "out.txt" >nul 2>&1 && set /a draw=draw+1
findstr /c:"other is the winner" "out.txt" >nul 2>&1 && set /a lose=lose+1
ECHO Wins: %win% & echo:Draws: %draw% & echo:Losses: %lose%
del out.txt

set /a loopcount-=1
if %loopcount% gtr 0 goto loop
:exitloop

ECHO FINISHED RUNNING! Final scores: & echo:Wins: %win% & echo:Draws: %draw% & echo:Losses: %lose%
pause

The conditional operator && helps to not have to set parenthesized code blocks. Also getting rid of the overhead of @ everywhere and using the age old @echo off

Gerhard
  • 22,678
  • 7
  • 27
  • 43