设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1881|回复: 4
打印 上一主题 下一主题

[通用发布] 2048 on Ruby

[复制链接]

Lv1.梦旅人

梦石
0
星屑
66
在线时间
1641 小时
注册时间
2011-9-26
帖子
313
跳转到指定楼层
1
发表于 2014-4-11 22:17:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 Shy07 于 2015-8-24 20:46 编辑

2048 都知道吧,这游戏跟 TETRIS 一样戳中消除强迫症的弱点,玩起来根本停不下来

施工完毕,建议使用 Ace ,并开启控制台运行,当然原生 Ruby 更方便

20140416     修正输入方向后,数字没有移动时,会产生新数字的 bug

RUBY 代码复制
  1. =begin
  2. puts "#------------------------------#"
  3. puts "| Use your keyborad.           |"
  4. puts "| W: Up A:Left S:Down D:Right  |"
  5. puts "| Return: Next step            |"
  6. puts "#------------------------------#"
  7. puts "#------------------------------#"
  8. puts "|Score:        0000000000000000|"
  9. puts "#------------------------------#"
  10. puts "#------+------+------+------#"
  11. puts "|____2_|____2_|____2_|____2_|"
  12. puts "├------+------+------+------┤"
  13. puts "|____2_|____2_|____2_|____2_|"
  14. puts "├------+------+------+------┤"
  15. puts "|____2_|____2_|____2_|____2_|"
  16. puts "├------+------+------+------┤"
  17. puts "|_2048_|____2_|____2_|____2_|"
  18. puts "#------+------+------+------#"
  19. =end
  20.  
  21. $score = 0
  22. $changed = false
  23.  
  24. lines = []; 4.times { lines << [nil, nil, nil, nil] }
  25.  
  26. helper        = -> {
  27.                       puts "#------------------------------#"
  28.                       puts "| Use your keyborad.           |"
  29.                       puts "| W: Up A:Left S:Down D:Right  |"
  30.                       puts "| Return: Next step            |"
  31.                       puts "#------------------------------#"
  32.                     }
  33.  
  34. score         = -> {
  35.                       puts "#------------------------------#"
  36.                       puts "|Score:        #{"%016d" % $score}|"
  37.                       puts "#------------------------------#"
  38.                     }
  39.  
  40. header        = -> { puts "#------+------+------+------#" }
  41. block         = -> n { "_#{"_" * (4 - n.to_s.length)}#{n}_|" }
  42. liner         = -> a { puts a.inject("|") {|m, i| m + block.call(i) }}
  43. footer        = -> { puts "#------+------+------+------#" }
  44.  
  45. new_num = -> {
  46.                 loop do
  47.                   x, y = rand(4), rand(4)
  48.                   if lines[x][y].nil?
  49.                     lines[x][y] = rand < 0.8 ? 2 : 4
  50.                     break
  51.                   end
  52.                 end
  53.               }
  54.  
  55. start = -> { 2.times { new_num.call } }
  56.  
  57. restart = -> {
  58.                 loop do
  59.                   puts "Play again? (Y/N)"
  60.                   str = gets
  61.                   case str.rstrip.to_sym
  62.                   when :Y, :y
  63.                     lines.clear; 4.times { lines << [nil, nil, nil, nil] }
  64.                     $score = 0; new_num.call
  65.                     break
  66.                   when :N, :n; exit; end
  67.                 end
  68.               }
  69.  
  70. game_over     = -> {
  71.                       puts "#------------------------------#"
  72.                       puts "|          Game Over!          |"
  73.                       puts "#------------------------------#"
  74.                       restart.call
  75.                     }
  76.  
  77. congrulations = -> {
  78.                       puts "#------------------------------#"
  79.                       puts "|Congrulations!You've got 2048!|"
  80.                       puts "#------------------------------#"
  81.                       restart.call
  82.                     }
  83.  
  84. refresh = -> { score.call; header.call; lines.each {|l| liner.call l }; footer.call}
  85.  
  86. complement = -> l { (4 - l.size).times { l << nil } if l.size < 4 }
  87.  
  88. arrange = -> line {
  89.                     case line.size
  90.                     # when 0, 1
  91.                     when 2
  92.                       if line[0] == line[1]
  93.                         line[0] += line[1]; line[1] = nil; $score += line[0]
  94.                       end
  95.                     when 3
  96.                       if line[0] == line[1]
  97.                         line[0] += line[1]; line[1], line[2] = line[2], nil; $score += line[0]
  98.                       elsif line[1] == line[2]
  99.                         line[1] += line[2]; line[2] = nil; $score += line[1]
  100.                       end
  101.                     when 4
  102.                       if line[0] == line[1]
  103.                         line[0] += line[1]; line[1], line[2], line[3] = line[2], line[3], nil; $score += line[0]
  104.                       end
  105.                       if line[1] == line[2]
  106.                         line[1] += line[2]; line[2], line[3] = line[3], nil; $score += line[1]
  107.                       end
  108.                       if !line[2].nil? && line[2] == line[3]
  109.                         line[2] += line[3]; line[3] = nil; $score += line[2]
  110.                       end
  111.                     end
  112.                   }
  113.  
  114. process = -> args {
  115.                     rows = []
  116.                     if args[0]
  117.                       rows = lines
  118.                     else
  119.                       4.times { |i| rows << [ lines[3][i], lines[2][i], lines[1][i], lines[0][i] ] }
  120.                     end
  121.                     rows.each do |row|
  122.                       row.reverse! if args[1]
  123.                       row.compact!
  124.                       arrange.call row
  125.                       complement.call row
  126.                       row.reverse! if args[1]
  127.                       4.times { |i| lines[3][i], lines[2][i], lines[1][i], lines[0][i] = rows[i] } unless args[0]
  128.                     end
  129.                   }
  130.  
  131. left  = -> { process.call [ true, false] }
  132. down  = -> { process.call [false, false] }
  133. right = -> { process.call [ true,  true] }
  134. up    = -> { process.call [false,  true] }
  135.  
  136. check = -> {
  137.               lines.each { |l| l.each { |b| congrulations.call if b == 2048 } }
  138.               game_over.call if lines.inject(0) {|mem, line| mem + line.compact.size } == 16
  139.             }
  140.  
  141. game = -> {
  142.             helper.call
  143.             start.call
  144.             loop do
  145.               $nums  = 0
  146.               image = []; lines.each { |l| image << l.clone }
  147.               refresh.call
  148.               str = gets
  149.               case str.rstrip.to_sym
  150.               when :a; left.call
  151.               when :s; down.call
  152.               when :d; right.call
  153.               when :w; up.call;
  154.               else next; end
  155.               check.call
  156.               new_num.call if image != lines
  157.             end
  158.           }
  159.  
  160. game.call

