refactoring algorithm to find the winner of the game