Tick method
This commit is contained in:
parent
0d41087391
commit
5b714395f0
@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "persistance", "persistance\
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "model", "model\model.csproj", "{41FA02D6-4257-46E6-BAF0-1D975169C05E}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "model", "model\model.csproj", "{41FA02D6-4257-46E6-BAF0-1D975169C05E}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{05D087EA-57EA-4A01-82B5-C36C7242EF80}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -24,5 +26,9 @@ Global
|
|||||||
{41FA02D6-4257-46E6-BAF0-1D975169C05E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{41FA02D6-4257-46E6-BAF0-1D975169C05E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{41FA02D6-4257-46E6-BAF0-1D975169C05E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{41FA02D6-4257-46E6-BAF0-1D975169C05E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{41FA02D6-4257-46E6-BAF0-1D975169C05E}.Release|Any CPU.Build.0 = Release|Any CPU
|
{41FA02D6-4257-46E6-BAF0-1D975169C05E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{05D087EA-57EA-4A01-82B5-C36C7242EF80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{05D087EA-57EA-4A01-82B5-C36C7242EF80}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{05D087EA-57EA-4A01-82B5-C36C7242EF80}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{05D087EA-57EA-4A01-82B5-C36C7242EF80}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
@ -10,7 +10,8 @@ public class SnakeLevel{
|
|||||||
Obstacle,
|
Obstacle,
|
||||||
Egg,
|
Egg,
|
||||||
Snake,
|
Snake,
|
||||||
SnakeHead
|
SnakeHead,
|
||||||
|
OutOfBounds
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum SnakeDirection{
|
public enum SnakeDirection{
|
||||||
@ -20,16 +21,23 @@ public class SnakeLevel{
|
|||||||
Right
|
Right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum GameState{
|
||||||
|
Running,
|
||||||
|
Dead
|
||||||
|
}
|
||||||
|
|
||||||
private readonly List<Point> _obstacles;
|
private readonly List<Point> _obstacles;
|
||||||
public ReadOnlyCollection<Point> Obstacles => _obstacles.AsReadOnly();
|
public ReadOnlyCollection<Point> Obstacles => _obstacles.AsReadOnly();
|
||||||
public Point SnakeHead {get; private set;}
|
public Point SnakeHead {get; private set;}
|
||||||
private readonly List<Point> _snake;
|
private readonly List<Point> _snake;
|
||||||
// Head is not in snake
|
// Head is not in snake
|
||||||
|
// Last is closest to the head
|
||||||
public ReadOnlyCollection<Point> Snake => _snake.AsReadOnly();
|
public ReadOnlyCollection<Point> Snake => _snake.AsReadOnly();
|
||||||
public int SnakeLength => Snake.Count + 1;
|
public int SnakeLength => Snake.Count + 1;
|
||||||
private readonly List<Point> _eggs;
|
private readonly List<Point> _eggs;
|
||||||
public ReadOnlyCollection<Point> Eggs => _eggs.AsReadOnly();
|
public ReadOnlyCollection<Point> Eggs => _eggs.AsReadOnly();
|
||||||
public SnakeDirection SnakeHeadDirection {get; private set;}
|
public SnakeDirection SnakeHeadDirection {get; private set;}
|
||||||
|
public GameState State {get; private set;}
|
||||||
|
|
||||||
public SnakeLevel(int size, IEnumerable<Point> obstacles, int snake_start_length){
|
public SnakeLevel(int size, IEnumerable<Point> obstacles, int snake_start_length){
|
||||||
this.Size = size;
|
this.Size = size;
|
||||||
@ -55,9 +63,48 @@ public class SnakeLevel{
|
|||||||
return LevelBlock.Snake;
|
return LevelBlock.Snake;
|
||||||
}else if(_eggs.Contains(p)){
|
}else if(_eggs.Contains(p)){
|
||||||
return LevelBlock.Egg;
|
return LevelBlock.Egg;
|
||||||
|
}else if(p.X < 0 || p.Y < 0 || p.X>=Size || p.Y >= Size){
|
||||||
|
return LevelBlock.OutOfBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
return LevelBlock.Empty;
|
return LevelBlock.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Tick(){
|
||||||
|
Point new_snake_head = (0,0);
|
||||||
|
switch(SnakeHeadDirection){
|
||||||
|
case SnakeDirection.Up:
|
||||||
|
new_snake_head = (SnakeHead.X, SnakeHead.Y - 1);
|
||||||
|
break;
|
||||||
|
case SnakeDirection.Down:
|
||||||
|
new_snake_head = (SnakeHead.X, SnakeHead.Y + 1);
|
||||||
|
break;
|
||||||
|
case SnakeDirection.Left:
|
||||||
|
new_snake_head = (SnakeHead.X - 1, SnakeHead.Y);
|
||||||
|
break;
|
||||||
|
case SnakeDirection.Right:
|
||||||
|
new_snake_head = (SnakeHead.X + 1, SnakeHead.Y);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this[new_snake_head] is LevelBlock.Obstacle or LevelBlock.Snake or LevelBlock.OutOfBounds){
|
||||||
|
State = GameState.Dead;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var first = Snake[0];
|
||||||
|
|
||||||
|
for(int i=_snake.Count-1; i>0; i--){
|
||||||
|
_snake[i-1] = _snake[i];
|
||||||
|
}
|
||||||
|
_snake[^1] = SnakeHead;
|
||||||
|
|
||||||
|
if(this[new_snake_head] is LevelBlock.Egg){
|
||||||
|
_snake.Insert(0, first);
|
||||||
|
_eggs.Remove(new_snake_head);
|
||||||
|
}
|
||||||
|
|
||||||
|
SnakeHead = new_snake_head;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user