Extended to four dimensions. Refactored winning condition.