Basically, you have a function that finds all the legitimate moves, So in the horizontal/vertical movement you add x + distance and check if that is legal, then x+ 2 * distance, etc. If it's a legal move, you add it to an array. Then you find a random integer of the size of the array to get the move.