• 首页
  • 博客专栏
  • 博客专家
  • 行家
  • 观点
  • 排行榜
  • 我的博客
帮助 订阅 img博客搬家
{"code":1,"message":"","data":{"id":156212,"article":{"id":37954,"publicAccount":{"id":489,"name":"Python小屋","weixin":"Python_xiaowu","intro":"","body":"","image":"http://ss.csdn.net/p?http://wx.qlogo.cn/mmhead/Q3auHgzwzM4as06h7uCUfQNfUgu4tok4Xic3iccpEZpk998g96yE9EVw/0","href":null,"biz":"MzI4MzM2MDgyMQ==","category":{"id":10,"keyName":"sd","displayName":"软件研发","createdAt":1487578543000,"updatedAt":1487578543000,"status":1},"createdAt":1498098247000,"updatedAt":1498098247000,"status":1,"recommend":0,"qrcode":"http://mp.weixin.qq.com/mp/qrcode?scene=10000001&size=120&__biz=MzI4MzM2MDgyMQ==&mid=2247484509&idx=1&sn=92b40f3498e12426cab9e5b25021b9e8","articleCount":0,"viewCount":0},"category":{"id":10,"keyName":"sd","displayName":"软件研发","createdAt":1487578543000,"updatedAt":1487578543000,"status":1},"biz":"MzI4MzM2MDgyMQ==","mid":"2247484104","idx":1,"sn":"37d0eb3dd6ab53057e4fe9fb65aff8eb","author":"应根球,董付国","content":"","contentUrl":"http://mp.weixin.qq.com/s?__biz=MzI4MzM2MDgyMQ==&mid=2247484104&idx=1&sn=37d0eb3dd6ab53057e4fe9fb65aff8eb&chksm=eb8aa992dcfd208495c47b34692ed655110e10b5fc998d5a07070c1c29dcb9f9fa50492b6319&scene=27#wechat_redirect","cover":"http://mmbiz.qpic.cn/mmbiz/xXrickrc6JTObmS1MMjGXHygkQB8wFCYgHkriaEn0n08wPQ7IOZV7TdeQD7wWEj9Zia5QplTLicdFSJQp5QmPHj4eQ/0?wx_fmt=png","digest":"数字拼图游戏与拼图游戏原理一致,把打乱了的数字或图片经移动,拼成给定的目标数字或图片,其中总有一个空的地方,让相邻(上下左右)的方块移动,直至达到目标。","isMulti":0,"sourceUrl":"","title":"Python编写的数字拼图游戏(含爬山算法人机对战功能)","description":"数字拼图游戏与拼图游戏原理一致,把打乱了的数字或图片经移动,拼成给定的目标数字或图片,其中总有一个空的地方,让相邻(上下左右)的方块移动,直至达到目标。","datetime":1483948817000,"status":1,"viewCount":35,"praiseCount":0,"commentCount":0,"createdAt":1498098334000,"updatedAt":1498098334000,"origin":"proxy","recommend":0},"url":"http://mp.weixin.qq.com/s?__biz=MzI4MzM2MDgyMQ==&mid=2247484104&idx=1&sn=37d0eb3dd6ab53057e4fe9fb65aff8eb&chksm=eb8aa992dcfd208495c47b34692ed655110e10b5fc998d5a07070c1c29dcb9f9fa50492b6319&scene=27#wechat_redirect","title":"Python编写的数字拼图游戏(含爬山算法人机对战功能)","source":"<p style=\"text-indent: 2em; line-height: 2em;\">数字拼图游戏与拼图游戏原理一致,把打乱了的数字或图片经移动,拼成给定的目标数字或图片,其中总有一个空的地方,让相邻(上下左右)的方块移动,直至达到目标。</p> \n<p style=\"text-indent: 2em; line-height: 2em;\">游戏代码由浙江温州永嘉县教师发展中心<strong>应根球</strong>老师提供,我略做修改和优化。<br></p> \n<p style=\"text-indent: 2em; line-height: 2em;\">代码有点长,用手机阅读可能不太方便,可以复制地址到电脑上用浏览器查看。</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">import</span> random</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#显示数字拼图</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">disp</span>(s, d):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#s和d是两个数字字符串,把0换成空格,把数字摆放到指定位置</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; s = <span style=\"color: rgb(61, 167, 66);\">''</span>.join(s).replace(<span style=\"color: rgb(61, 167, 66);\">'0'</span>, <span style=\"color: rgb(61, 167, 66);\">' '</span>)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; d = <span style=\"color: rgb(61, 167, 66);\">''</span>.join(d).replace(<span style=\"color: rgb(61, 167, 66);\">'0'</span>, <span style=\"color: rgb(61, 167, 66);\">' '</span>)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">'''</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">+---+---+---+ &nbsp; &nbsp; &nbsp; &nbsp;+---+---+---+</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">| {0[0]} | {0[1]} | {0[2]} | &nbsp; &nbsp; &nbsp; &nbsp;| {1[0]} | {1[1]} | {1[2]} |</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">|---+---+---| &nbsp; &nbsp; &nbsp; &nbsp;|---+---+---|</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">| {0[3]} | {0[4]} | {0[5]} | &nbsp; ==&gt; &nbsp;| {1[3]} | {1[4]} | {1[5]} |</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">|---+---+---| &nbsp; &nbsp; &nbsp; &nbsp;|---+---+---|</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">| {0[6]} | {0[7]} | {0[8]} | &nbsp; &nbsp; &nbsp; &nbsp;| {1[6]} | {1[7]} | {1[8]} |</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">|---+---+---| &nbsp; &nbsp; &nbsp; &nbsp;|---+---+---|</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">'''</span>.format(s, d))</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#移动数字</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">move</span>(s, numstr):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> (numstr <span style=\"color: rgb(255, 169, 0);\">not in</span> <span style=\"color: rgb(61, 167, 66);\">\"12345678\"</span>) <span style=\"color: rgb(255, 169, 0);\">or</span> (<span style=\"color: rgb(255, 169, 0);\">not</span> numstr):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">return</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp;&nbsp;</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; t1 = s.index(<span style=\"color: rgb(61, 167, 66);\">'0'</span>)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; t2 = s.index(numstr)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#字符在字符串中的位置除3的商对应游戏图中的行下标</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">&nbsp; &nbsp; #除3的余数对应游戏图中的列下标</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; t = <span style=\"color: rgb(172, 57, 255);\">zip</span>(<span style=\"color: rgb(172, 57, 255);\">divmod</span>(t1,3), <span style=\"color: rgb(172, 57, 255);\">divmod</span>(t2,3))</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; t = ''.join([<span style=\"color: rgb(172, 57, 255);\">str</span>(<span style=\"color: rgb(172, 57, 255);\">abs</span>(i-j)) <span style=\"color: rgb(255, 169, 0);\">for</span> i,j <span style=\"color: rgb(255, 169, 0);\">in</span> t])</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#如果输入的数字与空格相邻则移动</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> t <span style=\"color: rgb(255, 169, 0);\">in</span> (<span style=\"color: rgb(61, 167, 66);\">'01'</span>,<span style=\"color: rgb(61, 167, 66);\">'10'</span>):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; s[t1], s[t2] = s[t2], s[t1]</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#获取空格周边可移动数字</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">getMoveable</span>(s):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#空格位置的行、列坐标</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; p, q = <span style=\"color: rgb(172, 57, 255);\">divmod</span>(s.index(<span style=\"color: rgb(61, 167, 66);\">'0'</span>), 3)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#空格上下的位置坐标</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; ls = [(p, i) <span style=\"color: rgb(255, 169, 0);\">for</span> i <span style=\"color: rgb(255, 169, 0);\">in</span> (q+1, q-1) <span style=\"color: rgb(255, 169, 0);\">if</span> i <span style=\"color: rgb(255, 169, 0);\">in</span> <span style=\"color: rgb(172, 57, 255);\">range</span>(3)]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#空给左右的位置坐标</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; ls += [(i, q) <span style=\"color: rgb(255, 169, 0);\">for</span> i <span style=\"color: rgb(255, 169, 0);\">in</span> (p+1, p-1) <span style=\"color: rgb(255, 169, 0);\">if</span> i <span style=\"color: rgb(255, 169, 0);\">in</span> <span style=\"color: rgb(172, 57, 255);\">range</span>(3)]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">return</span> ls</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#爬山算法状态计算函数,从当前状态s到终态d所需要的总步数</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">getInstance</span>(s,d):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#依次计算s和d中相同数字的距离,并求所有距离之和</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; sumi = 0</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">for</span> n <span style=\"color: rgb(255, 169, 0);\">in</span> <span style=\"color: rgb(61, 167, 66);\">'12345678'</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; t1 = <span style=\"color: rgb(172, 57, 255);\">divmod</span>(s.index(n), 3)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; t2 = <span style=\"color: rgb(172, 57, 255);\">divmod</span>(d.index(n), 3)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; sumi += <span style=\"color: rgb(172, 57, 255);\">abs</span>(t1[0]-t2[0]) + <span style=\"color: rgb(172, 57, 255);\">abs</span>(t1[1]-t2[1])</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">return</span> sumi</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp;&nbsp;</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#让机器根据与目标各数字差距之和的策略选定一个移动数字</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">choiceNum</span>(s, moveable, d):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; tmp = [100,<span style=\"color: rgb(61, 167, 66);\">'0'</span>]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">for</span> i <span style=\"color: rgb(255, 169, 0);\">in</span> moveable:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; s1 = s[::]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; s1[s1.index(<span style=\"color: rgb(61, 167, 66);\">'0'</span>)], s1[s1.index(i)] = i, <span style=\"color: rgb(61, 167, 66);\">'0'</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; getI = getInstance(s1, d)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> getI &lt; tmp[0]:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = getI, i</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">elif</span> getI == tmp[0]:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = getI, random.choice([i, tmp[1]])</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">return</span> tmp[1]</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#打乱数字拼图顺序,以得到初始状态</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">shufflemove</span>(d, times):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; s2 = d[::]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; mov = <span style=\"color: rgb(61, 167, 66);\">'0'</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">for</span> i <span style=\"color: rgb(255, 169, 0);\">in</span> range(times):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; mov1 = mov</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; mov = [s2[x[0]*3+x[1]] <span style=\"color: rgb(255, 169, 0);\">for</span> x <span style=\"color: rgb(255, 169, 0);\">in</span> getMoveable(s2)]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> mov1 <span style=\"color: rgb(255, 169, 0);\">in</span> mov:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mov.remove(mov1)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; mov = random.choice(mov)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; move(s2, mov)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">return</span> s2</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#做题,当who为computer时为计算机解题</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#当who为human,即非computer时,人工解题</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">def</span> <span style=\"color: rgb(0, 82, 255);\">do</span>(s, d, who):</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; s1 = s[::]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; num = <span style=\"color: rgb(61, 167, 66);\">''</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; bushu = 0</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp;<span style=\"color: rgb(255, 169, 0);\"> while True</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> who != <span style=\"color: rgb(61, 167, 66);\">'computer'</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; disp(s1,d)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#已成功、步数太大或人工放弃时返回</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> s1==d <span style=\"color: rgb(255, 169, 0);\">or</span> bushu&gt;=1000 <span style=\"color: rgb(255, 169, 0);\">or</span> num=='0':</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">return</span> bushu</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#确定可输入的数字</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; n_of_m = [s1[x[0]*3+x[1]] <span style=\"color: rgb(255, 169, 0);\">for</span> x <span style=\"color: rgb(255, 169, 0);\">in</span> getMoveable(s1)] &nbsp; &nbsp; &nbsp; &nbsp;</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> who == <span style=\"color: rgb(61, 167, 66);\">'computer'</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#机器答题不允许后退</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> num <span style=\"color: rgb(255, 169, 0);\">in</span> n_of_m:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n_of_m.remove(num)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = choiceNum(s, n_of_m, d)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">else</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#人工答题允许后退</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prompt = <span style=\"color: rgb(61, 167, 66);\">'第{0}步{1}(输入0退出)=&gt;'</span>.format(bushu, n_of_m)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num = <span style=\"color: rgb(172, 57, 255);\">input</span>(prompt)[0]</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#输入0步数设为999,视为主动放弃并退出</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 169, 0);\">if</span> num == <span style=\"color: rgb(61, 167, 66);\">'0'</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bushu = 999</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: rgb(255, 41, 65);\">#移动数字</span></p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; move(s1,num)</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; &nbsp; &nbsp; bushu += 1</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#主程序开始</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">'-'</span>*34)</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">'拼图游戏'</span>.center(34,<span style=\"color: rgb(61, 167, 66);\">'*'</span>))</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">'''</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">**玩法:左边的图通过移动空格相邻的数字到</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">空格处,最终得到右边的图,游戏即完成。只</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">要输入空格相邻的数字,该数字即被移到空格</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">处。=&gt;左边的数字为你已经移动的步数及你可</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">移动的数字。完成任务的步数越少,你的游戏</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">成绩越高。祝你幸运!</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(61, 167, 66);\">'''</span>)</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">'-'</span>*34)</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#为简化,设定固定数字目标,其中0显示为空方块</span></p> \n<p style=\"line-height: 1.5em;\">d = <span style=\"color: rgb(172, 57, 255);\">list</span>(<span style=\"color: rgb(61, 167, 66);\">'123804765'</span>)</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#为简化,让机器从目标开始随机逆移动,先只移动6步</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#也可以设置难度,移动步数越多,恢复越难。</span></p> \n<p style=\"line-height: 1.5em;\">s = shufflemove(d,6)</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#先让计算机用简易爬山算法去解题,由于爬山算法本身的原因,不一定能得到最优解</span></p> \n<p style=\"line-height: 1.5em;\">cpstep = do(s, d,&nbsp;<span style=\"color: rgb(61, 167, 66);\">'computer'</span>)<br></p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#显示开始与结束状态及机器解题情况</span></p> \n<p style=\"line-height: 1.5em;\">disp(s, d)</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">'这个题目机器用了{0}步!\\n'</span>.format(cpstep))</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">if</span> cpstep &gt; 1000:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">\"机器用了1000步还没解出,看你的了!\\n\"</span>)</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#用于计人工移动的步数</span></p> \n<p style=\"line-height: 1.5em;\">bushu = 0</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#人工解题开始</span></p> \n<p style=\"line-height: 1.5em;\">hmstep = do(s, d, <span style=\"color: rgb(61, 167, 66);\">'human'</span>)</p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 41, 65);\">#显示游戏结果</span></p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">if</span> hmstep &lt; cpstep:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">\"你胜利,真了不起!\"</span>)</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">elif</span> hmstep == cpstep:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">\"你与机器持平局,加油!\"</span>)</p> \n<p style=\"line-height: 1.5em;\"><span style=\"color: rgb(255, 169, 0);\">else</span>:</p> \n<p style=\"line-height: 1.5em;\">&nbsp; &nbsp; <span style=\"color: rgb(172, 57, 255);\">print</span>(<span style=\"color: rgb(61, 167, 66);\">\"哈,你输给了机器,这可是用爬山算法哦!\"</span>)</p> \n<p><br></p> \n<p><br></p> \n<p style=\"line-height: 1.5em;\"><strong>某次玩游戏的过程如下</strong>:</p> \n<p style=\"line-height: 1.5em;\"><br></p> \n<p style=\"line-height: 1.5em;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTOmasmWT8bTJq3jfCsIfbAZtnogCSEoRKz4PW60v1rWMXqYDAeNgdBcxaWl34Dn2CYQLGM13zzTug/0?wx_fmt=png\" data-ratio=\"1.85679012345679\" data-w=\"405\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTOmasmWT8bTJq3jfCsIfbAZtnogCSEoRKz4PW60v1rWMXqYDAeNgdBcxaWl34Dn2CYQLGM13zzTug/640?wx_fmt=png\"><br></p> \n<p style=\"line-height: 1.5em;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTOmasmWT8bTJq3jfCsIfbAZA3fbx5mI5u9v0j6SznSCQyQD7IpdRM5eNpDfWoibJaic83JmOJpushlA/0?wx_fmt=png\" data-ratio=\"1.4238578680203047\" data-w=\"394\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTOmasmWT8bTJq3jfCsIfbAZA3fbx5mI5u9v0j6SznSCQyQD7IpdRM5eNpDfWoibJaic83JmOJpushlA/640?wx_fmt=png\"><br></p> \n<p style=\"line-height: 1.5em;\"><img data-s=\"300,640\" data-type=\"png\" data-src=\"http://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTOmasmWT8bTJq3jfCsIfbAZ7S1ibUYOoqROFXBZuTjulGROEGkz3FMnd02zkqw9YVicYXp9wUFeQJxg/0?wx_fmt=png\" data-ratio=\"1.1484593837535013\" data-w=\"357\" src=\"http://ss.csdn.net/p?http://mmbiz.qpic.cn/mmbiz_png/xXrickrc6JTOmasmWT8bTJq3jfCsIfbAZ7S1ibUYOoqROFXBZuTjulGROEGkz3FMnd02zkqw9YVicYXp9wUFeQJxg/640?wx_fmt=png\"><br></p>","level":3,"createdAt":1498098334000,"updatedAt":1498098334000}}

原
查看原文>>
img 关注
声明:该内容由作者授权CSDN展示,内容版权归作者所有,如需转载请与作者联系。如有侵权,请联系admin@csdn.net删除。
我来说几句发表
            公众号