added the mechanism to judge whether the game is finished