Get the latest tech news

Exercise: Minesweeper in 100 lines of Ruby


Ruby is such an expressive language. You can often do surprisingly much with just a few lines of code. It’s why I find it so satisfying to think about how to accomplish the same thing in fewer lines of Ruby1. If we accept the claim that that number of bugs correlates with number of lines of code this is not just a fun exercise. There’s real business value in accomplishing a feature with less lines of code. ↩

The rest of the article walks through my implementation which landed on exactly 100 lines (counted by cloc in lib folder) by a happy accident. The Mine and Empty nested subclasses are why here I inherited from Data defined class, they’ll be needed outside this context. To print the board on the command line we’ll iterate over the grid, converting the cells to ASCII characters:

Get the Android app

Or read this on Hacker News

Read more on:

Photo of exercise

exercise

Photo of lines

lines

Photo of Minesweeper

Minesweeper

Related news:

News photo

Ultra simplified "MNIST" in 60 lines of Python with NumPy

News photo

Beating NumPy matrix multiplication in 150 lines of C

News photo

Netflix has reimagined Minesweeper and it's out now