Initial commit

This commit is contained in:
2024-10-02 13:32:23 +02:00
commit b49da1e0de
8 changed files with 651 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
using System.Numerics;
namespace Snake.Model;
// 0,0 is top left
public readonly record struct Point{
public readonly int X;
public readonly int Y;
public Point(int x, int y){
X = x;
Y = y;
}
public static implicit operator Point((int x, int y) tuple){
return new Point(tuple.x, tuple.y);
}
}
+63
View File
@@ -0,0 +1,63 @@
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;
}
}
}
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>