努力したWiki

推敲の足りないメモ書き多数

ユーザ用ツール

サイト用ツール


documents:proglang:python:py-001:py-001-020

第3章の前に 不具合報告

AIも見逃すバグがあったんですわ。「AIさんもアレだねぇ」ってなってました。以下のコードは不具合修正版です。

sample102.py
import random
from enum import IntEnum
 
class Cells(IntEnum):
    BLANK      = 0
    BLACK_CHIP = 1
    WHITE_CHIP = 2
    WALL       = 9
 
board = [
    [9,9,9,9,9,9,9,9,9,9],
    [9,0,0,0,0,0,0,0,0,9],
    [9,0,0,0,0,0,0,0,0,9],
    [9,0,0,0,0,0,0,0,0,9],
    [9,0,0,0,1,2,0,0,0,9],
    [9,0,0,0,2,1,0,0,0,9],
    [9,0,0,0,0,0,0,0,0,9],
    [9,0,0,0,0,0,0,0,0,9],
    [9,0,0,0,0,0,0,0,0,9],
    [9,9,9,9,9,9,9,9,9,9],
]
 
searchVectors = [
    [ 0, -1],  ## upper
    [ 1, -1],  ## upper right
    [ 1,  0],  ## right
    [ 1,  1],  ## lower right
    [ 0,  1],  ## lower
    [-1,  1],  ## lower left
    [-1,  0],  ## left
    [-1, -1]   ## upper left
]
 
 
def SearchAndReverse(mycolor, posX, posY, revers=False) :
    counter = 0
 
    if  board[posX][posY] != Cells.BLANK :
        return counter
 
    for sv in searchVectors:
        locatorX = posX + sv[0]
        locatorY = posY + sv[1]
        opponent = Cells.WHITE_CHIP if mycolor == Cells.BLACK_CHIP else Cells.BLACK_CHIP
 
        ## If the opponent's cells continue
        localCounter = 0
        while board[locatorX][locatorY] == opponent :
            locatorX += sv[0]
            locatorY += sv[1]
            localCounter += 1
 
        ## sandwiched by pieces
        if (board[locatorX][locatorY] == mycolor) and (localCounter != 0) :
            locatorX -= sv[0]
            locatorY -= sv[1]
 
            ## Reverse over your opponent's pieces
            counter += localCounter
            while (locatorX != posX) or (locatorY != posY) :
                if revers :
                    board[locatorX][locatorY] = mycolor
                locatorX -= sv[0]
                locatorY -= sv[1]
 
    if (counter != 0) and revers :
        board[posX][posY] = mycolor
 
    return counter
 
def CorrectValidPosition(mycolor) :
    poslist=[]
 
    for y in range(1, 8):
        for x in range(1, 8):
            count = SearchAndReverse(mycolor, x, y)
            if count != 0 :
                poslist.append([x, y, count])
    return poslist
 
 
def DisplayBoard():
    for y in range(0, 10):
        for x in range(0, 10):
            if board[x][y] == Cells.BLANK:
                print("・", end="")
 
            elif board[x][y] == Cells.BLACK_CHIP:
                print("●", end="")
 
            elif board[x][y] == Cells.WHITE_CHIP:
                print("○", end="")
 
            elif board[x][y] == Cells.WALL:
                print("■", end="")
        print()
 
 
## demo
 
DisplayBoard()
 
for i in range(1,3):
    print(f"BLACK: search valid position")
    pos = random.choice( CorrectValidPosition(Cells.BLACK_CHIP) )
    print(f"BLACK: put {pos}")
    SearchAndReverse(Cells.BLACK_CHIP, pos[0], pos[1], revers=True)
 
    DisplayBoard()
 
    print(f"WHITE: search valid position")
    pos = random.choice( CorrectValidPosition(Cells.WHITE_CHIP) )
    print(f"WHITE: put {pos}")
    SearchAndReverse(Cells.WHITE_CHIP, pos[0], pos[1], revers=True)
 
    DisplayBoard()

何が問題なのでしょうか。 前回のコードとの差分はこちら。

$ diff -c sample101.py sample102.py
*** sample101.py        Tue May 19 01:34:04 2026
--- sample102.py        Tue May 19 03:08:47 2026
***************
*** 34,39 ****
--- 34,43 ----
 
  def SearchAndReverse(mycolor, posX, posY, revers=False) :
      counter = 0
+
+     if  board[posX][posY] != Cells.BLANK :
+         return counter
+
      for sv in searchVectors:
          locatorX = posX + sv[0]
          locatorY = posY + sv[1]
$

そう、駒を置こうとした場所が空きマスでない場合の判定が抜けてたんだね。だから前回の実行結果に不具合の様子が残ってる。

documents/proglang/python/py-001/py-001-020.txt · 最終更新: by k896951

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki