"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
#!/usr/bin/python3
'''
Othello is a 2-player strategy game revolving around capturing as many pieces as possible
at the conclusion of the game. Each player chooses either black or white as their color.
To capture a piece, black (white) must flip the opponent's rival color piece to turn it White
(Black). To flip a piece, the player must place his piece around the opponent's piece
so it is in the middle of their own pieces. For example, if the board shows "BW" then black
can play a piece next to W so it becomes "BWB" and so the W is flipped to become "BBB".
For more information on game rules, please refer to http://www.ultraboardgames.com/othello/game-rules.php
'''
import random
import sys
import copy
from time import sleep
'''
Board layout is 8x8 for efficient search....
10x10, border value = 3, empty = 0, 1 = black, 2 = white
As only linear arrays are needed it will be mapped to a 100 element data array.
'''
FREE=0
WHITE=1
BLACK=2
BORDER=3
nextpos = [-10, -9 , 1, 11, 10, 9, -1, -11] # next board pos in linear... clockwise, first = up... = -10,
board = []
for i in range(100):
board.append(0)
def clear_board(board):
for col in range(10):
for row in range(10):
if row==0 or row==9 or col==0 or col == 9:
board[row*10+col] = BORDER
else:
board[row*10+col] = FREE;
def set_board(board,row,col,val):
board[10*row+col] = val
def flip_board(board,row,col):
board[10*row+col] = 3 - board[10*row+col]
def setup_board(board):
clear_board(board)
set_board(board,4,4,WHITE)
set_board(board,4,5,WHITE)
set_board(board,5,4,BLACK)
set_board(board,5,5,BLACK)
mark = ['.','W','B','#']
colmark=[' ','a','b','c','d','e','f','g','h',' ']
def print_board(board):
str=""
for row in range(10):
str = str + "%d "%row
for col in range(10):
str = str + mark[board[10*row+col]]+" "
str = str + "\n"
str = str + " "
for col in range(10):
str = str + colmark[col] + " "
str = str + "\n\n"
print(str)
def gen_moves(board, player): # Naive...
moves=[]
opponent=3-player
for field in range(11,89):
if board[field] == FREE:
for direction in range(8):
start = field+nextpos[direction]
if board[start] == opponent:
while board[start] == opponent:
start = start+nextpos[direction]
if board[start] == player:
moves.append(field) # found a valid move
break # no need to look further, next field
return moves;
def print_moves(moves):
print ("moves: ")
for m in moves:
print("%d %c" % ( m/10, colmark[m % 10]))
print("\n")
setup_board(board)
print_board(board)
print("WHITE:\n")
moves = gen_moves(board,WHITE)
print_moves(moves)
print("\nBLACK:\n")
moves = gen_moves(board,BLACK)
print_moves(moves)
exit
From novice to tech pro — start learning today.
4h could never have been a legal move, so how could that be selected?