63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using System.Collections.ObjectModel;
|
|
|
|
namespace Snake.Model;
|
|
|
|
public class SnakeLevel{
|
|
public int Size {get; }
|
|
|
|
public enum LevelBlock{
|
|
Empty,
|
|
Obstacle,
|
|
Egg,
|
|
Snake,
|
|
SnakeHead
|
|
}
|
|
|
|
public enum SnakeDirection{
|
|
Up,
|
|
Down,
|
|
Left,
|
|
Right
|
|
}
|
|
|
|
private readonly List<Point> _obstacles;
|
|
public ReadOnlyCollection<Point> Obstacles => _obstacles.AsReadOnly();
|
|
public Point SnakeHead {get; private set;}
|
|
private readonly List<Point> _snake;
|
|
// Head is not in snake
|
|
public ReadOnlyCollection<Point> Snake => _snake.AsReadOnly();
|
|
public int SnakeLength => Snake.Count + 1;
|
|
private readonly List<Point> _eggs;
|
|
public ReadOnlyCollection<Point> Eggs => _eggs.AsReadOnly();
|
|
public SnakeDirection SnakeHeadDirection {get; private set;}
|
|
|
|
public SnakeLevel(int size, IEnumerable<Point> obstacles, int snake_start_length){
|
|
this.Size = size;
|
|
_obstacles = new List<Point>(obstacles);
|
|
_eggs = [];
|
|
_snake = [];
|
|
|
|
SnakeHead = (size / 2, snake_start_length-1);
|
|
for(int i=0; i<snake_start_length-1; i++){
|
|
_snake.Add((size / 2, i));
|
|
}
|
|
|
|
SnakeHeadDirection = SnakeDirection.Down;
|
|
}
|
|
|
|
public LevelBlock this[Point p]{
|
|
get{
|
|
if(_obstacles.Contains(p)){
|
|
return LevelBlock.Obstacle;
|
|
}else if(p == SnakeHead){
|
|
return LevelBlock.SnakeHead;
|
|
}else if(_snake.Contains(p)){
|
|
return LevelBlock.Snake;
|
|
}else if(_eggs.Contains(p)){
|
|
return LevelBlock.Egg;
|
|
}
|
|
|
|
return LevelBlock.Empty;
|
|
}
|
|
}
|
|
} |