Advertisement
anilak

Tron3D

Sep 17th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2.  
  3. class Tron3D
  4. {
  5.     static bool IsInside(bool[,] arr, int x, int y)
  6.     {
  7.         if (x >= 0 && x < arr.GetLength(0)) return true;
  8.         else return false;
  9.     }
  10.  
  11.     static int Play(int player, bool[,] forbidden, int[,] currPlace, bool[] isLoser, string movement, int movePos)
  12.     {
  13.         int i = movePos;
  14.         while (i < movement.Length && movement[i] != 'M')
  15.         {
  16.             if (movement[i] == 'L')
  17.             {
  18.                 if (currPlace[player, 2] == 0)
  19.                 {
  20.                     currPlace[player, 2] = currPlace[player, 3];
  21.                     currPlace[player, 3] = 0;
  22.                 }
  23.                 else
  24.                 {
  25.                     currPlace[player, 3] = -currPlace[player, 2];
  26.                     currPlace[player, 2] = 0;
  27.                 }
  28.             }
  29.             else
  30.             {
  31.                 if (currPlace[player, 2] == 0)
  32.                 {
  33.                     currPlace[player, 2] = -currPlace[player, 3];
  34.                     currPlace[player, 3] = 0;
  35.                 }
  36.                 else
  37.                 {
  38.                     currPlace[player, 3] = currPlace[player, 2];
  39.                     currPlace[player, 2] = 0;
  40.                 }
  41.             }
  42.             i++;
  43.         }
  44.         if (currPlace[player, 1] + currPlace[player, 3] == forbidden.GetLength(1))
  45.         {
  46.             currPlace[player, 1] = 0;
  47.         }
  48.         else if (currPlace[player, 1] + currPlace[player, 3] == -1)
  49.         {
  50.             currPlace[player, 1] = forbidden.GetLength(1) - 1;
  51.         }
  52.         else if (IsInside(forbidden, currPlace[player, 0] + currPlace[player, 2], currPlace[player, 1] + currPlace[player, 3])
  53.             && !forbidden[currPlace[player, 0] + currPlace[player, 2], currPlace[player, 1] + currPlace[player, 3]])
  54.         {
  55.             currPlace[player, 0] += currPlace[player, 2];
  56.             currPlace[player, 1] += currPlace[player, 3];
  57.         }
  58.         else if (!IsInside(forbidden, currPlace[player, 0] + currPlace[player, 2], currPlace[player, 1] + currPlace[player, 3]))
  59.         {
  60.             isLoser[player] = true;
  61.         }
  62.         else
  63.         {
  64.             currPlace[player, 0] += currPlace[player, 2];
  65.             currPlace[player, 1] += currPlace[player, 3];
  66.             isLoser[player] = true;
  67.         }
  68.         return ++i;
  69.     }
  70.  
  71.     static void Main()
  72.     {
  73.         bool[] isLoser = new bool[2];
  74.         string[] input = Console.ReadLine().Split(' ');
  75.         int x = int.Parse(input[0]);
  76.         int y = int.Parse(input[1]);
  77.         int z = int.Parse(input[2]);
  78.         bool[,] forbidden = new bool[x + 1, 2 * (y + z)];
  79.         string[] play = new string[2];
  80.         play[0] = Console.ReadLine();   //movement of the first player;
  81.         play[1] = Console.ReadLine();   //movement of the second player;
  82.         int[,] currPlace = new int[2, 4];//initaial coordinates and moving direction;
  83.         currPlace[0, 0] = x / 2;        //x coordinate for the first player;
  84.         currPlace[0, 1] = y / 2;        //y coordinate for the first player;
  85.         currPlace[0, 2] = 0;            //direction 0 means no change in x while moving for the first player;
  86.         currPlace[0, 3] = 1;            //direction 1 means change with +1 in y while moving for the first player;
  87.         currPlace[1, 0] = x / 2;        //same for the second player
  88.         currPlace[1, 1] = y + z + y / 2;
  89.         currPlace[1, 2] = 0;            
  90.         currPlace[1, 3] = -1;
  91.         forbidden[currPlace[0, 0], currPlace[0, 1]] = true;
  92.         forbidden[currPlace[1, 0], currPlace[1, 1]] = true;
  93.         //Console.WriteLine("{0} {1}", currPlace[0, 0], currPlace[0, 1]);
  94.         //Console.WriteLine("{0} {1}", currPlace[1, 0], currPlace[1, 1]);
  95.         int i = 0;
  96.         for (int j = 0; !isLoser[0] && !isLoser[1];)
  97.         {
  98.             i = Play(0, forbidden, currPlace, isLoser, play[0], i);
  99.             //Console.WriteLine("{0} {1}", currPlace[0, 0], currPlace[0, 1]);
  100.             j = Play(1, forbidden, currPlace, isLoser, play[1], j);
  101.             //Console.WriteLine("{0} {1}", currPlace[1, 0], currPlace[1, 1]);
  102.             if(currPlace[0, 0] == currPlace[1, 0] && currPlace[0, 1] == currPlace[1, 1])
  103.             {
  104.                 isLoser[0] = true;
  105.                 isLoser[1] = true;
  106.             }
  107.             forbidden[currPlace[0, 0], currPlace[0, 1]] = true;
  108.             forbidden[currPlace[1, 0], currPlace[1, 1]] = true;
  109.         }
  110.         if (isLoser[0] && isLoser[1]) Console.WriteLine("DRAW");
  111.         else if (!isLoser[0] && isLoser[1]) Console.WriteLine("RED");
  112.         else if (isLoser[0] && !isLoser[1]) Console.WriteLine("BLUE");
  113.         Console.WriteLine(Math.Abs(currPlace[0, 0] - x / 2) + Math.Abs(currPlace[0, 1] - y / 2));
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement