# ==============================Start Setting============================== # $Othello: 10×10マス (黒("●")、白("〇")、空欄("_")のどれか。ただし、8×8部分のみを表示し、1、10の行、列は常に空欄) # $position: 石を置く場所(35と入力する場合、3行目、5列目) # $endflag: ゲーム終了を判断するフラグ(passが両プレイヤーから連続した場合に終了) # $turnchangeflag: 手番交代を判断するフラグ(交代時にオセロの8×8部分を表示) # $trialnum: 現在、何手目かのカウント(無効となる場所に石を置いたときはカウントされない) # $player: プレイヤー(AかB) # ========================================================================= # ========================================================================= # 結果表示用 $blackwin、$whitewin、$even # 手順の記録の保存 text.txt # ========================================================================= $blackwin=0 $whitewin=0 $even=0 $textfile="text.txt" # 20回のゲーム試行 for ($i = 1; $i -le 20; $i++) { $Othello=@( @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_'), @('_','_','_','_','_','_','_','_','_','_') ) $Othello[4][4]='〇' $Othello[4][5]='●' $Othello[5][4]='●' $Othello[5][5]='〇' $position="00" $endflag=0 $turnchangeflag=0 $trialnum=0 $player="A" $inputArrayNum = 0 #echo ("Game:"+$i+"| Start Othello") echo ("Game:"+$i+"| Start Othello") | Out-file $textfile -Append # Othello field display for ($n=1; $n -lt 9; $n++) { # $Othello[$n][1]+$Othello[$n][2]+$Othello[$n][3]+$Othello[$n][4]+$Othello[$n][5]+$Othello[$n][6]+$Othello[$n][7]+$Othello[$n][8] $Othello[$n][1]+$Othello[$n][2]+$Othello[$n][3]+$Othello[$n][4]+$Othello[$n][5]+$Othello[$n][6]+$Othello[$n][7]+$Othello[$n][8] | Out-file $textfile -Append } # =========================================================================== # ==============================Playing Othello============================== # =========================================================================== # 一連のゲームの中で石を交互に置くループ。終了フラグの判定によりループを抜ける。 while ($true) { # ==============================Auto placing============================== # 自動で石を置く。 # 空欄("_")となっているところを配列にして、ランダムに置き換えたのち、要素0から順に入れていく。 # すべての場所で石を置くことができない場合、passにする。 # ========================================================================= if ($turnchangeflag -ge 1) { $inputArrayNum = 0 } $turnchangeflag=0 #echo ("Input position (like 35 (line 3, row 5)) or pass") #空欄("_")となっている行と列を1次配列として取得 if ($inputArrayNum -eq 0) { $blankArray=@() $inputArray=@() for ($n=1; $n -le 8; $n++) { for ($m=1; $m -le 8; $m++) { if ($Othello[$n][$m] -eq '_') { $blankArray+= [string]$n + [string]$m }else{ } } } #置く場所がない場合はpass, 置ける場所がある場合は、ランダムなリスト($inputArray)に入れ替える。 if ($blankArray.count -eq 0) { $position = "pass" $inputArrayNum = 0 }else{ $inputArray = get-random -input $blankArray -count ([int]::MaxValue) } } #置ける場所を試した後はpass, 要素0から順に置ける場所に石を置く($positionに代入)。 if ($inputArrayNum -ge $inputArray.count) { $position = "pass" $inputArrayNum = 0 } elseif ($inputArray.count -eq 1) { $position = $blankArray[0] $inputArrayNum ++ } else { $position = $inputArray[$inputArrayNum] $inputArrayNum ++ } # ==============================Auto placing============================== # ==============================Stone placing============================== # 手動で石を置く。 # 上の自動で石を置く場合、Read-Hostをコメントアウト。 # ========================================================================= # 石を置くとき、無効な場所や入力値の場合のループ。正しい入力値かpassでループを抜ける。 while ($true) { if ($player -eq "A") { #$position = Read-Host "Player A (〇)" #[string]$position = get-random -minimum 11 -maximum 88 #echo ("Player A (〇):"+$position) if($position -eq "pass") { $player="B" $endflag=$endflag+1 break } $line=$position.substring(0,1) $row=$position.substring(1,1) if (($line -ge 1) -And ($line -le 8) -And ($row -ge 1) -And ($row -le 8)) { if ($Othello[$line][$row] -eq "_") { break } } } elseif ($player -eq "B") { #$position = Read-Host "Player B (●)" #[string]$position = get-random -minimum 11 -maximum 88 #echo ("Player B (●):"+$position) if($position -eq "pass") { $player="A" $endflag=$endflag+1 break } $line=$position.substring(0,1) $row=$position.substring(1,1) if (($line -ge 1) -And ($line -le 8) -And ($row -ge 1) -And ($row -le 8)) { if ($Othello[$line][$row] -eq "_") { break } } } } # ==============================Stone placing============================== # ==============================Reverse Logic============================== # 石を裏返す処理。checkcntで置いた場所の周りの8方向をチェック。extensionで1方向の延長線上をチェック(8×8マスとして最大8)。 # AとBで処理の内容は同じ(石の色やプレイヤー名が違うのみ)。 if ($position -eq "pass") { } else { if ($player -eq "A") { for ($checkcnt=0; $checkcnt -lt 8; $checkcnt++) { [Int]$startline=$line [Int]$startrow=$row [Int]$opponentstonenum=0 for ($extension=0; $extension -lt 8; $extension++) { if ($checkcnt -eq 0) { $startline=$startline-1 } elseif ($checkcnt -eq 1) { $startline=$startline-1 $startrow=$startrow+1 } elseif ($checkcnt -eq 2) { $startrow=$startrow+1 } elseif ($checkcnt -eq 3) { $startline=$startline+1 $startrow=$startrow+1 } elseif ($checkcnt -eq 4) { $startline=$startline+1 } elseif ($checkcnt -eq 5) { $startline=$startline+1 $startrow=$startrow-1 } elseif ($checkcnt -eq 6) { $startrow=$startrow-1 } elseif ($checkcnt -eq 7) { $startline=$startline-1 $startrow=$startrow-1 } else { } # 周り状況をチェック。空欄、自分の石、相手の石に応じて処理を変える。 # 相手の石が1つ以上あり($opponentstonenumが1以上)、自分の石がある場合に実際に裏返す。 if ($Othello[$startline][$startrow] -eq "_") { break } elseif ($Othello[$startline][$startrow] -eq "●") { $opponentstonenum=$opponentstonenum+1 } elseif ($Othello[$startline][$startrow] -eq "〇") { if ($opponentstonenum -ge 1) { $player="B" $turnchangeflag=$turnchangeflag+1 $endflag=0 if ($checkcnt -eq 0) { #for (%startline%,1,%line%) do ( for ($startline;$startline -le $line;$startline++) { $Othello[$startline][$startrow]='〇' } break } elseif ($checkcnt -eq 1) { for ($startline;$startline -le $line;$startline++) { $Othello[$startline][$startrow]='〇' $startrow=$startrow-1 } break } elseif ($checkcnt -eq 2) { for ($startrow;$startrow -ge $row;$startrow--) { $Othello[$startline][$startrow]='〇' } break } elseif ($checkcnt -eq 3) { for ($startrow;$startrow -ge $row;$startrow--) { $Othello[$startline][$startrow]='〇' $startline=$startline-1 } break } elseif ($checkcnt -eq 4) { for ($startline;$startline -ge $line;$startline--) { $Othello[$startline][$startrow]='〇' } break } elseif ($checkcnt -eq 5) { for ($startline;$startline -ge $line;$startline--) { $Othello[$startline][$startrow]='〇' $startrow=$startrow+1 } break } elseif ($checkcnt -eq 6) { for ($startrow;$startrow -le $row;$startrow++) { $Othello[$startline][$startrow]='〇' } break } elseif ($checkcnt -eq 7) { for ($startrow;$startrow -le $row;$startrow++) { $Othello[$startline][$startrow]='〇' $startline=$startline+1 } break } } else { break } } } } } elseif ($player -eq "B") { for ($checkcnt=0; $checkcnt -lt 8; $checkcnt++) { [Int]$startline=$line [Int]$startrow=$row [Int]$opponentstonenum=0 for ($extension=0; $extension -lt 8; $extension++) { if ($checkcnt -eq 0) { $startline=$startline-1 } elseif ($checkcnt -eq 1) { $startline=$startline-1 $startrow=$startrow+1 } elseif ($checkcnt -eq 2) { $startrow=$startrow+1 } elseif ($checkcnt -eq 3) { $startline=$startline+1 $startrow=$startrow+1 } elseif ($checkcnt -eq 4) { $startline=$startline+1 } elseif ($checkcnt -eq 5) { $startline=$startline+1 $startrow=$startrow-1 } elseif ($checkcnt -eq 6) { $startrow=$startrow-1 } elseif ($checkcnt -eq 7) { $startline=$startline-1 $startrow=$startrow-1 } else { } if ($Othello[$startline][$startrow] -eq "_") { break } elseif ($Othello[$startline][$startrow] -eq "〇") { $opponentstonenum=$opponentstonenum+1 } elseif ($Othello[$startline][$startrow] -eq "●") { if ($opponentstonenum -ge 1) { $player="A" $turnchangeflag=$turnchangeflag+1 $endflag=0 if ($checkcnt -eq 0) { for ($startline;$startline -le $line;$startline++) { $Othello[$startline][$startrow]='●' } break } elseif ($checkcnt -eq 1) { for ($startline;$startline -le $line;$startline++) { $Othello[$startline][$startrow]='●' $startrow=$startrow-1 } break } elseif ($checkcnt -eq 2) { for ($startrow;$startrow -ge $row;$startrow--) { $Othello[$startline][$startrow]='●' } break } elseif ($checkcnt -eq 3) { for ($startrow;$startrow -ge $row;$startrow--) { $Othello[$startline][$startrow]='●' $startline=$startline-1 } break } elseif ($checkcnt -eq 4) { for ($startline;$startline -ge $line;$startline--) { $Othello[$startline][$startrow]='●' } break } elseif ($checkcnt -eq 5) { for ($startline;$startline -ge $line;$startline--) { $Othello[$startline][$startrow]='●' $startrow=$startrow+1 } break } elseif ($checkcnt -eq 6) { for ($startrow;$startrow -le $row;$startrow++) { $Othello[$startline][$startrow]='●' } break } elseif ($checkcnt -eq 7) { for ($startrow;$startrow -le $row;$startrow++) { $Othello[$startline][$startrow]='●' $startline=$startline+1 } break } } else { break } } } } } } # ==============================Reverse Logic============================== # ==============================Othello field display============================== # 手番が変わるときのみ表示 if ($turnchangeflag -ge 1) { $trialnum=$trialnum+1 #"=====Trial:"+$trialnum+"=====" "=====Trial:"+$trialnum+"=====" | Out-file $textfile -Append for ($n=1; $n -lt 9; $n++) { #$Othello[$n][1]+$Othello[$n][2]+$Othello[$n][3]+$Othello[$n][4]+$Othello[$n][5]+$Othello[$n][6]+$Othello[$n][7]+$Othello[$n][8] $Othello[$n][1]+$Othello[$n][2]+$Othello[$n][3]+$Othello[$n][4]+$Othello[$n][5]+$Othello[$n][6]+$Othello[$n][7]+$Othello[$n][8] | Out-file $textfile -Append } } # ==============================Othello field display============================== # ゲーム終了の判定 if ($trialnum -ge 60) { break } elseif ($endflag -ge 2) { break } } # =========================================================================== # ==============================Playing Othello============================== # =========================================================================== # ==============================Result============================== # ○、●の数をカウント # 勝った数、負けた数、引き分けをカウント # ================================================================== $whitecnt = 0 $blackcnt = 0 for ($n=1; $n -le 8; $n++) { for ($m=1; $m -le 8; $m++) { if ($Othello[$n][$m] -eq "●") { $blackcnt++ } elseif ($Othello[$n][$m] -eq "〇") { $whitecnt++ } } } echo ("●"+$blackcnt+"vs○"+$whitecnt+"Game:"+$i+"| End Othello") echo ("●"+$blackcnt+"vs○"+$whitecnt+"Game:"+$i+"| End Othello") | Out-file $textfile -Append if ($blackcnt -gt $whitecnt) { $blackwin++ } elseif ($blackcnt -lt $whitecnt) { $whitewin++ } elseif ($blackcnt -eq $whitecnt) { $even++ } } echo ("●"+$blackwin+"vs○"+$whitewin+"vsE"+$even)