def abmax(gm, d, a, b): print("now calculate abmax") print("d=", d) print("alpha=", a) print("beta=", b) if d == 0 or game.isFinished(gm): print("returns ", [game.value(gm), gm]) return [game.value(gm), gm] v = float("-inf") ns = game.getNext(gm) print("next moves:", len(ns), " possible moves ") bestMove = 0 for st in ns: tmp = abmin(st, d - 1, a, b) if tmp[0] > v: v = tmp[0] bestMove = st if v >= b: return [v, st] if v > a: a = v return [v, bestMove]
def abmin(s, d, a, b): if d == 0 or game.isFinished(s): return [game.value(s), 0] v = float("inf") ns = game.getNext(s) bestMove = 0 for i in ns: tmp = abmax(copy.deepcopy(i), d - 1, a, b) if tmp[0] < v: v = tmp[0] bestMove = i if v <= a: return [v, i] if v < b: b = v return [v, bestMove]
def abmin(gm, d, a, b): if d == 0 or game.isFinished(gm): game.checkSeq(gm) return [game.value(gm), 0] v = float("inf") ns = game.getNext(gm) bestMove = 0 for st in ns: tmp = abmax(st, d - 1, a, b) if tmp[0] < v: v = tmp[0] bestMove = st if v <= a: return [v, st] if v < b: b = v return [v, bestMove]
def abmax(gm, d, a, b): if d == 0 or game.isFinished(gm): # print("returns ", [game.value(gm), gm]) game.checkSeq(gm) return [game.value(gm), gm] v = float("-inf") ns = game.getNext(gm) bestMove = 0 for st in ns: tmp = abmin(st, d - 1, a, b) if tmp[0] > v: v = tmp[0] bestMove = st if v >= b: return [v, st] if v > a: a = v return [v, bestMove]
def abmin(gm, d, a, b): #print("now calculate abmin") #print("d=", d) #print("a=", a) #print("b=", b) if d == 0 or game.isFinished(gm): #print("returns ", [game.value(gm), gm]) return [game.value(gm), 0] v = float("inf") ns = game.getNext(gm) #print("next moves:", len(ns), " possible moves ") bestMove = 0 for st in ns: tmp = abmax(st, d - 1, a, b) if tmp[0] < v: v = tmp[0] bestMove = st if v <= a: return [v, st] if v < b: b = v return [v, bestMove]
game.decideWhoIsFirst(board) comp_count = 0 average = 0 if int(input("10 set of games enter 1, 1 set enter something else : ")) == 1: for i in range(10): for i in range(0, 100): #This loops takes about 15 seconds on my computer #for i in range(0,50): while not game.isFinished(board): if game.isHumTurn(board): game.inputRandom(board) #game.inputMove(board) else: board = alphaBeta.go(board) #game.printState(board) if game.value(board) == 10**20: #the computer (or smart agent) won comp_count += 1 #print("Start another game") game.create(board) average += comp_count print("The agent beat you:", comp_count, " out of ", i + 1) comp_count = 0 else: for i in range(0, 100): #This loops takes about 15 seconds on my computer #for i in range(0,50): while not game.isFinished(board): if game.isHumTurn(board): game.inputRandom(board) #game.inputMove(board) else: board = alphaBeta.go(board)