/// <summary> /// Store the supplied character at the specified position /// </summary> /// <param name="putPosition">x/y coordinate position to store the supplied character.</param> /// <param name="value">the character to store.</param> /// <exception cref="InvalidOperationException">Thrown when an invalid position is specified</exception> public void PutValue(CoOrds putPosition, char value) { if (CheckPosition(putPosition)) { char[] yChars = _befungeGrid[putPosition.y].ToCharArray(); yChars[putPosition.x] = value; _befungeGrid[putPosition.y] = new string(yChars); } }
/// <summary> /// Get the character at the specified position /// </summary> /// <returns> /// Returns the character stored at the specified position /// </returns> /// <param name="getPosition">x/y coordinate of the character to retrieve.</param> /// <exception cref="InvalidOperationException">Thrown when an invalid position is specified</exception> public char GetValue(CoOrds getPosition) { char rtn = ' '; if (CheckPosition(getPosition)) { rtn = _befungeGrid[getPosition.y][getPosition.x]; } return(rtn); }
private bool CheckPosition(CoOrds checkPosition) { bool isValid = (0 <= checkPosition.x && checkPosition.x <= MaxExtent.x && 0 <= checkPosition.y && checkPosition.y <= MaxExtent.y); if (!isValid) { string message = $"Invalid Position specified: [{checkPosition.x},{checkPosition.y}]."; throw new InvalidOperationException(message); } return(isValid); }
/// <summary> /// Create the BefungeRunTime /// </summary> /// <param name="befungeCode">A String containing befunge code.</param> /// <param name="mode">The mode (Number or String).</param> /// <param name="outputStream">An optional TextWriter. It can be used to output to a file.</param> /// <param name="inputStream">An optional TextReader. It can be used to pass input from a file or the console.</param> public BefungeRunTime(string befungeCode, IMode mode, TextWriter outputStream = null, TextReader inputStream = null) { _befungeGrid = CreateGridFromString(befungeCode); _intStack = new Stack <int>(); CurrentPosition = new CoOrds(0, 0); CurrentMode = mode; ReadInstruction(); CurrentDirection = MoveRight.Instance; EndProgram = false; _outputStream = outputStream == null ? Console.Out : outputStream; _inputStream = inputStream == null ? Console.In : inputStream; }