生成“A Life of Logic 逻辑人生”游戏终盘(类似数
游戏规则:
“逻辑生活”是一个基于逻辑的优雅拼图游戏,其目标是在满足三条简单规则的同时,以0和1填充图板:
1.没有3个相同的数字彼此相邻。
2.每行和每列必须具有相同数量的0和1。
3.没有相同的行或列。
游戏界面示例:
alifeoflogic.JPGPython生成终盘的完整代码:
去掉注释和空行,代码不足百行
修改该level值,可以穷举出不同级别(N x N)的终盘数据。
觉得代码有用,请点个赞,谢谢!
# -*- coding: utf-8 -*-
import math
import itertools as it
# 生成可能的行
def generatePossibleRow(level):
# 列表-元组
possibleRows = []
rows = list(it.product(range(2), repeat=level)) # 穷举二进制元组,例如:(0,0,0,0) ~ (1,1,1,1)
for row in rows:
if(matchRule1(row)): # 仅取符合规则1的元组
possibleRows.append(row)
return possibleRows
# 生成可能的组,将可能的行拆分到组(每个组的0、1数量相同)
def generatePossibleGroups(possibleRows, level):
possibleGroups = []
allGroups = [[]for i in range(level+1)] # 创建空的二维列表
for row in possibleRows:
allGroups[countNumber1(row)].append(row) # 按照1的数量,将可能的行进行分组
for group in allGroups:
if(len(group)>=level): # 去掉无效的分组
possibleGroups.append(group)
return possibleGroups
# 统计数字1出现的次数
def countNumber1(row):
resule = 0
for num in row:
if(num==1):
resule = resule+1
return resule
# 规则1:每行不能出现连续3个或以上相同的0或者1
def matchRule1(row):
result = True
sequential0 = 0
sequential1 = 0
for num in row:
if(num==0):
sequential0 = sequential0+1
sequential1 = 0
else:
sequential0 = 0
sequential1 = sequential1 + 1
if(sequential0>=3 or sequential1>=3):
result = False
break
return result
# 规则2:矩阵每列的0、1数量相同
def matchRule2(matrix, level):
result = True
columns = [[]for i in range(level)] # 创建空的二维列表
# 矩阵中列数据转为行数据
for row in matrix:
idx = 0
for binnum in row:
columns[idx].append(binnum)
idx = idx+1
# 统计每行1的数量
countResult = []
for column in columns:
result = 0
for binnum in column:
if(binnum==1):
result = result+1
countResult.append(result)
# 统计数据去重,结果大于1,不符合规则
if(len(set(countResult))>1):
result = False
return result
# 规则3:判断矩阵每列不能出现连续3个或以上相同的0或者1
def matchRule3(matrix, level):
result = True
columns = [[]for i in range(level)] # 创建空的二维列表
# 矩阵中列数据转为行数据
for row in matrix:
idx = 0
for binnum in row:
columns[idx].append(binnum)
idx = idx+1
# 判断每列是否出现连续3个或以上相同的0或者1
for column in columns:
if(not matchRule1(column)):
result = False
break
return result
if __name__ == "__main__":
level = 3
matchIdx = 0
# 可能的行
possibleRows = generatePossibleRow(level)
# 可能的分组
possibleGroups = generatePossibleGroups(possibleRows, level)
for group in possibleGroups:
groupIdx = possibleGroups.index(group)
# 迭代器,返回possibleGroups中所有长度为level的子序列
# matrixIdx = 0
for matrix in it.combinations(group, level):
# matrixIdx = matrixIdx+1
if(matchRule2(matrix, level)):
# 将矩阵变形(调整行的顺序,不会影响每行每列0、1的数量)
# variantIdx = 0
for variant in it.permutations(matrix, level):
# variantIdx = variantIdx+1
if(matchRule3(variant, level)):
# print ('Group Index:', groupIdx, '\r\nMatrix Index:', matrixIdx, '\r\nVariant Index:', variantIdx)
matchIdx = matchIdx+1
print('Match Index:', matchIdx)
for row in variant:
print (row)
level=3,穷举出3*3终盘数据
Match Index: 1
(0, 0, 1)
(0, 1, 0)
(1, 0, 0)
Match Index: 2
(0, 0, 1)
(1, 0, 0)
(0, 1, 0)
Match Index: 3
(0, 1, 0)
(0, 0, 1)
(1, 0, 0)
Match Index: 4
(0, 1, 0)
(1, 0, 0)
(0, 0, 1)
Match Index: 5
(1, 0, 0)
(0, 0, 1)
(0, 1, 0)
Match Index: 6
(1, 0, 0)
(0, 1, 0)
(0, 0, 1)
Match Index: 7
(0, 1, 1)
(1, 0, 1)
(1, 1, 0)
Match Index: 8
(0, 1, 1)
(1, 1, 0)
(1, 0, 1)
Match Index: 9
(1, 0, 1)
(0, 1, 1)
(1, 1, 0)
Match Index: 10
(1, 0, 1)
(1, 1, 0)
(0, 1, 1)
Match Index: 11
(1, 1, 0)
(0, 1, 1)
(1, 0, 1)
Match Index: 12
(1, 1, 0)
(1, 0, 1)
(0, 1, 1)
level=4,穷举出4*4终盘数据
Match Index: 1
(0, 0, 1, 1)
(0, 1, 0, 1)
(1, 0, 1, 0)
(1, 1, 0, 0)
Match Index: 2
(0, 0, 1, 1)
(0, 1, 0, 1)
(1, 1, 0, 0)
(1, 0, 1, 0)
Match Index: 3
(0, 0, 1, 1)
(1, 0, 1, 0)
(0, 1, 0, 1)
(1, 1, 0, 0)
Match Index: 4
(0, 0, 1, 1)
(1, 0, 1, 0)
(1, 1, 0, 0)
(0, 1, 0, 1)
Match Index: 5
(0, 0, 1, 1)
(1, 1, 0, 0)
(0, 1, 0, 1)
(1, 0, 1, 0)
Match Index: 6
(0, 0, 1, 1)
(1, 1, 0, 0)
(1, 0, 1, 0)
(0, 1, 0, 1)
Match Index: 7
(0, 1, 0, 1)
(0, 0, 1, 1)
(1, 0, 1, 0)
(1, 1, 0, 0)
Match Index: 8
(0, 1, 0, 1)
(0, 0, 1, 1)
(1, 1, 0, 0)
(1, 0, 1, 0)
Match Index: 9
(0, 1, 0, 1)
(1, 0, 1, 0)
(0, 0, 1, 1)
(1, 1, 0, 0)
Match Index: 10
(0, 1, 0, 1)
(1, 0, 1, 0)
(1, 1, 0, 0)
(0, 0, 1, 1)
Match Index: 11
(0, 1, 0, 1)
(1, 1, 0, 0)
(0, 0, 1, 1)
(1, 0, 1, 0)
Match Index: 12
(0, 1, 0, 1)
(1, 1, 0, 0)
(1, 0, 1, 0)
(0, 0, 1, 1)
Match Index: 13
(1, 0, 1, 0)
(0, 0, 1, 1)
(0, 1, 0, 1)
(1, 1, 0, 0)
Match Index: 14
(1, 0, 1, 0)
(0, 0, 1, 1)
(1, 1, 0, 0)
(0, 1, 0, 1)
Match Index: 15
(1, 0, 1, 0)
(0, 1, 0, 1)
(0, 0, 1, 1)
(1, 1, 0, 0)
Match Index: 16
(1, 0, 1, 0)
(0, 1, 0, 1)
(1, 1, 0, 0)
(0, 0, 1, 1)
Match Index: 17
(1, 0, 1, 0)
(1, 1, 0, 0)
(0, 0, 1, 1)
(0, 1, 0, 1)
Match Index: 18
(1, 0, 1, 0)
(1, 1, 0, 0)
(0, 1, 0, 1)
(0, 0, 1, 1)
Match Index: 19
(1, 1, 0, 0)
(0, 0, 1, 1)
(0, 1, 0, 1)
(1, 0, 1, 0)
Match Index: 20
(1, 1, 0, 0)
(0, 0, 1, 1)
(1, 0, 1, 0)
(0, 1, 0, 1)
Match Index: 21
(1, 1, 0, 0)
(0, 1, 0, 1)
(0, 0, 1, 1)
(1, 0, 1, 0)
Match Index: 22
(1, 1, 0, 0)
(0, 1, 0, 1)
(1, 0, 1, 0)
(0, 0, 1, 1)
Match Index: 23
(1, 1, 0, 0)
(1, 0, 1, 0)
(0, 0, 1, 1)
(0, 1, 0, 1)
Match Index: 24
(1, 1, 0, 0)
(1, 0, 1, 0)
(0, 1, 0, 1)
(0, 0, 1, 1)
Match Index: 25
(0, 0, 1, 1)
(0, 1, 1, 0)
(1, 0, 0, 1)
(1, 1, 0, 0)
Match Index: 26
(0, 0, 1, 1)
(0, 1, 1, 0)
(1, 1, 0, 0)
(1, 0, 0, 1)
Match Index: 27
(0, 0, 1, 1)
(1, 0, 0, 1)
(0, 1, 1, 0)
(1, 1, 0, 0)
Match Index: 28
(0, 0, 1, 1)
(1, 0, 0, 1)
(1, 1, 0, 0)
(0, 1, 1, 0)
Match Index: 29
(0, 0, 1, 1)
(1, 1, 0, 0)
(0, 1, 1, 0)
(1, 0, 0, 1)
Match Index: 30
(0, 0, 1, 1)
(1, 1, 0, 0)
(1, 0, 0, 1)
(0, 1, 1, 0)
Match Index: 31
(0, 1, 1, 0)
(0, 0, 1, 1)
(1, 0, 0, 1)
(1, 1, 0, 0)
Match Index: 32
(0, 1, 1, 0)
(0, 0, 1, 1)
(1, 1, 0, 0)
(1, 0, 0, 1)
Match Index: 33
(0, 1, 1, 0)
(1, 0, 0, 1)
(0, 0, 1, 1)
(1, 1, 0, 0)
Match Index: 34
(0, 1, 1, 0)
(1, 0, 0, 1)
(1, 1, 0, 0)
(0, 0, 1, 1)
Match Index: 35
(0, 1, 1, 0)
(1, 1, 0, 0)
(0, 0, 1, 1)
(1, 0, 0, 1)
Match Index: 36
(0, 1, 1, 0)
(1, 1, 0, 0)
(1, 0, 0, 1)
(0, 0, 1, 1)
Match Index: 37
(1, 0, 0, 1)
(0, 0, 1, 1)
(0, 1, 1, 0)
(1, 1, 0, 0)
Match Index: 38
(1, 0, 0, 1)
(0, 0, 1, 1)
(1, 1, 0, 0)
(0, 1, 1, 0)
Match Index: 39
(1, 0, 0, 1)
(0, 1, 1, 0)
(0, 0, 1, 1)
(1, 1, 0, 0)
Match Index: 40
(1, 0, 0, 1)
(0, 1, 1, 0)
(1, 1, 0, 0)
(0, 0, 1, 1)
Match Index: 41
(1, 0, 0, 1)
(1, 1, 0, 0)
(0, 0, 1, 1)
(0, 1, 1, 0)
Match Index: 42
(1, 0, 0, 1)
(1, 1, 0, 0)
(0, 1, 1, 0)
(0, 0, 1, 1)
Match Index: 43
(1, 1, 0, 0)
(0, 0, 1, 1)
(0, 1, 1, 0)
(1, 0, 0, 1)
Match Index: 44
(1, 1, 0, 0)
(0, 0, 1, 1)
(1, 0, 0, 1)
(0, 1, 1, 0)
Match Index: 45
(1, 1, 0, 0)
(0, 1, 1, 0)
(0, 0, 1, 1)
(1, 0, 0, 1)
Match Index: 46
(1, 1, 0, 0)
(0, 1, 1, 0)
(1, 0, 0, 1)
(0, 0, 1, 1)
Match Index: 47
(1, 1, 0, 0)
(1, 0, 0, 1)
(0, 0, 1, 1)
(0, 1, 1, 0)
Match Index: 48
(1, 1, 0, 0)
(1, 0, 0, 1)
(0, 1, 1, 0)
(0, 0, 1, 1)
Match Index: 49
(0, 1, 0, 1)
(0, 1, 1, 0)
(1, 0, 0, 1)
(1, 0, 1, 0)
Match Index: 50
(0, 1, 0, 1)
(0, 1, 1, 0)
(1, 0, 1, 0)
(1, 0, 0, 1)
Match Index: 51
(0, 1, 0, 1)
(1, 0, 0, 1)
(0, 1, 1, 0)
(1, 0, 1, 0)
Match Index: 52
(0, 1, 0, 1)
(1, 0, 0, 1)
(1, 0, 1, 0)
(0, 1, 1, 0)
Match Index: 53
(0, 1, 0, 1)
(1, 0, 1, 0)
(0, 1, 1, 0)
(1, 0, 0, 1)
Match Index: 54
(0, 1, 0, 1)
(1, 0, 1, 0)
(1, 0, 0, 1)
(0, 1, 1, 0)
Match Index: 55
(0, 1, 1, 0)
(0, 1, 0, 1)
(1, 0, 0, 1)
(1, 0, 1, 0)
Match Index: 56
(0, 1, 1, 0)
(0, 1, 0, 1)
(1, 0, 1, 0)
(1, 0, 0, 1)
Match Index: 57
(0, 1, 1, 0)
(1, 0, 0, 1)
(0, 1, 0, 1)
(1, 0, 1, 0)
Match Index: 58
(0, 1, 1, 0)
(1, 0, 0, 1)
(1, 0, 1, 0)
(0, 1, 0, 1)
Match Index: 59
(0, 1, 1, 0)
(1, 0, 1, 0)
(0, 1, 0, 1)
(1, 0, 0, 1)
Match Index: 60
(0, 1, 1, 0)
(1, 0, 1, 0)
(1, 0, 0, 1)
(0, 1, 0, 1)
Match Index: 61
(1, 0, 0, 1)
(0, 1, 0, 1)
(0, 1, 1, 0)
(1, 0, 1, 0)
Match Index: 62
(1, 0, 0, 1)
(0, 1, 0, 1)
(1, 0, 1, 0)
(0, 1, 1, 0)
Match Index: 63
(1, 0, 0, 1)
(0, 1, 1, 0)
(0, 1, 0, 1)
(1, 0, 1, 0)
Match Index: 64
(1, 0, 0, 1)
(0, 1, 1, 0)
(1, 0, 1, 0)
(0, 1, 0, 1)
Match Index: 65
(1, 0, 0, 1)
(1, 0, 1, 0)
(0, 1, 0, 1)
(0, 1, 1, 0)
Match Index: 66
(1, 0, 0, 1)
(1, 0, 1, 0)
(0, 1, 1, 0)
(0, 1, 0, 1)
Match Index: 67
(1, 0, 1, 0)
(0, 1, 0, 1)
(0, 1, 1, 0)
(1, 0, 0, 1)
Match Index: 68
(1, 0, 1, 0)
(0, 1, 0, 1)
(1, 0, 0, 1)
(0, 1, 1, 0)
Match Index: 69
(1, 0, 1, 0)
(0, 1, 1, 0)
(0, 1, 0, 1)
(1, 0, 0, 1)
Match Index: 70
(1, 0, 1, 0)
(0, 1, 1, 0)
(1, 0, 0, 1)
(0, 1, 0, 1)
Match Index: 71
(1, 0, 1, 0)
(1, 0, 0, 1)
(0, 1, 0, 1)
(0, 1, 1, 0)
Match Index: 72
(1, 0, 1, 0)
(1, 0, 0, 1)
(0, 1, 1, 0)
(0, 1, 0, 1)