def negamax(candidates, role, deep, alpha, beta): board.currentSteps = [] for i in range(len(candidates)): p = candidates[i] board.put(p, role) steps = [p] v = r(deep - 1, -beta, -alpha, R.reverse(role), 1, steps[:], 0) v.score *= -1 alpha = max(alpha, v.score) board.remove(p) p.v = v return alpha
def main(): e = False winname = [] win = 0 los = 0 while True: if e: break board.init(15) #print(board.board) game_id = joinGame() size = -1 while True: time.sleep(6) gjson = check_game(game_id) if (gjson['is_success'] == True): if gjson['current_turn'] == '我是小号': #print("(len(gjson['board']):"+str(len(gjson['board']))) p = Point.point last_step = gjson['last_step'] if (len(last_step) != 0): p.x = toInt(last_step[0]) p.y = toInt(last_step[1]) board.put(p, R.hum) if (len(gjson['board']) == 0): p.x = 7 p.y = 7 #elif len(gjson['board'])==2: # p.x = toInt(last_step[0])+1 # p.y = toInt(last_step[1])+1 else: s = time.time() p = m(R.com, 7) print('spend time:' + str(time.time() - s)) if playGame(game_id, toChar(p.x) + toChar(p.y))['is_success'] == True: board.put(p, R.com) if gjson['winner'] != 'None': winname.append(gjson['winner']) if gjson['winner'] == '我是小号': win += 1 else: los += 1 e = True print('win count:' + str(win) + ' los count:' + str(los)) print('winplyers:' + str(winname)) break else: break
def cs(): p = Point.point if (len(board.allSteps) > 1): s = time.time() p = m(R.com, 6) print(time.time() - s) board.put(p, R.com) else: p.x = board.currentSteps[0].x + 1 p.y = board.currentSteps[0].y + 1 cp(p.x, p.y) if 'score' in p.__dict__: return p.x, p.y, p.score else: return p.x, p.y, 0
def hp(x, y): p = Point.point p.x = x p.y = y board.put(p, R.hum)
def cp(x, y): p = Point.point p.x = x p.y = y board.put(p, R.com)
def r(deep, alpha, beta, role, step, steps, spread): global Cache global cacheGet if (board.zobrist.code in Cache): c = Cache[board.zobrist.code] if (c.deep >= deep): cacheGet += 1 score = Score() score.score = c.score.score score.steps = steps score.step = step + c.score.step score.c = c return score #else: # if (mymath.greatOrEqualThan(c.score.score, SCORE.FOUR) or mymath.littleOrEqualThan(c.score.score, -SCORE.FOUR)): # cacheGet +=1 # return c.score _e = board.evaluate(role) leaf = Score() leaf.score = _e leaf.step = step leaf.steps = steps global count count += 1 if deep <= 0 or mymath.greatOrEqualThan( _e, SCORE.FIVE) or mymath.littleOrEqualThan(_e, -SCORE.FIVE): return leaf best = Score() global MIN best.score = MIN best.step = step best.steps = steps points = board.gen(role, step > 1 if board.count > 10 else step > 3, step > 1) if not len(points): return leaf for i in range(len(points)): p = points[i] board.put(p, role) _deep = deep - 1 _spread = spread if (_spread < 1): if ((role == R.com and p.scoreHum >= SCORE.FIVE) or (role == R.hum and p.scoreCom >= SCORE.FIVE)): _deep += 2 _spread += 1 _steps = steps[:] _steps.append(p) v = r(_deep, -beta, -alpha, R.reverse(role), step + 1, _steps, _spread) v.score *= -1 board.remove(p) if (v.score > best.score): best = v alpha = max(best.score, alpha) global ABcut if (mymath.greatOrEqualThan(v.score, beta)): ABcut += 1 v.score = MAX - 1 v.abcut = 1 return v cache(deep, best) return best
def cs(): if (len(board.allSteps) > 1): p = m(R.com, 8) board.put(p, R.com) test(board.board)