• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python实现井字棋小游戏代码

python 海叔叔 4年前 (2021-12-04) 39次浏览 已收录 0个评论

关键词:python,游戏代码,python游戏代码,python小游戏代码,井字棋

python实现井字棋小游戏代码

来源搞代码网《python实现井字棋小游戏代码》http://www.gaodaima.com/68534.html

#构建棋盘
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()

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python实现井字棋小游戏代码
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址