added logic to resolve game ending