import struct, string class OthelloBoard: def __init__(self): self.board = ([' ']*8,[' ']*8,[' ']*8,[' ']*8 ,[' ']*8 ,[' ']*8 ,[' ']*8 ,[' ']*8) self.size = 8 self.board[4][4] = 'W' self.board[3][4] = 'B' self.board[3][3] = 'W' self.board[4][3] = 'B' # a list of unit vectors (row, col) self.directions = [ (-1,-1), (-1,0), (-1,1), (0,-1),(0,1),(1,-1),(1,0),(1,1)] #prints the board def PrintBoard(self): print(" 0 1 2 3 4 5 6 7") linestr = " " for j in range(self.size): linestr = linestr + "+-" linestr = linestr + "+" for i in range(self.size): strng = str(i) + "|" print(linestr) for k in range(self.size): strng = strng + self.board[i][k] + "|" print(strng) print(linestr) def board_full(self): for i in range(self.size): for j in range(self.size): if self.get_square(i,j)==" ": return False return True #determines the score of the board by adding +1 for every tile owned by player, and -1 for every tile owned by opp def score(self, player, opp): score = 0 for i in range(self.size): for j in range(self.size): if(self.get_square(i,j)==player): score +=1 elif(self.get_square(i,j)==opp): score -= 1 return score #returns true if the square was played, false if the move is not allowed def play_square(self, col, row, player, opp): if(self.get_square(col,row)!=" "): return False if(player == opp): print("player and opponent cannot be the same") return False legal = False #for each direction, check to see if the move is legal by seeing if the adjacent square #in that direction is occuipied by the opponent. If it isnt check the next direction. #if it is, check to see if one of the players pieces is on the board beyond the oppponents piece, #if the chain of opponents pieces is flanked on both ends by the players pieces, flip #the opponents pieces for Dir in self.directions: #look across the length of the board to see if the neighboring squares are empty, #held by the player, or held by the opponent for i in range(self.size): if ((( row + i*Dir[0])=0 ) and (( col + i*Dir[1])>=0 ) and (( col + i*Dir[1])=0 ) and (( col + i*Dir[1])>=0 ) and (( col + i*Dir[1]) 0): print("the winner is: "+ Human) elif(b.score(Human, CPU) == 0): print("the game is a draw") else: print("the winner is: " + CPU) main()