put everything inside a function

also fix bug when AI is making invalid move