From: Joop Van den tillaart Date: 2007-09-11T17:57:38+09:00 Subject: Re: Filling individual cells in a grid Hi guys, i'm back with a new problem with my grid: I have three celltypes: 1 = road 2 = housing 3 = green Each type has a table where preference scores are stored regarding the adjacency of other celltypes...these are stored in the hash ATable... Now i have to code an algorithm that iterates over all cells and which calculates adjacency scores...I have made a word document where i try to explain what the algorithm needs to do. Me and a teacher have coded two things for this: ---------------------------- def scoreA(cell) # here the algorithm has to be placed end ---------------------------- The adjacency score of one cell needs to be calculated within this method... ---------------------------- for cell1 in grid for cell2 in grid # # compare cell1 with cell2 next if cell1 == cell2 next if cell1.locked || cell2.locked # calculate score for two cells together a_before = scoreA(cell1) + scoreA(cell2) # swap cells cell1.cell_type, cell2.cell_type = cell2.cell_type, cell1.cell_type # calculate score for two cells together a_after = scoreA(cell1) + scoreA(cell2) # if last score is lower or even as the score before swap, then swap cells back to first situation if a_after <= a_before cell1.cell_type, cell2.cell_type = cell2.cell_type, cell1.cell_type end end end ---------------------------- the whole swapping procedure should be in this code above... Now I think i have to design the code which calculates the score of one cell regarding its neighbouring cells with the help of the ATable hash. This piece of code has to be in the method scoreA(cell)... but I'm really stuck in how to do this. Besides that I realize that it is a long story / question I ask so maybe it won't get much reply, but on the other hand if someone is interested in helping me out a bit I would be very grateful... Anyways I have attached my ruby file and a word document in which i tried to explain the procedure of the algorithm... Thanks in advance! Attachments: http://www.ruby-forum.com/attachment/265/testje.rb -- Posted via http://www.ruby-forum.com/.