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

habe meine Version mal überarbeitet und SDL mit reingepackt ... aber der Aufwand SDL zu installieren ist 10x größer als der Nutzen :o
Code   

Code:
# Autor: Christian Hoff
require "sdl"

class GameOfLife
  include Enumerable

  def initialize(size)
    @size = size
    @feld = Array.new(@size){Array.new(@size,false)}

    # Startpopulation
    @feld[6][9] = true
    @feld[7][7] = true
    @feld[7][8] = true
    @feld[7][9] = true
    @feld[7][10] = true
    @feld[7][11] = true
    @feld[8][6] = true
    @feld[8][7] = true
    @feld[8][9] = true
    @feld[8][11] = true
    @feld[8][12] = true
    @feld[9][7] = true
    @feld[9][9] = true
    @feld[9][11] = true
    @feld[10][9] = true

    # Zusatz
    @screen = nil
    init_sdl()
  end

  def init_sdl()
    SDL.init(SDL::INIT_VIDEO)
    @screen = SDL::setVideoMode(800, 600, 16,SDL::SWSURFACE)
  end

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

  def to_s
    self.inject(string=""){|string,senkrecht|
      senkrecht.each{|waagerecht| (waagerecht)?(string+="O "):(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

  def run()
    running = true
    rmask = 0xff000000
    gmask = 0x00ff0000
    bmask = 0x0000ff00
    amask = 0x000000ff
    surface = SDL::Surface.new(SDL::HWSURFACE, 800, 600, 16, rmask, gmask, bmask, amask)

    while (running)
      while(event = SDL::Event.poll)
        case event
          when SDL::Event::Quit
            running = false;
        end
      end

      for x in (0..(@size-1))
        for y in (0..(@size-1))
          surface.draw_rect(x*10,y*10,10,10, [255,255,255,255], true) if (@feld[y][x])
        end
      end

      @screen.fillRect(0, 0, 800, 600, 0)
      SDL::Surface.blit(surface,0,0,800,600,@screen,0,0)
      surface.fillRect(0, 0, 800, 600, 0)
      @screen.flip()
      self.step()
      sleep(0.1)
    end
  end

end

game = GameOfLife.new(50)
game.run
__________________
WYSIWYG
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