3x3 Magic Square Backtracking recursively?

I have this pseudo-code that I am supposed to use to come up with the coded algorithm
for filling a 3x3 Magic Square. I can’t seem to grasp my head around this pseudo code
for some reason. Could somebody help me with this?

pseudo-code

recursive_funtion(position) {
 	for number from 1 to 9, not used elsewhere already {
 		put this number on this position, make it unavailable
 		if solution valid {
 			if solution complete {
  				done, show solution
  			}else{
  				recursive_function(next_position)
 			}
 		}
 		(make this space blank again, and the number available)
 		}
}

My code so far (Completely wrong im sure)

int[][] square = new int[3][3];
	ArrayList<Integer> used_nums = new ArrayList<Integer>();
	
	
	public MagicSquare()
	{
		rec_backtrack(0);
		print_square();
	}
	public void rec_backtrack(int level)
	{
		int step = 0;
		for(int i = 1; i < 10; i++)
		{
			if(!used_nums.contains(i))
			{
				square[step][level] = i;
				used_nums.add(i);
				step++;
			}
			if(step == 3) //we have inserted to all 3 spots now, check for solution
			{
				if((square[0][level] + square[1][level] + square[2][level]) == 15) //acceptable solution
				{
					rec_backtrack(level++);
				}
				else
				{
					step--;
				}
			}
		}
	}

I had to do this stuff. Well, a Sudoku solver for a class years and years ago.

I’m not looking too closely at your code. I’m assuming that you’re doing most things right and just trying to match statement chunks to see if you’re obviously missing something. Which you are.

(make this space blank again, and the number available)

I don’t see a chunk of code in your thinger that’s doing this action. IE- Nothing like this following chunk.


            square[step][level] = BLANK_INT;
            used_nums.remove(i);

Fix that chunk and add it in the appropriate place and you might be closer to your goal. :smiley:

Ah. Yea, thanks for that, but im still pretty sure the rest of my code is way off. :\