马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
写了个井字棋不过需要双人游戏哈哈def printTable():
print(' | |')
print(''+board[1]+'|'+board[2]+'|'+board[3])
print(' | |')
print('-----')
print(' | |')
print(''+board[4]+'|'+board[5]+'|'+board[6])
print(' | |')
print('-----')
print(' | |')
print(''+board[7]+'|'+board[8]+'|'+board[9])
print(' | |')
def isWinner(board):
#print(board)
if board[1] == 'O' and board[4] == 'O' and board[7] == 'O'or\
board[2] == 'O' and board[5] == 'O' and board[8] == 'O'or\
board[3] == 'O' and board[6] == 'O' and board[9] == 'O'or\
board[1] == 'O' and board[2] == 'O' and board[3] == 'O'or\
board[4] == 'O' and board[5] == 'O' and board[6] == 'O'or\
board[7] == 'O' and board[8] == 'O' and board[9] == 'O'or\
board[1] == 'O' and board[5] == 'O' and board[9] == 'O'or\
board[3] == 'O' and board[5] == 'O' and board[7] == 'O':
return 'O'
if board[1] == 'X' and board[4] == 'X' and board[7] == 'X'or\
board[2] == 'X' and board[5] == 'X' and board[8] == 'X'or\
board[3] == 'X' and board[6] == 'X' and board[9] == 'X'or\
board[1] == 'X' and board[2] == 'X' and board[3] == 'X'or\
board[4] == 'X' and board[5] == 'X' and board[6] == 'X'or\
board[7] == 'X' and board[8] == 'X' and board[9] == 'X'or\
board[1] == 'X' and board[5] == 'X' and board[9] == 'X'or\
board[3] == 'X' and board[5] == 'X' and board[7] == 'X':
return 'X'
def boardisFree(board,step):
return board[int(step)] == ' '
board = [' ',]*10
printTable()
plear = 'X'
while True:
if plear == 'X':
step = input('X走,请输入(1~9):')
if boardisFree(board,step):
board[int(step)] = 'X'
plear = 'O'
#print(board)
printTable()
if(isWinner(board) == 'X'):
print('X玩家赢了')
answer = input('是否再来一局(N/Y):')
if answer.upper() == 'Y':
board = [' ',]*10
continue
if answer.upper() == 'N':
break
else:
print('该格子已有棋')
printTable()
continue
if plear == 'O':
step = input('O走,请输入(1~9):')
if boardisFree(board,step):
board[int(step)] = 'O'
plear = 'X'
#print(board)
printTable()
if(isWinner(board) == 'O'):
print('O玩家赢了')
answer = input('是否再来一局(N/Y):')
if answer.upper() == 'Y':
board = [' ',]*10
continue
if answer.upper() == 'N':
break
else:
print('该格子已有棋')
printTable()
continue
|