From 1a23989a8f57ba7516252c01749cc3c93d075a44 Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 3 Apr 2024 14:57:21 -0400 Subject: [PATCH] reimplemented 'pool' roll macro --- src/Lex.cs | 6 ++++++ src/Roll.cs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Lex.cs b/src/Lex.cs index 3e26b5b..bd43c7e 100644 --- a/src/Lex.cs +++ b/src/Lex.cs @@ -81,6 +81,12 @@ namespace Dungeoneer.Interpreter { public TokenSet() : base() { } public TokenSet(List set) : base(set) { } + public Token Pop() { + Token output = this[0]; + RemoveAt(0); + return output; + } + public override string ToString() { var output = ""; foreach(Token token in this) diff --git a/src/Roll.cs b/src/Roll.cs index 7477084..dbc625a 100644 --- a/src/Roll.cs +++ b/src/Roll.cs @@ -1,6 +1,7 @@ using System.Security.Cryptography; using System.Text.RegularExpressions; +using Dungeoneer.Error; using Dungeoneer.Interpreter; namespace Dungeoneer { @@ -9,9 +10,35 @@ namespace Dungeoneer { public static void Pool(TokenSet input) { if(input.Count != 2) + throw new SyntaxException($"expected 2 arguments ({input.Count} given)"); - foreach(var token in input) - ); + DiceToken dicePool = null; + DcToken target = null; + foreach(var token in input) { + switch(token) { + case DiceToken dice: + if(dicePool != null) + throw new SyntaxException("two dice"); + dicePool = dice; + break; + case DcToken dc: + if(target != null) + throw new SyntaxException("two dc values"); + target = dc; + break; + } + } + + bool success = false; + foreach(var roll in dicePool.Result) { + if(roll > target.Value) { + success = true; + break; + } + } + + string message = success ? $"{Format.Green}pass" : "{Format.Red}fail"; + Console.WriteLine($"{dicePool} > {target}\n => {message}{Format.Reset}"); } }