I think you want temp_p_num = p_num inside the for p_num loop and the test temp_p_num == boxes[p_num] should be p_num == boxes[temp_p_num]. IOW The prisoner p_num opens a box with his number in it
Thanks to the comment from Martin Brown, that answered it and it works now as expected!