Example #1
0
File: AI.py Project: Anzhit/2048AI
def monte_play(game_state):
	ans=0.0
	numRuns = 10

	if(game_state.maxValue() >= 16):
		numRuns = 20
	elif(game_state.maxValue() >= 32):
		numRuns = 50
	elif(game_state.maxValue() >= 256):
		numRuns = 75
	elif(game_state.maxValue() >= 512):
		numRuns = 120

	for i in range(numRuns):
		# print(i)
		tmp=TwentyFortyEight()
		tmp.cells=game_state.cells
		tmp.score=game_state.score


		count = 0
		while(tmp.canMove()):
			# count += 1
			# print(count)
			dir=random.choice([1,2,3,4])
			tmp.move(dir)
			tmp.new_tile()
			#print(game_state.cells)
		ans+=tmp.score1()
	ans /= numRuns

	return ans
def run_2048_new_tile_test():
    """
    Checks if new_tile() creates a new tile of either number 2 with 90%
    possibility or a 4 with 10% possibility.
    """
    twos = 0
    fours = 0
    NUM_COUNTS = 1000
    for count in range(NUM_COUNTS):
        o_game_board = TwentyFortyEight(2, 2)
        o_game_board.new_tile()
        twos += sum([row.count(2) for row in o_game_board.arr_grid])
        fours += sum([row.count(4) for row in o_game_board.arr_grid])

    print "twos: %d / %d - %4.1f percent" % (twos, NUM_COUNTS, (twos * 100.0) / NUM_COUNTS)
    print "fours: %d / %d - %4.1f percent" % (fours, NUM_COUNTS, (fours * 100.0) / NUM_COUNTS)
Example #3
0
class Environment(object):
    def __init__(self):
        self.S = TwentyFortyEight()
        self.S.make_tables()
        self.score = 0
        self.S.new_tile()
        print("New Episode")

    def reset(self):
        self.S.score = 0
        self.S.cells = 0
        self.score = 0
        self.S.new_tile()
        return self.S.vectorize_state()

    def step(self, action):
        score_prev = self.S.score1()
        cells = self.S.cells
        self.S.move(action + 1)
        r = self.S.score1() - score_prev
        self.score = self.S.score1()
        if not cells == self.S.cells:
            self.S.new_tile()
        if not self.S.canMove():
            return self.S.vectorize_state(), r, True
        return self.S.vectorize_state(), r, False

    def seed(self, a):
        return
Example #4
0
# 		x.new_tile()
# 	# print("GAME ENDs")
# 	# print(x.maxValue())
# 	print("Score:"+str(x.score)+"\t Max Tile:"+str(x.maxValue()))
# 	# x.__str__()
# 	# print(x.get_available_moves())



occ = np.zeros(16)
for i in range(500):
	x=TwentyFortyEight()
	x.make_tables()
	# x.print_tables()
	# print("Generated Tables")
	x.new_tile()
	# avail_moves = x.get_available_moves()
	while(True):
		# x.__str__()
		# print("-----------------------")
		temp = x.vectorize_state();
		tempx = np.zeros((1, 256))
		for j in range(16):
			tempx[0, j*16 + temp[j]] = 1

		probs=y.eval(feed_dict={x1: tempx})[0]
		# print(probs)
		# dir = eminimax(x,2)
		# dir=minimax_alpha_beta(x,6)
		# dir=monte_carlo(x)
		avail_moves = x.get_available_moves()