本文整理汇总了Python中board.Board.setBlock方法的典型用法代码示例。如果您正苦于以下问题:Python Board.setBlock方法的具体用法?Python Board.setBlock怎么用?Python Board.setBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类board.Board
的用法示例。
在下文中一共展示了Board.setBlock方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: successor
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import setBlock [as 别名]
def successor(node, move):
'''returns set of possible board configurations for placing a single element'''
set = []
for row in range(3):
for column in range(3):
if node.board.getBlock(row, column) == ' ':
newBoard = Board()
copyBoard(node.board, newBoard)
newBoard.setBlock(row, column, move)
newNode = Node(newBoard)
set.append(newNode)
return set
示例2: game
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import setBlock [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
示例3: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import setBlock [as 别名]
#.........这里部分代码省略.........
minimaxWelcome = '''Welcome to tic-tac-toe! This game has 3 levels of difficulty.
Easy, Medium, and Hard. Easy can plan ahead 1 move, Medium can
plan ahead 2 moves, and Hard can plan ahead to the end of the
game. Please select a difficulty.'''
if self.gameType is 'dumb':
tkMessageBox.showinfo('Welcome', dumbWelcome)
if self.gameType is 'minimax':
tkMessageBox.showinfo('Welcome', minimaxWelcome)
#open new frame to get difficulty
root = Toplevel(master=self.master)
root.title('Difficulty')
difficulty = StringVar()
Radiobutton(root, text='Easy', indicatoron=0, variable=difficulty, value='E', width=10, command=lambda x=root: self.close(x)).grid(row=0)
Radiobutton(root, text='Medium', indicatoron=0, variable=difficulty, value='M', width=10,command=lambda x=root: self.close(x)).grid(row=1)
Radiobutton(root, text='Hard', indicatoron=0, variable=difficulty, value='H', width=10, command=lambda x=root: self.close(x)).grid(row=2)
root.protocol('WM_DELETE_WINDOW', lambda x=root: self.close(x))
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
示例4: main
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import setBlock [as 别名]
def main(debug):
'''test suite for board class'''
elements = ['X', 'O', ' ']
#build valid and invalid boards
presetGood = [
['X','X','X'],
['O','O','O'],
[' ',' ',' ']]
presetBad = [
[' ','A',' '],
[' ',' ',' '],
[' ',' ',' ']]
#test constructor
if debug: print 'test constructor with no preset'
b = Board()
for row in range(3):
for column in range(3):
assert(' ' == b.board[row][column])
if debug: print 'passed constructor with no preset\n'
#----------------------------------------------------------------------------------
if debug: print 'test constructor with preset'
if debug: print '\ttest good board'
b = Board(preset=presetGood)
for row in range(3):
for column in range(3):
assert(elements.index(b.board[row][column]) in [0,1,2])
if debug: print '\tpassed good board\n'
if debug: print '\ttest bad board'
try: b = Board(preset=presetBad)
except:
if debug: print '\tcaught bad board'
if debug: print 'passed constructor with preset\n'
#----------------------------------------------------------------------------------
if debug: print 'test getBlock'
b = Board(presetGood)
if debug: print '\ttest row out of range'
try: x = b.setBlock(5,2)
except:
if debug: print '\tcaught row out of range\n'
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()
#.........这里部分代码省略.........
示例5: Board
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import setBlock [as 别名]
import agent
from board import Board
preset = [
['X', 'X', ' '],
['O', 'O', ' '],
['O', 'X', ' ']]
nextpreset = [
['X',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
generated = [
[' ', ' ', ' '],
[' ', ' ', 'X'],
[' ', 'O', 'X']]
a = Board(preset=generated)
print a.toString(), '\n\n'
move = agent.difficult(a, 'H', 'O')
a.setBlock(move[0], move[1], '+')
print a.toString()
示例6: __init__
# 需要导入模块: from board import Board [as 别名]
# 或者: from board.Board import setBlock [as 别名]
#.........这里部分代码省略.........
for row in range(3):
for column in range(3):
self.gifBoard[row][column].image = blank
if self.debug: print 'images referenced in gif matrix'
for row in range(3):
for column in range(3):
self.gifBoard[row][column].grid(row=(row+self.offset)*self.scale, column=column*self.scale, rowspan=self.scale, columnspan=self.scale, sticky=W+N+E+S)
self.buttonX = Button(self.master, image=X, command=self.setX)
self.buttonO = Button(self.master, image=O, command=self.setO)
self.buttonBlank = Button(self.master, image=blank, command=self.setBlank)
self.buttonX.image = X
self.buttonO.image = O
self.buttonBlank.image = blank
self.buttonX.grid(row=0, column=0, rowspan=self.scale, columnspan=self.scale, sticky=W+N+E+S)
self.buttonO.grid(row=0, column=1*scale, rowspan=self.scale, columnspan=self.scale, sticky=W+N+E+S)
self.buttonBlank.grid(row=0, column=2*self.scale, rowspan=self.scale, columnspan=self.scale, sticky=W+N+E+S)
self.alert = StringVar()
self.alert.set('Placing ' + self.placing)
self.alertText = Entry(self.master, textvariable=self.alert)
self.alertText.grid(row=(3+self.offset)*self.scale, columnspan=3*self.scale)
self.outputButton = Button(self.master, text="Output Board", justify=CENTER, command=self.outputBoard)
self.outputButton.grid(row=(4+self.offset)*self.scale, columnspan=3*self.scale)
self.close = Button(self.master, text="Exit", justify=CENTER, command=self.master.destroy)
self.close.grid(row=(5+self.offset)*self.scale, columnspan=3*self.scale)
if self.debug: print 'buttons bound to grid'
def setX(self):
self.placing = 'X'
self.updateAlert('Placing ' + self.placing)
def setO(self):
self.placing = 'O'
self.updateAlert('Placing ' + self.placing)
def setBlank(self):
self.placing = ' '
self.updateAlert('Placing blank')
def outputBoard(self):
self.updateAlert(self.board.toList())
def updateAlert(self, text):
self.alert.set(text)
def updateBoard(self, row, column):
if self.placing is ' ':
self.updateAlert('Placing blank')
else:
self.updateAlert('Placing ' + self.placing)
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
def updatePlayer(self, row, column):
#check if block is already occupied
if self.board.getBlock(row, column) is not ' ' and self.placing is not ' ': return -1
if self.debug: print 'updated player at', row, column
self.board.setBlock(row, column, self.placing)
self.updateGif()
return 1
def updateGif(self):
'''update visual board by replacing the button'''
if self.debug: print self.board.toString(), '\n'
for row in range(3):
for column in range(3):
if self.board.getBlock(row, column) is 'X':
#create a new identical button with image X
X = PhotoImage(file='X.gif')
self.gifBoard[row][column]= Button(self.master, image=X, command=(lambda x=row,y=column:self.updateBoard(x,y)))
self.gifBoard[row][column].image = X
self.gifBoard[row][column].grid(row=(row+self.offset)*self.scale, column=column*self.scale, rowspan=self.scale, columnspan=self.scale, sticky=N+S+E+W)
elif self.board.getBlock(row, column) is 'O':
#create a new identical button with image O
O = PhotoImage(file='O.gif')
self.gifBoard[row][column]= Button(self.master, image=O, command=(lambda x=row,y=column:self.updateBoard(x,y)))
self.gifBoard[row][column].image = O
self.gifBoard[row][column].grid(row=(row+self.offset)*self.scale, column=column*self.scale, rowspan=self.scale, columnspan=self.scale, sticky=N+S+E+W)
elif self.board.getBlock(row, column) is ' ':
#create a new identical button with blank as the image
blank = PhotoImage(file='blank.gif')
self.gifBoard[row][column]= Button(self.master, image=blank, command=(lambda x=row,y=column:self.updateBoard(x,y)))
self.gifBoard[row][column].image = blank
self.gifBoard[row][column].grid(row=(row+self.offset)*self.scale, column=column*self.scale, rowspan=self.scale, columnspan=self.scale, sticky=N+S+E+W)