Advertisement
Guest User

05. Knight Path

a guest
Mar 31st, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | None | 0 0
  1. using System;
  2.  
  3. class KnightPath
  4. {
  5.     static void Main()
  6.     {
  7.         int[] numbers = new int[8];
  8.  
  9.         for (int i = 0; i < numbers.Length; i++)
  10.         {
  11.             numbers[i] = 0;
  12.         }
  13.  
  14.         string input = Console.ReadLine();
  15.  
  16.         int currentNumber = 0;
  17.         int position = 0;
  18.  
  19.         numbers[currentNumber] = 1;
  20.         while (input != "stop")
  21.         {
  22.  
  23.             string[] inputAsArray = input.Split();
  24.  
  25.             string firstCommand = inputAsArray[0];
  26.             string secondCommand = inputAsArray[1];
  27.            
  28.  
  29.             if (firstCommand == "left" && secondCommand == "up")
  30.             {
  31.                 if (position + 2 <= 7 && currentNumber - 1 >= 0)
  32.                 {
  33.                     position += 2;
  34.                     currentNumber--;
  35.                 }
  36.             }
  37.             else if (firstCommand == "left" && secondCommand == "down")
  38.             {
  39.                 if (position + 2 <= 7 && currentNumber + 1 <= 7)
  40.                 {
  41.                     position += 2;
  42.                     currentNumber++;
  43.                 }
  44.             }
  45.             else if (firstCommand == "right" && secondCommand == "up")
  46.             {
  47.                 if (position - 2 >= 0 && currentNumber - 1 >= 0)
  48.                 {
  49.                     position -= 2;
  50.                     currentNumber--;;
  51.                 }
  52.             }
  53.             else if (firstCommand == "right" && secondCommand == "down")
  54.             {
  55.                 if (position - 2 >= 0 && currentNumber + 1 <= 7)
  56.                 {
  57.                     position -= 2;
  58.                     currentNumber++;
  59.                 }
  60.             }
  61.             else if (firstCommand == "up" && secondCommand == "left")
  62.             {
  63.                 if (position + 1 <= 7 && currentNumber - 2 >= 0)
  64.                 {
  65.                     position++;
  66.                     currentNumber -= 2;
  67.                 }
  68.             }
  69.             else if (firstCommand == "up" && secondCommand == "right")
  70.             {
  71.                 if (position - 1 >= 0 && currentNumber - 2 >= 0)
  72.                 {
  73.                     position--;
  74.                     currentNumber -= 2;
  75.                 }
  76.             }
  77.             else if (firstCommand == "down" && secondCommand == "left")
  78.             {
  79.                 if (position + 1 <= 7 && currentNumber + 2 <= 7)
  80.                 {
  81.                     position++;
  82.                     currentNumber += 2;
  83.                 }
  84.             }
  85.             if (firstCommand == "down" && secondCommand == "right")
  86.             {
  87.                 if (position - 1 >= 0 && currentNumber + 2 <= 7)
  88.                 {
  89.                     position--;
  90.                     currentNumber += 2;
  91.                 }
  92.             }
  93.             if (((numbers[currentNumber] >> position) & 1) == 0)
  94.             {
  95.                 numbers[currentNumber] ^= (1 << position);
  96.             }
  97.  
  98.             input = Console.ReadLine();
  99.         }
  100.  
  101.         bool zeroBoard = true;
  102.  
  103.         foreach (int number in numbers)
  104.         {
  105.             if (number != 0)
  106.             {
  107.                 Console.WriteLine(number);
  108.                 zeroBoard = false;
  109.             }
  110.         }
  111.  
  112.         if (zeroBoard)
  113.         {
  114.             Console.WriteLine("[Board is empty]");
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement