This is revision of the article in Japanese (Othello code in CentOS).
When the code below is coped and pasted in notepad and saved as character code "UTF-8" and file extension "py", it will work in command prompt (python (file name).py).
- This is othello game. White and black stones are alternatively and automatically placed (The place will be decided ramdomly).
- If there is no place, pass is done and the turn is changed to counterpart one.
- If there is no place for both players, the game ends.
- In the code below, recursion is used.
■ Example screen (Results are shown in console)
■ Code
import random as rd
def checkRevinLine(i, j, inc, stone_pos):
revListonLine = []
if board[stone_pos[0] + i][stone_pos[1] + j] == "-":
revListonLine = []
elif board[stone_pos[0] + i][stone_pos[1] + j] == stoneA_:
revListonLine.append([stone_pos[0] + i, stone_pos[1] + j])
elif board[stone_pos[0] + i][stone_pos[1] + j] == stoneB_:
revListonLine.append([stone_pos[0] + i, stone_pos[1] + j])
Temp = checkRevinLine(i + inc[0], j + inc[1], inc, stone_pos)
if Temp == []:
revListonLine = []
else:
revListonLine.extend(Temp)
return revListonLine
def checkReversible(board, stone_pos):
listtobeChanged = []
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if board[stone_pos[0] + i][stone_pos[1] + j] == stoneB_:
listtobeChanged.extend(checkRevinLine(i, j, [i, j], stone_pos))
return listtobeChanged
def changePlayer(currentTurn):
global turn_
global stoneA_
global stoneB_
if currentTurn == "○":
turn_ = "●"
stoneA_ = "●"
stoneB_ = "○"
else:
turn_ = "○"
stoneA_ = "○"
stoneB_ = "●"
def showConsole(currentturn, position, board):
if position != [0, 0]:
print("== " + currentturn + "の番: " + str(position[0]) + "行 " + str(position[1]) + "列目 ==")
for i in range(1, 9, 1):
boardline = board[i][1:9]
print(" ".join(boardline))
print("======================")
turn = 1
changePlayer("Start")
leftBlankList = [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [2, 1], [2, 2], [2, 3], [2, 4],
[2, 5], [2, 6], [2, 7], [2, 8], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8],
[4, 1], [4, 2], [4, 3], [4, 6], [4, 7], [4, 8], [5, 1], [5, 2], [5, 3], [5, 6], [5, 7], [5, 8],
[6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [6, 8], [7, 1], [7, 2], [7, 3], [7, 4],
[7, 5], [7, 6], [7, 7], [7, 8], [8, 1], [8, 2], [8, 3], [8, 4], [8, 5], [8, 6], [8, 7], [8, 8]]
board = [['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '●', '○', '―', '―', '―', '―'],
['―', '―', '―', '―', '○', '●', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―'],
['―', '―', '―', '―', '―', '―', '―', '―', '―', '―']]
endflag_by_pass = 0
tempBlankList = leftBlankList.copy()
showConsole(turn_, [0, 0], board)
while len(leftBlankList) > 0:
stone_pos = rd.choice(tempBlankList)
tempBlankList.remove(stone_pos)
stoneList_tobeChanged = checkReversible(board, stone_pos)
if len(stoneList_tobeChanged) > 0:
endflag_by_pass = 0
for pos in stoneList_tobeChanged:
if turn_ == "○":
board[stone_pos[0]][stone_pos[1]] = "○"
board[pos[0]][pos[1]] = "○"
else:
board[stone_pos[0]][stone_pos[1]] = "●"
board[pos[0]][pos[1]] = "●"
showConsole(turn_, stone_pos, board)
leftBlankList.remove(stone_pos)
tempBlankList = leftBlankList.copy()
changePlayer(turn_)
turn = turn + 1
if len(tempBlankList) == 0:
if endflag_by_pass == 1:
break
tempBlankList = leftBlankList.copy()
changePlayer(turn_)
stoneList_tobeChanged = []
endflag_by_pass = 1
countW = 0
countB = 0
for i in range(1, 9, 1):
countW = countW + board[i].count("○")
countB = countB + board[i].count("●")
print('●' + str(countB) + 'vs〇' + str(countW) + ' | End Othello')