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


Python Board.isFull方法代码示例

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


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

示例1: TicTacToeGame

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isFull [as 别名]
class TicTacToeGame(object):
    def __init__(self, player='X',turn='X'):
        self.board = Board()
        self.turn = turn
        self.player = player

    def play(self):
        isWon = False
        while not isWon:
            # if this evaluates to True, then isWon must be False
            # so we have a cat's game
            if self.board.isFull():
                winner = "The cat"
                break

            print self.board.prettyPrint()
            self.board = self.nextMove()
            isWon, winner = self.board.whoWon()

        print "Game was won by %s" % winner

    def nextMove(self):
        if self.turn == self.player:
            bestMove = None
            # starting with -1 so we'll definitely replace it
            bestMoveProbabilityOfWinning = -1
            for board in self.board.possibleMoves():
                pWinning = boardWinning(board, self.player)
                print pWinning
                if pWinning > bestMoveProbabilityOfWinning:
                    print "%f better than %f" % (pWinning, bestMoveProbabilityOfWinning)
                    bestMoveProbabilityOfWinning = pWinning
                    bestMove = board
            nextBoard = bestMove
        else:
            validMove = False
            newBoardSpaces = self.board.spaces[:]
            while not validMove:
                spaceIndex = int(raw_input("What space do you want to play in? "))
                if spaceIndex < 1 or spaceIndex > 9:
                    print "Please select a valid space"
                else:
                    spaceIndex -= 1  # map index to board array
                    if self.board.spaces[spaceIndex]:
                        print "That space is full!"
                    else:
                        validMove = True
                        # Player is 'O' if AI is 'X' and vice versa
                        newBoardSpaces[spaceIndex] = 'O' if self.player == 'X' else 'X'
                        nextBoard = Board(newBoardSpaces)

        self.turn = 'O' if self.turn == 'X' else 'X'
        return nextBoard
开发者ID:yjkogan,项目名称:UltimateTicTacToe,代码行数:55,代码来源:basicTicTacToe.py

示例2: main

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isFull [as 别名]
def main():
	# Initialize the game engine
	pygame.init()

	# Set the height and width of the screen
	tileSize = 100
	size = (100+4*tileSize, 100+4*tileSize)
	screen = pygame.display.set_mode(size)
	gameBoard = Board(tileSize, screen)
	gameSolver = Solver(gameBoard) 
	 
	pygame.display.set_caption("Jeremy's 2048 Game")
	 
	# Loop until the user clicks the close button.
	done = False
	gameStart = False
	clock = pygame.time.Clock()

	# Clear the screen and set the screen background
	screen.fill(gameBoard.COLORDICT[2])

	# Loop as long as done == False
	while not done:

	    for event in pygame.event.get():  # User did something
	        if event.type == pygame.QUIT:  # If user clicked close
	            done = True  # Flag that we are done so we exit this loop

	        if pygame.key.get_focused():
		        keys = pygame.key.get_pressed()

		        if not gameStart:
		        	if keys[pygame.K_SPACE]:
			        	gameBoard.beginGame()
			        	gameStart = True
		        else:
		        	if keys[pygame.K_i] and gameBoard.upPossible(gameBoard.boardMatrix):
		        		gameBoard.move(0,gameBoard.boardMatrix, gameBoard.prevboardMatrix)
		        		# gameBoard.getNewTile(gameBoard.boardMatrix)

		        	elif keys[pygame.K_k] and gameBoard.downPossible(gameBoard.boardMatrix):
		        		gameBoard.move(1,gameBoard.boardMatrix, gameBoard.prevboardMatrix)
		        		# gameBoard.getNewTile(gameBoard.boardMatrix)

		        	elif keys[pygame.K_j] and gameBoard.leftPossible(gameBoard.boardMatrix):
		        		gameBoard.move(2,gameBoard.boardMatrix, gameBoard.prevboardMatrix)
		        		# gameBoard.getNewTile(gameBoard.boardMatrix)

		        	elif keys[pygame.K_l] and gameBoard.rightPossible(gameBoard.boardMatrix):
		        		gameBoard.move(3,gameBoard.boardMatrix, gameBoard.prevboardMatrix)
		        		# gameBoard.getNewTile(gameBoard.boardMatrix)

		        	elif keys[pygame.K_u]:
		        		gameBoard.numOfMoves -= 1
		        		gameBoard.printMatrix(gameBoard.prevboardMatrix)
		        		gameBoard.refreshBoard(gameBoard.prevboardMatrix)
		        		gameBoard.boardMatrix[:] = gameBoard.prevboardMatrix[:]

		        	elif keys[pygame.K_s]:
		        		gameSolver.solve()

		        	if gameBoard.isFull():
		        		if gameBoard.upPossible(gameBoard.boardMatrix):
		        			pass
		        		elif gameBoard.downPossible(gameBoard.boardMatrix):
		        			pass
		        		elif gameBoard.rightPossible(gameBoard.boardMatrix):
		        			pass
		        		elif gameBoard.leftPossible(gameBoard.boardMatrix):
		        			pass
		        		else:
		        			gameStart = False
		        			gameBoard.gameOver()
	 
	    # All drawing code happens after the for loop and but
	    # inside the main while not done loop.
	 
	    # Go ahead and update the screen with what we've drawn.
	    # This MUST happen after all the other drawing commands.
	    pygame.display.flip()
	 
	    # This limits the while loop to a max of 60 times per second.
	    # Leave this out and we will use all CPU we can.
	    clock.tick(60)
	 
	# Be IDLE friendly
	pygame.quit()
