Advertisement
alidzhikov

CollectTheCoins

May 8th, 2015
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. class CollectTheCoins
  3. {
  4.     static void Main()
  5.     {
  6.         string[] board = new string[4];
  7.  
  8.         string boardLine = String.Empty;
  9.         for (int row = 0; row < board.GetLength(0); row++)
  10.         {
  11.             board[row] = Console.ReadLine();
  12.            
  13.         }
  14.  
  15.         int currentRow = 0;
  16.         int currentCol = 0;
  17.         int coinsCount = 0;
  18.         int wallHitsCount = 0;
  19.         string movementCommands = Console.ReadLine();
  20.         foreach (char currentDirection in movementCommands)
  21.         {
  22.             if (currentDirection == 'V')
  23.             {
  24.                 currentRow++;
  25.                 if (currentRow >= board.GetLength(0))
  26.                 {
  27.                     currentRow--;
  28.                     wallHitsCount++;
  29.                     continue;
  30.                 }
  31.                 else if (currentCol >= board[currentRow].Length)
  32.                 {
  33.                     currentRow--;
  34.                     wallHitsCount++;
  35.                     continue;
  36.                 }
  37.             }
  38.             else if (currentDirection == '>')
  39.             {
  40.                 currentCol++;
  41.                 if (currentCol >= board[currentRow].Length)
  42.                 {
  43.                     currentCol--;
  44.                     wallHitsCount++;
  45.                     continue;
  46.                 }
  47.             }
  48.             else if (currentDirection == '<')
  49.             {
  50.                 currentCol--;
  51.                 if (currentCol < 0)
  52.                 {
  53.                     currentCol++;
  54.                     wallHitsCount++;
  55.                     continue;
  56.                 }
  57.             }
  58.             else if (currentDirection == '^')
  59.             {
  60.                 currentRow--;
  61.                 if (currentRow < 0)
  62.                 {
  63.                     currentRow++;
  64.                     wallHitsCount++;
  65.                     continue;
  66.                 }
  67.                 else if (currentCol >= board[currentRow].Length)
  68.                 {
  69.                     currentRow++;
  70.                     wallHitsCount++;
  71.                     continue;
  72.                 }
  73.             }
  74.  
  75.             if (board[currentRow][currentCol].Equals('$'))
  76.             {
  77.                 coinsCount++;
  78.             }
  79.         }
  80.         Console.WriteLine("Coins collected: {0}", coinsCount);
  81.         Console.WriteLine("Walls hit: {0}", wallHitsCount);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement