import struct, string class OthelloBoard: def __init__(self): self.board = [[' ']*8 for i in range(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 column numbers print(" ",end="") for i in range(self.size): print(i+1,end=" ") print() # Build horizontal separator linestr = " " + ("+-" * self.size) + "+" # Print board for i in range(self.size): print(linestr) # Separator print(i+1,end="|") # Row number for j in range(self.size): print(self.board[i][j],end="|") # board[i][j] and pipe separator print() # End line 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, row, col, player, opp): if(self.get_square(row,col)!=" "): 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]) b.size: raise ValueError("Board requires input between 1 and %d, inclusive." % b.size) col = int(input("Pick a col (1-8): ")) if col < 1 or col > b.size: raise ValueError("Board requires input between 1 and %d, inclusive." % b.size) # ValueError occurs when user inputs non-integer values # or integers outside of the range of the board except ValueError: print("Illegal move.") continue # Loop back to while True loop # Check validity of move. If valid, change board # and exit while True loop if(b.play_square(row-1,col-1,player,opp)): b.PrintBoard() break print("Illegal move.") else: print("No move available.") player = CPU opp = Human continue elif player==CPU : if b.all_pieces(player): break if(b.has_move(player, opp)): print("CPU has available moves.") make_simple_cpu_move(b, CPU, Human) b.PrintBoard() player = Human opp = CPU else: print("CPU has no available moves.") player = Human opp = CPU continue if( b.score(Human, CPU) > 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()