> From: Knut erik Teigen > Sent: Thursday, August 03, 2006 12:21 PM > > Hello > I've just started learning Ruby, and for a first project, I Welcome! > #!/usr/bin/ruby -w > > def solve(i,j,grid) > > #termination condition > if j==9 #completed one row, start next > j=0 > i+=1 > if i==9 #then we're done! > return true > end > end > > #we don't want to test for given values, so > #jump to next cell > return solve(i,j+1,grid) if grid[i*9+j] != 0 > > #recursive part. > #check each value for this cell, > #if value gets set, continue with next cell > for value in 1..9 > if possible(i,j,value,grid) > grid[i*9+j]=value > return true if solve(i,j+1,grid) > end > end You want to return nil or false here. Plus something like grid[i*9+j]=0 is missing. > end > > #checks if value can be placed in this cell without > #conflicts in rows, columns or boxes > def possible(i,j,value,grid) > > #checks row > row = i*9 > return false if grid[row...row+9].index(value)!=nil > > #checks column > col = j > for i in 0..8 this destroys your parameter 'i' > if grid[i*9+col]==value > return false > end > end > > #checks box > #integer math to find start of box > i0=(i/3).to_i*3 > j0=(j/3).to_i*3 > for i in i0...i0+3 > for j in j0...j0+3 > if grid[i*9+j]==value > return false > end > end > end > > #value not found, so it's a possible choice for this cell > return true > > end > ... snip ... cheers Simon