バッチファイル(オセロ002~010)から少し修正したものです。
下のコードをメモ帳にコピー後、文字コードANSI、拡張子batで保存、実行で動くと思います。
・オセロのゲームで、白と黒で交互に自動的に石を置く。
置く場所は、始点をランダムに決めたあと置けない場合は置ける場所を下(または右)に向けて探す。
・置くことができない場合、passして相手側の手順になる。
・白と黒どちらも置けない場合は終了。
■ 実行例
■ サンプルコード
@echo off
rem ==============================Start Setting==============================
echo Start Othello
echo If there is no place for stone, input "pass"
set position=00
set /a line=0
set /a row=0
set turnchangeflag=0
set trialnum=1
set player=A
set playerBtrial=0
set playerAtrial=0
set index=-1
set endindex=-1
rem %1 is command line parameter. If something is set, file is created and data is saved.
rem ------------------------------
if "%1"=="" (
rem No save
) else (
echo Filename:"%1.txt"
)
rem ------------------------------
rem /l:optional symbol, %%n:variable within for code、(1,1,8):(start number, increase, end number)
rem initial setting on field, stone (white:〇, black:●), :space:_
rem ------------------------------
for /l %%n in (0,1,9) do (
for /l %%m in (0,1,9) do (
set array[%%n][%%m]=_
)
)
set array[4][4]=〇
set array[4][5]=●
set array[5][4]=●
set array[5][5]=〇
rem ------------------------------
rem initial display for field
rem ------------------------------
setlocal enabledelayedexpansion
for /l %%n in (1,1,8) do (
echo !array[%%n][1]!!array[%%n][2]!!array[%%n][3]!!array[%%n][4]!!array[%%n][5]!!array[%%n][6]!!array[%%n][7]!!array[%%n][8]!
)
rem ------------------------------
rem ==============================Start Setting==============================
rem ==============================Playing Othello==============================
:trial
set checkcnt=1
rem If field is all filled, go to "result".
if %trialnum% gtr 60 (
goto :result
)
rem ------------------------------
if %player%==A (
if %playerAtrial%==0 (
set /a index=%random%*63/32767+1
)
if %playerAtrial%==0 (
set /a endindex=index
set /a playerAtrial=playerAtrial+1
set /a line=%index%/8+1
set /a row=%index%%%8+1
set /a index=index+1
) else (
if %index%==64 (
set /a index=index-64
set /a playerAtrial=playerAtrial+1
set /a line=0
set /a row=0
) else (
set /a playerAtrial=playerAtrial+1
set /a line=%index%/8+1
set /a row=%index%%%8+1
set /a index=index+1
)
)
) else if %player%==B (
if %playerBtrial%==0 (
set /a index=%random%*63/32767+1
)
if %playerBtrial%==0 (
set /a endindex=index
set /a playerBtrial=playerBtrial+1
set /a line=%index%/8+1
set /a row=%index%%%8+1
set /a index=index+1
) else (
if %index%==64 (
set /a index=index-64
set /a playerBtrial=playerBtrial+1
set /a line=0
set /a row=0
) else (
set /a playerBtrial=playerBtrial+1
set /a line=%index%/8+1
set /a row=%index%%%8+1
set /a index=index+1
)
)
)
set position=%line%,%row%
if %index%==%endindex% (
if %playerBtrial% gtr 1 (
set position=pass
) else if %playerAtrial% gtr 1 (
set position=pass
)
)
rem echo %playerAtrial%,%playerBtrial%,%index%,%line%,%row%,%position%,%turnchangeflag%
rem ------------------------------
rem Trim space (half-width, full-width) before or after characters. If space only, go to trial.
rem ------------------------------
:TRIM
if "%position:~0,1%"==" " (
SET position=%position:~1%
GOTO TRIM
)
if "%position:~-1%"==" " (
SET position=%position:~0,-1%
GOTO TRIM
)
if "%position:~0,1%"==" " (
SET position=%position:~1%
GOTO TRIM
)
if "%position:~-1%"==" " (
SET position=%position:~0,-1%
GOTO TRIM
)
if "%position%"=="" (
goto :trial
)
rem ------------------------------
rem If pass is inputted, player turn is changed and flag (turnchangeflag) is decreased, and if it exceeds -1, go to "result".
rem ------------------------------
if "%position%"=="pass" (
set /a turnchangeflag=turnchangeflag-1
if %turnchangeflag% lss -1 (
goto :result
) else if %player%==A (
set player=B
set playerAtrial=0
goto :trial
) else if %player%==B (
set player=A
set playerBtrial=0
goto :trial
)
) else (
set line=%position:~0,1%
set row=%position:~-1%
)
rem ------------------------------
rem Check if stone is placed within the field or if stone is already placed on the position.
rem ------------------------------
if %line% gtr 8 (
GOTO :trial
)
if %row% gtr 8 (
GOTO :trial
)
if %line% lss 1 (
GOTO :trial
)
if %row% lss 1 (
GOTO :trial
)
if not !array[%line%][%row%]!==_ (
GOTO :trial
)
rem ------------------------------
rem ==============================Reverse Logic==============================
:check
set startline=%line%
set startrow=%row%
set opponentstonenum=0
:checkline
rem Check direction depends on checkcnt number (i.e. 1 means up, 2 means upper right ...)
rem ------------------------------
if %checkcnt%==1 (
set /a startline=startline-1
) else if %checkcnt%==2 (
set /a startline=startline-1
set /a startrow=startrow+1
) else if %checkcnt%==3 (
set /a startrow=startrow+1
) else if %checkcnt%==4 (
set /a startline=startline+1
set /a startrow=startrow+1
) else if %checkcnt%==5 (
set /a startline=startline+1
) else if %checkcnt%==6 (
set /a startline=startline+1
set /a startrow=startrow-1
) else if %checkcnt%==7 (
set /a startrow=startrow-1
) else if %checkcnt%==8 (
set /a startline=startline-1
set /a startrow=startrow-1
) else (
rem ------------------------------
rem If there is reversible stone, flag (turnchangeflag) will be 1 and the following code is executed.
rem ------------------------------
if %turnchangeflag%==1 (
set /a trialnum=trialnum+1
echo Trialnum:%trialnum%, Player:%player%, %position%
for /l %%n in (1,1,8) do (
echo !array[%%n][1]!!array[%%n][2]!!array[%%n][3]!!array[%%n][4]!!array[%%n][5]!!array[%%n][6]!!array[%%n][7]!!array[%%n][8]!
)
set /a turnchangeflag=0
if "%1"=="" (
rem No save
) else (
echo Trialnum:%trialnum%, Player:%player%, %position% >> "%1.txt"
for /l %%n in (1,1,8) do (
echo !array[%%n][1]!!array[%%n][2]!!array[%%n][3]!!array[%%n][4]!!array[%%n][5]!!array[%%n][6]!!array[%%n][7]!!array[%%n][8]! >> "%1.txt"
)
)
if %player%==A (
set player=B
) else if %player%==B (
set player=A
)
)
goto :trial
)
rem If position to be checked is space (_), go to next direction.
rem If counterpart stone, check one further position with flag(opponentstonenum) up.
rem If own stone with flag(opponentstonenum) of 1 or more, reverse stones.
rem If there is one reversible stone, change player (increase flag(turnchangeflag))
rem ------------------------------
if %player%==A (
if !array[%startline%][%startrow%]!==_ (
set /a checkcnt=checkcnt+1
goto :check
) else if !array[%startline%][%startrow%]!==● (
set /a opponentstonenum=opponentstonenum+1
goto :checkline
) else if !array[%startline%][%startrow%]!==〇 (
if %opponentstonenum% geq 1 (
if %checkcnt%==1 (
for /l %%n in (%startline%,1,%line%) do (
set array[%%n][%startrow%]=〇
)
) else if %checkcnt%==2 (
for /l %%n in (%startline%,1,%line%) do (
set array[%%n][!startrow!]=〇
set /a startrow=startrow-1
)
) else if %checkcnt%==3 (
for /l %%n in (%row%,1,%startrow%) do (
set array[%startline%][%%n]=〇
)
) else if %checkcnt%==4 (
for /l %%n in (%startline%,-1,%line%) do (
set array[%%n][!startrow!]=〇
set /a startrow=startrow-1
)
) else if %checkcnt%==5 (
for /l %%n in (%line%,1,%startline%) do (
set array[%%n][%startrow%]=〇
)
) else if %checkcnt%==6 (
for /l %%n in (%startline%,-1,%line%) do (
set array[%%n][!startrow!]=〇
set /a startrow=startrow+1
)
) else if %checkcnt%==7 (
for /l %%n in (%startrow%,1,%row%) do (
set array[%startline%][%%n]=〇
)
) else if %checkcnt%==8 (
for /l %%n in (%startline%,1,%line%) do (
set array[%%n][!startrow!]=〇
set /a startrow=startrow+1
)
)
set turnchangeflag=1
set playerAtrial=0
)
set /a checkcnt=checkcnt+1
goto :check
)
)
rem Basically same as Player A
rem Flag(playerBtrial) to check a position in which stone can be placed is reset.
rem ------------------------------
if %player%==B (
if !array[%startline%][%startrow%]!==_ (
set /a checkcnt=checkcnt+1
goto :check
) else if !array[%startline%][%startrow%]!==〇 (
set /a opponentstonenum=opponentstonenum+1
goto :checkline
) else if !array[%startline%][%startrow%]!==● (
if %opponentstonenum% geq 1 (
if %checkcnt%==1 (
for /l %%n in (%startline%,1,%line%) do (
set array[%%n][%startrow%]=●
)
) else if %checkcnt%==2 (
for /l %%n in (%startline%,1,%line%) do (
set array[%%n][!startrow!]=●
set /a startrow=startrow-1
)
) else if %checkcnt%==3 (
for /l %%n in (%row%,1,%startrow%) do (
set array[%startline%][%%n]=●
)
) else if %checkcnt%==4 (
for /l %%n in (%startline%,-1,%line%) do (
set array[%%n][!startrow!]=●
set /a startrow=startrow-1
)
) else if %checkcnt%==5 (
for /l %%n in (%line%,1,%startline%) do (
set array[%%n][%startrow%]=●
)
) else if %checkcnt%==6 (
for /l %%n in (%startline%,-1,%line%) do (
set array[%%n][!startrow!]=●
set /a startrow=startrow+1
)
) else if %checkcnt%==7 (
for /l %%n in (%startrow%,1,%row%) do (
set array[%startline%][%%n]=●
)
) else if %checkcnt%==8 (
for /l %%n in (%startline%,1,%line%) do (
set array[%%n][!startrow!]=●
set /a startrow=startrow+1
)
)
set turnchangeflag=1
set playerBtrial=0
)
set /a checkcnt=checkcnt+1
goto :check
)
)
rem ------------------------------
rem ==============================Reverse Logic==============================
rem ==============================Playing Othello==============================
rem ==============================Display Result==============================
:result
rem Calcuate numbers of white and black stones.
rem ------------------------------
set whitecnt=0
set blackcnt=0
for /l %%n in (1,1,8) do (
for /l %%m in (1,1,8) do (
if !array[%%n][%%m]!==〇 (
set /a whitecnt=whitecnt+1
) else if !array[%%n][%%m]!==● (
set /a blackcnt=blackcnt+1
)
)
)
rem ------------------------------
rem Display in command line and save them if necessary.
rem ------------------------------
if %whitecnt% gtr %blackcnt% (
echo Player A win (A:%whitecnt%, B:%blackcnt%^)
) else if %whitecnt% equ %blackcnt% (
echo Draw (A:%whitecnt%, B:%blackcnt%^)
) else if %whitecnt% lss %blackcnt% (
echo Player B win (A:%whitecnt%, B:%blackcnt%^)
)
if "%1"=="" (
rem No save
) else (
if %whitecnt% gtr %blackcnt% (
echo Player A win (A:%whitecnt%, B:%blackcnt%^) >> "%1.txt"
) else if %whitecnt% equ %blackcnt% (
echo Draw (A:%whitecnt%, B:%blackcnt%^) >> "%1.txt"
) else if %whitecnt% lss %blackcnt% (
echo Player B win (A:%whitecnt%, B:%blackcnt%^) >> "%1.txt"
)
)
rem ------------------------------
rem ==============================Display Result==============================