/// <summary> /// Main method where timer-reload event is happening /// </summary> /// <param name="source"></param> /// <param name="e"></param> private void ReloadEvent(Object source, ElapsedEventArgs e) { try { _turnNumber++; _snake.MoveInDirection(_currentDirection); // if game was lost we are offing timer // setting all properties back to if (_snake.IsEatenByYourself() || !_gameboard.IsInsideGameboard(_snake.GetHead())) { SetupPropertiesForStartOfGame(); return; } // and here checking if food was eaten // we need to re-generate it if (_food.IsCellWithFood(_snake.GetHead())) { // add cell to tail of snake _snake.AddNewPiece(); // if found food in head of snake we delete it _food.DeleteFoodByCell(_snake.GetHead()); // regenerate new food _food.GenerateNewFood(_cellProvider.GetNonUsedCells()); } } finally { _timer.Start(); } }
public void AddNewCellToSnake_ShouldLengthenTail() { // test is done according to rule // that snake is at first pointing to the top // and consists of 2 cells initially // Arrange _snake.LocateSnakeAtStart(_cellProvider.GetSnakeStartingCells()); var cellCount = _snake.SnakePieces.Count; var tailCell = _snake.SnakePieces.Last.Value; var expectedAddedCell = tailCell + new Cell() { X = 0, Y = 1 }; // Act _snake.AddNewPiece(); // Assert _snake.SnakePieces.Count.ShouldBe(cellCount + 1); _snake.SnakePieces.Last.Value.Equals(expectedAddedCell).ShouldBeTrue(); }
static void Main(string[] args) { // GameService gameService = new GameService(); var gameboard = new Gameboard(20, 20); var snake = new Snake(); var food = new Food(); var cellProvider = new CellProvider(gameboard, snake); food.FoodCells.Add(new Cell() { X = 10, Y = 9 }); var k = 0; var direction = Direction.Top; while (true) { Thread.Sleep(1000); k++; if (k == 5 || k == 10 || k == 15) { direction += 1; continue; } if (k == 50) { break; } snake.MoveInDirection(direction); // if game was lost we are offing timer // setting all properties back to if (snake.IsEatenByYourself() || !gameboard.IsInsideGameboard(snake.GetHead())) { Console.WriteLine("\n\n\n restart of game \n\n\n"); snake.LocateSnakeAtStart(cellProvider.GetSnakeStartingCells()); food.DeleteAllFood(); food.GenerateNewFood(cellProvider.GetNonUsedCells()); continue; } // and here checking if food was eating // -> do we need to re-generate it ??? if (food.IsCellWithFood(snake.GetHead())) { // add cell to tail of snake snake.AddNewPiece(); // if found food in head of snake we delete it food.DeleteFoodByCell(snake.GetHead()); // regenerate new food food.GenerateNewFood(cellProvider.GetNonUsedCells()); } Console.WriteLine($"turn #{k}:"); Console.WriteLine(snake); Console.WriteLine(food); Console.WriteLine(); } Console.ReadLine(); }