Mine.rb

Hey all,

I’m looking to do a simple game with a curses-esque interface as my
first Ruby project. My previous programming experience is a whole lot
of VAX Basic and a tiny little bit of C almost a decade ago. (I know:
doomed!)

Anyways, mine.rb in the sample programs directory seems to have a lot
of the functionality I’m looking for, but the comment are all en
japonais.

Anyone know of an easily-available, simple sample program that does
kind of the same thing?

Thanks,

Manny

swedberg211@yahoo.com (Manny Swedberg) writes:

Anyways, mine.rb in the sample programs directory seems to have a lot
of the functionality I’m looking for, but the comment are all en
japonais.

Wow, I never looked in there before. Minesweeper in 176 lines is
quite impressive!

Anyone know of an easily-available, simple sample program that does
kind of the same thing?

I haven’t looked, but the usual places to search are RAA, Rubyforge,
and maybe even Google.

But surely that 176 line program is short enough to decipher even
without readable commentary. If you have questions on a particular
piece of code, plenty of folk here can probably help.

[snip]

Anyone know of an easily-available, simple sample program that does
kind of the same thing?

Here is a smallish game-of-life I recently made… no fancy colors…

Is that usable to you ?

A sample output

ruby gameoflife.rb
lets play a game
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

0 0 0 0 0 0
0 0 0 1 0 0
0 1 0 0 1 0
0 1 0 0 1 0
0 0 1 0 0 0
0 0 0 0 0 0

0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

The implementation + testsuite… BTW: is under Ruby’s license.

wc -l gameoflife.rb test_gameoflife.rb
73 gameoflife.rb
108 test_gameoflife.rb
181 total
expand -t2 gameoflife.rb
module GameOfLife
def determine_destiny(alive, count)
unless alive
return (count == 3)
end
(count == 2) or (count == 3)
end
def get(cells, y, x)
return 0 if x < 0 or y < 0
return 0 if y >= cells.size
row = cells[y]
return 0 if x >= row.size
row
end
def count_neighbours(cells, x, y)
n = 0
n += get(cells, y-1, x-1)
n += get(cells, y-1, x)
n += get(cells, y-1, x+1)
n += get(cells, y, x-1)
n += get(cells, y, x+1)
n += get(cells, y+1, x-1)
n += get(cells, y+1, x)
n += get(cells, y+1, x+1)
n
end
def lifecycle(cells)
y = 0
next_cells = cells.map do |row|
x = 0
next_row = row.map do |cell|
n = count_neighbours(cells, x, y)
x += 1
determine_destiny((cell != 0), n) ? 1 : 0
end
y += 1
next_row
end
next_cells
end
end

if $0 == FILE
puts “lets play a game”
class Game
include GameOfLife
def initialize
@cells = [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]
]
end
def next
@cells = lifecycle(@cells)
end
def inspect
rows = @cells.map do |row|
row.join(" “)
end
rows.join(”\n")
end
end
game = Game.new
loop do
p game
gets
game.next
end
end

expand -t2 test_gameoflife.rb
require ‘test/unit’
require ‘gameoflife’
class TestGameOfLife < Test::Unit::TestCase
include GameOfLife
def test_destiny_populated
data = [
[0, false],
[1, false],
[2, true],
[3, true],
[4, false],
[5, false],
[6, false]
]
input, expected = data.transpose
actual = input.map do |count|
determine_destiny(true, count)
end
assert_equal(expected, actual)
end
def test_destiny_empty
data = [
[0, false],
[1, false],
[2, false],
[3, true],
[4, false],
[5, false],
[6, false]
]
input, expected = data.transpose
actual = input.map do |count|
determine_destiny(false, count)
end
assert_equal(expected, actual)
end
def test_count_neighbours0
cells = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
]
n = count_neighbours(cells, 1, 1)
assert_equal(0, n)
end
def test_count_neighbours1
cells = [
[1, 0, 0],
[0, 1, 1],
[0, 1, 0]
]
n = count_neighbours(cells, 1, 1)
assert_equal(3, n)
end
def test_count_neighbours2
cells = [
[0, 1, 1],
[1, 1, 1],
[1, 1, 0]
]
n = count_neighbours(cells, 1, 1)
assert_equal(6, n)
end
def test_count_neighbours3
cells = [
[0, 1, 1],
[1, 1, 1],
[1, 1, 0]
]
n = count_neighbours(cells, 0, 0)
assert_equal(3, n)
end
def test_count_neighbours4
cells = [
[0, 1, 1],
[1, 1, 1],
[1, 1, 0]
]
n = count_neighbours(cells, 2, 2)
assert_equal(3, n)
end
def test_count_neighbours5
cells = [
[1, 0, 1, 0],
[0, 1, 1, 1],
[1, 0, 0, 1]
]
n1 = count_neighbours(cells, 1, 1)
n2 = count_neighbours(cells, 2, 1)
assert_equal([4, 4], [n1, n2])
end
def test_lifecycle1
cells = [
[0, 1, 1],
[1, 1, 1],
[1, 1, 0]
]
expected_cells = [
[1, 0, 1],
[0, 0, 0],
[1, 0, 1]
]
actual = lifecycle(cells)
assert_equal(expected_cells, actual)
end
end
require ‘test/unit/ui/console/testrunner’
Test::Unit::UI::Console::TestRunner.run(TestGameOfLife)

···

On Sun, 04 Apr 2004 02:32:43 -0800, Manny Swedberg wrote:


Simon Strandgaard

George Ogata g_ogata@optushome.com.au wrote in message news:87r7v35u6e.fsf@optushome.com.au

swedberg211@yahoo.com (Manny Swedberg) writes:

Anyways, mine.rb in the sample programs directory seems to have a lot
of the functionality I’m looking for, but the comment are all en
japonais.

Wow, I never looked in there before. Minesweeper in 176 lines is
quite impressive!

:wink:

Anyone know of an easily-available, simple sample program that does
kind of the same thing?

I haven’t looked, but the usual places to search are RAA, Rubyforge,
and maybe even Google.

But surely that 176 line program is short enough to decipher even
without readable commentary. If you have questions on a particular
piece of code, plenty of folk here can probably help.

Thanks, George. Haven’t looked any further into mine.rb, but after I
posted this I found CursWrap, which was just what I needed. Really,
really nice ncurses interface. If you’re here, Julian: nice job!

So far I’ve got a little ‘@’ who can walk around on the screen and a
scary red ‘g’ (for ‘goblin’!) who dances around like a monkey; ai
courtesty of rand(). The next step is to make it so they can kill
each other, and then all I have to do is add in a whoppingHeapOGold
object (‘$’) so you can win the game.

I’m liking this language a lot.

Best,

Manny

May the numbers be ever increasing!

http://rubyforge.org/softwaremap/trove_list.php?form_cat=80

Yours,

Tom

···

On Mon, 2004-04-05 at 09:19, Manny Swedberg wrote:

So far I’ve got a little ‘@’ who can walk around on the screen and a
scary red ‘g’ (for ‘goblin’!) who dances around like a monkey; ai
courtesty of rand(). The next step is to make it so they can kill
each other, and then all I have to do is add in a whoppingHeapOGold
object (‘$’) so you can win the game.