import requests url = "https://www.othello.org/egev/" response = requests.get(url) html_content = response.text lines = html_content.splitlines() boardRaw = [] whoseMove_W_or_B = "?" for line in lines: if "gTurn" in line: for i in range(len(line)): if line[i] == "1": whoseMove_W_or_B = "B" if line[i] == "2": whoseMove_W_or_B = "W" if "gBoard" in line: for i in range(len(line)): if line[i] == "1": boardRaw += [1] if line[i] == "2": boardRaw += [2] if line[i] == "0": boardRaw += [0] board = [0, 0, 0, 0, 0, 0, 0, 0, 0] for i in range(64): if i % 8 == 0: board += [0, 0] board += [boardRaw[i]] board += [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] i = 11 available = [] while i < 19: x = i while x < 90: if board[99 - x] == 0: available += [99 - x] x += 10 i += 1 alph = ["a", "b", "c", "d", "e", "f", "g", "h" ] if whoseMove_W_or_B == "W": colorToWin = 2 moveColor = 2 nonMoveColor = 1 colorToLose = 1 else: colorToWin = 1 moveColor = 1 nonMoveColor = 2 colorToLose = 2 directions = [1,-1,10,-10,11,-11,9,-9] flipped = [] gameOver = False howManyFlipped = [] keeper = 99 linesOfPlay = [] listOfPointers = [] loser = 1 moves = [] numMoves = 0 numStillEmpty = len(available) passes = False stop = False totalFlips = 0 while stop == False: while (not gameOver) and numStillEmpty > 0 and stop == False: flips = 0 testSquarePointer = 0 while flips == 0 and testSquarePointer < numStillEmpty: testSquare = available[testSquarePointer] for i in range(8): whichDirection = directions[i] testSquareAdder = whichDirection while board[testSquare + testSquareAdder] == nonMoveColor: testSquareAdder += whichDirection if board[testSquare + testSquareAdder] == moveColor: testSquareAdder -= whichDirection while board[testSquare + testSquareAdder] == nonMoveColor: board[testSquare + testSquareAdder] = moveColor flips += 1 flipped += [testSquare + testSquareAdder] testSquareAdder -= whichDirection testSquarePointer += 1 if flips > 0: passes = False board[testSquare] = moveColor numStillEmpty -= 1 available.pop(testSquarePointer - 1) listOfPointers += [testSquarePointer - 1] howManyFlipped += [flips] totalFlips += flips moves += [(testSquare, moveColor)] numMoves += 1 else : if passes == True: gameOver = True else : passes = True tempHolder = moveColor moveColor = nonMoveColor nonMoveColor = tempHolder finalMoversCount = sum(1 for i in board if i == colorToWin) if finalMoversCount > (64 - finalMoversCount + numStillEmpty): loser = colorToLose else: loser = colorToWin if colorToWin != loser: keeper = moves[0][0] workAround = [] workAround = [t for t in moves] linesOfPlay.append(workAround) else: keeper = 99 tryToRevise = False while tryToRevise == False and numMoves > 0 and stop == False: moveColor = moves[numMoves - 1][1] tryToRevise = True if moveColor == loser else False nonMoveColor = 2 if moveColor == 1 else 1 board[moves[numMoves - 1][0]] = 0 tempHolder = howManyFlipped[numMoves - 1] + 1 for i in range(1, tempHolder): board[(flipped[totalFlips - i])] = nonMoveColor del flipped[-(howManyFlipped[numMoves - 1]):] totalFlips -= howManyFlipped[numMoves - 1] available.insert(listOfPointers[(numMoves - 1)], moves[numMoves - 1][0]) if tryToRevise == True: testSquarePointer = listOfPointers[numMoves - 1] + 1 del listOfPointers[-1] numMoves -= 1 if numMoves == 0 and moveColor != loser: stop = True del moves[-1] del howManyFlipped[-1] numStillEmpty += 1 if tryToRevise == True: if testSquarePointer >= numStillEmpty: tryToRevise = False if numMoves == 0: stop = True if tryToRevise == True: flips = 0 passes = False gameOver = False while flips == 0 and testSquarePointer < numStillEmpty: testSquare = available[testSquarePointer] for i in range(8): whichDirection = directions[i] testSquareAdder = whichDirection while board[testSquare + testSquareAdder] == nonMoveColor: testSquareAdder += whichDirection if board[testSquare + testSquareAdder] == moveColor: testSquareAdder -= whichDirection while board[testSquare + testSquareAdder] == nonMoveColor: board[testSquare + testSquareAdder] = moveColor flips += 1 flipped.append(testSquare + testSquareAdder) testSquareAdder -= whichDirection testSquarePointer += 1 if flips > 0: board[testSquare] = moveColor numStillEmpty -= 1 available.pop(testSquarePointer - 1) listOfPointers += [testSquarePointer - 1] howManyFlipped += [flips] totalFlips += flips moves += [(testSquare, moveColor)] numMoves += 1 tempHolder = moveColor moveColor = nonMoveColor nonMoveColor = tempHolder testSquarePointer = 0 else: tryToRevise = False if numMoves == 0: stop = True if keeper == 99: print("No winning move found.") else: raw_list = [x for x in linesOfPlay if x[0][0] == keeper] pruned_list = [] pruned_list += [raw_list[-1]] tempHolder = 0 for i in range(len(raw_list) - 2, -1, -1): numMoves = 0 while numMoves < 100: if raw_list[i][numMoves][0] == pruned_list[tempHolder][numMoves][0]: numMoves += 1 else: if pruned_list[tempHolder][numMoves][1] == colorToLose: tempHolder += 1 pruned_list += [raw_list[i]] numMoves = 200 endOfList = len(pruned_list) print() print(" SOLUTION TO TODAY'S PUZZLE:") print() print(" The various lines of text below represent winning lines of") print(" play against every possible defense by the opponent. Within") print(" each line of text, the opponent's moves are shown in parentheses.") print() for i in range(endOfList): outputLine = " " for x in range(len(pruned_list[i])): sqLet = pruned_list[i][x][0] if pruned_list[i][x][1] == colorToLose: outputLine += "(" + (alph[(sqLet % 10) - 1] + str(sqLet // 10)) + ") " else: outputLine += (alph[(sqLet % 10) - 1] + str(sqLet // 10)) + " " print(outputLine) print() print(" Winning first move: " + alph[(keeper % 10) - 1] + str(keeper // 10)) print()