Advertisement
gabi11

C# Adv.(Exam 24022019) - 02. Tron Racers

Jun 22nd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ExamPreparation
  5. {
  6.     class Program
  7.     {
  8.         static char[][] battleField;
  9.  
  10.         static int firstPlayerRow;
  11.         static int firstPlayerCol;
  12.  
  13.         static int secondPlayerRow;
  14.         static int secondPlayerCol;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             int rows = int.Parse(Console.ReadLine());
  19.  
  20.             battleField = new char[rows][];
  21.  
  22.             Initialize();
  23.  
  24.             while (true)
  25.             {
  26.                 string[] directions = Console.ReadLine().Split();
  27.  
  28.                 string firstPlayerDirection = directions[0];
  29.                 string secondPlayerDirection = directions[1];
  30.  
  31.                 if (firstPlayerDirection == "down")
  32.                 {
  33.                     firstPlayerRow++;
  34.  
  35.                     if (firstPlayerRow == battleField.Length)
  36.                     {
  37.                         firstPlayerRow = 0;
  38.                     }
  39.                 }
  40.                 else if (firstPlayerDirection == "up")
  41.                 {
  42.                     firstPlayerRow--;
  43.  
  44.                     if (firstPlayerRow < 0)
  45.                     {
  46.                         firstPlayerRow = battleField.Length - 1;
  47.                     }
  48.                 }
  49.                 else if (firstPlayerDirection == "left")
  50.                 {
  51.                     firstPlayerCol--;
  52.  
  53.                     if (firstPlayerCol < 0)
  54.                     {
  55.                         firstPlayerCol = battleField[firstPlayerRow].Length - 1;
  56.                     }
  57.                 }
  58.                 else if (firstPlayerDirection == "right")
  59.                 {
  60.                     firstPlayerCol++;
  61.  
  62.                     if (firstPlayerCol == battleField[firstPlayerRow].Length)
  63.                     {
  64.                         firstPlayerCol = 0;
  65.                     }
  66.                 }
  67.  
  68.                 if (battleField[firstPlayerRow][firstPlayerCol] == 's')
  69.                 {
  70.                     battleField[firstPlayerRow][firstPlayerCol] = 'x';
  71.                     End();
  72.                 }
  73.                 else
  74.                 {
  75.                     battleField[firstPlayerRow][firstPlayerCol] = 'f';
  76.                 }
  77.  
  78.  
  79.                 //Second player
  80.                 if (secondPlayerDirection == "down")
  81.                 {
  82.                     secondPlayerRow++;
  83.  
  84.                     if (secondPlayerRow == battleField.Length)
  85.                     {
  86.                         secondPlayerRow = 0;
  87.                     }
  88.                 }
  89.                 else if (secondPlayerDirection == "up")
  90.                 {
  91.                     secondPlayerRow--;
  92.  
  93.                     if (secondPlayerRow < 0)
  94.                     {
  95.                         secondPlayerRow = battleField.Length - 1;
  96.                     }
  97.                 }
  98.                 else if (secondPlayerDirection == "left")
  99.                 {
  100.                     secondPlayerCol--;
  101.  
  102.                     if (secondPlayerCol < 0)
  103.                     {
  104.                         secondPlayerRow = battleField[secondPlayerRow].Length - 1;
  105.                     }
  106.                 }
  107.                 else if (secondPlayerDirection == "left")
  108.                 {
  109.                     secondPlayerCol--;
  110.  
  111.                     if (secondPlayerCol < 0)
  112.                     {
  113.                         secondPlayerCol = battleField[secondPlayerRow].Length - 1;
  114.                     }
  115.                 }
  116.                 else if (secondPlayerDirection == "right")
  117.                 {
  118.                     secondPlayerCol++;
  119.  
  120.                     if (secondPlayerCol == battleField[secondPlayerRow].Length)
  121.                     {
  122.                         secondPlayerCol = 0;
  123.                     }
  124.                 }
  125.  
  126.                 if (battleField[secondPlayerRow][secondPlayerCol] == 'f')
  127.                 {
  128.                     battleField[secondPlayerRow][secondPlayerCol] = 'x';
  129.                     End();
  130.                 }
  131.                 else
  132.                 {
  133.                     battleField[secondPlayerRow][secondPlayerCol] = 's';
  134.                 }
  135.             }
  136.         }
  137.  
  138.         private static void End()
  139.         {
  140.             for (int row = 0; row < battleField.Length; row++)
  141.             {
  142.                 for (int col = 0; col < battleField[row].Length; col++)
  143.                 {
  144.                     Console.Write(battleField[row][col]);
  145.                 }
  146.                 Console.WriteLine();
  147.             }
  148.  
  149.             Environment.Exit(0);
  150.         }
  151.  
  152.         private static void Initialize()
  153.         {
  154.             for (int row = 0; row < battleField.Length; row++)
  155.             {
  156.                 char[] input = Console.ReadLine().ToCharArray();
  157.                 battleField[row] = new char[input.Length];
  158.  
  159.                 for (int col = 0; col < input.Length; col++)
  160.                 {
  161.                     battleField[row][col] = input[col];
  162.  
  163.                     if (battleField[row][col] == 'f')
  164.                     {
  165.                         firstPlayerRow = row;
  166.                         firstPlayerCol = col;
  167.                     }
  168.                     else if (battleField[row][col] == 's')
  169.                     {
  170.                         secondPlayerRow = row;
  171.                         secondPlayerCol = col;
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement