用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

井字棋

2020-10-07 作者: 毛豆举报

[python]代码库

[python]代码库
view source
print
?
#构建棋盘   来自whoami
import os
def buildLine(n):
    line=[]
    for i in range(2*n+1):
        line.append('-')
    return line
 
def buildBlanks(n):
    blanks=[]
    for i in range(n):
        blanks.append('|')
        blanks.append(' ')
    blanks.append('|')
    return blanks
 
def buildPlate(n):
    plate=[]
    for i in range(n):
        plate.append(buildLine(n))
        plate.append(buildBlanks(n))
    plate.append(buildLine(n))
    return plate
def show(plate):
    for row in plate:
        print(''.join(row))
#落子
def dropPiece(plate,row,col,piece):
    row=2*row-1
    col=col*2-1
    plate[row][col]=piece
#判断胜负
def judge(plate):
#判断是否有落子的空位,如果没有,则返回
#判断每一行字符都相同,有则返回该字符,从而判断胜负
    n=len(plate)
    for i in range(1,n-1,2):
        isEqua=True
        first=plate[i][1]
        for j in range(1,n-1,2):
            if plate[i][j] !=first:
                isEqua=False
                break
        if isEqua:
            return first
#判断每一列的字符都相同,有则返回该字符
    for i in range(1,n-1,2):
        isEqua=True
        first=plate[1][i]
        for j in range(1,n-1,2):
            if plate[j][i]!=first:
                isEqua=False
                break
        if isEqua:
                return first
#判断对角线的字符都相同,有则返回该字符
    isEqua = True
    for i in range(1,n-1,2):
        first=plate[1][1]
        if plate[i][i]!=first:
            isEqua=False
            break
    if isEqua:
        return first
    x=n-2
    y=1
    z=y
    first=plate[x][y]
    while True:
        x-=2
        y+=2
        if first!=plate[x][y]:
            isEqua=True
            break
        if x==z:
            break
    if not isEqua:
        return first
    # first=plate[5][1]
    # if plate[5][1]==plate[3][3]==plate[1][5]:
    #     return first
 
def main():
    i=1
    a = int(input("输入棋盘大小:"))
    plate = buildPlate(a)
    show(plate)
    m=len(plate)
    while True:
        piece = 'o'
        if i%2==0:
            piece = 'x'
        x=int(input("enter the location x:"))
        y = int(input("enter the location y:"))
        if not (0<=x<=a and 0<=y<=a):
            print("请输入0-"+str(a)+"以内的数")
            continue
        if plate[2*x-1][2*y-1]==' ':
            dropPiece(plate, x, y, piece)
        else:
            print("此位置已有棋子")
            continue
        i += 1
        show(plate)
        if judge(plate)=='o' or judge(plate)=='x':
            print("the winner is "+judge(plate))
            break
        s = 0
        for j in range(1, m - 1, 2):
            for k in range(1, m - 1, 2):
                if plate[j][k] == ' ':
                    s = s + 1
        if s == 0:
            print("平局")
            break
        windows: os.system('cls')
main()


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...