《Python游戏编程快速上手》第十章TicTacToe

《Python游戏编程快速上手》第九章的内容是对上一个游戏进行了些小拓展,所以我就不写博客了,非常简单大家自己去看吧。
所以我们今天看下第十章,第十章主要是讲了一个叫TicTacToe的游戏,这个游戏其实就是大家玩过的OX棋。如图:
《Python游戏编程快速上手》第十章TicTacToe_第1张图片

执O先行,要尽量使自己先连成一条线,并尽可能阻拦对方连成一条线。
规则很简单,说实话我的这个游戏的代码有点乱,但我暂时不想去调整它,所以大家将就看下就好,有问题随时欢迎提出:

import random

def chooseXO():
    while True:
        print("Do you want to be X or O?")
        player = input()
        if player is 'X':
            return ['X', 'O']
        elif player is 'O':
            return ['O', 'X']
        else:
            print("Please input correcter character.")


def showTable(table):
    print(table[0] + '|' + table[1] + '|' + table[2])
    print("-+-+-")
    print(table[3] + '|' + table[4] + '|' + table[5])
    print("-+-+-")
    print(table[6] + '|' + table[7] + '|' + table[8])


def judgeWinner(table):
    for i in range(3):
        if table[i*3 + 0] == table[i*3 + 1] == table[i*3 + 2] != ' ':
            return 1
        if table[i] == table[i + 3] == table[i + 6] != ' ':
            return 1
    if table[0] == table[4] == table[8] != ' ':
        return 1
    if table[2] == table[4] == table[6] != ' ':
        return 1
    if ' ' not in table:
        return -1
    return 0


def playerPos(table, player):
    while True:
        print("Please input a position.")
        p = int(input())
        if p not in range(1, 10) or table[p - 1] != ' ':
            print("You can only input between 1 and 9")
        else:
            #showTable(table)
            table[p - 1] = player
            return


def computePlayer(table, computer):
    closeWin = [[computer, computer, ' '],[computer, ' ', computer], [' ', computer, computer]]
    for i in range(3):
        if table[i*3 + 0:i*3+2]  == closeWin[0]:
            return i+2+1
        elif table[i*3 + 0:i*3+2]  == closeWin[1]:
            return i+1+1
        elif table[i*3 + 0:i*3+2]  == closeWin[2]:
            return i+1
        if [table[i + 0],table[i+3], table[i+6]]  == closeWin[0]:
            return i+6+1
        elif [table[i + 0],table[i+3], table[i+6]]  == closeWin[1]:
            return i+3+1
        elif [table[i + 0],table[i+3], table[i+6]]  == closeWin[2]:
            return i+1
    if [table[0], table[4], table[8]] == closeWin[0]:
        return 8+1
    elif [table[0], table[4], table[8]] == closeWin[1]:
        return 4+1
    elif [table[0], table[4], table[8]] == closeWin[2]:
        return 0+1
    if [table[2], table[4], table[6]] == closeWin[0]:
        return 6+1
    elif [table[2], table[4], table[6]] == closeWin[1]:
        return 4+1
    elif [table[2], table[4], table[6]] == closeWin[2]:
        return 2+1
    first = [i for i in [0, 2, 6, 8] if table[i] == ' ']
    if len(first) != 0:
        return random.choice(first)+1
    if table[4] == ' ':
        return 4+1
    else:
        third = [i for i in [1, 3, 5, 7] if table[i] == ' ']
        return random.choice(third)+1


def printResult(res, who):
    if res == 1:
        print(who + " win!")
    elif res == -1:
        print("There is no block.No winner!")


def main():
    print("Welcome !")
    print("This game 'O' is first.")
    character = chooseXO()
    player = character[0]
    computer = character[1]
    table = [' ' for i in range(9)]
    showTable(table)
    if "O" == player:
        print("You are the first.")
        playerPos(table, player)
    else:
        print("Computer is first.")

    while (True):
        print("Computer position.")
        compute = computePlayer(table, computer)
        table[compute - 1] = computer
        showTable(table)
        res = judgeWinner(table)
        if res != 0:
            printResult(res, 'Computer')
            break
        print("You turn.")
        playerPos(table, player)
        showTable(table)
        res = judgeWinner(table)
        if res != 0:
            printResult(res, 'You')
            break


if __name__ == '__main__':
    while True:
        main()
        print("Do you want try again?y or n")
        if input() == 'n':
            break

这个的代码质量真心不高,以后有时间再改吧。(挖坑中。。。)


加油啊,少年!

你可能感兴趣的:(python游戏)