Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CollectTheCoijns
- {
- public class CollectTheCoins
- {
- public static int[] currPositionOnTheBoard;
- public static string[] boardLayout;
- public static int coinsCollected = 0;
- public static int wallsHit = 0;
- public static void Main()
- {
- boardLayout = new string[4];
- for (int currRow = 0; currRow < boardLayout.Length; currRow++)
- {
- boardLayout[currRow] = Console.ReadLine();
- }
- string movesToMake = Console.ReadLine();
- currPositionOnTheBoard = new int[2];
- currPositionOnTheBoard[0] = 0;
- currPositionOnTheBoard[1] = 0;
- if (boardLayout[currPositionOnTheBoard[0]][currPositionOnTheBoard[1]] == '$')
- {
- coinsCollected++;
- }
- for (int currMove = 0; currMove < movesToMake.Length; currMove++)
- {
- char move = movesToMake[currMove];
- switch (move)
- {
- case '<':
- MakeAMoveAndCollectACoin(0, -1);
- break;
- case '^':
- MakeAMoveAndCollectACoin(-1, 0);
- break;
- case '>':
- MakeAMoveAndCollectACoin(0, 1);
- break;
- case 'V':
- MakeAMoveAndCollectACoin(1, 0);
- break;
- default:
- break;
- }
- }
- Console.WriteLine($"Coins collected: {coinsCollected}");
- Console.WriteLine($"Walls hit: {wallsHit}");
- }
- public static void MakeAMoveAndCollectACoin(int upAndDown, int rightAndLeft)
- {
- int[] lastPositionOnTheBoard = currPositionOnTheBoard.ToArray();
- try
- {
- currPositionOnTheBoard[0] += upAndDown;
- currPositionOnTheBoard[1] += rightAndLeft;
- if (boardLayout[currPositionOnTheBoard[0]][currPositionOnTheBoard[1]] == '$')
- {
- coinsCollected++;
- }
- }
- catch (IndexOutOfRangeException)
- {
- currPositionOnTheBoard = lastPositionOnTheBoard.ToArray();
- wallsHit++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment