fixed bugs with detecting whether game ends