Einzelnen Beitrag anzeigen
Alt 09.07.10, 23:20   #12 (permalink)
chumbalum
 
Benutzerbild von chumbalum
 
Registriert seit: 09.07.10
chumbalum Leistung: Facit NTK
chumbalum eine Nachricht über ICQ schicken
Likes: 0
Standard

Alles klar, dann sollte das jetzt wohl passen:

Code   

Code:
class GameOfLife
  include Enumerable
  def initialize(size)
    @size = size
    @feld = Array.new(@size){Array.new(@size,false)}

    # Startpopulation
    @feld[1][2] = true
    @feld[1][3] = true
    @feld[1][4] = true
    @feld[2][2] = true
    @feld[2][4] = true
  end

  def each(&block)
    @feld.each(&block)
  end

  def to_s
    self.inject(string=""){|string,senkrecht|
      senkrecht.each{|waagerecht| (waagerecht)?(string+="X "):(string+="_ ")
      }
      string+="\n"
    }
  end

  def step()
    buffer_array = []
    for x in (0..(@size-1))
      for y in (0..(@size-1))
        counter = 0
        
        counter += 1 if @feld[y-1][x-1] if (x-1 >= 0 && y-1 >= 0)
        counter += 1 if @feld[y-1][x] if (y-1 >= 0)
        counter += 1 if @feld[y-1][x+1] if (x+1 < @size && y-1 >= 0)
        counter += 1 if @feld[y][x-1] if (x-1 >= 0)
        counter += 1 if @feld[y][x+1] if (x+1 < @size)
        counter += 1 if @feld[y+1][x-1] if (x-1 >= 0 && y+1 < @size)
        counter += 1 if @feld[y+1][x] if (y+1 < @size)
        counter += 1 if @feld[y+1][x+1] if (x+1 < @size && y+1 < @size)

        # => Eine tote Zelle mit genau drei lebenden Nachbarn wird in der Folgegeneration neu geboren.
        if ((counter == 3) && (@feld[y][x]==false))
          buffer_array << [y,x,true]
        end

        # => Lebende Zellen mit weniger als zwei lebenden Nachbarn sterben in der Folgegeneration an Einsamkeit.
        if((counter < 2) && (@feld[y][x]==true))
          buffer_array << [y,x,false]
        end

        # => Eine lebende Zelle mit zwei oder drei lebenden Nachbarn bleibt in der Folgegeneration lebend.
        if(((counter == 2) || (counter ==3)) && @feld[y][x]==true)
          buffer_array << [y,x,true]
        end

        # => Lebende Zellen mit mehr als drei lebenden Nachbarn sterben in der Folgegeneration an Überbevölkerung.
        if ((counter > 3) && (@feld[y][x]==true))
          buffer_array << [y,x,false]
        end
      end
    end

    buffer_array.each{|elem_state| @feld[elem_state[0]][elem_state[1]] = elem_state[2]}

  end
end

game = GameOfLife.new(8)
5.times do
  puts game
  game.step()
  puts
end


Steps   

_ _ _ _ _ _ _ _
_ _ X X X _ _ _
_ _ X _ X _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _

_ _ _ X _ _ _ _
_ _ X _ X _ _ _
_ _ X _ X _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _

_ _ _ X _ _ _ _
_ _ X _ X _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _

_ _ _ X _ _ _ _
_ _ _ X _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _

_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _


etwas blöd mit Ruby, weil man die Console nicht löschen kann, wodurch man auch keine Animation hinbekommt

Geändert von chumbalum (10.07.10 um 01:15 Uhr)
chumbalum ist offline   Mit Zitat antworten
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61