[python-chinese] 一个计算24点(或n点)的游戏.

-- Zoom.Quiet [2004-08-12 16:39:28]

Contents

# hoxide

## 精解

```   1 funs = [ lambda x, item: (x+item[0],
2                                str(x)+'+('+item[1]+')'
3                               ),
4       lambda x, item: (x-item[0],
5                                str(x)+'-('+item[1]+')'
6                               ),
7       lambda x, item: (item[0]-x,
8                                '('+item[1]+')-'+str(x)
9                               ),
10       lambda x, item: (x*item[0],
11                                str(x)+'*('+item[1]+')'
12                               ),
13       lambda x, item:   (item[0]==0 and (0,'ZZZ')) or \
14                         (x/item[0],
15                                str(x)+'/('+item[1]+')'
16                               ),
17       lambda x, item:   (x==0 and (0,'ZZZ')) or \
18                         (item[0]/x,
19                                '('+item[1]+')/'+str(x)
20                               )
21 ]
22
23 def con(num):
24     l = len(num)
25     p = list()
26     if l==1: return {num[0]:str(num[0])}
27     for i in range(l):
28         for f in funs:
29             p += map(lambda item: f(num[i],item),
30                        con(num[:i]+num[i+1:]).items()
31                     )
32     return dict(p)
33
34 print con(map(float,[1,5,6,7])).get(21.0,0)
```

# Chaox

```   1 from __future__ import division
2 from operator import add, sub, mul
3 from time import *
4
5
6
7 div = lambda x, y : x/y
8 ops = [add, sub, mul, div]
9 opsc = ['+','-','*','/']
10 equation = []
11
12 data = input('Pls input the four numbers:(separate them by comma)')
13
14 xtime = time()
15
16 #--------------------------------------------------------
17 # list all the sort according to the four numbers, it is
18 # copied from AllStartFromGame at http://python.cn
19 #--------------------------------------------------------
20 def permute(seq):
21   l = len(seq)
22   if l == 1:
23     return [seq]
24   else:
25     res=[]
26     for i in range(len(seq)):
27       rest = seq[:i] + seq[i+1:]
28       for x in permute(rest):
29         res.append(seq[i:i+1] + x)
30     return res
31
32 listofall = permute(data)
33 newlist = list(set(listofall))
34
35
36 #--------------------------------------------------------
37 # consider the five different structures of combinations
38 #--------------------------------------------------------
39 for a in newlist:
40     for i in range(4):
41         for j in range(4):
42             for k in range(4):
43                 express = []
44                 value = (a[0],opsc[i],a[1],opsc[j],a[2],opsc[k],a[3])
45                 express.append("((%d %s %d) %s %d) %s %d" % value)
46                 express.append("(%d %s (%d %s %d)) %s %d" % value)
47                 express.append("%d %s ((%d %s %d) %s %d)" % value)
48                 express.append("%d %s (%d %s (%d %s %d))" % value)
49                 express.append("(%d %s %d) %s (%d %s %d)" % value)
50                 for s in express:
51                     try:
52                         x = eval(s)
53                     except:
54                         continue
55                     if x < 24.00001 and x > 23.99999:
56                         equation.append(s)
57
58 ytime = time()
59 cacltime = ytime - xtime
60
61 equation = list(set(equation))
62 for e in equation:
63     print e
64
65 print "there are %d" % len(equation), "items at all"
66 print "all the time of calculation is %s s" % cacltime
```

## 实例

Chaox底部有个用pygame实现的24点游戏的实例

### 讨论

• 24pointHowto -- 关于二十四点游戏的编程思路与基本算法

• 网络社团--技术文档--算24点程序：原理，面向过程的C实现，面向对象的

• 但是 Python 中的 map lambda filter 等等函式编程的支持,非常不习惯,怎么没有办法看明白!可以比较通俗的讲解一下子? 加些"猪屎吧!" -- Zoomq
• 来个24pointAandQ吧.

# Alex Dong

## 思路

• １．对于n个数，从中间任意取出2个数。
• ２．对这两个数进行计算，把结果存放回原来的数中，现在就有n-1个数。
• 反复循环1,2两步操作，直到最终只剩下2个数。
• 在递归的过程中需要记录两个东西：
• 现有的计算和组合顺序。
• 未进行的计算顺序，以便于下次穷举。

# monkeyXite

PyProgramGames/24point (last edited 2009-12-25 07:18:56 by localhost)

• Page.execute = 2.119s
• getACL = 0.539s
• init = 0.029s