mine.rb

Discussion in 'Ruby' started by Manny Swedberg, Apr 4, 2004.

  1. 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
    Manny Swedberg, Apr 4, 2004
    #1
    1. Advertising

  2. Manny Swedberg

    George Ogata Guest

    (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.
    George Ogata, Apr 4, 2004
    #2
    1. Advertising

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


    --
    Simon Strandgaard
    Simon Strandgaard, Apr 5, 2004
    #3
  4. George Ogata <> wrote in message news:<>...
    > (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.


    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
    Manny Swedberg, Apr 5, 2004
    #4
  5. Manny Swedberg

    Tom Copeland Guest

    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.


    May the numbers be ever increasing!

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

    Yours,

    Tom
    Tom Copeland, Apr 5, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bjoern
    Replies:
    0
    Views:
    560
    Bjoern
    Oct 21, 2003
  2. xyZed
    Replies:
    8
    Views:
    363
    xyZed
    Apr 5, 2006
  3. FLEB
    Replies:
    2
    Views:
    405
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Aug 29, 2004
  4. milton

    who delete mine?

    milton, Sep 16, 2003, in forum: C Programming
    Replies:
    3
    Views:
    384
    Default User
    Sep 16, 2003
  5. MaHahaXixi
    Replies:
    8
    Views:
    308
    MaHahaXixi
    Apr 14, 2005
Loading...

Share This Page