在Ruby中构建事件循环时,如何避免无限递归?(How do I avoid infinite recursion when building an event loop in Ruby?)

编程入门 行业动态 更新时间:2024-10-27 11:24:28
在Ruby中构建事件循环时,如何避免无限递归?(How do I avoid infinite recursion when building an event loop in Ruby?)

我想在Ruby中实现一个游戏循环,但是我当前的实现收到的“堆栈级别太深”(SystemStackError)。

这是我在我的俄罗斯方块般的下滑块游戏中取得的成就:

# falling block game class Tetris class EndGame < StandardError; end def start @canvas = Canvas.new @canvas.banner = "New game" @canvas.draw update end def update step update rescue EndGame puts "Game over!" end def step puts "Take one step..." # TODO: do stuff here end # draws our game board class Canvas SIZE = 10 attr_accessor :banner, :board def initialize @board = SIZE.times.map { Array.new(SIZE) } end def update(point, marker) x, y = point @board[x, y] = marker end alias_method :draw, :to_s def draw [banner, separator, body, separator].join("\n") end private def separator "=" * SIZE end def body @board.map do |row| row.map { |e| e || " " }.join end.join("\n") end end end game = Tetris.new game.start

产生此错误:

Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... gameloop.rb:20:in `puts': stack level too deep (SystemStackError) from gameloop.rb:20:in `puts' from gameloop.rb:20:in `step' from gameloop.rb:13:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' ... 10067 levels... from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:9:in `start' from gameloop.rb:59:in `<main>'

UPDATE

Ruby默认情况下不启用Tail Call Optimizations,但可以在Ruby VM中启用它。

RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false }

I want to implement a game loop in Ruby, but my current implementation receives a 'stack level too deep' (SystemStackError).

This is how far I got on my Tetris-like falling block game:

# falling block game class Tetris class EndGame < StandardError; end def start @canvas = Canvas.new @canvas.banner = "New game" @canvas.draw update end def update step update rescue EndGame puts "Game over!" end def step puts "Take one step..." # TODO: do stuff here end # draws our game board class Canvas SIZE = 10 attr_accessor :banner, :board def initialize @board = SIZE.times.map { Array.new(SIZE) } end def update(point, marker) x, y = point @board[x, y] = marker end alias_method :draw, :to_s def draw [banner, separator, body, separator].join("\n") end private def separator "=" * SIZE end def body @board.map do |row| row.map { |e| e || " " }.join end.join("\n") end end end game = Tetris.new game.start

Produces this error:

Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... Take one step... gameloop.rb:20:in `puts': stack level too deep (SystemStackError) from gameloop.rb:20:in `puts' from gameloop.rb:20:in `step' from gameloop.rb:13:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' ... 10067 levels... from gameloop.rb:14:in `update' from gameloop.rb:14:in `update' from gameloop.rb:9:in `start' from gameloop.rb:59:in `<main>'

UPDATE

Ruby does not enable Tail Call Optimizations by default, but it can be enabled in the Ruby VM.

RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false }

最满意答案

使用while true ...循环而不是递归。 您还可以添加一个短暂的sleep以避免更多的更新。

Use a while true ... loop instead of recursion. You can also add a short sleep to avoid more updates than necessary.

更多推荐

本文发布于:2023-08-04 16:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   事件   avoid   Ruby   infinite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!