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 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()