# -*- coding:utf-8 -*-
import time
import random
#井字棋 人机对战
def drawBoard(board):
blank_board = '| '*3+'|'
edge_board = '+-----'*3+'+'
def drawLine(board_line):
insert_sym = '|'
print blank_board
print "|%3s%3s%3s%3s%3s |" % (board_line[0],insert_sym,board_line[1],insert_sym,board_line[2])
print blank_board
print edge_board
print edge_board
drawLine(board[7:10])
drawLine(board[4:7])
drawLine(board[1:4])
def inputPlayerLetter():
letter = ''
while not (letter == 'X' or letter =='O'):
print 'you want use X or O?'
letter = raw_input().upper()
if letter =='X':
return ['X','O']
else:
return ['O','X']
def playerMove(board,letter):
move=' '
while move not in '1 2 3 4 5 6 7 8 9'.split():
print 'you next chioes(1-9)'
move = raw_input()
try:
if not isSpaceFree(board,int(move)):
print 'There is already a pawn on this board'
move = ''
continue
except:
print 'Enter a non-compliant rule, enter a valid number (1-9)'
continue
board[int(move)] = letter
return isWinner(board,letter)
def computerMove(board,letter):
print 'wait......'
while True:
move = random.randint(1,9)
if not isSpaceFree(board,move):
move = 0
continue
else:
break
board[int(move)] = letter
return isWinner(board,letter)
def playAgain():
#startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False
print 'Do you want play again? (yes or no)'
return raw_input().lower().startswith('y')
def isWinner(bo,le):
#有连成线的返回True
return ((bo[7]==le and bo[8]==le and bo[9]==le)or
(bo[4]==le and bo[5]==le and bo[6]==le)or
(bo[1]==le and bo[2]==le and bo[3]==le)or
(bo[7]==le and bo[4]==le and bo[1]==le)or
(bo[8]==le and bo[5]==le and bo[2]==le)or
(bo[9]==le and bo[6]==le and bo[3]==le)or
(bo[7]==le and bo[5]==le and bo[3]==le)or
(bo[9]==le and bo[5]==le and bo[1]==le))
def isSpaceFree(board,move):
#传入的board[move]的值不是'X','O'返回True
return board[move] == ' ' or board[move] in '1 2 3 4 5 6 7 8 9'.split()
def isBoardFull(board):
#board 中的的值有不是'X','O'的 返回True
for i in range(1,10):
if isSpaceFree(board,i):
return False
return True
if __name__=='__main__':
print 'welcome to this game'
while True:
theBoard = '0 1 2 3 4 5 6 7 8 9'.split()
playerLetter,computerLetter = inputPlayerLetter()
letter = (playerLetter,computerLetter)
turn = 0
gameIsPlaying = True
while gameIsPlaying:
drawBoard(theBoard)
print 'go'
if playerMove(theBoard,playerLetter):
drawBoard(theBoard)
print ' you are winner'
break
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print 'Game over '
break
drawBoard(theBoard)
if computerMove(theBoard,computerLetter):
drawBoard(theBoard)
print ' computer is winner'
break
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print 'Game over '
break
if not playAgain():
break
# -*- coding:utf-8 -*-
#井字棋 玩家对战
def drawBoard(board):
blank_board = '| '*3+'|'
edge_board = '+-----'*3+'+'
def drawLine(board_line):
insert_sym = '|'
print blank_board
print "|%3s%3s%3s%3s%3s |" % (board_line[0],insert_sym,board_line[1],insert_sym,board_line[2])
print blank_board
print edge_board
print edge_board
drawLine(board[7:10])
drawLine(board[4:7])
drawLine(board[1:4])
def inputPlayerLetter():
letter = ''
while not (letter == 'X' or letter =='O'):
print 'you want use X or O?'
letter = raw_input().upper()
if letter =='X':
return ['X','O']
else:
return ['O','X']
def playerMove(board,letter):
move=' '
while move not in '1 2 3 4 5 6 7 8 9'.split():
print 'you next chioes(1-9)'
move = raw_input()
try:
if not isSpaceFree(board,int(move)):
print 'There is already a pawn on this board'
move = ''
continue
except:
print 'Enter a non-compliant rule, enter a valid number (1-9)'
continue
board[int(move)] = letter
return isWinner(board,letter)
def playAgain():
print 'Do you want play again? (yes or no)'
return raw_input().lower().startswith('y')
def isWinner(bo,le):
return ((bo[7]==le and bo[8]==le and bo[9]==le)or
(bo[4]==le and bo[5]==le and bo[6]==le)or
(bo[1]==le and bo[2]==le and bo[3]==le)or
(bo[7]==le and bo[4]==le and bo[1]==le)or
(bo[8]==le and bo[5]==le and bo[2]==le)or
(bo[9]==le and bo[6]==le and bo[3]==le)or
(bo[7]==le and bo[5]==le and bo[3]==le)or
(bo[9]==le and bo[5]==le and bo[1]==le))
def isSpaceFree(board,move):
return board[move] == ' ' or board[move] in '1 2 3 4 5 6 7 8 9'.split()
def isBoardFull(board):
for i in range(1,10):
if isSpaceFree(board,i):
return False
return True
if __name__=='__main__':
print 'welcome to this game'
while True:
theBoard = '0 1 2 3 4 5 6 7 8 9'.split()
player1Letter,player2Letter = inputPlayerLetter()
letter = (player1Letter,player2Letter)
print ' '+letter[0]+' to go first'
turn = 0
gameIsPlaying = True
while gameIsPlaying:
drawBoard(theBoard)
current_letter = letter[turn]
print ' '+current_letter+' to go'
if playerMove(theBoard,current_letter):
drawBoard(theBoard)
print ' '+current_letter+' is winner'
gameIsPlaying = False
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print 'Game over '
break
else:
turn = (turn+1)%2
if not playAgain():
break