评分

参与人数 6星屑 +307 收起 理由
卡奥尼特 + 15 我很赞同
喵呜喵5 + 120 精品文章
satgo1546 + 120 为什么我要给这么多糖
无脑之人 + 20 控制台简直丧心病狂w
余烬之中 + 20 虽然不怎么玩但是点赞
kuerlulu + 12

查看全部评分

愿善用者善用之
https://github.com/Shy07/SINRGE2

Lv3.寻梦者 (版主)

…あたしは天使なんかじゃないわ

梦石
0
星屑
2207
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

2
发表于 2014-4-13 17:12:23 | 只看该作者
一堆lambda简直丧心病狂~ 界面挺漂亮
小建议:控制台的输入直接使用Win32API接收方向键。我也拿控制台写过,感觉这样效果还不错~
            每一步之后可以先用 cls 命令清掉控制台
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
66
在线时间
1641 小时
注册时间
2011-9-26
帖子
313
3
 楼主| 发表于 2014-4-14 19:25:54 | 只看该作者
taroxd 发表于 2014-4-13 17:12
一堆lambda简直丧心病狂~ 界面挺漂亮
小建议:控制台的输入直接使用Win32API接收方向键。我也拿控制台写过 ...

哈哈,我就知道会有人吐槽 lambda :-D
不用 Win32API 是考虑到原生 Ruby 的兼容性(我工作用的系统是 Mac OS,helper 里回车写成 return 而不是 enter 也是这原因)
清掉控制台感觉没什么必要,作为历史记录看看自己哪一步犯错也是不错的
愿善用者善用之
https://github.com/Shy07/SINRGE2
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-26 08:17

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表