Re: [romandie.rb] romandie.rb Digest, Vol 26, Issue 2

2 views
Skip to first unread message

Stéphane Tavera

unread,
Sep 17, 2010, 6:25:36 AM9/17/10
to roman...@zarb.org
Salut à tous,

Ah le coup du clone ;-)
J'en connais qui sont tombés dans le panneau.
En fouillant un peu, l'idiome pour une "deep copy" d'un array est
Marshal.load(Marshal.dump(a)).

cf http://gist.github.com/584027

J'ai trouvé très intéressant de voir les différentes approches hier soir.

à retenir : mieux préciser le pb au départ, et faire attention au temps nécessaire
à découvrir un environnement (emacs, windaube, ...)

A+

On Sep 17, 2010, at 12:00 PM, romandie....@zarb.org wrote:

> Send romandie.rb mailing list submissions to
> roman...@zarb.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://www.zarb.org/mailman/listinfo/romandie.rb
> or, via email, send a message with subject or body 'help' to
> romandie....@zarb.org
>
> You can reach the person managing the list at
> romandie...@zarb.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of romandie.rb digest..."
>
>
> Today's Topics:
>
> 1. LifeGame - Suite ? la r?union du 16 sept. (Yann Lugrin)
> 2. Re: LifeGame - Suite ? la r?union du 16 sept. (Andreas Kundig)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 17 Sep 2010 10:42:53 +0200
> From: Yann Lugrin <yann....@gmail.com>
> To: "Discussions du RUG romandie.rb" <roman...@zarb.org>
> Subject: [romandie.rb] LifeGame - Suite ? la r?union du 16 sept.
> Message-ID:
> <AANLkTinT7f=Tk-KyOgFuOPREZU83+2UJRiMf=2fb...@mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> Bonjour ? tous,
>
> J'ai push le code que nous avons produit avec Damien hier sur Gist, je
> l'ai aussi debug et ajout? quelques tests, mais l'?tait dans lequel il
> ?tait hier en fin de session est dispo ? l'origine de l'historique.
>
> http://gist.github.com/583865
>
> Je lui avais dit ? Damien que clone sur un Array multi dimensionnel ?a
> ne marchai pas :D
>
> --
> Yann Lugrin
> yann....@sans-savoir.net
>
>
> ------------------------------
>
> Message: 2
> Date: Fri, 17 Sep 2010 11:41:55 +0200
> From: Andreas Kundig <andreas...@gmx.ch>
> To: "Discussions du RUG romandie.rb" <roman...@zarb.org>
> Subject: Re: [romandie.rb] LifeGame - Suite ? la r?union du 16 sept.
> Message-ID: <4C9337E...@gmx.ch>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> # Ecrit par Thibaud et Andr?as ? Lausanne,
> # d?bugg? par St?phane et Andr?as dans le train pour Gen?ve.
> # Nul doute qu'il reste pas mal de choses ? am?liorer, mais ?a marche.
> Dans DOS.
> # Pour unix il faut en tout cas remplacer system("cls") par system("clear").
> #
> # J'avais promis de mettre ?a sous gist, mais je suis trop ? la bourre
> pour apprendre ?a ce matin.
> #
> # Thibaud, notre bug a ?t? d'oublier que cloner un tableau de tableaux
> ne clone que le tableau ext?rieur,
> # de sorte que board_clone[x][y]=false modifie aussi l'original.
>
> class Life
> attr_accessor :board, :size
>
> def initialize(sequence = "0000000000011100000000000",
> options = { :size => 5 })
> @size = options[:size]
> @board = []
> sequence.split(//).each_with_index do | cell,i |
> @board << [] if i%size == 0
> @board[-1] << (cell == "1")
> end
> end
>
> def live
> new_board = []#board.clone
> board.each_index do |row|
> new_board[row] = []
> board[row].each_index do |col|
> new_board[row][col] = new_cell(row, col)
> end
> end
> @board = new_board
> end
>
> def new_cell(row, col)
> case count_living_neighbours(row,col)
> when 2
> board[row][col]
> when 3
> true
> else
> false
> end
> end
>
> def count_living_neighbours(row, col)
> count = 0
> count += 1 if board[row-1][col-1]
> count += 1 if board[row-1][col]
> count += 1 if board[row-1][(col+1)%size]
> count += 1 if board[row][col-1]
> count += 1 if board[row][(col+1)%size]
> count += 1 if board[(row+1)%size ][col-1]
> count += 1 if board[(row+1)%size ][col]
> count += 1 if board[(row+1)%size ][(col+1)%size]
> count
> end
>
>
> def display
> system('cls')
>
> board.each do |line|
> line.each do |cell|
> print cell ? 'X' : 0
> end
> print "\n"
> end
> sleep 1
> end
>
> def play
> loop do
> display
> live
> end
> end
> end
>
> life = Life.new
> life.play
>
>
> ------------------------------
>
> _______________________________________________
> romandie.rb mailing list
> roman...@zarb.org
> https://www.zarb.org/mailman/listinfo/romandie.rb
>
>
> End of romandie.rb Digest, Vol 26, Issue 2
> ******************************************

_______________________________________________
romandie.rb mailing list
roman...@zarb.org
https://www.zarb.org/mailman/listinfo/romandie.rb

Yann Lugrin

unread,
Sep 17, 2010, 7:07:16 AM9/17/10
to Discussions du RUG romandie.rb
C'est la méthode que j'ai utilisé au final.

2010/9/17 Stéphane Tavera <stephan...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages