Advertisement
d_brezoev

Tron3D56

Jan 17th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.89 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 Tron3D
  8. {
  9.     public enum Direction
  10.     {
  11.         Up,
  12.         Down,
  13.         Right,
  14.         Left
  15.     }
  16.     public class Player
  17.     {
  18.         public int currentRow { get; set; }
  19.         public int currentCol { get; set; }
  20.         public int nextRow { get; set; }
  21.         public int nextCol { get; set; }
  22.         public Direction currentDirection { get; set; }
  23.         public bool loose = false;
  24.         public string movesCollection { get; set; }
  25.         public bool Loose
  26.         {
  27.             get
  28.             {
  29.                 return this.loose;
  30.             }
  31.             set
  32.             {
  33.                 this.loose = value;
  34.             }
  35.         }
  36.     }
  37.     class Tron3D
  38.     {
  39.         static void Main(string[] args)
  40.         {
  41.             string input = Console.ReadLine();
  42.             string[] dims = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  43.             int[] dimensions = new int[3];
  44.             for (int i = 0; i < dimensions.Length; i++)
  45.             {
  46.                 dimensions[i] = int.Parse(dims[i]);
  47.             }
  48.             int x = dimensions[0];
  49.             int y = dimensions[1];
  50.             int z = dimensions[2];
  51.            
  52.             string[,] matrix = new string[x + 1,(y + z + y + z)];
  53.             Player redPlayer = new Player();
  54.             Player bluePlayer = new Player();
  55.  
  56.             redPlayer.movesCollection = Console.ReadLine();
  57.             bluePlayer.movesCollection = Console.ReadLine();
  58.  
  59.             redPlayer.currentRow = (x / 2);
  60.             redPlayer.currentCol = y / 2;
  61.             redPlayer.currentDirection = Direction.Right;
  62.             redPlayer.nextRow = redPlayer.currentRow;
  63.             redPlayer.nextCol = redPlayer.currentCol+1;
  64.             matrix[redPlayer.currentRow,redPlayer.currentCol] = "used";
  65.  
  66.             bluePlayer.currentRow = x / 2;
  67.             bluePlayer.currentCol = y+z+y/2;
  68.             bluePlayer.currentDirection = Direction.Left;
  69.             bluePlayer.nextRow = bluePlayer.currentRow;
  70.             bluePlayer.nextCol = bluePlayer.currentCol-1;
  71.             matrix[bluePlayer.currentRow, bluePlayer.currentCol] = "used";
  72.  
  73.             int redPlayerIndexStart = 0;
  74.             int bluePlayerIndexStart = 0;            
  75.             while (true)
  76.             {
  77.                 //red player
  78.                 #region
  79.                 for (int i = redPlayerIndexStart; i < redPlayer.movesCollection.Length; i++)
  80.                 {
  81.                     if (redPlayer.movesCollection[i] == 'L')
  82.                     {
  83.                         if (redPlayer.currentDirection == Direction.Up)
  84.                         {
  85.                             redPlayer.nextRow--;
  86.                             redPlayer.nextCol--;
  87.                             redPlayer.currentDirection = Direction.Left;
  88.                         }
  89.                         else if (redPlayer.currentDirection == Direction.Down)
  90.                         {
  91.                             redPlayer.nextRow++;
  92.                             redPlayer.nextCol++;
  93.                             redPlayer.currentDirection = Direction.Right;
  94.                         }
  95.                         else if (redPlayer.currentDirection == Direction.Right)
  96.                         {
  97.                             redPlayer.nextRow++;
  98.                             redPlayer.nextCol--;
  99.                             redPlayer.currentDirection = Direction.Up;
  100.                         }
  101.                         else if (redPlayer.currentDirection == Direction.Left)
  102.                         {
  103.                             redPlayer.nextRow--;
  104.                             redPlayer.nextCol++;
  105.                             redPlayer.currentDirection = Direction.Down;
  106.                         }
  107.                     }
  108.                     else if (redPlayer.movesCollection[i] == 'R')
  109.                     {
  110.                         if (redPlayer.currentDirection == Direction.Up)
  111.                         {
  112.                             redPlayer.nextRow--;
  113.                             redPlayer.nextCol++;
  114.                             redPlayer.currentDirection = Direction.Right;
  115.                         }
  116.                         else if (redPlayer.currentDirection == Direction.Down)
  117.                         {
  118.                             redPlayer.nextRow++;
  119.                             redPlayer.nextCol--;
  120.                             redPlayer.currentDirection = Direction.Left;
  121.                         }
  122.                         else if (redPlayer.currentDirection == Direction.Right)
  123.                         {
  124.                             redPlayer.nextRow--;
  125.                             redPlayer.nextCol--;
  126.                             redPlayer.currentDirection = Direction.Down;
  127.                         }
  128.                         else if (redPlayer.currentDirection == Direction.Left)
  129.                         {
  130.                             redPlayer.nextRow++;
  131.                             redPlayer.nextCol++;
  132.                             redPlayer.currentDirection = Direction.Up;
  133.                         }
  134.                     }
  135.                     else if (redPlayer.movesCollection[i] == 'M')
  136.                     {                        
  137.                         redPlayer.currentRow = redPlayer.nextRow;
  138.                         redPlayer.currentCol = redPlayer.nextCol;
  139.                         if (redPlayer.currentDirection == Direction.Up)
  140.                         {
  141.                             redPlayer.nextRow++;
  142.                         }
  143.                         else if (redPlayer.currentDirection == Direction.Down)
  144.                         {
  145.                             redPlayer.nextRow--;
  146.                         }
  147.                         else if (redPlayer.currentDirection == Direction.Right)
  148.                         {
  149.                             redPlayer.nextCol++;
  150.                         }
  151.                         else if (redPlayer.currentDirection == Direction.Left)
  152.                         {
  153.                             redPlayer.nextCol--;
  154.                         }
  155.  
  156.                         // if we hit M then we continue
  157.                         redPlayerIndexStart = i + 1;
  158.                         break;
  159.                     }                    
  160.                 }
  161.                 #endregion
  162.  
  163.                 //blue player
  164.                 #region
  165.                 for (int i = bluePlayerIndexStart; i < bluePlayer.movesCollection.Length; i++)
  166.                 {
  167.                     if (bluePlayer.movesCollection[i] == 'L')
  168.                     {
  169.                         if (bluePlayer.currentDirection == Direction.Up)
  170.                         {
  171.                             bluePlayer.nextRow--;
  172.                             bluePlayer.nextCol--;
  173.                             bluePlayer.currentDirection = Direction.Left;
  174.                         }
  175.                         else if (bluePlayer.currentDirection == Direction.Down)
  176.                         {
  177.                             bluePlayer.nextRow++;
  178.                             bluePlayer.nextCol++;
  179.                             bluePlayer.currentDirection = Direction.Right;
  180.                         }
  181.                         else if (bluePlayer.currentDirection == Direction.Right)
  182.                         {
  183.                             bluePlayer.nextRow++;
  184.                             bluePlayer.nextCol--;
  185.                             bluePlayer.currentDirection = Direction.Up;
  186.                         }
  187.                         else if (bluePlayer.currentDirection == Direction.Left)
  188.                         {
  189.                             bluePlayer.nextRow--;
  190.                             bluePlayer.nextCol++;
  191.                             bluePlayer.currentDirection = Direction.Down;
  192.                         }
  193.                     }
  194.                     else if (bluePlayer.movesCollection[i] == 'R')
  195.                     {
  196.                         if (bluePlayer.currentDirection == Direction.Up)
  197.                         {
  198.                             bluePlayer.nextRow--;
  199.                             bluePlayer.nextCol++;
  200.                             bluePlayer.currentDirection = Direction.Right;
  201.                         }
  202.                         else if (bluePlayer.currentDirection == Direction.Down)
  203.                         {
  204.                             bluePlayer.nextRow++;
  205.                             bluePlayer.nextCol--;
  206.                             bluePlayer.currentDirection = Direction.Left;
  207.                         }
  208.                         else if (bluePlayer.currentDirection == Direction.Right)
  209.                         {
  210.                             bluePlayer.nextRow--;
  211.                             bluePlayer.nextCol--;
  212.                             bluePlayer.currentDirection = Direction.Down;
  213.                         }
  214.                         else if (bluePlayer.currentDirection == Direction.Left)
  215.                         {
  216.                             bluePlayer.nextRow++;
  217.                             bluePlayer.nextCol++;
  218.                             bluePlayer.currentDirection = Direction.Up;
  219.                         }
  220.                     }
  221.                     else if (bluePlayer.movesCollection[i] == 'M')
  222.                     {
  223.                         bluePlayer.currentRow = bluePlayer.nextRow;
  224.                         bluePlayer.currentCol = bluePlayer.nextCol;
  225.                         if (bluePlayer.currentDirection == Direction.Up)
  226.                         {
  227.                             bluePlayer.nextRow++;
  228.                         }
  229.                         else if (bluePlayer.currentDirection == Direction.Down)
  230.                         {
  231.                             bluePlayer.nextRow--;
  232.                         }
  233.                         else if (bluePlayer.currentDirection == Direction.Right)
  234.                         {
  235.                             bluePlayer.nextCol++;
  236.                         }
  237.                         else if (bluePlayer.currentDirection == Direction.Left)
  238.                         {
  239.                             bluePlayer.nextCol--;
  240.                         }
  241.  
  242.                         // if we hit M then we continue
  243.                         bluePlayerIndexStart = i + 1;
  244.                         break;
  245.                     }
  246.                 }
  247.                 #endregion                                
  248.  
  249.                 //check if in matrix
  250.                 if (redPlayer.currentRow < 0)
  251.                 {
  252.                     redPlayer.currentRow = 0;
  253.                     redPlayer.Loose = true;
  254.                 }
  255.                 if (redPlayer.currentRow == matrix.GetLength(0))
  256.                 {
  257.                     redPlayer.currentRow = matrix.GetLength(0) - 1;
  258.                     redPlayer.Loose = true;
  259.                 }
  260.                 if (bluePlayer.currentRow < 0)
  261.                 {
  262.                     bluePlayer.currentRow =0;
  263.                     bluePlayer.Loose = true;
  264.                 }
  265.                 if (bluePlayer.currentRow == matrix.GetLength(0))
  266.                 {
  267.                     bluePlayer.currentRow = matrix.GetLength(0) - 1;
  268.                     bluePlayer.Loose = true;
  269.                 }
  270.                 //
  271.                 if (redPlayer.currentCol < 0)
  272.                 {
  273.                     redPlayer.currentCol = matrix.GetLength(1) - 1;                  
  274.                 }
  275.                 if (redPlayer.currentCol == matrix.GetLength(1))
  276.                 {
  277.                     redPlayer.currentCol = 0;                  
  278.                 }
  279.                 if (bluePlayer.currentCol <  0)
  280.                 {
  281.                     bluePlayer.currentCol = matrix.GetLength(1) - 1;
  282.                 }
  283.                 if (bluePlayer.currentCol > matrix.GetLength(1))
  284.                 {
  285.                     bluePlayer.currentCol = 0;
  286.                 }
  287.  
  288.                 //check if hit same cell
  289.                 if (redPlayer.currentRow == bluePlayer.currentRow &&
  290.                     redPlayer.currentCol == bluePlayer.currentCol)
  291.                 {
  292.                     redPlayer.Loose = true;
  293.                     bluePlayer.Loose = true;
  294.                 }
  295.  
  296.                 //check if cell is used
  297.                 if (matrix[redPlayer.currentRow, redPlayer.currentCol] == "used")
  298.                 {
  299.                     redPlayer.Loose = true;
  300.                 }
  301.                 if (matrix[bluePlayer.currentRow, bluePlayer.currentCol] == "used")
  302.                 {
  303.                     bluePlayer.Loose = true;
  304.                 }
  305.  
  306.                 //mark cells as used
  307.                 matrix[redPlayer.currentRow, redPlayer.currentCol] = "used";
  308.                 matrix[bluePlayer.currentRow, bluePlayer.currentCol] = "used";
  309.  
  310.                 //if someone is lost
  311.                 if (redPlayer.Loose || bluePlayer.Loose)
  312.                 {
  313.                     break;
  314.                 }              
  315.             }
  316.  
  317.             //print result according to who wins
  318.             if ((redPlayer.Loose==true) && (bluePlayer.Loose==false))
  319.             {
  320.                 Console.WriteLine("BLUE");
  321.             }
  322.             else if ((redPlayer.Loose==false) && (bluePlayer.Loose==true))
  323.             {
  324.                 Console.WriteLine("RED");
  325.             }
  326.             else if (redPlayer.Loose == true && bluePlayer.Loose == true)
  327.             {
  328.                 Console.WriteLine("DRAW");
  329.             }
  330.             else if (redPlayer.Loose ==false && bluePlayer.Loose == false)
  331.             {
  332.                 Console.WriteLine("DRAW");
  333.             }            
  334.             int distanceRow = Math.Abs(redPlayer.currentRow - x / 2);
  335.             int distanceCol = Math.Abs(redPlayer.currentCol - (y/2));
  336.             if (distanceCol > matrix.GetLength(1) / 2)
  337.             {
  338.                 distanceCol = matrix.GetLength(1) - distanceCol;
  339.             }
  340.             Console.WriteLine(distanceRow + distanceCol);
  341.         }      
  342.     }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement