实现功能:
在图中的9宫格里,玩家1、玩家2使用‘X’or‘O’作为棋子,先连成3个的一方获胜。
主要涉及的函数:
1.选择先走符号的函数:
inputPlayerLetter
2.打印棋盘:
drawBoard
3.轮流输入要走的位置:
playerMove
4.胜利判断:
isWinner
5.棋盘填满:
isBoardFull
6.一盘结束时,是否还要继续
playAgain
函数分解:
1.选择先走符号的函数:inputPlayerLetter
返回:下棋顺序的list
可以选择'X'or'O'代表你的棋子
def inputPlayerLetter():
input_letter=""
while not (input_letter =='X' or input_letter =='O'):
print u'你想使用X,还是O'
input_letter=raw_input().upper()
if input_letter=='X':
return ['X','O']
else:
return ['O','X']
注解:
1.在选择棋子时,可能输入的不是符合要求的棋子,这时需要重新输入,所以程序应含有while循环体
2.玩家在选择棋子时,可能输入的是小写x,o,在判断输入正确与否是,应统一成大写,使用input_letter=raw_input().upper()语句
3.判断玩家所做的选择,返回值是把玩家1、2对应的棋子按照先后顺序放入一个list中
2..打印棋盘:drawBoard(棋盘编号)
def drawBoard(board):
# board=[0,1,2,3,4,5,6,7,8,9],为了将board[1]=1,对应所以在list中加入了0)
blank_board = '| '*3+'|'
edge_board = '+-----'*3+'+'
def boardNum(board_num):
print blank_board
print '|%3s%3s%3s%3s%3s |'%(board_num[0],'|',board_num[1],'|',board_num[2])
print blank_board
print edge_board
print edge_board
boardNum(board[7:10])
boardNum(board[4:7])
boardNum(board[1:4])
注解:
1.画出边框+-----+-----+-----+,对应的语句
edge_board = '+-----'*3+'+'
2.画出分界| | | |,对应的语句
blank_board = '| '*3+'|'
3.棋盘数字| 7 | 8 | 9 |,对应的语句
'|%3s%3s%3s%3s%3s |'%(board_num[0],'|',board_num[1],'|',board_num[2])
4.
画出棋盘的逻辑先一层边框,再一层分界,一层棋盘数字,一层分界,一层边框。循环3次。在棋盘数字那层每次传人的数字不一样,可以采用调用三次函数,每次传人参数不同
boardNum(board[7:10])
boardNum(board[4:7])
boardNum(board[1:4])
3.轮流输入要走的位置:playerMove(棋盘编好信息和玩家顺序list)
def playerMove(board_num,letter):
player=[player1letter,player2letter]=letter
print player1letter+u'先'
turn=0
while True:
current_player=player[turn]
print u'玩家'+current_player+u'输入你要放置棋子的格数'
stepin=raw_input()
if stepin in ['1','2','3','4','5','6','7','8','9']:
step=int(stepin)
if board_num[step]== 'X' or board_num[step] =='O':
print u'你选择的格子上已经有棋子了,请重新选择'
continue
board_num[step]=current_player
drawBoard(board_num)
if isWinner(board_num,current_player):
print current_player+ u'赢了'
break
if isBoardFull(board_num):
print u"棋盘已满,平局"
break
turn=(turn+1)%2
else:
print u'你的输入不正确,请重新输入(1-9)'
注解:
1.两个之间的轮流,可以创建一个变量通过轮换一次,变量加一对2取余
turn=0
current_player=player[turn]
turn=(turn+1)%2
2.玩家输入所要走的位置,需要判断输入是否符合条件。
(a)输入是否在1-9中,raw_input采集到的输入是str型,所以应判断
stepin=raw_input()
if stepin in ['1','2','3','4','5','6','7','8','9']:
为了将棋盘上board_num的值变为输入的值,需要将输入的值变为int型
step=int(stepin)
(b)需要判断所走的位置是否已经有棋子
if board_num[step]== 'X' or board_num[step] =='O':
print u'你选择的格子上已经有棋子了,请重新选择'
continue
continue不再执行下面的语句,跳入循环体开头,重新执行
(c)需要判断是否有一方获胜
调用判断获胜的isWinner函数,需要向函数传入的参数是当前的棋盘信息和当前的玩家是哪一方,如果返回为True,则说明当前玩家赢了,break退出循环体
if isWinner(board_num,current_player):
print current_player+ u'赢了' break
(d)需要判断棋盘是否被走满
调用棋盘是否满函数isBoardFull,传人的参数是棋盘信息,如果走满,则输出平局,如果没有走满继续
if isBoardFull(board_num):
print u"棋盘已满,平局"
break
4.胜利判断:isWinner(当前的棋盘信息和当前的玩家是哪一方)
返回:true\false
罗列出所有赢得情况相交集,只要有一种情况发生,则返回true
def isWinner(bo,le):
return ((bo[7] == le and bo[8] == le and bo[9] == le) or # across the top
(bo[4] == le and bo[5] == le and bo[6] == le) or # across the middle
(bo[1] == le and bo[2] == le and bo[3] == le) or # across the bottom
(bo[7] == le and bo[4] == le and bo[1] == le) or # down the left side
(bo[8] == le and bo[5] == le and bo[2] == le) or # down the middle
(bo[9] == le and bo[6] == le and bo[3] == le) or # down the right side
(bo[7] == le and bo[5] == le and bo[3] == le) or # diagonal
(bo[9] == le and bo[5] == le and bo[1] == le)) # diagonal
5.棋盘填满:isBoardFull(当前的棋盘信息)
返回:true\false
def isBoardFull(board_num):
n=0
for x in xrange(1,10):
if not board_num[x] in [1,2,3,4,5,6,7,8,9]:
n=n+1
if n==9:
return True
else:
return False
6.一盘结束时,是否还要继续: playAgain()
返回:true\false
def tryAgain():
print u'还想再玩一把么?(yes or no)'
return raw_input().lower().startswith('y')#startswith()函数是监测是否字符串以y开头,如果是返回ture,不是返回false