python井字棋算法及代码井字棋盘看起来像⼀个⼤的井字符号(#),有9 个空格,可以包含X、O 或
关于落⼦问题
由于只能采⽤键盘输⼊,所以需要对棋盘进⾏坐标表⽰;
即直接⽤1-9个9个数字来表⽰位置,
7|8|9
-+-+-
4|5|6
-+-+-
1|2|3
其索引顺序与数字键盘上的数字键排列⼀致,下棋时看着数字键下,较为简便。
计算机的算法--寻最佳落⼦位置
⾸先简单的将棋盘划分为三个部分——中⼼(1),⾓(4),边(4)。
中⼼虽然只有⼀个但却不是最重要的,三个部分落⼦的优先顺序依次为:⾓、中⼼、边。
因此,井字棋的计算机算法计算最佳落⼦位置的顺序如下:
1 直接落⼦获胜
2 阻⽌玩家获胜
3 在⾓上落⼦
4 在中⼼落⼦
5 在边上落⼦
游戏流程
1、开始
2、选⼦ X或者O
3、随机先⼿
4、轮流下棋
5、是否分出胜负
5.1 分出胜负 跳到6
5.2 未分出胜负 跳到4
6、再来⼀局
6.1是, 跳到2
6.2否, 退出
游戏代码:
import random
def printBoard(borad):
print(borad[7] + '|' + borad[8] + '|' + borad[9])
print('-+-+-')
print(borad[4] + '|' + borad[5] + '|' + borad[6])
print('-+-+-')
print(borad[1] + '|' + borad[2] + '|' + borad[3])
'''printBoard 定义了棋盘打印输出函数
与数字键盘排列⼀致'''
def inputPlayerLetter():
'''#让玩家选择棋⼦
返回⼀个列表,显⽰玩家和电脑的棋⼦类型
'''
letter = ''
while not (letter == 'X' or letter == 'O'):
print('Do you want to be X or O?')
letter = input().upper()
if letter == 'X':
return ['X', 'O']
else:
return ['O', 'X']
def whoGoesFirst():
'''随机先⼿'''
if random.randint(0, 1) == 0:
return 'Computer'
else:
return 'Player'
def playAgain():
'''再玩⼀次?'''
print('Do you want to play again?(yes or no)')
return input().lower().startswith('y')
def makeMove(board, letter, move):
'''落⼦'''
board[move] = letter
def isWinner(board, occupy):
判断是否获胜
return ((board[1] == occupy and board[2] == occupy and board[3] == occupy) or        (board[4] == occupy and board[5] == occupy and board[6] == occupy) or        (board[7] == occupy and board[8] == occupy and board[9] == occupy) or        (board[1] == occupy and board[4] == occupy and board[7] == occupy) or        (board[2] == occupy and board[5] == occupy and board[8] == occupy) or        (board[3] == occupy and board[6] == occupy and board[9] == occupy) or        (board[1] == occupy and board[5] == occupy and board[9] == occupy) or        (board[3] == occupy and board[5] == occupy and board[7] == occupy))
def getBoardCopy(board):
复制⼀份棋盘给电脑落⼦使⽤
depuBoard = []
for i in board:
depuBoard.append(i)
return depuBoard
def isSpaceFree(board, move):
判断这个位置是否有⼦,⽆⼦返回True
return board[move] == ' '
def getPlayerMove(board):
move = ' '
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)): print('What is your next move?(1-9)')
move = input()
return int(move)
def choosePossibleMoverFromList(board, moveList):
随机返回⼀个可以落⼦的坐标,若⽆⼦可下,则返回None
possibleMoves = []
for i in moveList:
if isSpaceFree(board, i):
possibleMoves.append(i)
if len(possibleMoves) != 0:
return random.choice(possibleMoves)
else:
return None
def getComputerMove(board, computerLetter):
确定电脑的落⼦位置
if computerLetter == 'X':
playerLetter == 'O'
else:
playerLetter == 'X'
'''先判断电脑⽅能否通过⼀次落⼦直接获得游戏胜利'''
for i in range(1, 10):
copy = getBoardCopy(board)
if isSpaceFree(copy, i):
makeMove(copy, computerLetter, i)
if isWinner(copy, computerLetter):
return i
'''判断玩家下⼀次落⼦是否获胜,若能,则再该点落⼦'''
for i in range(1, 10):
copy = getBoardCopy(board)
if isSpaceFree(copy, i):
makeMove(copy, playerLetter, i)
if isWinner(copy, playerLetter):
return i
'''若⾓上能落⼦,则在⾓上落⼦'''
move = choosePossibleMoverFromList(board, [1, 3, 5, 7])
if move != None:
python新手代码及作用
return move
'''若中⼼能落⼦,则在中⼼落⼦'''
if isSpaceFree(board, 5):
return 5
'''若边上能落⼦,则在边上落⼦'''
return choosePossibleMoverFromList(board, [2, 4, 6, 8])
def isBoardFull(board):
''' 如果棋盘满了,返回True'''
for i in range(1, 10):
if isSpaceFree(board, i):
return False
return True
print('Welcome to the TicTacToe game!')
while True:
update board
theBoard = [' '] * 10
playerLetter, computerLetter = inputPlayerLetter()
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
gameIsPlaying = True
while gameIsPlaying:
if turn == 'Player':
# 玩家回合
printBoard(theBoard)
move = getPlayerMove(theBoard)
makeMove(theBoard, playerLetter, move)
if isWinner(theBoard, playerLetter):
printBoard(theBoard)
print('WowYou win the game')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
printBoard(theBoard)
print('The game is tie')
break
else:
turn = 'Computer'
else:
# 电脑回合
move = getComputerMove(theBoard, computerLetter)        makeMove(theBoard, computerLetter, move)
if isWinner(theBoard, computerLetter):
printBoard(theBoard)
print('Oh!,The computer win!,You lose.')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
printBoard(theBoard)
print('The game is tie')
break
else:
turn = 'Player'
if not playAgain():
break

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。