开发者ID:jerbotron,项目名称:2048_python,代码行数:89,代码来源:main.py

示例3: game

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isFull [as 别名]
def game():
    from board import Board
    from string import lower
    import agent
    import random
    
    #make a new, empty board
    b = Board()
    
    #show player empty board
    print '\n\n'
    print 'Welcome to tic-tac-toe.\nHere is the board, when placing your move,\n', 
    print 'be sure to use the format column,row when specifying where to place your X.\n',
    print 'i.e. the top left square is 1,1 and the bottom right square is 3,3\n',
    print 'You can type Quit to forfeit at any time.\n\n\n'
    
    print b.toString()
    
    #continue placing until board is full, or someone has won
    while not b.hasWinner() and not b.isFull():
        #player goes first (is X)
        player = raw_input('\n\nWhat column and row do you want to put X in?\n(Use column,row format)\n')
            
        #see if the player quits
        if lower(player) == "quit" or lower(player[0]) == 'q': print 'You lost the game.  Thank you for playing!'; exit()
        
        #make sure player entered something
        if len(player) < 3:
            print 'Please enter your column,row to place the X, or type quit to forfeit.'
            continue
        
        #make sure the player input is formatted correctly
        if not len(player) == 3 and player[1] == ',':
            #incorrect format
            print 'Please enter column and row as column,row with no space and a comma separating the values'
            continue
        
        #make sure the user entered integers
        try:
            column = int(player[0]) -1 
            row = int(player[2]) -1
        except:
            print 'Please make sure your coordinates are numbers.'
            continue
        
        #make sure row and column are in bounds
        if not 0 <= row <= 2: print 'Row is out of bounds, please try again'; continue
        if not 0 <= column <= 2: print 'Column is out of bounds, please try again'; continue
        
        #make sure block is already empty
        if b.getBlock(row, column) == ' ':
            b.setBlock(row, column, 'X')
        else: 
            print 'Block already contains an X or O, please enter other coordinates.'
            continue
        
        print '\n\n' + b.toString()
        
        #check to see if the player's move won or filled the board
        if b.hasWinner(): break
        if b.isFull(): break
        
        print '\nNow the computer will place an O.'
        
        #find random empty square for computer's turn
        row,column = agent.dumb(b)
            
        b.setBlock(row, column, 'O')
        
        print '\n\n' + b.toString()
        
    if b.hasWinner(): 
        #if player won
        if b.hasWinner() == 'X': print 'Congratulations! You won!'
        if b.hasWinner() == 'O': print 'Sorry, but you lost.'
        
    elif b.isFull():
        print '\nThere was no winner.'
        
        
    #ask the user if they want to play again
    playAgain = raw_input('\nDo you want to play again? (y/n)\n')
    if len(playAgain) == 0:
        return False
    if lower(playAgain[0]) == 'y':
        return True
    else:
        return False
开发者ID:andy-shaw,项目名称:tic-tac-toe,代码行数:90,代码来源:dumbGame.py

示例4: __init__

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isFull [as 别名]

