mine.rb

M

Manny Swedberg

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
 
G

George Ogata

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.
 
S

Simon Strandgaard

On Sun, 04 Apr 2004 02:32:43 -0800, Manny Swedberg wrote:
[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

server> 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.


server> wc -l gameoflife.rb test_gameoflife.rb
73 gameoflife.rb
108 test_gameoflife.rb
181 total
server> 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[x]
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
server> 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)
server>
 
M

Manny Swedberg

George Ogata said:
Wow, I never looked in there before. Minesweeper in 176 lines is
quite impressive!
;)


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top