Advertisement
Guest User

Untitled

a guest
Sep 21st, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2.  
  3. class CollectTheCoins
  4. {
  5.     static int currentRow = 0;
  6.     static int currentCol = 0;
  7.     static int coinCount = 0;
  8.     static int wallHits = 0;
  9.  
  10.     static void Main()
  11.     {
  12.         string[] board = new string[4];
  13.         FillTheBoard(board);
  14.  
  15.         string commands = Console.ReadLine();
  16.         MoveAroundTheBoard(commands, board);
  17.  
  18.         Console.WriteLine();
  19.         Console.WriteLine("Coins collected: {0}", coinCount);
  20.         Console.WriteLine("Walls hit: {0}", wallHits);
  21.     }
  22.  
  23.     static void FillTheBoard(string[] board)
  24.     {
  25.         for (int i = 0; i < board.Length; i++)
  26.         {
  27.             board[i] = Console.ReadLine();
  28.         }
  29.     }
  30.  
  31.     static void MoveAroundTheBoard(string commands, string[] board)
  32.     {
  33.         //Collect the coin on [0,0] (if there is one)
  34.         CollectCoin(board);
  35.  
  36.         foreach (char command in commands)
  37.         {
  38.             if (command == '>')
  39.             {
  40.                 PerformMove(board, currentRow, currentCol + 1);
  41.             }
  42.             else if (command == '<')
  43.             {
  44.                 PerformMove(board, currentRow, currentCol - 1);
  45.             }
  46.             else if (command == '^')
  47.             {
  48.                 PerformMove(board, currentRow - 1, currentCol - 1);
  49.             }
  50.             else if (command == 'V')
  51.             {
  52.                 PerformMove(board, currentRow + 1, currentCol - 1);
  53.             }
  54.             else
  55.             {
  56.                 Console.WriteLine();
  57.                 Console.WriteLine("\"{0}\" is an unrecognised command.", command);
  58.                 Console.WriteLine("The command \"{0}\" was not executed.", command);
  59.             }
  60.         }
  61.     }
  62.  
  63.     static void PerformMove(string[] field, int row, int col)
  64.     {
  65.         if (ValidMove(field, row, col))
  66.         {
  67.             currentCol = col;
  68.             currentRow = row;
  69.             CollectCoin(field);
  70.         }
  71.         else
  72.         {
  73.             wallHits++;
  74.         }
  75.     }
  76.  
  77.     static bool ValidMove(string[] field, int row, int col)
  78.     {
  79.         bool isValid = false;
  80.  
  81.         if (row >= 0 && row < field.Length)
  82.         {
  83.             if (col >= 0 && col < field[row].Length)
  84.             {
  85.                 isValid = true;
  86.             }
  87.         }
  88.         return isValid;
  89.     }
  90.  
  91.     static void CollectCoin(string[] field)
  92.     {
  93.         if (field[currentRow][currentCol] == '$')
  94.         {
  95.             coinCount++;
  96.         }
  97.     }
  98.  
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement