Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Snake
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[,] matrix = new char[n, n];
- //вход
- for (int row = 0; row < n; row++)
- {
- string currentRow = Console.ReadLine();
- for (int col = 0; col < currentRow.Length; col++)
- {
- matrix[row, col] = currentRow[col];
- }
- }
- int snakeRow = 0;
- int snakeCol = 0;
- int lairRow1 = 0;
- int lairCol1 = 0;
- int lairRow2 = 0;
- int lairCol2 = 0;
- //проверка дали матрицата съдържа дадена буква и къде
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- if (matrix[row, col] == 'S')
- {
- snakeRow = row;
- snakeCol = col;
- }
- if (matrix[row, col] == 'B' && matrix[row, col] != matrix[lairRow1, lairCol1])
- {
- lairRow1 = row;
- lairCol1 = col;
- }
- if (matrix[row, col] == 'B')
- {
- if (matrix[row,col]!=matrix[lairRow1,lairCol1])
- {
- lairRow2 = row;
- lairCol2 = col;
- }
- }
- }
- }
- string input = Console.ReadLine();
- int food = 0;
- while (food! < 10)
- {
- matrix[snakeRow, snakeCol] = '.';
- snakeRow = MoveRow(snakeRow, input);
- snakeCol = MoveCol(snakeCol, input);
- if (!IsPossitionValid(snakeRow, snakeCol, n, n))
- {
- Console.WriteLine("Game over!");
- break;
- }
- if (matrix[snakeRow, snakeCol] == '*')
- {
- food++;
- }
- if (matrix[snakeRow, snakeCol] == 'B')
- {
- if (matrix[snakeRow, snakeCol] == matrix[lairRow1, lairCol1])
- {
- matrix[snakeRow, snakeCol] = '.';
- matrix[snakeRow, snakeCol] = matrix[lairRow2, lairCol2];
- if (!IsPossitionValid(snakeRow, snakeCol, n, n))
- {
- Console.WriteLine("Game over!");
- break;
- }
- }
- else
- {
- matrix[snakeRow, snakeCol] = '.';
- matrix[snakeRow, snakeCol] = matrix[lairRow1, lairCol1];
- if (!IsPossitionValid(snakeRow, snakeCol, n, n))
- {
- Console.WriteLine("Game over!");
- break;
- }
- }
- }
- matrix[snakeRow, snakeCol] = 'S';
- input = Console.ReadLine();
- }
- Console.WriteLine($"You won! You fed the snake.\nFood eaten: {food}");
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- //илизане от граници на матрицата
- public static bool IsPossitionValid(int row, int col, int rows, int cols)
- {
- if (row < 0 || row >= rows)
- {
- return false;
- }
- if (col < 0 || col >= cols)
- {
- return false;
- }
- return true;
- }
- //движение горе-долу
- public static int MoveRow(int row, string movement)
- {
- if (movement == "up")
- {
- return row - 1;
- }
- if (movement == "down")
- {
- return row + 1;
- }
- return row;
- }
- //движение ляво-дясно
- public static int MoveCol(int col, string movement)
- {
- if (movement == "left")
- {
- return col - 1;
- }
- if (movement == "right")
- {
- return col + 1;
- }
- return col;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment