Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ITVillage
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Startup
- {
- private static Board board;
- public static void Main()
- {
- string[] rowsOfFieldsOfTheBoard = SplitStringAndApplyFunc(Console.ReadLine(),
- Constants.Pipe,
- Methods.GetOnlyLettersAndDigits);
- int[] initialPositionOfPlayer = SplitStringAndApplyFunc(Console.ReadLine(),
- Constants.WhiteSpace,
- Methods.ParseFunc)
- .DecrementAllElements()
- .ToArray();
- int[] moves = SplitStringAndApplyFunc(Console.ReadLine(),
- Constants.WhiteSpace,
- Methods.ParseFunc);
- board = new Board();
- board.FillTheBoard(rowsOfFieldsOfTheBoard);
- Player player = new Player(initialPositionOfPlayer[0], initialPositionOfPlayer[1], moves);
- string result = default(string);
- while (result == default(string))
- {
- player.TakeTheMoneyFromTheOwnedInns();
- char fieldType = player.MakeAMove(board);
- Board.FieldConsequences[fieldType](player);
- player.IndexOfCurrentMove++;
- result = player.GetOutcomeForPlayerOrDefault();
- }
- Console.WriteLine(result);
- }
- private static TType[] SplitStringAndApplyFunc<TType>(string toSplit, char toSplitBy, Func<string, TType> func)
- {
- return toSplit.Split(new[] { toSplitBy }, StringSplitOptions.RemoveEmptyEntries).Select(func).ToArray();
- }
- }
- public class Player
- {
- private Position currentPosition;
- private int indexOfCurrentPosition;
- private int coins;
- private int innsOwned;
- private int[] moves;
- private int indexOfCurrentMove;
- private bool steppedOnNakov;
- public Player(int row, int col, int[] moves)
- {
- this.currentPosition = new Position(row, col);
- this.indexOfCurrentPosition = GetIndexOfCurrentPosition(currentPosition);
- this.coins = 50;
- this.innsOwned = 0;
- this.moves = moves;
- this.indexOfCurrentMove = 0;
- this.steppedOnNakov = false;
- }
- public int Coins
- {
- get { return this.coins; }
- set { this.coins = value; }
- }
- public int IndexOfCurrentMove
- {
- get { return this.indexOfCurrentMove; }
- set { this.indexOfCurrentMove = value; }
- }
- public int IndexOfCurrentPosition
- {
- get { return this.indexOfCurrentPosition; }
- set { this.indexOfCurrentPosition = value; }
- }
- public bool SteppedOnNakov
- {
- get { return this.steppedOnNakov; }
- set { this.steppedOnNakov = value; }
- }
- public int InnsOwned
- {
- get { return this.innsOwned; }
- set { this.innsOwned = value; }
- }
- public char MakeAMove(Board board)
- {
- int indexOfCurrentMove = this.indexOfCurrentMove;
- int movesToMake = this.moves[indexOfCurrentMove];
- this.indexOfCurrentPosition = (this.indexOfCurrentPosition + movesToMake) % Board.AllValidFields.Length;
- Position nextPlayersPosition = Board.AllValidFields[indexOfCurrentPosition];
- this.currentPosition.Row = nextPlayersPosition.Row;
- this.currentPosition.Col = nextPlayersPosition.Col;
- return board.GetField(currentPosition);
- }
- private int GetIndexOfCurrentPosition(Position toGetIndexFor)
- {
- int indexToReturn = 0;
- for (int currField = 0; currField < Board.AllValidFields.Length; currField++)
- {
- Position field = Board.AllValidFields[currField];
- if (field.Row == toGetIndexFor.Row && field.Col == toGetIndexFor.Col)
- {
- break;
- }
- indexToReturn++;
- }
- return indexToReturn;
- }
- public string GetOutcomeForPlayerOrDefault()
- {
- string outcome = default(string);
- if (this.coins < 0)
- {
- outcome = "<p>You lost! You ran out of money!<p>";
- }
- else if (this.innsOwned == Board.NumberOfInns)
- {
- outcome = $"<p>You won! You own the village now! You have {this.coins} coins!<p>";
- }
- else if (this.indexOfCurrentMove >= this.moves.Length)
- {
- outcome = $"<p>You lost! No more moves! You have {this.coins} coins!<p>";
- }
- else if (this.steppedOnNakov)
- {
- outcome = "<p>You won! Nakov's force was with you!<p>";
- }
- return outcome;
- }
- public void TakeTheMoneyFromTheOwnedInns()
- {
- this.coins += this.innsOwned * Constants.MoneyForAnInn;
- }
- }
- public class Board
- {
- public static readonly Position[] AllValidFields = new Position[]
- {
- new Position(0, 0),
- new Position(0, 1),
- new Position(0, 2),
- new Position(0, 3),
- new Position(1, 3),
- new Position(2, 3),
- new Position(3, 3),
- new Position(3, 2),
- new Position(3, 1),
- new Position(3, 0),
- new Position(2, 0),
- new Position(1, 0),
- };
- public static readonly Dictionary<char, Action<Player>> FieldConsequences = new Dictionary<char, Action<Player>>
- {
- [Constants.WifiPub] = player =>
- {
- player.Coins -= Constants.WifiPubCoctailCost;
- },
- [Constants.WifiInn] = player =>
- {
- if (player.Coins >= Constants.InnCostToBuy)
- {
- player.Coins -= Constants.InnCostToBuy;
- player.InnsOwned++;
- }
- else
- {
- player.Coins -= Constants.InnCostToStay;
- }
- },
- [Constants.FreelanceProject] = player =>
- {
- player.Coins += Constants.FreelanceProjectPayment;
- },
- [Constants.Storm] = player =>
- {
- player.IndexOfCurrentMove += Constants.TurnsSkippedIntoStorm;
- },
- [Constants.SuperVlado] = player =>
- {
- player.Coins *= Constants.SuperVladoMultiplyPower;
- },
- [Constants.Nakov] = player =>
- {
- player.SteppedOnNakov = true;
- }
- };
- private char[,] board;
- public static int NumberOfInns = 0;
- public Board()
- {
- this.board = new char[Constants.BoardDimensions, Constants.BoardDimensions];
- }
- public char GetField(Position toGetFieldFrom)
- {
- return this.board[toGetFieldFrom.Row, toGetFieldFrom.Col];
- }
- public void FillTheBoard(string[] rowsOfFieldsOfTheBoard)
- {
- for (int row = 0; row < board.GetLength(0); row++)
- {
- for (int col = 0; col < board.GetLength(1); col++)
- {
- char currentField = rowsOfFieldsOfTheBoard[row][col];
- this.board[row, col] = currentField;
- if (currentField == Constants.WifiInn)
- {
- NumberOfInns++;
- }
- }
- }
- }
- }
- public static class Constants
- {
- public const int BoardDimensions = 4;
- public const char WifiPub = 'P';
- public const char WifiInn = 'I';
- public const char FreelanceProject = 'F';
- public const char Storm = 'S';
- public const char SuperVlado = 'V';
- public const char Nakov = 'N';
- public const char EmptyField = '0';
- public const int InnCostToBuy = 100;
- public const int InnCostToStay = 10;
- public const int MoneyForAnInn = 20;
- public const int WifiPubCoctailCost = 5;
- public const int FreelanceProjectPayment = 20;
- public const int TurnsSkippedIntoStorm = 2;
- public const int SuperVladoMultiplyPower = 10;
- public const char Pipe = '|';
- public const char WhiteSpace = ' ';
- }
- public static class Methods
- {
- public static readonly Func<string, int> ParseFunc = toParse => int.Parse(toParse);
- public static readonly Func<string, string> GetOnlyLettersAndDigits = toTrim =>
- {
- StringBuilder result = new StringBuilder();
- foreach (char symbol in toTrim)
- {
- if (char.IsDigit(symbol) || char.IsLetter(symbol))
- {
- result.Append(symbol);
- }
- }
- return result.ToString();
- };
- public static int[] DecrementAllElements(this int[] array)
- {
- for (int currElement = 0; currElement < array.Length; currElement++)
- {
- array[currElement]--;
- }
- return array;
- }
- }
- public struct Position
- {
- private int row;
- private int col;
- public Position(int row, int col)
- {
- this.row = row;
- this.col = col;
- }
- public int Row
- {
- get { return this.row; }
- set { this.row = value; }
- }
- public int Col
- {
- get { return this.col; }
- set { this.col = value; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement