TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Exercise: Minesweeper in 100 lines of Ruby

96 pointsby damir10 months ago

12 comments

waf10 months ago
Fun exercise! I found that the Ruby in this post resembles modern C# (probably because Ruby is a gold standard in readability, and in the last 5 years or so C# has been making big strides in readability). I&#x27;ve recreated the Ruby code mostly line-for-line in C# and posted it here: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;waf&#x2F;5c6a04899e8250cb9a89406b978c9bcc" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;waf&#x2F;5c6a04899e8250cb9a89406b978c9bcc</a><p>It mapped from Ruby to C# really well. The only concept I had to add was a `BoardCell` base type for the `Mine` and `Empty` types so we could return either `Mine` or `Empty` in a typesafe way. Everything else matched conceptually 1-1.<p>It&#x27;s 118 lines total, with 18 lines of whitespace, so 100 lines exactly. I figure the original Ruby code was also not counting whitespace so it&#x27;s a fair comparison. I tried to not use any &quot;code golf tricks&quot; that would minimize line count. It&#x27;s a full program; it could be copy&#x2F;pasted into a modern .NET 8 project and run without any additional setup.<p>Note, I posted this same comment on the original blog post, but I&#x27;ve posted it here too as it might be interesting for HN folks.
评论 #41025139 未加载
评论 #41024915 未加载
akdfax10 months ago
Very nice syntax. Ruby is definitely underappreciated at the moment.
RodgerTheGreat10 months ago
I had a go at an implementation of my own in Lil, using an imperative style rather than OOP. I tried to loosely follow the organization as shown here, but opted for a very different board representation, and I included some niceties like displaying board legends to aid in choosing the proper coordinates.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;JohnEarnest&#x2F;Decker&#x2F;blob&#x2F;main&#x2F;examples&#x2F;lilt&#x2F;mines.lil">https:&#x2F;&#x2F;github.com&#x2F;JohnEarnest&#x2F;Decker&#x2F;blob&#x2F;main&#x2F;examples&#x2F;lil...</a><p>I&#x27;m really not convinced that OOP simplifies or clarifies this type of program.
评论 #41022618 未加载
pooriar10 months ago
I&#x27;m currently implementing my favorite 2 player board game - Raptor - in Ruby, and this article is giving me great ideas for how to structure it better. Thanks so much to the author for writing this up!
评论 #41027635 未加载
igor4710 months ago
Cool! Mine sweeper is a really fun easy game to implement. I did a version in Python a few years ago: <a href="https:&#x2F;&#x2F;github.com&#x2F;igor47&#x2F;sweeper">https:&#x2F;&#x2F;github.com&#x2F;igor47&#x2F;sweeper</a> but mine is closer to 500 lines
评论 #41020791 未加载
oneeyedpigeon10 months ago
Warning to Mac users: you&#x27;ll need at least ruby 2.7 to run this because it uses a feature called &#x27;argument forwarding&#x27;. Looks like the latest ruby installed on macOS 15 is 2.6.<p>I eventually got it working with a combination of techniques from [this stackoverflow post](<a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;8730676&#x2F;how-can-i-switch-to-ruby-1-9-3-installed-using-homebrew" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;8730676&#x2F;how-can-i-switch...</a>) but it&#x27;s not left my system in the cleanest of states.
评论 #41025162 未加载
评论 #41026354 未加载
aaronpkelly10 months ago
Why are all of radanskoric&#x27;s replies in this story marked as [dead]? He is replying to people here and all his comments won&#x27;t show for people who don&#x27;t have showdead enabled<p>And when I check his profile, almost every comment he&#x27;s made is also marked as [dead] - but I don&#x27;t see a good reason why?
评论 #41050113 未加载
评论 #41033614 未加载
wkjagt10 months ago
Very fun stuff, and nice post.<p>Reminds me of my 2048 game clone in Ruby in about 100 lines (<a href="https:&#x2F;&#x2F;github.com&#x2F;wkjagt&#x2F;2048">https:&#x2F;&#x2F;github.com&#x2F;wkjagt&#x2F;2048</a>)<p>And I just saw that I did this - oh god - 9 years ago. Time goes way too fast.
abhishekbasu10 months ago
This was fun. I saw a post on Pyxel a couple of days ago, and decided to write mine in Python using it.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;abhishekbasu&#x2F;minesweeper">https:&#x2F;&#x2F;github.com&#x2F;abhishekbasu&#x2F;minesweeper</a>
twobitshifter10 months ago
don’t you have to be able to flag the mines?
评论 #41021470 未加载
评论 #41021904 未加载
评论 #41021437 未加载
raymond_goo10 months ago
I raise you 49 lines of typescript<p><a href="https:&#x2F;&#x2F;stackblitz.com&#x2F;edit&#x2F;gridgame-minesweeper?file=index.ts" rel="nofollow">https:&#x2F;&#x2F;stackblitz.com&#x2F;edit&#x2F;gridgame-minesweeper?file=index....</a>
评论 #41022046 未加载
henning10 months ago
The stupid thing about object-oriented programming and modern software is that code like this would never make it through code review on an &quot;agile&quot; team.<p>- &quot;Oh, why are you doing procedural case statements? that&#x27;s a SOLID anti-pattern, please refactor to polymorphism&quot;<p>- &quot;Oh, why is the Ascii Renderer class meddling in cell mine logic? `cell.neighbour_mines.zero? ? &quot;_&quot; : cell.neighbour_mines` should be its own method in a `Mine` class that is properly tested&quot;<p>You&#x27;re never allowed to just write code that solves a problem. It never ends. Your team has unlimited authority to slow you down with completely useless &quot;feedback&quot; that does not help anyone.
评论 #41021384 未加载
评论 #41021119 未加载
评论 #41022667 未加载
评论 #41020935 未加载