def makeMove(board, color, weights=_AB_DEFAULT): if color == _BLACK: board.bPosMoves = board.allMoves(_BLACK) numMoves = len(board.bPosMoves) if numMoves == 0: return else: try: (x, y), val = maxiMin(board, _BLACK, _ALPHA_BETA_SEARCH_DEPTH, -9999999, 9999999) board.place(x, y, _BLACK) return except TypeError: b.makeMove(board, color, weights) return else: board.wPosMoves = board.allMoves(_WHITE) numMoves = len(board.wPosMoves) if numMoves == 0: return else: try: (x, y), val = maxiMin(board, _WHITE, _ALPHA_BETA_SEARCH_DEPTH, -9999999, 9999999) board.place(x, y, _WHITE) return except TypeError: b.makeMove(board, color, weights) return
def randVSab(isStatRun, _type): mainBoard = s.Board() inGame = True toMove = _BLACK bMoves = 0.0 rMoves = 0.0 rBranch = 0.0 bBranch = 0.0 greedyWin = 0 randWin = 0 while inGame: #mainBoard.printer() # uncomment to watch entire game #time.sleep(0.4) # # collect branch factor data to pass to stats functions if _type % 2 == 1: if toMove == _BLACK: rMoves += 1 rBranch += len(mainBoard.bPosMoves) base.makeMove(mainBoard, toMove, base._GREEDY_BALANCED) else: bMoves += 1 bBranch += len(mainBoard.wPosMoves) ab.makeMove(mainBoard, toMove, ab._AB_DEFAULT) else: if toMove == _WHITE: rMoves += 1 rBranch += len(mainBoard.wPosMoves) base.makeMove(mainBoard, toMove, base._GREEDY_BALANCED) else: bMoves += 1 bBranch += len(mainBoard.bPosMoves) ab.makeMove(mainBoard, toMove, ab._AB_DEFAULT) inGame, toMove = s.update(mainBoard, toMove, inGame) if _type % 2 == 1 and mainBoard.winner == _WHITE: greedyWin = 1 elif _type % 2 == 0 and mainBoard.winner == _BLACK: greedyWin = 1 else: randWin = 1 if isStatRun: return mainBoard, rBranch / rMoves, bBranch / bMoves, greedyWin, randWin else: mainBoard.printer() print readable(mainBoard.winner), "wins"
def baseVSbase(isStatRun, w1=base._GREEDY_BALANCED, w2=base._GREEDY_BALANCED): mainBoard = s.Board() inGame = True toMove = _BLACK bMoves = 0.0 wMoves = 0.0 wBranch = 0.0 bBranch = 0.0 while inGame: #mainBoard.printer() # uncomment to watch entire game #time.sleep(0.4) # # collect branch factor data to pass to stats functions if toMove == _BLACK: bMoves += 1 bBranch += len(mainBoard.bPosMoves) base.makeMove(mainBoard, toMove, w1) else: wMoves += 1 wBranch += len(mainBoard.wPosMoves) base.makeMove(mainBoard, toMove, w2) inGame, toMove = s.update(mainBoard, toMove, inGame) if isStatRun: bWin = 0 wWin = 0 tie = 0 margin = (mainBoard.blackTileCT - mainBoard.whiteTileCT) if mainBoard.winner == _BLACK: bWin = 1 winnerW = w1 elif mainBoard.winner == _WHITE: wWin = 1 winnerW = w2 else: tie = 1 winnerW = [] return [ bWin, wWin, tie, bBranch / bMoves, wBranch / wMoves, margin, winnerW ] else: mainBoard.printer() print readable(mainBoard.winner), "wins"
def onePlayerREPL(): try: print "\n ===== Welcome to Othello =====\n" print " input example: b4\n" print " q to quit\n" # setup game mainBoard = s.Board() toMove = _BLACK inGame = True # player choose color while True: try: _in = raw_input( "which color would you like to play? (b/w)\n> ") if _in == 'b': playerColor = _BLACK aiColor = _WHITE break elif _in == 'w': playerColor = _WHITE aiColor = _BLACK break except: print "invalid input" # player choose opponent while True: try: _in = raw_input( "against which AI would you like to play? (normal/hard)\n> " ) if _in == 'normal': aiType = "base" break elif _in == 'hard': aiType = "ab" break except: print "invalid input" # make moves while inGame: mainBoard.printer() if playerColor == toMove: inGame = s.makeMove(mainBoard, playerColor) else: time.sleep(0.8) if aiType == "base": base.makeMove(mainBoard, aiColor, base._GREEDY_BALANCED) else: ab.makeMove(mainBoard, aiColor, ab._AB_DEFAULT) inGame, toMove = s.update(mainBoard, toMove, inGame) except KeyboardInterrupt: pass print readable(mainBoard.winner), "wins" print "\n\ngoodbye \n"