opencv3+python3.5成语填字游戏(三)成语填字游戏

2018-05-22  本文已影响0人  mler801

本篇介绍填字游戏解密算法,本算法尚且存在一些问题,并不适合所有成语填字游戏。

GitHub源代码下载

#寻找横向的四字成语方格
def findXFour(mi, i, j):
    if (j+1 == 10 or mi[i][j+1] == '0') and j-1>=0 and mi[i][j-1] != '0':
        return [[mi[i][j-3],mi[i][j-2],mi[i][j-1],mi[i][j]],
                [i,j-3,i,j-2,i,j-1,i,j]]
    elif (j+2 ==10 or (0 if j+2>10 else mi[i][j+2] == '0')) and j-1>=0 and mi[i][j-1] != '0':
        return [[mi[i][j-2],mi[i][j-1],mi[i][j],mi[i][j+1]],
                [i,j-2,i,j-1,i,j,i,j+1]]
    elif (j+3 ==10 or (0 if j+3>10 else mi[i][j+3] == '0')) and j-1>=0 and mi[i][j-1] != '0':
        return [[mi[i][j-1],mi[i][j],mi[i][j+1],mi[i][j+2]],
                [i,j-1,i,j,i,j+1,i,j+2]]
    elif (j+4 ==10 or (0 if j+4>10 else mi[i][j+4] == '0')) and (mi[i][j+1] != '0'):
        return [[mi[i][j],mi[i][j+1],mi[i][j+2],mi[i][j+3]],
                [i,j,i,j+1,i,j+2,i,j+3]]
    else:
        return []
#寻找纵向的四字成语方格
def findYFour(mi, i, j):
    if (i+1==10 or mi[i+1][j] == '0') and i-1>=0 and mi[i-1][j] != '0':
        return [[mi[i-3][j],mi[i-2][j],mi[i-1][j],mi[i][j]],
                [i-3,j,i-2,j,i-1,j,i,j]]
    elif (i+2==10 or (0 if i+2>10 else mi[i+2][j] == '0')) and i-1>=0 and mi[i-1][j] != '0':
        return [[mi[i-2][j],mi[i-1][j],mi[i][j],mi[i+1][j]],
                [i-2,j,i-1,j,i,j,i+1,j]]
    elif (i+3==10 or (0 if i+3>10 else mi[i+3][j] == '0')) and i-1>=0 and mi[i-1][j] != '0':
        return [[mi[i-1][j],mi[i][j],mi[i+1][j],mi[i+2][j]],
                [i-1,j,i,j,i+1,j,i+2,j]]
    elif (i+4==10 or (0 if i+4>10 else mi[i+4][j] == '0')) and (mi[i+1][j] == '0'):
        return [[mi[i][j],mi[i+1][j],mi[i+2][j],mi[i+3][j]],
                [i,j,i+1,j,i+2,j,i+3,j]]
    else:
        return []
#改变对应方格的原生成成语填字矩阵
def changeMi(res, micopy):
    counts= []
    for j in range(len(chengyus)):
        count = 0
        if res[0][0] == chengyus[j][0]:
            count += 1
        if res[0][1] == chengyus[j][1]:
            count += 1
        if res[0][2] == chengyus[j][2]:
            count += 1
        if res[0][3] == chengyus[j][3]:
            count += 1
        counts.append(count)
    m = counts.index(max(counts))    
    #print(max(counts))
    micopy[res[1][0]][res[1][1]] = chengyus[m][0]
    micopy[res[1][2]][res[1][3]] = chengyus[m][1]
    micopy[res[1][4]][res[1][5]] = chengyus[m][2]
    micopy[res[1][6]][res[1][7]] = chengyus[m][3]
    chengyus.remove([chengyus[m][0],chengyus[m][1],chengyus[m][2],chengyus[m][3]])
#判断res此四字成语是否在nodes里面存在    
def isExist(nodes,res):
    for i in range(len(nodes)):
        if(nodes[i][0][0] == res[0][0] and nodes[i][0][1] == res[0][1] 
           and nodes[i][0][2] == res[0][2] and nodes[i][0][3] == res[0][3]):
            return 1
    return 0
#实际的查找成语方格并修改原成语矩阵的函数
def getResult(mi, i,j, nodes, micopy):
    temp = findXFour(mi, i, j)
    res = (len(temp) != 0 and temp or findYFour(mi, i, j))
    
    if(len(res) > 0 and not isExist(nodes, res)):
        nodes.append(res)
        changeMi(res, micopy)
import copy
#总体的揭解谜函数    
def solve(mi):
    nodes = []
    micopy = copy.deepcopy(mi)  #对象拷贝,深拷贝
    for i in range(len(mi)):
        for j in range(len(mi[i])):
            if(mi[i][j] != '0'):
                getResult(mi, i,j, nodes, micopy)
   # print(nodes)
    return micopy
生成的填字游戏:


[['清', '1', '1', '1', '0', '0', '劳', '1', '1', '高'], 
 ['0', '0', '辨', '0', '0', '0', '0', '0', '0', '1'], 
 ['0', '0', '1', '0', '0', '0', '0', '0', '0', '1'], 
 ['1', '亲', '1', '1', '0', '0', '1', '如', '1', '水'], 
 ['0', '0', '0', '步', '1', '1', '花', '0', '0', '0'], 
 ['0', '0', '0', '1', '0', '0', '1', '0', '0', '0'], 
 ['比', '1', '可', '1', '0', '0', '1', '1', '归', '1'], 
 ['1', '0', '0', '0', '0', '0', '0', '0', '0', '1'], 
 ['1', '0', '0', '0', '0', '0', '0', '0', '0', '水'], 
 ['飞', '1', '1', '火', '0', '0', '1', '起', '1', '秀']]


求解后的填字游戏:


[['清', '风', '明', '月', '0', '0', '劳', '苦', '功', '高'], 
 ['0', '0', '辨', '0', '0', '0', '0', '0', '0', '山'], 
 ['0', '0', '是', '0', '0', '0', '0', '0', '0', '流'],
 ['非', '亲', '非', '故', '0', '0', '心', '如', '止', '水'], 
 ['0', '0', '0', '步', '步', '莲', '花', '0', '0', '0'], 
 ['0', '0', '0', '自', '0', '0', '怒', '0', '0', '0'], 
 ['比', '屋', '可', '封', '0', '0', '放', '虎', '归', '山'],
 ['翼', '0', '0', '0', '0', '0', '0', '0', '0', '清'], 
 ['双', '0', '0', '0', '0', '0', '0', '0', '0', '水'],
 ['飞', '蛾', '扑', '火', '0', '0', '后', '起', '之', '秀']]
image image
上一篇 下一篇

猜你喜欢

热点阅读