I think this thinking way can help us to solve and understand this problem quickly.
First,Let's begin with some tries.
when the rock is 1(win) 2(win) 3(win) 4(lose) I think everyone can do this,then we stop and think.
After you take rock and your friend becomes "you" think it carefully
for example. when there are 4 rocks,after your taking ,there are three situations.so your friend begin with
you lose(when there are 4 rocks) = your friend win = start with(4-1),(4-2),(4-3) win
so there must be 3 win before 1 lose
so 4 is a group.
in c language we just return n%4