Advertisement
simonradev

ITVillage

May 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.45 KB | None | 0 0
  1. namespace ITVillage
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.    
  9.     public class Startup
  10.     {
  11.         private static Board board;
  12.  
  13.         public static void Main()
  14.         {
  15.             string[] rowsOfFieldsOfTheBoard = SplitStringAndApplyFunc(Console.ReadLine(),
  16.                                                                       Constants.Pipe,
  17.                                                                       Methods.GetOnlyLettersAndDigits);
  18.             int[] initialPositionOfPlayer = SplitStringAndApplyFunc(Console.ReadLine(),
  19.                                                                     Constants.WhiteSpace,
  20.                                                                     Methods.ParseFunc)
  21.                                                                     .DecrementAllElements()
  22.                                                                     .ToArray();
  23.             int[] moves = SplitStringAndApplyFunc(Console.ReadLine(),
  24.                                                   Constants.WhiteSpace,
  25.                                                   Methods.ParseFunc);
  26.  
  27.             board = new Board();
  28.             board.FillTheBoard(rowsOfFieldsOfTheBoard);
  29.  
  30.             Player player = new Player(initialPositionOfPlayer[0], initialPositionOfPlayer[1], moves);
  31.  
  32.             string result = default(string);
  33.             while (result == default(string))
  34.             {
  35.                 player.TakeTheMoneyFromTheOwnedInns();
  36.  
  37.                 char fieldType = player.MakeAMove(board);
  38.  
  39.                 Board.FieldConsequences[fieldType](player);
  40.  
  41.                 player.IndexOfCurrentMove++;
  42.                 result = player.GetOutcomeForPlayerOrDefault();
  43.             }
  44.  
  45.             Console.WriteLine(result);
  46.         }
  47.        
  48.         private static TType[] SplitStringAndApplyFunc<TType>(string toSplit, char toSplitBy, Func<string, TType> func)
  49.         {
  50.             return toSplit.Split(new[] { toSplitBy }, StringSplitOptions.RemoveEmptyEntries).Select(func).ToArray();
  51.         }
  52.     }
  53.  
  54.     public class Player
  55.     {
  56.         private Position currentPosition;
  57.         private int indexOfCurrentPosition;
  58.         private int coins;
  59.  
  60.         private int innsOwned;
  61.  
  62.         private int[] moves;
  63.         private int indexOfCurrentMove;
  64.  
  65.         private bool steppedOnNakov;
  66.  
  67.         public Player(int row, int col, int[] moves)
  68.         {
  69.             this.currentPosition = new Position(row, col);
  70.             this.indexOfCurrentPosition = GetIndexOfCurrentPosition(currentPosition);
  71.             this.coins = 50;
  72.  
  73.             this.innsOwned = 0;
  74.  
  75.             this.moves = moves;
  76.             this.indexOfCurrentMove = 0;
  77.  
  78.             this.steppedOnNakov = false;
  79.         }
  80.        
  81.         public int Coins
  82.         {
  83.             get { return this.coins; }
  84.             set { this.coins = value; }
  85.         }
  86.  
  87.         public int IndexOfCurrentMove
  88.         {
  89.             get { return this.indexOfCurrentMove; }
  90.             set { this.indexOfCurrentMove = value; }
  91.         }
  92.  
  93.         public int IndexOfCurrentPosition
  94.         {
  95.             get { return this.indexOfCurrentPosition; }
  96.             set { this.indexOfCurrentPosition = value; }
  97.         }
  98.  
  99.         public bool SteppedOnNakov
  100.         {
  101.             get { return this.steppedOnNakov; }
  102.             set { this.steppedOnNakov = value; }
  103.         }
  104.  
  105.         public int InnsOwned
  106.         {
  107.             get { return this.innsOwned; }
  108.             set { this.innsOwned = value; }
  109.         }
  110.  
  111.         public char MakeAMove(Board board)
  112.         {
  113.             int indexOfCurrentMove = this.indexOfCurrentMove;
  114.             int movesToMake = this.moves[indexOfCurrentMove];
  115.  
  116.             this.indexOfCurrentPosition = (this.indexOfCurrentPosition + movesToMake) % Board.AllValidFields.Length;
  117.  
  118.             Position nextPlayersPosition = Board.AllValidFields[indexOfCurrentPosition];
  119.  
  120.             this.currentPosition.Row = nextPlayersPosition.Row;
  121.             this.currentPosition.Col = nextPlayersPosition.Col;
  122.  
  123.             return board.GetField(currentPosition);
  124.         }
  125.  
  126.         private int GetIndexOfCurrentPosition(Position toGetIndexFor)
  127.         {
  128.             int indexToReturn = 0;
  129.  
  130.             for (int currField = 0; currField < Board.AllValidFields.Length; currField++)
  131.             {
  132.                 Position field = Board.AllValidFields[currField];
  133.  
  134.                 if (field.Row == toGetIndexFor.Row && field.Col == toGetIndexFor.Col)
  135.                 {
  136.                     break;
  137.                 }
  138.  
  139.                 indexToReturn++;
  140.             }
  141.  
  142.             return indexToReturn;
  143.         }
  144.  
  145.         public string GetOutcomeForPlayerOrDefault()
  146.         {
  147.             string outcome = default(string);
  148.  
  149.             if (this.coins < 0)
  150.             {
  151.                 outcome = "<p>You lost! You ran out of money!<p>";
  152.             }
  153.             else if (this.innsOwned == Board.NumberOfInns)
  154.             {
  155.                 outcome = $"<p>You won! You own the village now! You have {this.coins} coins!<p>";
  156.             }
  157.             else if (this.indexOfCurrentMove >= this.moves.Length)
  158.             {
  159.                 outcome = $"<p>You lost! No more moves! You have {this.coins} coins!<p>";
  160.             }
  161.             else if (this.steppedOnNakov)
  162.             {
  163.                 outcome = "<p>You won! Nakov's force was with you!<p>";
  164.             }
  165.  
  166.             return outcome;
  167.         }
  168.  
  169.         public void TakeTheMoneyFromTheOwnedInns()
  170.         {
  171.             this.coins += this.innsOwned * Constants.MoneyForAnInn;
  172.         }
  173.     }
  174.  
  175.     public class Board
  176.     {
  177.         public static readonly Position[] AllValidFields = new Position[]
  178.         {
  179.             new Position(0, 0),
  180.             new Position(0, 1),
  181.             new Position(0, 2),
  182.             new Position(0, 3),
  183.             new Position(1, 3),
  184.             new Position(2, 3),
  185.             new Position(3, 3),
  186.             new Position(3, 2),
  187.             new Position(3, 1),
  188.             new Position(3, 0),
  189.             new Position(2, 0),
  190.             new Position(1, 0),
  191.         };
  192.  
  193.         public static readonly Dictionary<char, Action<Player>> FieldConsequences = new Dictionary<char, Action<Player>>
  194.         {
  195.             [Constants.WifiPub] = player =>
  196.             {
  197.                 player.Coins -= Constants.WifiPubCoctailCost;
  198.             },
  199.             [Constants.WifiInn] = player =>
  200.             {
  201.                 if (player.Coins >= Constants.InnCostToBuy)
  202.                 {
  203.                     player.Coins -= Constants.InnCostToBuy;
  204.  
  205.                     player.InnsOwned++;
  206.                 }
  207.                 else
  208.                 {
  209.                     player.Coins -= Constants.InnCostToStay;
  210.                 }
  211.             },
  212.             [Constants.FreelanceProject] = player =>
  213.             {
  214.                 player.Coins += Constants.FreelanceProjectPayment;
  215.             },
  216.             [Constants.Storm] = player =>
  217.             {
  218.                 player.IndexOfCurrentMove += Constants.TurnsSkippedIntoStorm;
  219.             },
  220.             [Constants.SuperVlado] = player =>
  221.             {
  222.                 player.Coins *= Constants.SuperVladoMultiplyPower;
  223.             },
  224.             [Constants.Nakov] = player =>
  225.             {
  226.                 player.SteppedOnNakov = true;
  227.             }
  228.         };
  229.  
  230.         private char[,] board;
  231.         public static int NumberOfInns = 0;
  232.  
  233.         public Board()
  234.         {
  235.             this.board = new char[Constants.BoardDimensions, Constants.BoardDimensions];
  236.         }
  237.  
  238.         public char GetField(Position toGetFieldFrom)
  239.         {
  240.             return this.board[toGetFieldFrom.Row, toGetFieldFrom.Col];
  241.         }
  242.  
  243.         public void FillTheBoard(string[] rowsOfFieldsOfTheBoard)
  244.         {
  245.             for (int row = 0; row < board.GetLength(0); row++)
  246.             {
  247.                 for (int col = 0; col < board.GetLength(1); col++)
  248.                 {
  249.                     char currentField = rowsOfFieldsOfTheBoard[row][col];
  250.                     this.board[row, col] = currentField;
  251.  
  252.                     if (currentField == Constants.WifiInn)
  253.                     {
  254.                         NumberOfInns++;
  255.                     }
  256.                 }
  257.             }
  258.         }
  259.     }
  260.  
  261.     public static class Constants
  262.     {
  263.         public const int BoardDimensions = 4;
  264.  
  265.         public const char WifiPub = 'P';
  266.         public const char WifiInn = 'I';
  267.         public const char FreelanceProject = 'F';
  268.         public const char Storm = 'S';
  269.         public const char SuperVlado = 'V';
  270.         public const char Nakov = 'N';
  271.         public const char EmptyField = '0';
  272.  
  273.         public const int InnCostToBuy = 100;
  274.         public const int InnCostToStay = 10;
  275.         public const int MoneyForAnInn = 20;
  276.  
  277.         public const int WifiPubCoctailCost = 5;
  278.         public const int FreelanceProjectPayment = 20;
  279.         public const int TurnsSkippedIntoStorm = 2;
  280.         public const int SuperVladoMultiplyPower = 10;
  281.        
  282.         public const char Pipe = '|';
  283.         public const char WhiteSpace = ' ';
  284.     }
  285.  
  286.     public static class Methods
  287.     {
  288.         public static readonly Func<string, int> ParseFunc = toParse => int.Parse(toParse);
  289.  
  290.         public static readonly Func<string, string> GetOnlyLettersAndDigits = toTrim =>
  291.         {
  292.             StringBuilder result = new StringBuilder();
  293.  
  294.             foreach (char symbol in toTrim)
  295.             {
  296.                 if (char.IsDigit(symbol) || char.IsLetter(symbol))
  297.                 {
  298.                     result.Append(symbol);
  299.                 }
  300.             }
  301.  
  302.             return result.ToString();
  303.         };
  304.  
  305.         public static int[] DecrementAllElements(this int[] array)
  306.         {
  307.             for (int currElement = 0; currElement < array.Length; currElement++)
  308.             {
  309.                 array[currElement]--;
  310.             }
  311.  
  312.             return array;
  313.         }
  314.     }
  315.  
  316.     public struct Position
  317.     {
  318.         private int row;
  319.         private int col;
  320.  
  321.         public Position(int row, int col)
  322.         {
  323.             this.row = row;
  324.             this.col = col;
  325.         }
  326.  
  327.         public int Row
  328.         {
  329.             get { return this.row; }
  330.             set { this.row = value; }
  331.         }
  332.  
  333.         public int Col
  334.         {
  335.             get { return this.col; }
  336.             set { this.col = value; }
  337.         }
  338.     }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement