private void CheckForDeath() { var firstSegmentX = this.CurrentPosition.Peek()[0]; var firstSegmentY = this.CurrentPosition.Peek()[1]; var currentCount = CurrentPosition.Count; for (int i = 0; i < currentCount; i++) { var checkSegment = CurrentPosition.Dequeue(); if (i != 0 && checkSegment[0] == firstSegmentX && checkSegment[1] == firstSegmentY) { this.IsDead = true; } CurrentPosition.Enqueue(checkSegment); } if (firstSegmentX >= this.SnakeBoardWidth || firstSegmentX < 0) { this.IsDead = true; } if (firstSegmentY >= this.SnakeBoardHeight || firstSegmentY < 0) { this.IsDead = true; } }
public void UpdateBody(bool hasEaten) { var currentCount = CurrentPosition.Count; for (int i = 0; i < currentCount; i++) { var currentBlock = CurrentPosition.Dequeue(); if (i == 0) { this.PreviousPosition.Push(new int[] { currentBlock[0], currentBlock[1] }); switch (this.Direction) { case 'd': currentBlock[0] += 1; break; case 'w': currentBlock[1] -= 1; break; case 's': currentBlock[1] += 1; break; case 'a': currentBlock[0] -= 1; break; } } else { var previousBlock = this.PreviousPosition.Peek(); this.PreviousPosition.Push(new int[] { currentBlock[0], currentBlock[1] }); if (!hasEaten) { currentBlock[0] = previousBlock[0]; currentBlock[1] = previousBlock[1]; } } CurrentPosition.Enqueue(currentBlock); } this.CheckForDeath(); }