Advertisement
a_tifonoff

Knight Path

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