def playerB(board2, ply): print('Thinking ...') board1 = alphabeta(board2, ply) display(board1) lioncapture1 = capturewin(board1) lioncross1 = noncapturewin(board1) if lioncapture1 == b: print("Player B has won the game. Enter to exit.") input() quit() if lioncross1 == b: print("Player B has won the game. Enter to exit.") input() quit() elif lioncross1 == a: print("Player A has won the game. Enter to exit.") input() quit() return board1
def init_game_alphabeta(grid=1): grid = gr.create_grid(grid) start_location = gr.get_start_location() game = Game(grid, start_location) score, moves = alphabeta(game) if score < game.max_steps(): print("Score:", score) else: print("Score: infinity") return score, moves
def human_vs_computer(): current_state = State(width=7, height=6) while True: # Human Turn sys.stdout.write('\n') print(current_state) options = get_play_options(current_state) for i in range(0, current_state.width): sys.stdout.write(' ' + str(i) + ' ') sys.stdout.write('\n-> ') col = int(input()) for i, j in options: if col != j: continue current_state.board[i][j] = -1 update_game(current_state) # AI Turn current_state, _score = alphabeta(current_state, 5, MAX_PLAYER, score, done, successor) update_game(current_state)
p.append(q2) w2 = ((N - K) / N) * src[3] + (K / N) * des[3] p.append(w2) q3 = ((N - K) / N) * src[4] + (K / N) * des[4] p.append(q3) w3 = ((N - K) / N) * src[5] + (K / N) * des[5] p.append(w3) #loop for every pixel for i in range(f + 1): for j in range(f + 1): if intriangle(p[0], p[1], 0, 0, 0, 499, i, j) == 1: e1x = 0 - p[0] e1y = 0 - p[1] e2x = 0 - p[0] e2y = 499 - p[1] A = alphabeta(e1x, e2x, e1y, e2y, p[0], p[1], i, j) sx, sy = sdpoints(0 - src[0], 0 - src[1], 0 - src[0], 499 - src[1], src[0], src[1], A) dx, dy = sdpoints(0 - des[0], 0 - des[1], 0 - des[0], 499 - des[1], des[0], des[1], A) red = ((N - K) / N) * (srcimg1[sx, sy, 0]) + (K / N) * ( desimg1[dx, dy, 0]) green = ((N - K) / N) * (srcimg1[sx, sy, 1]) + (K / N) * ( desimg1[dx, dy, 1]) blue = ((N - K) / N) * (srcimg1[sx, sy, 2]) + (K / N) * ( desimg1[dx, dy, 2]) temp[i, j, 0] = red temp[i, j, 1] = green temp[i, j, 2] = blue
def move(self,boardState,player): newmove=alphabeta.alphabeta(player, boardState.bins, boardState.score1, boardState.score2) return (newmove+1)
tab = copy.deepcopy(initial_state) player0_plays = 0 player1_plays = 0 if args.singleAnswer: if args.level == 'a': level = FIVE_SECONDS_ALPHABETA_SA elif args.level == 'b': level = FIFTEEN_SECONDS_ALPHABETA_SA elif args.level == 'c': level = THIRTY_SECONDS_ALPHABETA_SA start = time.time() pos = alphabeta(tab, level) end = time.time() print('Evaluation time: {}s'.format(round(end - start, 7))) tab = play(tab, pos, PLAYER_1) print_state(tab) print("P_1 PLAYED: ", pos + 1) else: if args.level == 'a': level = FIVE_SECONDS_ALPHABETA m_level = FIVE_SECONDS_MINIMAX
def move(self, state, id, nextId): # return AB.alphabeta(state, -1, -math.inf, math.inf, True)._prevInp newState = AB.alphabeta(state, -1, -math.inf, math.inf, True) print('newState ' + str(newState)) return newState._prevInp
""" OthelloZero: main Author: Caleb Spradlin Date: 25/10/19 This file contains the main driver for the OthelloZero system, intiializes a board, and a game to run the board """ import argparse, copy, signal, sys, timeit, imp from OthelloGame import OthelloGame as Game from OthelloLogic import Board from OthelloIO import get_char_col, split_string from alphabeta import alphabeta if __name__ == "__main__": ab = alphabeta() currentPlayer = 0 ME = 1 OPP = -1 bd = Board() g = Game() color = g.initColor() g.display(bd) game_time = 300.0 time = {-1: game_time, 1: game_time} if color == -1: currentPlayer = ME else: currentPlayer = OPP while g.isNotOver is not False: if currentPlayer == ME:
def main(): global m, n, surface #start screen font = pygame.font.Font('Font.ttf', 72) text = font.render("Red", 1, (255, 0, 0)) textpos = text.get_rect(centerx=25 * n, centery=12 * m) surface.blit(text, textpos) text = font.render("Green", 1, (0, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=36 * m) surface.blit(text, textpos) font = pygame.font.Font('Font.ttf', 12) text = font.render("Choose a Color", 1, (100, 100, 100)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() this_loop = True while this_loop: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: y = pygame.mouse.get_pos()[1] if y < 25 * m: player_first = True else: player_first = False this_loop = False #depth screen surface.fill((0, 0, 0)) font = pygame.font.Font('Font.ttf', 12) text = font.render("How deep should I look?", 1, (100, 100, 100)) textpos = text.get_rect(centerx=25 * n, centery=12 * m) surface.blit(text, textpos) font = pygame.font.Font('Font.ttf', 48) depth = 3 text = font.render(str(depth), 1, (255, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() this_loop = True while this_loop: for event in pygame.event.get(): if (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN ) or event.type == pygame.MOUSEBUTTONDOWN: this_loop = False elif event.type == pygame.KEYDOWN: if event.key < 256 and chr(event.key) in '1234567890': rect = pygame.Rect(12 * m, 25 * n, 100, 100) pygame.draw.rect(surface, (0, 0, 0), rect, 0) pygame.display.update() depth = int(chr(event.key)) text = font.render(str(depth), 1, (255, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() #rows screen surface.fill((0, 0, 0)) font = pygame.font.Font('Font.ttf', 12) text = font.render("How many rows?", 1, (100, 100, 100)) textpos = text.get_rect(centerx=25 * n, centery=12 * m) surface.blit(text, textpos) font = pygame.font.Font('Font.ttf', 48) rows = 9 text = font.render(str(rows), 1, (255, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() this_loop = True while this_loop: for event in pygame.event.get(): if (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN ) or event.type == pygame.MOUSEBUTTONDOWN: this_loop = False elif event.type == pygame.KEYDOWN: if event.key < 256 and chr(event.key) in '1234567890': rect = pygame.Rect(12 * m, 25 * n, 100, 100) pygame.draw.rect(surface, (0, 0, 0), rect, 0) pygame.display.update() rows = int(chr(event.key)) text = font.render(str(rows), 1, (255, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() #columns screen surface.fill((0, 0, 0)) font = pygame.font.Font('Font.ttf', 12) text = font.render("How many columns?", 1, (100, 100, 100)) textpos = text.get_rect(centerx=25 * n, centery=12 * m) surface.blit(text, textpos) font = pygame.font.Font('Font.ttf', 48) columns = 6 text = font.render(str(columns), 1, (255, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() this_loop = True while this_loop: for event in pygame.event.get(): if (event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN ) or event.type == pygame.MOUSEBUTTONDOWN: this_loop = False elif event.type == pygame.KEYDOWN: if event.key < 256 and chr(event.key) in '1234567890': rect = pygame.Rect(12 * m, 25 * n, 100, 100) pygame.draw.rect(surface, (0, 0, 0), rect, 0) pygame.display.update() columns = int(chr(event.key)) text = font.render(str(columns), 1, (255, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update() #some initialization code m, n = rows, columns surface = pygame.display.set_mode((50 * n, 50 * m)) pygame.display.set_caption('Chain Reaction') tabuleiro = base.Tabuleiro(linhas=m, colunas=n) total_movimentos = 0 #game screen desenha_tabuleiro(tabuleiro) if not player_first: novo_movimento = alphabeta.alphabeta(tabuleiro)[0] lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, novo_movimento)) tabuleiro = base.movimento(tabuleiro, novo_movimento) total_movimentos += 1 this_loop = True while this_loop: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: x, y = pygame.mouse.get_pos() x, y = x / 50, y / 50 if not (tabuleiro.novo_movimento == base.sgn(tabuleiro[(y, x)]) or 0 == base.sgn(tabuleiro[(y, x)])): print "Illegal movimento!" continue exibe_movimento((y, x)) lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, (y, x))) tabuleiro = base.movimento(tabuleiro, (y, x)) total_movimentos += 1 if total_movimentos >= 2: if base.pontuacao(tabuleiro, tabuleiro.novo_movimento * (-1)) == 10000: vencedor = tabuleiro.novo_movimento * (-1) this_loop = False break novo_movimento = alphabeta.alphabeta(tabuleiro, depth)[0] exibe_movimento(novo_movimento) lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, novo_movimento)) tabuleiro = base.movimento(tabuleiro, novo_movimento) total_movimentos += 1 if total_movimentos >= 2: if base.pontuacao(tabuleiro, tabuleiro.novo_movimento * (-1)) == 10000: vencedor = tabuleiro.novo_movimento * (-1) this_loop = False break #winning screen while lock.locked(): continue m, n = 9, 6 surface = pygame.display.set_mode((50 * n, 50 * m)) font = pygame.font.Font('Font.ttf', 72) pygame.display.set_caption('Chain Reaction') if vencedor == 1: text = font.render("Red", 1, (255, 0, 0)) else: text = font.render("Green", 1, (0, 255, 0)) textpos = text.get_rect(centerx=25 * n, centery=12 * m) surface.blit(text, textpos) font = pygame.font.Font('Font.ttf', 48) text = font.render("Wins!", 1, (100, 100, 100)) textpos = text.get_rect(centerx=25 * n, centery=25 * m) surface.blit(text, textpos) pygame.display.update()
def main(): global m,n, surface #start screen '''font = pygame.font.Font('Font.ttf', 12) text = font.render("Iniciando...", 1, (100,100,100)) textpos = text.get_rect(centerx = 25*n, centery = 25*m) surface.blit(text, textpos) pygame.display.update()''' depth = random.randint(1,5) rows = random.randint(2, 9) columns = random.randint(2, 6) playerMinimax = random.randint(1,2) #some initialization code m, n = rows, columns '''surface = pygame.display.set_mode((50*n, 50*m)) pygame.display.set_caption('Chain Reaction')''' tabuleiro = base.Tabuleiro(linhas=m,colunas=n) global total_movimentos total_movimentos = 0 tempoMinimax = 0 tempoAlphabeta = 0 if playerMinimax == 1: #game screen print "MINIMAX COMECOU" '''desenha_tabuleiro(tabuleiro)''' this_loop = True start = time.time() while this_loop: iniciaMinimax = time.time() novo_movimento1 = minimax.minimax(tabuleiro)[0] terminaMinimax = time.time() tempoMinimax += (terminaMinimax - iniciaMinimax) print "Minimax movimentou!" #pygame.time.wait(2000) '''exibe_movimento(novo_movimento1)''' lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, novo_movimento1)) tabuleiro = base.movimento(tabuleiro, novo_movimento1) total_movimentos += 1 if total_movimentos >= 2: if base.pontuacao(tabuleiro,tabuleiro.novo_movimento*(-1)) == 10000: vencedor = tabuleiro.novo_movimento*(-1) this_loop = False break iniciaAlphabeta = time.time() novo_movimento = alphabeta.alphabeta(tabuleiro,depth)[0] terminaAlphabeta = time.time() tempoAlphabeta += (terminaAlphabeta - iniciaAlphabeta) print "Alphabeta movimentou!" #pygame.time.wait(2000) '''exibe_movimento(novo_movimento)''' lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, novo_movimento)) tabuleiro = base.movimento(tabuleiro, novo_movimento) total_movimentos += 1 if total_movimentos >= 2: if base.pontuacao(tabuleiro,tabuleiro.novo_movimento*(-1)) == 10000: vencedor = tabuleiro.novo_movimento*(-1) this_loop = False break end = time.time() tempoTotal = end - start else: #game screen print "ALPHABETA COMECOU" '''desenha_tabuleiro(tabuleiro)''' this_loop = True start = time.time() while this_loop: iniciaAlphabeta = time.time() novo_movimento = alphabeta.alphabeta(tabuleiro,depth)[0] terminaAlphabeta = time.time() tempoAlphabeta += (terminaAlphabeta - iniciaAlphabeta) print "Alphabeta movimentou!" #pygame.time.wait(2000) '''exibe_movimento(novo_movimento)''' lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, novo_movimento)) tabuleiro = base.movimento(tabuleiro, novo_movimento) total_movimentos += 1 if total_movimentos >= 2: if base.pontuacao(tabuleiro,tabuleiro.novo_movimento*(-1)) == 10000: vencedor = tabuleiro.novo_movimento*(-1) this_loop = False break iniciaMinimax = time.time() novo_movimento1 = minimax.minimax(tabuleiro)[0] terminaMinimax = time.time() tempoMinimax += (terminaMinimax - iniciaMinimax) print "Minimax movimentou!" #pygame.time.wait(2000) '''exibe_movimento(novo_movimento1)''' lock.acquire() thread.start_new_thread(inicia_reacao, (tabuleiro, novo_movimento1)) tabuleiro = base.movimento(tabuleiro, novo_movimento1) total_movimentos += 1 if total_movimentos >= 2: if base.pontuacao(tabuleiro,tabuleiro.novo_movimento*(-1)) == 10000: vencedor = tabuleiro.novo_movimento*(-1) this_loop = False break end = time.time() tempoTotal = end - start nomeIniciou = "" nomeVencedor = "" if playerMinimax == 1: nomeIniciou += "MINIMAX" if vencedor == 1: nomeVencedor += "MINIMAX" else: nomeVencedor += "ALPHABETA" else: nomeIniciou += "ALPHABETA" if vencedor == 1: nomeVencedor += "ALPHABETA" else: nomeVencedor += "MINIMAX" arq = open('contadorExecucoes.txt','r') contador = arq.read() arq.close() novoContador = int(contador) arq = open('contadorExecucoes.txt','w') arq.write(str(novoContador + 1)) arq.close() arq = open('log.txt','r') aux = arq.read() arq.close() log = "" log += "EXECUCAO #%s \n" % str(novoContador + 1) log += "Tempo total de execucao foi de %f ms.\n" % tempoTotal log += "Tempo total de escolhas de jogadas do MINIMAX foi de %f ms.\n" % tempoMinimax log += "Tempo total de escolhas de jogadas do ALPHABETA foi de %f ms.\n" % tempoAlphabeta log += "%s comecou jogando.\n" % nomeIniciou log += "O vencedor foi %s.\n\n" % nomeVencedor aux += log arq = open('log.txt','w') arq.write(aux) arq.close() #winning screen while lock.locked(): continue m, n = 9, 6 '''surface = pygame.display.set_mode((50*n, 50*m)) font = pygame.font.Font('Font.ttf', 72) pygame.display.set_caption('Chain Reaction')''' if playerMinimax == 1: if vencedor == 1: '''text = font.render("Red", 1, (255,0,0))''' arq = open('vitoriasMinimax.txt','r') vitorias = arq.read() v = int(vitorias) arq.close() arq = open('vitoriasMinimax.txt','w') arq.write(str(v+1)) arq.close() else: '''text = font.render("Green", 1, (0,255,0))''' arq = open('vitoriasAlphabeta.txt','r') vitorias = arq.read() v = int(vitorias) arq.close() arq = open('vitoriasAlphabeta.txt','w') arq.write(str(v+1)) arq.close() else: if vencedor == 1: '''text = font.render("Green", 1, (0,255,0))''' arq = open('vitoriasAlphabeta.txt','r') vitorias = arq.read() v = int(vitorias) arq.close() arq = open('vitoriasAlphabeta.txt','w') arq.write(str(v+1)) arq.close() else: '''text = font.render("Red", 1, (255,0,0))''' arq = open('vitoriasMinimax.txt','r') vitorias = arq.read() v = int(vitorias) arq.close() arq = open('vitoriasMinimax.txt','w') arq.write(str(v+1)) arq.close() '''textpos = text.get_rect(centerx = 25*n, centery = 12*m)