美文网首页Linux的小折腾
玩转Python字典、列表,Python版五子棋

玩转Python字典、列表,Python版五子棋

作者: 请叫我雯子小姐的小爷 | 来源:发表于2019-05-12 17:41 被阅读11次

本脚本没有使用任何Python的第三方模块,仅使用Python的字典以及列表完成。
脚本设计环境:Python3,Mac
注意:如需在Windows上运行,请做以下几处修改:

os.system('cls') #Windows上不识别clear,改为cls
'੦' --> 'O'  '◉'-->'E' #Windows上不能正确显示这两个符号,可以修改为你认为
draw_line = {} # 将init()的第一行改为这样,会取消列号,可以手动加在draw()模块中,否则会有行显示错乱现象

项目预览


image.png

设计思路,使用字典嵌套列表完成

draw_line = {'line0':[...],
             'line1':[...],
              ...
            }

主要包含6个主要模块,分别为

init() # 初始化棋盘及参数
change(x, y, res, make) #将棋子放于棋盘中
victory(res) #判断是否有一方胜利
ishave(x, y, res) #判断所下棋子位置是否已经有棋子
pvp(w, b) #人人对战主程序
draw(res,white,blank,number=0) # 将棋盘打印出来

主程序如下

# @Author : Rui Ma 
# @e-Mail : marui_06@outlook.com 
import os

def init():  ### use now
    draw_line = {'x':['1   ','2   ','3   ','4   ','5   ','6   ','7   ','8   ','9   ','10  ','11  ','12  ','13  ','14  ','15  ','16  ','17  ','18  ','19']}
    for i in range(37):
        if i % 2 == 0 or i == 37:
            x_line = []
            for j in range(37):
                if j % 2 == 0:
                    x_line.append('+')
                if j % 2 == 1:
                    x_line.append('---')
            x_line.append(' '+str(int(i/2+1)))
            draw_line[i]=x_line
        if i % 2 == 1:
            x_line = []
            for j in range(37):
                if j % 2 == 0:
                    x_line.append('|')
                if j % 2 == 1:
                    x_line.append('   ')
            draw_line[i]=x_line
    i = 0
    return i,draw_line

def draw(res,white,blank,number=0):
    os.system("clear")
    print("\n")
    if number%2 == 0:
        str1 = "--  第{0}手,{1} 出手  --".format(number+1,'੦')
    else:
        str1 = "--  第{0}手,{1} 出手  --".format(number+1,'◉')
    print(str1.center(68, '*'))
    for i in res:
        result = ''
        for j in res[i]:
            result += j
        print(result)
    strv = "'੦' 胜{0}局, '◉' 胜{1}局, 共{2}局".format(white,blank,white+blank)
    str2 = "--  'q' to quit  --"
    str3 = "--  输入两个数字,以空格分隔  --"
    print(str2.center(73, '*'))
    print("\n")
    print(strv.center(67,'*'))
    print(str3.center(61, '*'))
    print("\n")

def change(x, y, res, make):
    res[int(x)*2-2][int(y)*2-2]=make
    return res

def victory(res):
    col = [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28]
    def isvictory(a, b, c, d, e):
        if a != '+' and a != '|' and a == b and b == c and c == d and d == e:
            return 1
        else:
            return 0
    #横向判断
    for i in res:
        if i == 'x':
            pass
        else:
            for k in col:
                if isvictory(res[i][k],res[i][k+2],res[i][k+4],res[i][k+6],res[i][k+8]) == 1:
                    return 1
    #纵向判断
    row = [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36]
    for i in col:
        for j in row:
            if isvictory(res[i][j],res[i+2][j],res[i+4][j],res[i+6][j],res[i+8][j]) == 1:
                return 1
    #右斜方向判断
    for i in col:
        for j in col:
            if isvictory(res[i][j],res[i+2][j+2],res[i+4][j+4],res[i+6][j+6],res[i+8][j+8]) == 1:
                return 1
    #左斜方向判断
    col_z = [36,34,32,30,28,26,24,22,20,18,16,14,12,10,8]
    for i in col_z:
        for j in col_z:
            if isvictory(res[i][36-j],res[i-2][36-j+2],res[i-4][36-j+4],res[i-6][36-j+6],res[i-8][36-j+8]) == 1:
                return 1
