井字棋游戏

1.采用列表
2.玩家先出,电脑其后
3.电脑落子规则:(1)直接落子成功(2)阻止玩家赢(3)随机落子

import copy
import random


def showchessboard(chessboardnext):
    print(('       |       ' + '      |       ' + '      |'))
    print(str(chessboardnext[0]) + '      |      ' + str(chessboardnext[1]) + '      |      ' + str(
        chessboardnext[2]) + '      |')
    print(('       |       ' + '      |       ' + '      |'))
    print('———————————————————————————————————————')

    print(('       |       ' + '      |       ' + '      |'))
    print(str(chessboardnext[3]) + '      |      ' + str(chessboardnext[4]) + '      |      ' + str(
        chessboardnext[5]) + '      |')
    print(('       |       ' + '      |       ' + '      |'))
    print('———————————————————————————————————————')

    print(('       |       ' + '      |       ' + '      |'))
    print(str(chessboardnext[6]) + '      |      ' + str(chessboardnext[7]) + '      |      ' + str(
        chessboardnext[8]) + '      |')
    print(('       |       ' + '      |       ' + '      |'))

#判空
def isNuLL(chessboard,location):
    return chessboard[location]!='x'and chessboard[location]!='o'
#玩家落子
def playernow(chessboard):
    location=int(input("Please input the location you choose"))
    if isNuLL(chessboard,location):
        chessboard[location] = 'x'
    else:
        print('The location is not NULL,please choose another one')
        playernow(chessboard)

#电脑落子:直接落子获胜 阻止玩家落子 随机落子

#获取棋盘可落子位置
def locations(chessboard):
    locs=[]
    for i in range(0,9):
        if chessboard[i]!='x' and chessboard[i]!='o':
            locs.append(i)
    return locs
#落子是否可以直接赢
def isgetwin(chessboard):
    locs = locations(chessboard)
    for i in locs:
        chessboard[i]='o'
        if isWin(chessboard,'o')==1:
            print("The location computer choosed is " + str(i))
            return 1
        else:
            chessboard[i]=i
    return 0
#是否需要阻止玩家
def isstop(chessboard):
    locs = locations(chessboard)
    for j in locs:
        chessboardnow[j] = 'x'
        if isWin(chessboardnow, 'x') == 1:
            print("The location computer choosed is " + str(j))
            chessboardnow[j] = 'o'
            return 1
        else:
            chessboardnow[j] = j
    return 0



#电脑落子
def computernow(chessboard):
    locs = locations(chessboard)
    if isgetwin(chessboard)==0:
        if isstop(chessboard)==0:
            x = random.choice(locs)
            print("The location computer choosed is " + str(x))
            chessboard[x] = 'o'

#判赢
def isWin(chessboard,chess):
    if (chessboard[0]==chessboard[1]==chessboard[2]==chess) or (chessboard[3]==chessboard[4]==chessboard[5]==chess) or (chessboard[6]==chessboard[7]==chessboard[8]==chess) or (chessboard[0]==chessboard[3]==chessboard[6]==chess) or(chessboard[1]==chessboard[4]==chessboard[7]==chess) or (chessboard[2]==chessboard[5]==chessboard[8]==chess) or (chessboard[0]==chessboard[4]==chessboard[8]==chess) or (chessboard[2]==chessboard[4]==chessboard[6]==chess):
        # if chess=='x':
        #     print('Player Win')
        # else:
        #     print('Computer Win')
        return 1
    else:
        return 0




if __name__=='__main__':
    # 棋盘初始化
    chessboardnow = [0, 1, 2, 3, 4, 5, 6, 7, 8]
    chessboardnext = copy.deepcopy(chessboardnow)
    showchessboard(chessboardnext)
#先落子的最多下5次
    times=0
    lcs=locations(chessboardnow)
#玩家先落子
    while times<5:
        playernow(chessboardnow)
        times+=1
        chessboardnext = copy.deepcopy(chessboardnow)
        showchessboard(chessboardnext)
        if isWin(chessboardnow,'x')==1:
            print('Game Over,Player Win!')
            break
        elif times<5:
            computernow(chessboardnow)
            chessboardnext = copy.deepcopy(chessboardnow)
            showchessboard(chessboardnext)
            if isWin(chessboardnow,'o')==1:
                print('Game Over,Computer Win')
                break
        else:
            times=5
    if times==5 and isWin(chessboardnow,'x')!=1 :
        print('游戏结束,平局')
    # if lcs==[] and isWin(chessboardnow,'x')!=1 :
    #     print('游戏结束,平局')















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值