I fill the with random pieces (starting top left) and as I place each gem, I make sure that it isn't a 3rd one of 3 in a row or column (taking into account any non-grid squares due to different level layouts), and if it is I simply change it to another gem Once the whole grid is filled like this I then call my "Check for possible matches" function, and if this fails (i..e there are no possible moves), then I simply refill until it doesn't fail. If you design your levels sensibly most won't fail, or will only fail a couple of times.