Exemplo n.º 1
0
        private long DetermineValue(string value, SoundBoard soundBoard)
        {
            long result;

            if (long.TryParse(value, out result))
            {
                return(result);
            }

            long found = FindRegisterValueOrCreateNew(value, soundBoard);

            return(found);
        }
Exemplo n.º 2
0
        public string RunThroughInstructions(string filePath)
        {
            List <string> instructions = GetInstructions(filePath);

            _one = new SoundBoard();
            string received = null;

            do
            {
                string instruction = instructions[_one.CurrentInstruction];
                received = ProcessInstruction(instruction, _one, false);
                _one.CurrentInstruction++;
            } while (received == null);

            return(received);
        }
Exemplo n.º 3
0
        public string ProcessInstruction(string command, SoundBoard soundBoard, bool isPartB)
        {
            string[] commandSplit = command.Split(' ');
            switch (commandSplit[0])
            {
            case "snd":
                soundBoard.LastPlayed = DetermineValue(commandSplit[1], soundBoard);
                if (isPartB)
                {
                    soundBoard.TimesSentInstructions++;
                    if (soundBoard.Id == 0)
                    {
                        _one.MessagesSentFromOther.Enqueue(soundBoard.LastPlayed);
                    }
                    else
                    {
                        _zero.MessagesSentFromOther.Enqueue(soundBoard.LastPlayed);
                    }
                }
                break;

            case "set":
                DetermineValue(commandSplit[1], soundBoard);
                soundBoard.Registers[commandSplit[1]] = DetermineValue(commandSplit[2], soundBoard);
                break;

            case "add":
                DetermineValue(commandSplit[1], soundBoard);
                soundBoard.Registers[commandSplit[1]] += DetermineValue(commandSplit[2], soundBoard);
                break;

            case "mul":
                DetermineValue(commandSplit[1], soundBoard);
                soundBoard.Registers[commandSplit[1]] = soundBoard.Registers[commandSplit[1]] * DetermineValue(commandSplit[2], soundBoard);
                break;

            case "mod":
                DetermineValue(commandSplit[1], soundBoard);
                soundBoard.Registers[commandSplit[1]] = soundBoard.Registers[commandSplit[1]] % DetermineValue(commandSplit[2], soundBoard);
                break;

            case "rcv":
                if (!isPartB)
                {
                    if (soundBoard.Registers[commandSplit[1]] > 0)
                    {
                        return(soundBoard.LastPlayed.ToString());
                    }
                }
                else
                {
                    if (soundBoard.MessagesSentFromOther.Count > 0)
                    {
                        DetermineValue(commandSplit[1], soundBoard);
                        soundBoard.Registers[commandSplit[1]] = soundBoard.MessagesSentFromOther.Dequeue();
                        soundBoard.Paused = false;
                    }
                    else
                    {
                        soundBoard.Paused = true;
                    }
                }

                break;

            case "jgz":
                long firstValue = DetermineValue(commandSplit[1], soundBoard);
                if (firstValue > 0)
                {
                    soundBoard.CurrentInstruction += (int)DetermineValue(commandSplit[2], soundBoard) - 1;
                }
                break;

            default:
                throw new ArgumentException("Unknown command");
            }

            return(null);
        }