我正在嘗試從命令提示符讀取最后一行,因為我正在運行另一個程式,它將玩游戲,如果我是贏家、輸家或我們打平,最后會回顯。我有這個示例代碼:
編輯:“------”部分只是我想要實作的一個例子。基本上我想讀一句“玩家:我贏了比賽!”。或“比賽是平局!” 所以這條線試圖表明我想通過這條線掃描什么,并檢查它是否包含勝利,否則就是失敗
@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
uj5u.com熱心網友回復:
我最終想出了這個解決方案:
@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
uj5u.com熱心網友回復:
您的代碼的縮短版本,未經測驗:
@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
條件運算子&&有助于不必設定帶括號的代碼塊。也擺脫了@無處不在的開銷并使用了舊的@echo off
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/412086.html
標籤:
