From: Graham Foster Date: 2004-09-24T06:09:32+09:00 Subject: Re: Newbie: Case statement I'm embarassed to post this.. but this is the (completely braindead) solution I put together BEFORE I saw your solutions. You can see why I thought it needed improving. My starting point is Conway's game of LIFE (which should give you more of a clue what I'm doing here). It might also gie you a clue as to why I was thinking of some form of fall through / multiple case statement to reduce the duplication here The world is a simple 2D array of (randomly initialised) cells, and this is the "generation" stage. I'm new to OO as well as Ruby, so there are probably LOADS of improvements I need to learn. (Guess this method could be a class method, not an ordinary method too...?) The Cell class has this def hasneighbour @neighbour +=1 end # determine rules for the fate of any cells def fate case @neighbour when 2 # no change to the alive or dead status when 3 # cell springs into life @isalive = true else # all other conditions the cell dies @isalive = false end # reset the neighbour count for the next generation @neighbour = 0 return @isalive end The World class has this monster def calcGeneration # work through a grid, identifying all live cells # and incrementing the neighbour count of their neighbours # the faster we can weed out entries wih no live cell - the # faster this algorithm works @world.each_index { |row| @world[row].each_index { |col| # - ----- now what # for each row and column if @world[row][col].isalive case row # first row when 0 case col # first column when 0 @world[row][col+1].hasneighbour @world[row+1][col].hasneighbour @world[row+1][col+1].hasneighbour # middle columns when 1..(@x-2) @world[row][col-1].hasneighbour @world[row][col+1].hasneighbour @world[row+1][col].hasneighbour @world[row+1][col+1].hasneighbour @world[row+1][col-1].hasneighbour # RHS columns else @world[row][col-1].hasneighbour @world[row+1][col-1].hasneighbour @world[row+1][col].hasneighbour end # all middle rows when 1..(@y -2) case col when 0 @world[row-1][col].hasneighbour @world[row-1][col+1].hasneighbour @world[row][col+1].hasneighbour @world[row+1][col].hasneighbour @world[row+1][col+1].hasneighbour when 1..(@x-2) @world[row-1][col-1].hasneighbour @world[row-1][col].hasneighbour @world[row-1][col+1].hasneighbour @world[row][col-1].hasneighbour @world[row][col+1].hasneighbour @world[row+1][col-1].hasneighbour @world[row+1][col].hasneighbour @world[row+1][col+1].hasneighbour else @world[row-1][col-1].hasneighbour @world[row-1][col].hasneighbour @world[row][col-1].hasneighbour @world[row+1][col].hasneighbour @world[row+1][col-1].hasneighbour end else # bottom row case col when 0 @world[row-1][col].hasneighbour @world[row-1][col+1].hasneighbour @world[row][col+1].hasneighbour when 1..(@x-2) @world[row-1][col-1].hasneighbour @world[row-1][col].hasneighbour @world[row-1][col+1].hasneighbour @world[row][col-1].hasneighbour @world[row][col+1].hasneighbour else @world[row-1][col-1].hasneighbour @world[row-1][col].hasneighbour @world[row][col-1].hasneighbour end end end } } # then go through all cells in the world and determine their fate @world.each { |row| row.each { |cell| # for each cell determine its fate cell.fate } } end (Sorry for the long listing. ) Thanks for listening. My next task is to try to put some graphics on this then I can see what is going on! (all a very long way from my embedded real-time stuff in C & assembler!). Graham