Advertisement
kidroca

Radioactive Bunnies 2

Oct 11th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.94 KB | None | 0 0
  1. namespace Problem_2
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.  
  9.     class Program
  10.     {
  11.         static List<char[]> field = new List<char[]>();
  12.  
  13.         static int totalRows;
  14.  
  15.         static int totalCols;
  16.  
  17.         static int pRow;
  18.  
  19.         static int pCol;
  20.  
  21.         static bool gameEnded = false;
  22.  
  23.         static bool isWinning = true;
  24.  
  25.         static void Main()
  26.         {
  27.             ReadFieldData();
  28.  
  29.             SetPlayerPosition();
  30.  
  31.             DoCommands();
  32.  
  33.             PrintField(field);
  34.            
  35.             Console.WriteLine(
  36.                 "{0}: {1} {2}"
  37.                 , isWinning ? "won" : "dead"
  38.                 , pRow
  39.                 , pCol);
  40.         }
  41.  
  42.         private static void PrintField(List<char[]> field)
  43.         {
  44.             foreach (var line in field)
  45.             {
  46.                 Console.WriteLine(new string(line));
  47.             }
  48.         }
  49.  
  50.         private static void SetPlayerPosition()
  51.         {
  52.             for (int i = totalRows - 1; i >= 0; i--)
  53.             {
  54.                 int index;
  55.                 if ((index = Array.IndexOf(field[i], 'P')) != -1)
  56.                 {
  57.                     pRow = i;
  58.                     pCol = index;
  59.                     break;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         private static void DoCommands()
  65.         {
  66.             string commands = Console.ReadLine();
  67.  
  68.             foreach (var c in commands)
  69.             {
  70.                 switch (c)
  71.                 {
  72.                     case 'R':
  73.                         if (CheckPlayerTurn(pRow, pCol + 1))
  74.                         {
  75.                             DoPlayerTurn(pRow, pCol, pRow, pCol + 1);
  76.                             pCol++;
  77.                         }
  78.  
  79.                         break;
  80.                     case 'D':
  81.                         if (CheckPlayerTurn(pRow + 1, pCol))
  82.                         {
  83.                             DoPlayerTurn(pRow, pCol, pRow + 1, pCol);
  84.                             pRow++;
  85.                         }
  86.  
  87.                         break;
  88.                     case 'L':
  89.                         if (CheckPlayerTurn(pRow, pCol - 1))
  90.                         {
  91.                             DoPlayerTurn(pRow, pCol, pRow, pCol - 1);
  92.                             pCol--;
  93.                         }
  94.  
  95.                         break;
  96.                     case 'U':
  97.                         if (CheckPlayerTurn(pRow - 1, pCol))
  98.                         {
  99.                             DoPlayerTurn(pRow, pCol, pRow - 1, pCol);
  100.                             pRow--;
  101.                         }
  102.  
  103.                         break;
  104.                     default:
  105.                         break;
  106.                 }
  107.  
  108.                 if (gameEnded && isWinning)
  109.                 {
  110.                     field[pRow][pCol] = '.';
  111.                 }
  112.  
  113.                 DoBunniesTurn();
  114.              
  115.                 if (gameEnded)
  116.                 {
  117.                     break;
  118.                 }
  119.             }
  120.         }
  121.  
  122.         private static void DoBunniesTurn()
  123.         {
  124.             List<char[]> newField = field.Select(x => x.ToArray()).ToList();
  125.  
  126.             for (int i = 0; i < totalRows; i++)
  127.             {
  128.                 for (int j = 0; j < totalCols; j++)
  129.                 {
  130.                     if (field[i][j] == 'B')
  131.                     {
  132.                         SpreadBunnies(i, j, newField);
  133.                     }
  134.                 }
  135.             }
  136.  
  137.             field = newField;
  138.         }
  139.  
  140.         private static void SpreadBunnies(int i, int j, List<char[]> newField)
  141.         {        
  142.             if (IsWithinField(i, j + 1)) // Right
  143.             {
  144.                 SpreadTo(i, j + 1, newField);
  145.             }
  146.  
  147.            
  148.             if (IsWithinField(i + 1, j)) // Down
  149.             {
  150.                 SpreadTo(i + 1, j, newField);
  151.             }
  152.  
  153.            
  154.             if (IsWithinField(i, j - 1)) // Left
  155.             {
  156.                 SpreadTo(i, j - 1, newField);
  157.             }
  158.  
  159.          
  160.             if (IsWithinField(i - 1, j)) // Up
  161.             {
  162.                 SpreadTo(i - 1, j, newField);
  163.             }
  164.         }
  165.  
  166.         private static void SpreadTo(int i, int j, List<char[]> newField)
  167.         {
  168.             if (newField[i][j] == 'P')
  169.             {
  170.                 isWinning = false;
  171.                 gameEnded = true;
  172.             }
  173.  
  174.             newField[i][j] = 'B';
  175.         }
  176.  
  177.         private static void DoPlayerTurn(int i, int j, int i2, int j2)
  178.         {
  179.  
  180.             if (field[i2][j2] != 'B')
  181.             {
  182.                 field[i2][j2] = 'P';
  183.                 field[i][j] = '.';
  184.             }
  185.         }
  186.  
  187.         private static bool CheckPlayerTurn(int row, int col)
  188.         {
  189.             if (IsWithinField(row, col))
  190.             {
  191.                 if (field[row][col] == 'B')
  192.                 {
  193.                     isWinning = false;
  194.                     gameEnded = true;
  195.                 }
  196.  
  197.                 return true;
  198.             }
  199.             else
  200.             {
  201.                 gameEnded = true;
  202.                 return false;
  203.             }
  204.         }
  205.  
  206.         private static bool IsWithinField(int row, int col)
  207.         {
  208.             bool isWithin =
  209.                 0 <= row && row < totalRows &&
  210.                 0 <= col && col < totalCols;
  211.  
  212.             return isWithin;
  213.         }
  214.  
  215.         private static void ReadFieldData()
  216.         {
  217.             int[] rowsCols = Console
  218.                 .ReadLine()
  219.                 .Split(' ')
  220.                 .Select(int.Parse)
  221.                 .ToArray();
  222.  
  223.             totalRows = rowsCols[0];
  224.             totalCols = rowsCols[1];
  225.  
  226.             for (int i = 0; i < totalRows; i++)
  227.             {
  228.                 field.Add(Console
  229.                     .ReadLine()
  230.                     .Trim()
  231.                     .ToCharArray());
  232.             }
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement