public void Move() { for (int i = 0; i < body.Count; i++) { OrientedPoint nextPoint = getNextPoint(body[i]); if(i != body.Count - 1) nextPoint.Direction = body[i + 1].Direction; body[i] = nextPoint; } snakeMoved(); }
public void CheckNextMove() { if (!canMove(snake.Head)) { YouLose(); return; } OrientedPoint pt = Snake.getNextPoint(snake.Head); if (field[pt.X, pt.Y] == FieldState.Food) snake.Eat(); else snake.Move(); }
public bool canMove(OrientedPoint head) { if (!inFieldRange(head.X, head.Y)) return false; OrientedPoint nextPt = Snake.getNextPoint(head); if (!inFieldRange(nextPt.X, nextPt.Y)) return false; FieldState state = field[nextPt.X, nextPt.Y]; if (state == FieldState.Empty || state == FieldState.Food) return true; else return false; }
public void ChangeDirection(Direction direction) { if (direction < Direction.Up || direction > Direction.Left) throw new ArgumentOutOfRangeException("Wrong argument of direction"); OrientedPoint head = new OrientedPoint(Head.X, Head.Y, direction); if(body.Count > 1) { OrientedPoint nextPoint = getNextPoint(head); OrientedPoint neck = body[Length - 2]; // don't allow to move in opposite direction if(nextPoint.X != neck.X || nextPoint.Y != neck.Y) body[Length - 1] = head; } else body[Length - 1] = head; }
private bool checkSnake() { OrientedPoint pt = Tail; foreach (OrientedPoint nextChain in body) { if (nextChain == Head) break; pt = getNextPoint(pt); if (pt.X != nextChain.X || pt.Y != nextChain.Y) { throw new Exception("Error: wrong structure of snake!"); return false; } pt.Direction = nextChain.Direction; } return true; }
public static OrientedPoint getNextPoint(OrientedPoint previous) { switch(previous.Direction) { case Direction.Up: previous.Y--; break; case Direction.Down: previous.Y++; break; case Direction.Left: previous.X--; break; case Direction.Right: previous.X++; break; } return previous; }
public void SetStartPosition(OrientedPoint pt) { body.Clear(); body.Add(pt); }