def ishave(x, y, res):
    if res[int(x)*2-2][int(y)*2-2] == '+':
        return 1
    else:
        return 0
def pvp(w, b):
    i,res = init()
    draw(res,w,b)
    while True:
        try:
            y, x = input("输入坐标: ").split()
        except:
            os.system("clear")
            exit()
        if int(x) > 19 or int(y) > 19:
            print("超出棋盘范围,重新输入坐标")
        elif i % 2 == 0:
            if ishave(x,y,res) == 1:
                res = change(x,y,res,'੦')
                draw(res,w,b,number=i+1)
                i += 1
                if victory(res) == 1:
                    print('Game Over ੦ victory')
                    return 0
                    break
            else:
                print("已有棋子,重新输入坐标")
        else:
            if ishave(x,y,res) == 1:
                res = change(x,y,res,'◉')
                draw(res,w,b,number=i+1)
                i += 1
                if victory(res) == 1:
                    print('Game Over ◉ victory')
                    return 1
                    break
            else:
                print("已有棋子,重新输入坐标")

def main():
    os.system("clear")
    str1 = ' -- Choose Mode -- '
    str2 = ' 1 --   PvP   -- 1 '
    str3 = ' 2 --   PvE   -- 2 '
    print(str1.center(80, '*'))
    print(str2.center(80, '*'))
    print(str3.center(80, '*'))
    choose = input("\n'1' or '2': ")
    if int(choose) == 1:
        white, blank = 0, 0
        while True:
            result = pvp(white,blank)
            if result == 0:
                white += 1
                ifcontinue = input("是否继续(1: 是 2: 否): ")
                if int(ifcontinue) == 1 :
                    continue
                else:
                    break
            else:
                blank += 1
                ifcontinue = input("是否继续(1: 是 2: 否): ")
                if int(ifcontinue) == 1 :
                    continue
                else:
                    break
if __name__ == "__main__":
    main()

由于时间原因,目前只写了pvp部分,还没有加入人工智能算法,以后慢慢添加,欢迎提供意见或者建议

相关文章

  • 玩转Python字典、列表,Python版五子棋

    本脚本没有使用任何Python的第三方模块,仅使用Python的字典以及列表完成。脚本设计环境:Python3,M...

  • Python之列表、字典、元祖常见操作

    Python 将列表转为字典 Python元祖转为字典

  • Python notes(2/3)

    目录 一,python 字符串 二,Python列表(List) 三,Python 元组 四,Python字典 五...

  • Python入门——字典

    Python入门——字典 什么是字典 dictionary(字典)是除了列表以外Python中最灵活的数据类型。字...

  • 机器学习 -- Python基础(三)列表-字典-元组-集合

    2.2 Python数据类型 - 列表 2.2 Python数据类型 - 字典 2.2 Python数据类型...

  • 字典

    字典的操作方法 字典是python中唯一的映射类型,字典也是python中出列表外,最灵活的内置数据结构类型.列表...

  • Python-07-16

    菜鸟学Python,Python的组合数据类型:列表——案例元组——拓展[dir/help]集合字典列表:...

  • 2018-06-29

    python学习 学习python字符串、列表、元组、字典、日期和时间模块

  • 列表,字典排序

    列表嵌套字典,根据字典某一key排序python sort、sorted高级排序技巧(key的使用)Python要...

  • python 字典相关操作

    1. python 在列表、字典、集合中筛选数据 列表:filter函数、列表解析 字典:字典解析 集合:集合解析...

网友评论

    本文标题:玩转Python字典、列表,Python版五子棋

    本文链接:https://www.haomeiwen.com/subject/wsocaqtx.html