Fixed end game bug.

Now the game logic is right. Of course it needs urgent optimization.