Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace _01._Bombs
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[,] matrix = new char[n, n];
- int snakeRow = -1;
- int snakeCol = -1;
- int snakeEatCount = 0;
- int firstBombRow = -1;
- int firstBombCol = -1;
- int secondBombRow = -1;
- int secondBombCol = -1;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- var data = Console.ReadLine();
- var command = data.ToCharArray();
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[row, col] = command[col];
- if (matrix[row, col] == 'S')
- {
- snakeRow = row;
- snakeCol = col;
- }
- else if (matrix[row, col] == 'B')
- {
- if (firstBombRow == -1 && firstBombCol == -1)
- {
- firstBombRow = row;
- firstBombCol = col;
- }
- else
- {
- secondBombRow = row;
- secondBombCol = col;
- }
- }
- }
- }
- matrix[snakeRow, snakeCol] = '.';
- while (true)
- {
- int snakeOldRow = snakeRow;
- int snakeOldCol = snakeCol;
- if (snakeEatCount == 10) //down=(++col, wol), up=(--row, col), right=(++col, row), left=(--col, row);
- {
- Console.WriteLine("You won! You fed the snake.");
- Console.WriteLine($"Food eaten: {snakeEatCount}");
- matrix[snakeRow, snakeCol] = 'S';
- Print(matrix);
- break;
- }
- string cmd = Console.ReadLine();
- if (cmd == "up")
- {
- snakeRow--;
- }
- else if (cmd == "down")
- {
- snakeRow++;
- }
- else if (cmd == "right")
- {
- snakeCol++;
- }
- else if (cmd == "left")
- {
- snakeCol--;
- }
- if (!IsValidCell(snakeRow, snakeCol, n)) //is not ready
- {
- Console.WriteLine("Game over!");
- Console.WriteLine($"Food eaten: {snakeEatCount}");
- Print(matrix);
- break;
- }
- if (matrix[snakeRow, snakeCol] == '*')
- {
- snakeEatCount++;
- }
- if(matrix[snakeRow, snakeCol] == 'B')
- {
- matrix[snakeRow, snakeCol] = '.';
- if(snakeRow == firstBombRow && snakeCol == firstBombCol)
- {
- snakeRow = secondBombRow;
- snakeCol = secondBombCol;
- }
- else
- {
- snakeRow = firstBombRow;
- snakeCol = firstBombCol;
- }
- matrix[snakeRow, snakeCol] = 'S';
- }
- matrix[snakeRow, snakeCol] = '.';
- }
- }
- private static void Print(char[,] matrix)
- {
- 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();
- }
- }
- private static bool IsValidCell(int snakeRow, int snakeCol, int n)
- {
- return snakeCol >= 0 && snakeCol < n && snakeRow >= 0 && snakeRow < n;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement