当前位置: 首页>>代码示例>>Python>>正文


Python Grid.canMove方法代码示例

本文整理汇总了Python中Grid.Grid.canMove方法的典型用法代码示例。如果您正苦于以下问题:Python Grid.canMove方法的具体用法?Python Grid.canMove怎么用?Python Grid.canMove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Grid.Grid的用法示例。


在下文中一共展示了Grid.canMove方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from Grid import Grid [as 别名]
# 或者: from Grid.Grid import canMove [as 别名]
class GameManager:
    def __init__(self, size = 4):
        self.grid = Grid(size)
        self.possibleNewTiles = [2, 4]
        self.probability = defaultProbability
        self.initTiles  = defaultInitialTiles
        self.computerAI = None
        self.playerAI   = None
        self.displayer  = None
        self.over       = False

    def setComputerAI(self, computerAI):
        self.computerAI = computerAI

    def setPlayerAI(self, playerAI):
        self.playerAI = playerAI

    def setDisplayer(self, displayer):
        self.displayer = displayer

    def updateAlarm(self, currTime):
#        print "time elapsed = ", currTime - self.prevTime
#        st = raw_input()
        if currTime - self.prevTime > timeLimit + allowance:
            self.over = True
        else:
            while time.clock() - self.prevTime < timeLimit + allowance:
                pass

            self.prevTime = time.clock()

    def start(self):
        for i in xrange(self.initTiles):
            self.insertRandonTile()

        #self.displayer.display(self.grid)

        # Player AI Goes First
        turn = PLAYER_TURN
        maxTile = 0

        self.prevTime = time.clock()

        while not self.isGameOver() and not self.over:
            # Copy to Ensure AI Cannot Change the Real Grid to Cheat
            gridCopy = self.grid.clone()

            move = None

            if turn == PLAYER_TURN:
                print "Player's Turn:",
                move = self.playerAI.getMove(gridCopy)
                print actionDic[move]

                # Validate Move
                if move != None and move >= 0 and move < 4:
                    if self.grid.canMove([move]):
                        self.grid.move(move)

                        # Update maxTile
                        maxTile = self.grid.getMaxTile()
                    else:
                        print "Invalid PlayerAI Move"
                        self.over = True
                else:
                    print "Invalid PlayerAI Move - 1"
                    self.over = True
            else:
                print "Computer's turn:"
                move = self.computerAI.getMove(gridCopy)

                # Validate Move
                if move and self.grid.canInsert(move):
                    self.grid.setCellValue(move, self.getNewTileValue())
                else:
                    print "Invalid Computer AI Move"
                    self.over = True

            if not self.over:
                self.displayer.display(self.grid)

            # Exceeding the Time Allotted for Any Turn Terminates the Game
            self.updateAlarm(time.clock())

            turn = 1 - turn
        print maxTile

    def isGameOver(self):
        return not self.grid.canMove()

    def getNewTileValue(self):
        if randint(0,99) < 100 * self.probability:
            return self.possibleNewTiles[0]
        else:
            return self.possibleNewTiles[1];

    def insertRandonTile(self):
        tileValue = self.getNewTileValue()
        cells = self.grid.getAvailableCells()
        cell = cells[randint(0, len(cells) - 1)]
#.........这里部分代码省略.........
开发者ID:anhualin,项目名称:MyLearning,代码行数:103,代码来源:GameManager.py

示例2: __init__

# 需要导入模块: from Grid import Grid [as 别名]
# 或者: from Grid.Grid import canMove [as 别名]
class GameManager:
	def __init__(self, size = 4):
		# init some variables
		self.grid = Grid(size)
		self.possibleNewTileValue = [2, 4]
		self.possibility = defaultPossibility
		self.initTiles = defaultInitialTiles
		self.computerAI = None
		self.playerAI = None
		self.displayer = None
		self.over = False

	def setComputerAI(self, compAI):
		self.computerAI = compAI

	def setPlayerAI(self, playerAI):
		self.playerAI = playerAI

	def setDisplayer(self, displayer):
		self.displayer = displayer

	def updateAlarm(self, curTime):
		# 0.1 sec for the running time outside the AI module
		if curTime - self.lastTime > timeLimit + 0.1:
			self.over = True
		else:
			self.lastTime = curTime

	def start(self):
		#insert 2 random tiles
		for i in xrange(self.initTiles):
			self.insertRandonTile()

		# show the initial grid state
		self.displayer.display(self.grid)

		#player plays first
		turn = PLAYER_TURN
		maxTile = 0

		# set init alarm
		self.lastTime = time.clock()#time.time()

		# check game over conditions
		while not self.isGameOver() and not self.over:
			# make a copy make sure AI cannot change the real grid and cheat
			gridCopy = self.grid.clone()
			move = None

			if turn == PLAYER_TURN:
				print "Player's Turn"
				move = self.playerAI.getMove(gridCopy)
				print actionDic[move]

				#validate move
				if move != None and move >= 0 and move < 4:
					if self.grid.canMove([move]):
						self.grid.move(move)
						#update maxTile
						maxTile = self.grid.getMaxTile()
					else:
						print "Invalid PlayerAI Move"
						self.over = True
				else:
					print "Invalid PlayerAI Move - 1"
					self.over = True
			else:  # computer turn
				print "Computer's turn"
				move = self.computerAI.getMove(gridCopy)  # move should be (x, y) format
				#validate move
				if self.grid.canInsert(move):
					self.grid.setCellValue(move, self.getNewTileValue())
				else:
					print "Invalid Computer AI Move"
					self.over = True

			if not self.over:
				self.displayer.display(self.grid)

			# once you exceeds the time limit, previous action will be your last action
			self.updateAlarm(time.clock()) #time.time()
			turn = 1 - turn
		print maxTile


	def isGameOver(self):
		return not self.grid.canMove()

	def getNewTileValue(self):
		if randint(0,99) < 100 * self.possibility: 
			return self.possibleNewTileValue[0] 
		else: 
			return self.possibleNewTileValue[1]

	def insertRandonTile(self):
		tileValue = self.getNewTileValue()
		cells = self.grid.getAvailableCells()
		cell = cells[randint(0, len(cells) - 1)]
		self.grid.setCellValue(cell, tileValue)
开发者ID:18dubu,项目名称:2048AI,代码行数:101,代码来源:GameManager.py

示例3: __init__

# 需要导入模块: from Grid import Grid [as 别名]
# 或者: from Grid.Grid import canMove [as 别名]
class GameManager:
	def __init__(self, size = 4):
		#self.grid = Grid(size)
		self.possibleNewTiles = [2, 4]
		self.probability = defaultProbability
		self.initTiles  = defaultInitialTiles
		self.computerAI = None
		self.playerAI   = None
		self.over   = False
		self.grid = Grid(size)

	def setComputerAI(self, computerAI):
		self.computerAI = computerAI

	def setPlayerAI(self, playerAI):
		self.playerAI = playerAI

	def updateAlarm(self, currTime):
		if currTime - self.prevTime > timeLimit + allowance:
			print 'Out of time'
			self.over = True
		else:
			while time.clock() - self.prevTime < timeLimit + allowance:
				pass

		self.prevTime = time.clock()

	def start(self):
		#for i in xrange(self.initTiles):
		#	self.insertRandonTile()

		sel_board = randint(0, 4)

		if sel_board == 0:
			self.board1(self.grid)
		elif sel_board == 1:
			self.board2(self.grid)
		elif sel_board == 2:
			self.board3(self.grid)
		elif sel_board == 3:
			self.board4(self.grid)
		else:
			self.board5(self.grid)

		# Player AI Goes First
		turn = PLAYER_TURN
		maxTile = 0

		self.prevTime = time.clock()

		while not self.isGameOver() and not self.over:
			# Copy to Ensure AI Cannot Change the Real Grid to Cheat
			gridCopy = self.grid.clone()

			move = None

			if turn == PLAYER_TURN:
				#print "Player's Turn:",
				move = self.playerAI.getMove(gridCopy)
				#print actionDic[move]

				# Validate Move
				if move != None and move >= 0 and move < 4:
					if self.grid.canMove([move]):
						self.grid.move(move)
						#self.playerAI.utility(self.grid, True)

						# Update maxTile
						maxTile = self.grid.getMaxTile()
					else:
						#print "Invalid PlayerAI Move"
						self.over = True
				else:
					#print "Invalid PlayerAI Move - 1"
					self.over = True
			else:
				#print "Computer's turn:"
				move = self.computerAI.getMove(gridCopy)

				# Validate Move
				if move and self.grid.canInsert(move):
					self.grid.setCellValue(move, self.getNewTileValue())
				else:
					print "Invalid Computer AI Move"
					self.over = True

			# Exceeding the Time Allotted for Any Turn Terminates the Game
			self.updateAlarm(time.clock())

			turn = 1 - turn

	def isGameOver(self):
		return not self.grid.canMove()

	def getNewTileValue(self):
		if randint(0,99) < 100 * self.probability:
			return self.possibleNewTiles[0]
		else:
			return self.possibleNewTiles[1];

#.........这里部分代码省略.........
开发者ID:patrickdamery,项目名称:2048-player,代码行数:103,代码来源:bayesian.py


注:本文中的Grid.Grid.canMove方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。