#.........这里部分代码省略.........
            
            root.mainloop()

            if self.debug: print 'difficulty is', difficulty.get()
            
            if len(difficulty.get()) is not 0: self.difficulty = difficulty.get()
            else: 
                #notify player that they did not set the difficulty, so they are facing an easy opponent
                tkMessageBox.showinfo('Difficulty', 'You did not select a difficulty, so you will be on Easy.')
                self.difficulty = 'E'

            if self.opponent == 'X':
                row, column = agent.dumb(self.board)
                self.board.setBlock(row, column, self.opponent)
                self.updateGif()
                
    def close(self, tk):
        tk.withdraw()
        self.master.quit()
        
    def updateAlert(self, text):
        self.alert.set(text)
        
    def updateBoard(self, row, column):
    
        self.updateAlert('')
        if self.debug: print 'update ' + str(row) + ' ' + str(column)
        if self.updatePlayer(row,column) == -1: 
            self.updateAlert("Block already contains an X or O, please choose another box.")
            return
            
        #check for a winner or a full board
        if self.board.hasWinner(): self.endGame(); return
        if self.board.isFull(): self.endGame(); return
        
        self.updateOpponent()
        
        #check for a winner or a full board
        if self.board.hasWinner(): self.endGame(); return
        if self.board.isFull(): self.endGame(); return
        
    def updateOpponent(self):
        if self.gameType is 'dumb': row, column = agent.dumb(self.board)
        if self.gameType is 'minimax': row, column = agent.difficult(self.board, self.difficulty)
        
        if self.debug: print 'updated opponent at', row, column
        
        self.board.setBlock(row, column, self.opponent)
        
        self.updateGif()
        
    def updatePlayer(self, row, column):
        #check if block is already occupied
        if self.board.getBlock(row, column) is not ' ': return -1
        
        if self.debug: print 'updated player at', row, column
        
        self.board.setBlock(row, column, self.player)
        
        self.updateGif()
        return 1
        
    def updateGif(self):
        '''update visual board by replacing the button'''
        if self.debug: print self.board.toString(), '\n'
开发者ID:andy-shaw,项目名称:tic-tac-toe,代码行数:69,代码来源:game.py

示例5: main

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isFull [as 别名]

#.........这里部分代码省略.........
        
    if debug: print '\ttest column out of range'
    try: x = b.setBlock(2,5)
    except:
        if debug: print '\tcaught column out of range\n'
    
    if debug: print '\ttest for correct retrieval'
    for row in range(3):
        for column in range(3):
            assert(presetGood[row][column] == b.getBlock(row, column))
    if debug: print '\tpassed correct retrieval'
    if debug: print 'passed getBlock\n'
    
    #----------------------------------------------------------------------------------
    
    if debug: print 'test setBlock'
    b = Board()
    
    if debug: print '\ttest invalid assignment'
    try: b.setBlock(2,2,'A')
    except:
        if debug: print '\tcaught invalid assignment\n'
        
    if debug: print '\ttest row out of range'
    try: b.setBlock(5,2,'X')
    except:
        if debug: print '\tcaught row out of range\n'
        
    if debug: print '\ttest column out of range'
    try: b.setBlock(2,5,'X')
    except:
        if debug: print '\tcaught column out of range\n'
    
    if debug: print '\ttest for correct assignemtnt'
    for row in range(3):
        for column in range(3):
            b.setBlock(row, column, 'X')
            
    for row in range(3):
        for column in range(3):
            assert(b.board[row][column] == 'X')
    if debug: print '\tpassed correct assignemtnt'
    
    if debug: print 'passed setBlock\n'
    
    #----------------------------------------------------------------------------------
    
    if debug: print 'test toString'
    b = Board(presetGood)
    assert('X|X|X\n-----\nO|O|O\n-----\n | | ' == b.toString())
    if debug: print b.toString()
    if debug: print 'passed toString\n'
    
    #----------------------------------------------------------------------------------
    
    if debug: print 'test isFull'
    full = [
    ['X','X','X'],
    ['O','O','O'],
    ['X','X','X']]
    
    notFull = [
    ['X','X','X'],
    ['O',' ','O'],
    ['X','X','X']]
    
    b = Board(full)
    assert(True == b.isFull())
    b = Board(notFull)
    assert(False == b.isFull())
    if debug: print 'passed isFull\n'
    
    #----------------------------------------------------------------------------------
    
    if debug: print 'test hasWinner'
    
    xwin = [
    ['X',' ',' '],
    ['X','O',' '],
    ['X',' ','O']]
    
    b = Board(xwin)
    assert('X' == b.hasWinner())
    
    owin = [
    ['O','X',' '],
    ['O',' ',' '],
    ['O','X','X']]
    
    b = Board(owin)
    assert('O' == b.hasWinner())
    
    nowin = [
    ['X','O','O'],
    ['O','X','X'],
    ['X','O','O']]
    
    b = Board(nowin)
    assert(None == b.hasWinner())
    if debug: print 'passed hasWinner'
开发者ID:andy-shaw,项目名称:tic-tac-toe,代码行数:104,代码来源:_boardTest.py

示例6: testBoardFull

# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import isFull [as 别名]
 def testBoardFull(self):
   board = Board()
   for i in range(9):
     board.makeMove(i,'X')
   self.assertEqual(board.isFull(), True)
开发者ID:narasimha14,项目名称:TicTacToe,代码行数:7,代码来源:testsuite.py


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