simonradev

CollectingCoins

Jan 30th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CollectTheCoijns
  8. {
  9.     public class CollectTheCoins
  10.     {
  11.         public static int[] currPositionOnTheBoard;
  12.         public static string[] boardLayout;
  13.         public static int coinsCollected = 0;
  14.         public static int wallsHit = 0;
  15.  
  16.         public static void Main()
  17.         {
  18.             boardLayout = new string[4];
  19.  
  20.             for (int currRow = 0; currRow < boardLayout.Length; currRow++)
  21.             {
  22.                 boardLayout[currRow] = Console.ReadLine();
  23.             }
  24.  
  25.             string movesToMake = Console.ReadLine();
  26.  
  27.             currPositionOnTheBoard = new int[2];
  28.             currPositionOnTheBoard[0] = 0;
  29.             currPositionOnTheBoard[1] = 0;
  30.             if (boardLayout[currPositionOnTheBoard[0]][currPositionOnTheBoard[1]] == '$')
  31.             {
  32.                 coinsCollected++;
  33.             }
  34.  
  35.             for (int currMove = 0; currMove < movesToMake.Length; currMove++)
  36.             {
  37.                 char move = movesToMake[currMove];
  38.  
  39.                 switch (move)
  40.                 {
  41.                     case '<':
  42.                         MakeAMoveAndCollectACoin(0, -1);
  43.                         break;
  44.  
  45.                     case '^':
  46.                         MakeAMoveAndCollectACoin(-1, 0);
  47.                         break;
  48.  
  49.                     case '>':
  50.                         MakeAMoveAndCollectACoin(0, 1);
  51.                         break;
  52.  
  53.                     case 'V':
  54.                         MakeAMoveAndCollectACoin(1, 0);
  55.                         break;
  56.  
  57.                     default:
  58.                         break;
  59.                 }
  60.             }
  61.  
  62.             Console.WriteLine($"Coins collected: {coinsCollected}");
  63.             Console.WriteLine($"Walls hit: {wallsHit}");
  64.         }
  65.  
  66.         public static void MakeAMoveAndCollectACoin(int upAndDown, int rightAndLeft)
  67.         {
  68.             int[] lastPositionOnTheBoard = currPositionOnTheBoard.ToArray();
  69.  
  70.             try
  71.             {
  72.                 currPositionOnTheBoard[0] += upAndDown;
  73.                 currPositionOnTheBoard[1] += rightAndLeft;
  74.  
  75.                 if (boardLayout[currPositionOnTheBoard[0]][currPositionOnTheBoard[1]] == '$')
  76.                 {
  77.                     coinsCollected++;
  78.                 }
  79.             }
  80.             catch (IndexOutOfRangeException)
  81.             {
  82.                 currPositionOnTheBoard = lastPositionOnTheBoard.ToArray();
  83.  
  84.                 wallsHit++;
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment