private ChipBot GetBot(int identity) { ChipBot usedBot = swarm.Where(b => b.identity == identity).FirstOrDefault(); if (usedBot == null) { usedBot = new ChipBot() { identity = identity }; swarm.Add(usedBot); } return(usedBot); }
private void ExecuteCommand(ChipBotCommands commandType, string command) { switch (commandType) { case ChipBotCommands.Pickup: command = command.Remove(0, 5); string[] pickupBits = command.Split(new string[] { " goes to bot " }, StringSplitOptions.None); int value = Convert.ToInt32(pickupBits[0]); int botNumber = Convert.ToInt32(pickupBits[1]); currentBot.AddValue(value); break; case ChipBotCommands.DropOff: /*if (currentBot.HasBothChips()) * {*/ if (command.StartsWith("bot")) { command = command.Remove(0, command.IndexOf("gives") + 5); } string[] dropoffbits = command.Split(new string[] { " to output " }, StringSplitOptions.None); string what = dropoffbits[0]; int outputId = Convert.ToInt32(dropoffbits[1]); OutputBin outputBin = GetOutput(outputId); int valueToDrop = what.Equals("high") ? currentBot.GetHighValue() : currentBot.GetLowValue(); if (valueToDrop != -1) { outputBin.values.Add(valueToDrop); } else { commandBacklog.Add(new KeyValuePair <int, string>(currentBot.identity, command)); } //} break; case ChipBotCommands.HandOff: /*if (currentBot.HasBothChips()) * {*/ if (command.StartsWith("bot")) { command = command.Remove(0, command.IndexOf("gives") + 5); } string[] handoffbits = command.Split(new string[] { " to bot " }, StringSplitOptions.None); string highorlow = handoffbits[0]; int otherBotId = Convert.ToInt32(handoffbits[1]); int valueToHandOff = highorlow.Equals("high") ? currentBot.GetHighValue() : currentBot.GetLowValue(); ChipBot otherBot = GetBot(otherBotId); if (valueToHandOff != -1) { otherBot.AddValue(valueToHandOff); } else { commandBacklog.Add(new KeyValuePair <int, string>(currentBot.identity, command)); } //} break; default: Console.WriteLine("How the hell did you force this?"); break; } }