python——井字小游戲


import random def drawBoard(board): # 打印棋盤 
  
  
    # "board"是長度為10的列表,為了方便輸入,忽略第一個元素board[0] 
  
  
    print('\n\n\n\n') print('\t\t\t┌--─┬--─┬--─┐') print('\t\t\t│'+board[7]+''+board[8]+''+board[9]+'') print('\t\t\t├--─┼--─┼─--┤') print('\t\t\t│'+board[4]+''+board[5]+''+board[6]+'') print('\t\t\t├--─┼--─┼─--┤') print('\t\t\t│'+board[1]+''+board[2]+''+board[3]+'') print('\t\t\t└--─┴--─┴─--┘') 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(): # 再玩一次?輸入yes或y返回True 
    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(bo, le): # 判斷所給的棋子是否獲勝 
    # 參數為棋盤上的棋子(列表)和棋子符號 
    # 以下是所有可能勝利的情況,共8種 
    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 getBoardCopy(board): # 復制一份棋盤,供電腦落子時使用 
    dupeBoard = [] for i in board: dupeBoard.append(i) return dupeBoard 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 chooseRandomMoveFromList(board, movesList): # 隨機返回一個可以落子的坐標 
    # 如果沒有所給的movesList中沒有可以落子的,返回None 
    possibleMoves = [] for i in movesList: 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'  
  
  
    # Tic Tac Toe AI核心算法: 
    # 首先判斷電腦方能否通過一次落子直接獲得游戲勝利 
    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 = chooseRandomMoveFromList(board, [1, 3, 7, 9]) if move != None: return move # 如果能在中心落子的話,在中心落子 
    if isSpaceFree(board, 5): return 5  
  
  
    # 在邊上落子 
    return chooseRandomMoveFromList(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 Tic Tac Toe!') while True: # 更新棋盤 
    theBoard = [' '] * 10 playerLetter, computerLetter = inputPlayerLetter() turn = whoGoesFirst() print('The ' + turn + ' will go first.') gameIsPlaying = True while gameIsPlaying: if turn == 'player': # 玩家回合 
 drawBoard(theBoard) move = getPlayerMove(theBoard) makeMove(theBoard, playerLetter, move) if isWinner(theBoard, playerLetter): drawBoard(theBoard) print('Hooray! You have won the game!') gameIsPlaying = False else: if isBoardFull(theBoard): drawBoard(theBoard) print('The game is a tie!') break  
                else: turn = 'computer'  
  
  
        else: # 電腦回合 
            move = getComputerMove(theBoard, computerLetter) makeMove(theBoard, computerLetter, move) if isWinner(theBoard, computerLetter): drawBoard(theBoard) print('The computer has beaten you! You lose.') gameIsPlaying = False else: if isBoardFull(theBoard): drawBoard(theBoard) print('The game is a tie!') break  
                else: turn = 'player'  
  
  
    if not playAgain(): break 

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: