private static void ChangeMatrix(Santa santa, char[,] matrix) { if (matrix[santa.X, santa.Y] == 'V') // if is nice kid { GivePresentToNiceKid(santa); matrix[santa.X, santa.Y] = '-'; } if (matrix[santa.X, santa.Y] == 'X') // if is naughty kid { matrix[santa.X, santa.Y] = '-'; } if (matrix[santa.X, santa.Y] == 'C') // if it is a cookie { if (matrix[santa.X - 1, santa.Y] == 'V') { GivePresentToNiceKid(santa); matrix[santa.X - 1, santa.Y] = '-'; } else if (matrix[santa.X - 1, santa.Y] == 'X') { santa.Presents--; matrix[santa.X - 1, santa.Y] = '-'; } if (matrix[santa.X + 1, santa.Y] == 'V') { GivePresentToNiceKid(santa); matrix[santa.X + 1, santa.Y] = '-'; } else if (matrix[santa.X + 1, santa.Y] == 'X') { santa.Presents--; matrix[santa.X + 1, santa.Y] = '-'; } if (matrix[santa.X, santa.Y - 1] == 'V') { GivePresentToNiceKid(santa); matrix[santa.X, santa.Y - 1] = '-'; } else if (matrix[santa.X, santa.Y - 1] == 'X') { santa.Presents--; matrix[santa.X, santa.Y - 1] = '-'; } if (matrix[santa.X, santa.Y + 1] == 'V') { GivePresentToNiceKid(santa); matrix[santa.X, santa.Y + 1] = '-'; } else if (matrix[santa.X, santa.Y + 1] == 'X') { santa.Presents--; matrix[santa.X, santa.Y + 1] = '-'; } matrix[santa.X, santa.Y] = '-'; // remove the Cookie mark } }
private static void FillMatrix(int matrixSize, char[,] matrix, Santa santa) { for (int i = 0; i < matrixSize; i++) { var matrixLine = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j < matrixSize; j++) { matrix[i, j] = char.Parse(matrixLine[j]); FindInitialPlayerPNiceKidsCount(santa, matrix, j, i); } } }
static int HappySanta(Santa santa, string[,] matrix) { int presentsGoodKidsFromHappy = 0; if (matrix[santa.SantaRow - 1, santa.SantaCol] == "V") { presentsGoodKidsFromHappy++; santa.PresentsCount--; matrix[santa.SantaRow - 1, santa.SantaCol] = "-"; } if (matrix[santa.SantaRow + 1, santa.SantaCol] == "V") { presentsGoodKidsFromHappy++; santa.PresentsCount--; matrix[santa.SantaRow + 1, santa.SantaCol] = "-"; } if (matrix[santa.SantaRow, santa.SantaCol - 1] == "V") { presentsGoodKidsFromHappy++; santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol - 1] = "-"; } if (matrix[santa.SantaRow, santa.SantaCol + 1] == "V") { presentsGoodKidsFromHappy++; santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol + 1] = "-"; } if (matrix[santa.SantaRow - 1, santa.SantaCol] == "X") { santa.PresentsCount--; matrix[santa.SantaRow - 1, santa.SantaCol] = "-"; } if (matrix[santa.SantaRow + 1, santa.SantaCol] == "X") { santa.PresentsCount--; matrix[santa.SantaRow + 1, santa.SantaCol] = "-"; } if (matrix[santa.SantaRow, santa.SantaCol - 1] == "X") { santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol - 1] = "-"; } if (matrix[santa.SantaRow, santa.SantaCol + 1] == "X") { santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol + 1] = "-"; } return(presentsGoodKidsFromHappy); }
private static void FindInitialPlayerPNiceKidsCount(Santa santa, char[,] matrix, int j, int i) { if (matrix[i, j] == 'S') { matrix[i, j] = '-'; //need to remove initial santa mark santa.X = i; santa.Y = j; } if (matrix[i, j] == 'V') { santa.NiceKidsCount++; } }
private static bool ChangePlayerPosition(Santa santa, char[,] matrix, string playerCommand) // if hit the wall stay there { if (playerCommand == "up") { if (santa.X - 1 < 0) { santa.X = 0; return(true); } santa.X -= 1; return(false); } if (playerCommand == "down") { if (santa.X + 1 >= matrix.GetLength(0)) { santa.X = matrix.GetLength(0) - 1; return(true); } santa.X += 1; return(false); } if (playerCommand == "left") { if (santa.Y - 1 < 0) { santa.Y = 0; return(true); } santa.Y -= 1; return(false); } if (playerCommand == "right") { if (santa.Y + 1 >= matrix.GetLength(1)) { santa.Y = matrix.GetLength(1) - 1; return(true); } santa.Y += 1; return(false); } return(false); }
static Santa GetSanta(string[,] matrix, int countPresents) { int santaRow = -1; int santaCol = -1; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { if (matrix[row, col] == "S") { santaRow = row; santaCol = col; } } } Santa santa = new Santa(santaRow, santaCol, countPresents); return(santa); }
static void Main(string[] args) { Santa santa = new Santa(); santa.Presents = int.Parse(Console.ReadLine()); int matrixSize = int.Parse(Console.ReadLine()); char[,] matrix = new char[matrixSize, matrixSize]; FillMatrix(matrixSize, matrix, santa); int initialKidCount = santa.NiceKidsCount; string command = Console.ReadLine(); bool ifHitWall = false; while (command != "Christmas morning") { if (ChangePlayerPosition(santa, matrix, command) && ifHitWall == false) { ifHitWall = true; //if at least once hit a wall. Does, If Santa goes out of the neighborhood, means hit a wall ? } ChangeMatrix(santa, matrix); if (santa.Presents == 0) { break; } command = Console.ReadLine(); } matrix[santa.X, santa.Y] = 'S'; // mark santa final position if (santa.Presents == 0 || ifHitWall) { Console.WriteLine("Santa ran out of presents!"); } Print(matrix); Console.WriteLine(santa.NiceKidsCount == 0 ? $"Good job, Santa! {initialKidCount} happy nice kid/s." : $"No presents for {santa.NiceKidsCount} nice kid/s."); }
static void Main(string[] args) { int countPresents = int.Parse(Console.ReadLine()); int n = int.Parse(Console.ReadLine()); var matrix = GetMatrix(n); //PrintMatrix(matrix); //S-Santa coordinates //X-naughty kid no present //C cooky=>good Santa=> presents all kids around-up,down, left, right. Position C int goodKidsNum = GetGoodKidsNum(matrix); int presentsGoodKids = 0; Santa santa = GetSanta(matrix, countPresents); matrix[santa.SantaRow, santa.SantaCol] = "-"; while (true) { string position = Console.ReadLine(); if (position == "Christmas morning") { matrix[santa.SantaRow, santa.SantaCol] = "S"; PrintMatrix(matrix); if (presentsGoodKids == goodKidsNum) { Console.WriteLine($"Good job, Santa! {presentsGoodKids} happy nice kid/s."); } else { Console.WriteLine($"No presents for {goodKidsNum-presentsGoodKids} nice kid/s."); } break; } else { if (position == "up") { if (santa.SantaRow - 1 >= 0) { santa.SantaRow--; if (matrix[santa.SantaRow, santa.SantaCol] == "V") { presentsGoodKids++; santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol] = "-"; } else if (matrix[santa.SantaRow, santa.SantaCol] == "X") { matrix[santa.SantaRow, santa.SantaCol] = "-"; continue; } else if (matrix[santa.SantaRow, santa.SantaCol] == "C") { presentsGoodKids += HappySanta(santa, matrix); } } else { continue; } } else if (position == "down") { if (santa.SantaRow + 1 < n) { santa.SantaRow++; if (matrix[santa.SantaRow, santa.SantaCol] == "V") { presentsGoodKids++; santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol] = "-"; } else if (matrix[santa.SantaRow, santa.SantaCol] == "X") { matrix[santa.SantaRow, santa.SantaCol] = "-"; continue; } else if (matrix[santa.SantaRow, santa.SantaCol] == "C") { presentsGoodKids += HappySanta(santa, matrix); } } else { continue; } } else if (position == "left") { if (santa.SantaCol - 1 >= 0) { santa.SantaCol--; if (matrix[santa.SantaRow, santa.SantaCol] == "V") { presentsGoodKids++; santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol] = "-"; } else if (matrix[santa.SantaRow, santa.SantaCol] == "X") { matrix[santa.SantaRow, santa.SantaCol] = "-"; continue; } else if (matrix[santa.SantaRow, santa.SantaCol] == "C") { presentsGoodKids += HappySanta(santa, matrix); } } else { continue; } } else if (position == "right") { if (santa.SantaCol + 1 < n) { santa.SantaCol++; if (matrix[santa.SantaRow, santa.SantaCol] == "V") { presentsGoodKids++; santa.PresentsCount--; matrix[santa.SantaRow, santa.SantaCol] = "-"; } else if (matrix[santa.SantaRow, santa.SantaCol] == "X") { matrix[santa.SantaRow, santa.SantaCol] = "-"; continue; } else if (matrix[santa.SantaRow, santa.SantaCol] == "C") { presentsGoodKids += HappySanta(santa, matrix); } } else { continue; } } if (santa.PresentsCount == 0) { Console.WriteLine("Santa ran out of presents!"); matrix[santa.SantaRow, santa.SantaCol] = "S"; PrintMatrix(matrix); if (presentsGoodKids == goodKidsNum) { Console.WriteLine($"Good job, Santa! {presentsGoodKids} happy nice kid/s."); } else { Console.WriteLine($"No presents for {goodKidsNum - presentsGoodKids} nice kid/s."); } break; } } } }
private static void GivePresentToNiceKid(Santa santa) { santa.Presents--; santa.NiceKidsCount--; }