コード例 #1
0
def desenha_tabuleiro(tabuleiro=base.Tabuleiro()):
	surface.fill((0,0,0))
	font = pygame.font.Font('Font.ttf', 48)
	for pos in [(x,y) for x in xrange(tabuleiro.linhas) for y in xrange(tabuleiro.colunas)]:
		if abs(tabuleiro[pos]) >= tabuleiro.massa_critica(pos):
			color = (255,255,0)
		elif base.sgn(tabuleiro[pos]) == 0:
			color = (90,90,90)
		elif base.sgn(tabuleiro[pos]) == 1:
			color = (255,0,0)
		else:
			color = (0,255,0)
		text = font.render(str(tabuleiro[pos])[-1], 1, color)
		textpos = text.get_rect(centerx = pos[1]*50 + 25, centery = pos[0]*50 + 25)
		surface.blit(text, textpos)
	pygame.display.update()
コード例 #2
0
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 = adhoc.adhoc(tabuleiro)
        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 = adhoc.adhoc(tabuleiro)
                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()
コード例 #3
0
ファイル: IAvsIA.py プロジェクト: dnrocha1/chain-reaction-bot
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)