示例#1
0
	def alpha_beta_timed(self,c4_orig,depth):
		stime = time.time()
		c4temp = c4()
		self.c4h = c4h()
		c4temp.board = c4_orig.copy_board()
		c4temp.mode = c4_orig.mode
		c4temp.turn = c4_orig.turn
		c4temp.potgames = []
		turn = c4temp.turn
		mode = c4temp.mode
		for i in range(0,c4temp.xmax):
		#x = [i for i in range(c4temp.xmax)]
		#random.shuffle(x)
		#for i2 in x:
			c4temp.potgames.append(c4())
			i = len(c4temp.potgames) - 1
			c4temp.potgames[i].board = c4temp.copy_board()
			c4temp.potgames[i].turn = turn
			c4temp.potgames[i].mode = mode
			c4temp.potgames[i].sim = True
			if c4temp.potgames[i].play_piece(i):
				c4temp.potgames[i].turn = (turn + 1) % 2
				self.run_time = time.time() - self.start_time
				#if depth < self.maxd and not c4temp.potgames[i].is_winner():
				#run_time gets a little ahead, this comparison works
				if (not self.times_up(depth,c4temp.xmax)
					#and depth < self.maxd
					and not c4temp.potgames[i].is_winner()):
					c4temp.potgames[i].hval = self.alpha_beta_timed(c4temp.potgames[i],depth + 1)[0]
				else:
					c4temp.potgames[i].hval = self.c4h.h2(c4temp.potgames[i],i,(turn + 1) == c4temp.red)
				if depth == 0:	
					print "Board %d, depth %d, turn: %d, time: %f".rjust(37 + depth + 1) % (i,depth, c4temp.potgames[i].turn + 1,self.run_time)
					print " hval: %d".rjust(9 + depth) % (c4temp.potgames[i].hval)
					#c4temp.potgames[i].print_board()
				#pruning
				if (c4temp.potgames[i].hval == 1
					and (turn + 1) == c4temp.black): #my move
					return (1,i)
				if (c4temp.potgames[i].hval == -1 
					and (turn + 1) == c4temp.red): #opp move
					return (-1,i)
				#end pruning
			else: #if not potgame[i].play_piece(i)
				if (turn + 1) == c4temp.black:
					c4temp.potgames[i].hval = -2
				elif (turn + 1) == c4temp.red:
					c4temp.potgames[i].hval = 2					
		etime = time.time()
		rtime = etime - stime
		#print "Depth %d" % (depth)
		#print "run time: %f" % (rtime)
		if (turn + 1) == c4temp.black:
			pass
			#print "My move, max: %d" % self.max_h(c4temp.potgames)[1]
			return(self.max_h(c4temp.potgames))
		elif (turn + 1) == c4temp.red:
			pass
			#print "Opp move, max: %d" % self.max_h(c4temp.potgames)[1]
			return(self.min_h(c4temp.potgames))
示例#2
0
	def __init__(self):
		self.c4temp = c4()
		self.c4h = c4h()
		#seconds
		self.maxd = 4 # 0 - n
		#for alpha_beta_timed
		self.time_limit = 30 
		self.total_turns = 1
示例#3
0
	def alpha_beta(self,c4_orig,depth):
		stime = time.time()
		c4temp = c4()
		self.c4h = c4h()
		c4temp.board = c4_orig.copy_board()
		c4temp.mode = c4_orig.mode
		c4temp.turn = c4_orig.turn
		c4temp.potgames = []
		turn = c4temp.turn
		mode = c4temp.mode
		for i in range(0,c4temp.xmax):
			c4temp.potgames.append(c4())
			c4temp.potgames[i].board = c4temp.copy_board()
			c4temp.potgames[i].turn = turn
			c4temp.potgames[i].mode = mode
			c4temp.potgames[i].sim = True
			if c4temp.potgames[i].play_piece(i):
				c4temp.potgames[i].turn = (turn + 1) % 2
				if depth < self.maxd and not c4temp.potgames[i].is_winner():
					c4temp.potgames[i].hval = self.alpha_beta(c4temp.potgames[i],depth + 1)[0]
				else:
					c4temp.potgames[i].hval = self.c4h.h2(c4temp.potgames[i],i,(turn + 1) == c4temp.red)
				if depth == 0:	
					print "Board %d, depth %d, turn: %d".rjust(27 + depth + 1) % (i,depth, c4temp.potgames[i].turn + 1)
					print " hval: %d".rjust(9 + depth) % (c4temp.potgames[i].hval)
					#c4temp.potgames[i].print_board()
				#pruning
				if (c4temp.potgames[i].hval == 1
					and (turn + 1) == c4temp.black): #my move
					return (1,i)
				if (c4temp.potgames[i].hval == -1 
					and (turn + 1) == c4temp.red): #opp move
					return (-1,i)
				#end pruning
			else: #if not potgame[i].play_piece(i)
				if (turn + 1) == c4temp.black:
					c4temp.potgames[i].hval = -2
				elif (turn + 1) == c4temp.red:
					c4temp.potgames[i].hval = 2
					
		etime = time.time()
		rtime = etime - stime
		#print "Depth %d" % (depth)
		#print "run time: %f" % (rtime)
		if (turn + 1) == c4temp.black:
			pass
			#print "My move, max: %d" % self.max_h(c4temp.potgames)[1]
			return(self.max_h(c4temp.potgames))
		elif (turn + 1) == c4temp.red:
			pass
			#print "Opp move, max: %d" % self.max_h(c4temp.potgames)[1]
			return(self.min_h(c4temp.potgames))