2017-10-09 155 views
0

有一个有趣的纸牌游戏,我喜欢玩,当我与一群书呆子。Python卡牌游戏

规则很简单:一个人放下四张牌。在这个版本的游戏中,王室数量为10.游戏的重点是使用任何二元运算符来制作24。作为一个例子:

10,2,3,6

(10-6)*(2 * 3)

4 * 6

所以...为什么不用计算机来为我解决这个蛮横的问题?

from math import floor,ceil 

for i in range(1000): # not 10,000 yet. It would run too long. 
    one, two, three, four = i//(1000)+1,(i%1000)//100+1,(i%100)//10+1,i%10+1 
    operand = [int.__add__, 
       int.__sub__, 
       int.__mul__, 
       int.__truediv__, 
       int.__pow__, 
       int.__mod__, 
       int.__or__, # even though I'd have to be a pedant to use these, I'm still curious 
       int.__and__, 
       int.__xor__, 
       int.__lshift__, 
       int.__rshift__] 
    op_str = {int.__add__:"+", 
       int.__sub__:"-", 
       int.__mul__:"*", 
       int.__truediv__:"/", 
       int.__pow__:"**", 
       int.__mod__:"%", 
       int.__or__:"|", 
       int.__and__:"&", 
       int.__xor__:"^", 
       int.__lshift__:"<<", 
       int.__rshift__:">>"} 
    for j in range(pow(len(operand),3)): 
     try: 
      a = operand[j%len(operand)] 
      b = operand[(j%(len(operand)**2))//len(operand)] 
      c = operand[(j%(len(operand)**3))//(len(operand)**2)] 
      # serial 
      answer = a(one,b(two,c(three,four))) 
      if ceil(answer) == floor(answer) and answer == 24: 
       print(one, op_str[a], "(", two, op_str[b], "(", three, op_str[c], four, "))") 
       print(one, op_str[a], "(", two, op_str[b], "(", c(three, four), "))") 
       print(one, op_str[a], "(", b(two,c(three,four)), ")") 
       print(a(one,b(two,c(three,four)))) 
       continue 
      # tree 
      answer = c(a(one, two), b(three, four)) 
      if ceil(answer) == floor(answer) and answer == 24: 
       print("((", one, op_str[a], two, ")", op_str[b], "(", three, op_str[c], four, "))") 
       print("(", a(one, two), op_str[b], c(three, four), ")") 
       print(c(a(one, two), b(three, four))) 
       continue 
     except Exception: 
      pass # I just want to bypass any stupid problems with modulus, divide and the shifts 

除此之外,我越来越傻回答:

... 
((1 % 1) * (6 | 4)) 
(0 * 6) 
24 
((1 - 1) * (6 + 4)) 
(0 * 10) 
24 
... 

任何人看到这个问题?

回答

1

,我发现你的代码非常难以遵循,所以我想我会尝试更结构化的方法:

import operator as ops 
from itertools import permutations, combinations_with_replacement 

op = { symbol : getattr(ops, name) for name, symbol in dict(add  = '+', 
                  sub  = '-', 
                  mul  = '*', 
                  truediv = '/').items() } 
        # etc. I'll let you fill in the rest 

class Expression(object): 

    def __init__(self, opsymbol, left, right): 
     self.op  = op[opsymbol] 
     self.opsymbol = opsymbol 
     self.left  = left 
     self.right = right 

    def __repr__(self): 
     return "({left} {opsymbol} {right})".format(**vars(self)) 

    def eval(self): 
     left = evalexp(self.left) 
     right = evalexp(self.right) 
     return self.op(left, right) 

    def show(self): 
     return '{} = {}'.format(self, self.eval()) 

    def __hash__(self):  return hash(repr(self)) 
    def __eq__ (self, other): return repr(self) == repr(other) 

def evalexp(e): 
    return e.eval() if type(e) is Expression else e 

def search(*args, target=24): 
    assert len(args) == 4 
    found = set() 
    operators = tuple(combinations_with_replacement(op, 3)) 
    for a,b,c,d in permutations(args): 
     for op1, op2, op3 in operators: 
      for o1, o2, o3 in permutations((op1, op2, op3)): 

       t1 = Expression(o1, a, Expression(o2, b, Expression(o3, c, d))) 
       t2 = Expression(o1, a, Expression(o2, Expression(o3, b, c), d)) 
       t3 = Expression(o1, Expression(o2, a, b), Expression(o3, c, d)) 
       t4 = Expression(o1, Expression(o2, a, Expression(o3, b, c)), d) 
       t5 = Expression(o1, Expression(o2, Expression(o3, a, b), c), d) 

       for e in (t1, t2, t3, t4, t5): 
        try: 
         if e.eval() == target: 
          found.add(e) 
        except (ZeroDivisionError): 
         pass 
    return found 

found = search(2,3,6,10, target=24) 
for e in found: 
    print(e.show()) 

给出56行输出,包括

((10 - 6) * (2 * 3)) = 24 
((10 - (2 * 3)) * 6) = 24 
(((6 + 10) * 3)/2) = 24.0 
((2 + 10) * (6/3)) = 24.0 
(((10 + 6) * 3)/2) = 24.0 
((3/2) * (6 + 10)) = 24.0 
(3 * ((6 + 10)/2)) = 24.0 
((2 * (10 - 6)) * 3) = 24 
(6 * ((10 + 2)/3)) = 24.0 
((2 - 10) * (3 - 6)) = 24 
(2 * (3 * (10 - 6))) = 24 
2

这只是打印输出中的一些拼写错误。您将“树”计算定义为:

answer = c(a(one, two), b(three, four)) 

c作为最外面的函数。但是,然后用b作为最外层的功能打印它。这些行:

print("((", one, op_str[a], two, ")", op_str[b], "(", three, op_str[c], four, "))") 
print("(", a(one, two), op_str[b], c(three, four), ")") 

应改为:

print("((", one, op_str[a], two, ")", op_str[c], "(", three, op_str[b], four, "))") 
print("(", a(one, two), op_str[c], b(three, four), ")") 

你的答案将然后阅读:

... 
((1 % 1) | (6 * 4)) 
(0 | 24) 
24 
((1 - 1) + (6 * 4)) 
(0 + 24) 
24 
...