我正在尝试制作一个简单的单人纸牌,你可以随机获得随机数量的卡片堆。 heap1:4张牌,堆2张5张等等。 假设我们有四个值为4,5,7,1的值。 现在做的是从每个堆中删除一张卡以创建一个新卡。 所以下一步看起来像这样的4,5,7,1 - > 3,4,6,4。 当纸牌达到稳定数量时,纸牌已完成。 5,3,2 - > 4,2,1,3-> 3,1,2,4 ...现在,如果数字已经排序,我们将始终获得相同的四个值。
如何以最佳方式完成?我有一些编程基础知识但是 什么都没进展
到目前为止,我已经尝试了这个但是效果不好,我不知道如何制作循环 停在一个稳定的数字。
while len(y) > 1:
for i in range(len(y)):
y[i] -= 1
y = [x for x in y if x != 0]
y.append(len(y))
y.sort()
print(y)
答案 0 :(得分:2)
所以实际的卡片(颜色,面孔)根本不重要,因为你所有的模型都是很多堆?
使用列表进行设置,使用整数表示堆,使用random
模块生成初始配置。编程游戏逻辑应该是直截了当的。
更新
你的代码是一个好的开始。为了简化操作,请考虑将其分解为单独的功能。 E.g:
def next_configuration(y):
"""Take a configuration of heaps and remove one card from each
heap to create a new heap.
"""
new_y = [x - 1 for x in y if x > 1]
new_y.append(len(y))
new_y.sort(reverse=True)
return new_y
现在,您可以测试该函数是否执行了它应该执行的操作:
>>> next_configuration([5, 1, 3, 2])
[4, 4, 2, 1]
要将其播放到最后,请生成下一个配置,直到当前配置不再更改为止:
config = generate_initial_configuration()
for i in range(max_rounds):
new_config = next_configuration(config)
if new_config == config:
break
config = new_config
答案 1 :(得分:0)
这个怎么样:
def step(list):
new_list = [x-1 for x in list if (x-1) > 0] #You remove one from each heap
new_list.append(len(list)) #Now add the new heap
if sorted(new_list) == sorted(list):
return new_list, True
else:
return new_list, False
然后你可以调用类似的方法:
new_config, is_done = step(list)
直到is_done为真。我希望你理解我的想法。