From e6fcab2bfff15f2cfc99f57066c040e34e593584 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Mon, 22 Nov 2021 09:01:53 -0500 Subject: [PATCH 01/10] robot gravity --- challenges.sln | 6 ++ src/Space Robot/Robot Gravity.csproj | 18 ++++ src/Space Robot/RobotGravityTests.cs | 131 +++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 src/Space Robot/Robot Gravity.csproj create mode 100644 src/Space Robot/RobotGravityTests.cs diff --git a/challenges.sln b/challenges.sln index 1e855e7..398b290 100644 --- a/challenges.sln +++ b/challenges.sln @@ -27,6 +27,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ISSUE_TEMPLATE", "ISSUE_TEM .github\ISSUE_TEMPLATE\config.yml = .github\ISSUE_TEMPLATE\config.yml EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robot Gravity", "src\Space Robot\Robot Gravity.csproj", "{98FAB7D8-0C64-43B0-B03C-9CF08BED9089}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,6 +39,10 @@ Global {5468530B-AA66-4594-A126-4884664BE91D}.Debug|Any CPU.Build.0 = Debug|Any CPU {5468530B-AA66-4594-A126-4884664BE91D}.Release|Any CPU.ActiveCfg = Release|Any CPU {5468530B-AA66-4594-A126-4884664BE91D}.Release|Any CPU.Build.0 = Release|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Release|Any CPU.ActiveCfg = Release|Any CPU + {98FAB7D8-0C64-43B0-B03C-9CF08BED9089}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Space Robot/Robot Gravity.csproj b/src/Space Robot/Robot Gravity.csproj new file mode 100644 index 0000000..6f02502 --- /dev/null +++ b/src/Space Robot/Robot Gravity.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + Space_Robot + enable + + false + + + + + + + + + + diff --git a/src/Space Robot/RobotGravityTests.cs b/src/Space Robot/RobotGravityTests.cs new file mode 100644 index 0000000..673ab72 --- /dev/null +++ b/src/Space Robot/RobotGravityTests.cs @@ -0,0 +1,131 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Timers; + +namespace Space_Robot +{ + [TestClass] + public class RobotGravityTests + { + public double DetermineGravity(Robot robot) + { + // ----------------------- + // Complete challenge here. + // + // You are making a space robot to explore other planets. Your robot + // has a gravity sensor but you want to have a back up way to determine + // the current gravity in case the sensor is damaged. Your robot has the + // ability to jump and it has an height sensor. Use those features to + // determine the current acceleration due to gravity as a positive number + // within 0.1 units: + // + // - robot.Jump + // - robot.MaxJumpForce + // - robot.HeightSensor + // - robot.Mass + // + // ----------------------- + throw new NotImplementedException(); // <- delete + } + + public class Robot + { + [Obsolete("You don't need to make any Robots. :P")] + public Robot(Func<(double, DateTime)> getHeight, Action jump) + { + Timer timer = new((int)HeightSensorInterval.TotalMilliseconds); + timer.Elapsed += (o, e) => + { + var (height, time) = getHeight(); + HeightSensor?.Invoke(height, time); + }; + timer.Enabled = true; + JumpCallback = jump; + } + private Action JumpCallback { get; set; } + private readonly TimeSpan HeightSensorInterval = TimeSpan.FromSeconds(.01); + public double Mass { get; private set; } = 5; + public double MaxJumpForce { get; private set; } = 50d; + public event Action? HeightSensor; + public void Jump(double force) => JumpCallback?.Invoke(force); + } + + [TestMethod] + public void Test() + { + // measurements in meters per second squared (m/s²) + const double GravityOnMercury = 3.7; + const double GravityOnVenus = 8.87; + const double GravityOnEarth = 9.807; + const double GravityOnMars = 3.721; + const double GravityOnJupiter = 24.79; + const double GravityOnSaturn = 10.44; + const double GravityOnUranus = 8.87; + const double GravityOnNeptune = 11.15; + const double GravityOnPluto = 0.62; + const double GravityOnEarthMoon = 1.62; + + Robot InitializeRobot(double gravity) + { + (double InitialVelocity, DateTime Time)? jump = null; + + Robot robot = default!; +#pragma warning disable CS0618 // Type or member is obsolete + robot = new( + getHeight: GetHeight, + jump: force => + { + if (GetHeight().Height is 0) + { + jump = (force / robot.Mass, DateTime.Now); + } + }); +#pragma warning restore CS0618 // Type or member is obsolete + + (double Height, DateTime Time) GetHeight() + { + DateTime now = DateTime.Now; + if (jump is null) + { + return (0, now); + } + double seconds = (now - jump.Value.Time).TotalSeconds; + double height = -.5 * gravity * seconds * seconds + jump.Value.InitialVelocity * seconds; + if (height < 0) + { + jump = null; + height = 0; + } + return (height, now); + } + + return robot; + } + + void Test(double gravity) + { + try + { + Robot robot = InitializeRobot(gravity); + double determinedGravity = Math.Abs(DetermineGravity(robot)); + Assert.IsTrue(Math.Abs(determinedGravity - gravity) <= 0.1, $"expected {gravity} but got {determinedGravity}"); + } + catch (NotImplementedException) + { + Assert.Inconclusive(); + } + } + + Test(GravityOnMercury); + Test(GravityOnVenus); + Test(GravityOnEarth); + Test(GravityOnMars); + Test(GravityOnJupiter); + Test(GravityOnSaturn); + Test(GravityOnUranus); + Test(GravityOnNeptune); + Test(GravityOnPluto); + Test(GravityOnEarthMoon); + } + } +} \ No newline at end of file From ff3f203b9235be6c60d599802edf34c4354dbc7c Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 15:22:43 -0500 Subject: [PATCH 02/10] robot gravity updates --- challenges.sln | 2 +- src/Robot Gravity/README.md | 110 ++++++++++++++++++ .../Robot Gravity.csproj | 0 .../RobotGravityTests.cs | 11 +- 4 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 src/Robot Gravity/README.md rename src/{Space Robot => Robot Gravity}/Robot Gravity.csproj (100%) rename src/{Space Robot => Robot Gravity}/RobotGravityTests.cs (88%) diff --git a/challenges.sln b/challenges.sln index d7ce785..5d838a2 100644 --- a/challenges.sln +++ b/challenges.sln @@ -27,7 +27,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ISSUE_TEMPLATE", "ISSUE_TEM .github\ISSUE_TEMPLATE\config.yml = .github\ISSUE_TEMPLATE\config.yml EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robot Gravity", "src\Space Robot\Robot Gravity.csproj", "{98FAB7D8-0C64-43B0-B03C-9CF08BED9089}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robot Gravity", "src\Robot Gravity\Robot Gravity.csproj", "{98FAB7D8-0C64-43B0-B03C-9CF08BED9089}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Robot Gravity/README.md b/src/Robot Gravity/README.md new file mode 100644 index 0000000..a4eeb14 --- /dev/null +++ b/src/Robot Gravity/README.md @@ -0,0 +1,110 @@ +> This is a challenge developed for the C# discord server.
+> Want to Participate? Join the C# discord: + +# Robot Gravity + +You are making a robot to explore other planets. Your robot has a gravity sensor but you want to +have a back up way to determine the current gravity in case the sensor is damaged.Your robot has +the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. +Use those features to determine the current acceleration due to gravity as a positive number +within 0.1 units. + +Use these members from `Robot robot`: +- `robot.Jump` +- `robot.MaxJumpForce` +- `robot.HeightSensor` +- `robot.Mass` + +
+Hint #1: +The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + i` where +- `p` is vertical position +- `g` is acceleration due to gravity +- `s` is time +- `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) +- `i` is initial vertical (in this case i is always 0) +
+ +
+Hint #2: +`robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event. + +```cs +void HandleHeightSensor(double height, DateTime time) +{ + // some code here... +} + +robot.HeightSensor += HandleHeightSensor; +``` +
+ +### Quick start + +Cloning this repo is recommended. + +Complete the challenge in the MS Test project here: [RobotGravityTests.cs](RobotGravityTests.cs) + +### Feedback + +If you have any feedback on the challenges, [please open an issue](https://github.com/discord-csharp/challenges/issues/new/choose), mention the challenge, and ping the contributor(s) of the challenge. + +### Contributor(s) + +- [@zacharypatten](https://github.com/ZacharyPatten) + +### Source + +This was an original challege. :) + +--- + +Discord meta data. Do not edit. This is used for GitHub => Discord linking. + + + + + + + +
Name + Robot Gravity Challenge +
Description + You are making a robot to explore other planets. Your robot has a gravity sensor but you want to have a back up way to determine the current gravity in case the sensor is damaged. Your robot has the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. Use those features to determine the current acceleration due to gravity as a positive number within 0.1 units. +
Sample + + +```cs +public double DetermineGravity(Robot robot) +{ + // ----------------------- + // Complete challenge here. + // + // - robot.Jump + // - robot.MaxJumpForce + // - robot.HeightSensor + // - robot.Mass + // + // ----------------------- +} +``` + +
+Hint #1: +The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + i` where +- `p` is vertical position +- `g` is acceleration due to gravity +- `s` is time +- `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) +- `i` is initial vertical (in this case i is always 0) +
+ +
+Hint #2: +`robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event +
+
Contributed by + 438382611929366537 +
Self link + https://github.com/discord-csharp/challenges/tree/main/src/Wire%20Ends +
\ No newline at end of file diff --git a/src/Space Robot/Robot Gravity.csproj b/src/Robot Gravity/Robot Gravity.csproj similarity index 100% rename from src/Space Robot/Robot Gravity.csproj rename to src/Robot Gravity/Robot Gravity.csproj diff --git a/src/Space Robot/RobotGravityTests.cs b/src/Robot Gravity/RobotGravityTests.cs similarity index 88% rename from src/Space Robot/RobotGravityTests.cs rename to src/Robot Gravity/RobotGravityTests.cs index 673ab72..e0e99c8 100644 --- a/src/Space Robot/RobotGravityTests.cs +++ b/src/Robot Gravity/RobotGravityTests.cs @@ -12,12 +12,11 @@ public double DetermineGravity(Robot robot) // ----------------------- // Complete challenge here. // - // You are making a space robot to explore other planets. Your robot - // has a gravity sensor but you want to have a back up way to determine - // the current gravity in case the sensor is damaged. Your robot has the - // ability to jump and it has an height sensor. Use those features to - // determine the current acceleration due to gravity as a positive number - // within 0.1 units: + // You are making a robot to explore other planets. Your robot has a gravity sensor but you want to + // have a back up way to determine the current gravity in case the sensor is damaged.Your robot has + // the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. + // Use those features to determine the current acceleration due to gravity as a positive number + // within 0.1 units. // // - robot.Jump // - robot.MaxJumpForce From 44d928290526d165b4c8959d7b6f2ff0aa5fdb5f Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 15:25:26 -0500 Subject: [PATCH 03/10] robot gravity readme --- README.md | 1 + src/Robot Gravity/README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b9a308..d728130 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ |Challenge|Challenge Date|Contributor(s)|Source| |:-|:-|:-|:-| |[Wire Ends](src/Wire%20Ends)| |[@zacharypatten](https://github.com/ZacharyPatten)|_original_| +|[Robot Gravity](src/Robot%20Gravity)| |[@zacharypatten](https://github.com/ZacharyPatten)|_original_| Contributions are welcome. :) diff --git a/src/Robot Gravity/README.md b/src/Robot Gravity/README.md index a4eeb14..98bc332 100644 --- a/src/Robot Gravity/README.md +++ b/src/Robot Gravity/README.md @@ -106,5 +106,5 @@ The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + 438382611929366537 Self link - https://github.com/discord-csharp/challenges/tree/main/src/Wire%20Ends + https://github.com/discord-csharp/challenges/tree/main/src/Robot%20Gravity \ No newline at end of file From 8013b9783079542b5f96a91fe16373fc9f2c6016 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 17:28:23 -0500 Subject: [PATCH 04/10] readme --- src/Robot Gravity/README.md | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Robot Gravity/README.md b/src/Robot Gravity/README.md index 98bc332..61982c3 100644 --- a/src/Robot Gravity/README.md +++ b/src/Robot Gravity/README.md @@ -16,17 +16,20 @@ Use these members from `Robot robot`: - `robot.Mass`
-Hint #1: +Hint #1 + The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + i` where - `p` is vertical position - `g` is acceleration due to gravity - `s` is time - `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) - `i` is initial vertical (in this case i is always 0) +
-Hint #2: +Hint #2 + `robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event. ```cs @@ -37,6 +40,7 @@ void HandleHeightSensor(double height, DateTime time) robot.HeightSensor += HandleHeightSensor; ``` +
### Quick start @@ -72,6 +76,8 @@ Discord meta data. Do not edit. This is used for GitHub => Discord linking. Sample +Cloning the repoand complete the challenge in the MS Test project here: https://github.com/discord-csharp/challenges/tree/main/src/Robot%20Gravity/RobotGravityTests.cs + ```cs public double DetermineGravity(Robot robot) { @@ -88,18 +94,31 @@ public double DetermineGravity(Robot robot) ```
-Hint #1: +Hint #1 + The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + i` where - `p` is vertical position - `g` is acceleration due to gravity - `s` is time - `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) - `i` is initial vertical (in this case i is always 0) +
-Hint #2: -`robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event +Hint #2 + +`robot.HeightSensor` is an event. You need to make an event handler with the same method signature and subscribe to the event. + +```cs +void HandleHeightSensor(double height, DateTime time) +{ + // some code here... +} + +robot.HeightSensor += HandleHeightSensor; +``` +
Contributed by From 2f868ddd5a518bae89fd20aced6d07e8ea480567 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 17:32:28 -0500 Subject: [PATCH 05/10] revert readme --- challenges.sln | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/challenges.sln b/challenges.sln index 5d838a2..905a2fc 100644 --- a/challenges.sln +++ b/challenges.sln @@ -5,28 +5,6 @@ VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wire Ends", "src\Wire Ends\Wire Ends.csproj", "{5468530B-AA66-4594-A126-4884664BE91D}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{98007B9A-D93C-48B6-A106-4BB24311A99F}" - ProjectSection(SolutionItems) = preProject - .editorconfig = .editorconfig - .gitignore = .gitignore - CHALLENGE_TEMPLATE_README.md = CHALLENGE_TEMPLATE_README.md - LICENSE = LICENSE - README.md = README.md - EndProjectSection -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{F083909E-C980-4652-83EB-5B8D2FDD6699}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{AAAC3E3E-6216-4B2E-8FB1-30BE98072CDA}" - ProjectSection(SolutionItems) = preProject - .github\workflows\Continuous Integration.yml = .github\workflows\Continuous Integration.yml - EndProjectSection -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ISSUE_TEMPLATE", "ISSUE_TEMPLATE", "{8E572F2B-D7C5-4884-8D71-FF8792961683}" - ProjectSection(SolutionItems) = preProject - .github\ISSUE_TEMPLATE\bug.md = .github\ISSUE_TEMPLATE\bug.md - .github\ISSUE_TEMPLATE\config.yml = .github\ISSUE_TEMPLATE\config.yml - EndProjectSection -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Robot Gravity", "src\Robot Gravity\Robot Gravity.csproj", "{98FAB7D8-0C64-43B0-B03C-9CF08BED9089}" EndProject Global From 0edebfcc2ae86f354b3556024b825f086fd14e9e Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 17:33:46 -0500 Subject: [PATCH 06/10] csproj --- src/Robot Gravity/Robot Gravity.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Robot Gravity/Robot Gravity.csproj b/src/Robot Gravity/Robot Gravity.csproj index 6f02502..b9ce558 100644 --- a/src/Robot Gravity/Robot Gravity.csproj +++ b/src/Robot Gravity/Robot Gravity.csproj @@ -1,18 +1,14 @@ - net6.0 Space_Robot enable - false - - From f897734522d7a7be5810b07fc764bc29ab503720 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 17:45:35 -0500 Subject: [PATCH 07/10] oof --- src/Robot Gravity/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Robot Gravity/README.md b/src/Robot Gravity/README.md index 61982c3..8ab3837 100644 --- a/src/Robot Gravity/README.md +++ b/src/Robot Gravity/README.md @@ -76,7 +76,7 @@ Discord meta data. Do not edit. This is used for GitHub => Discord linking. Sample -Cloning the repoand complete the challenge in the MS Test project here: https://github.com/discord-csharp/challenges/tree/main/src/Robot%20Gravity/RobotGravityTests.cs +Cloning the repo and complete the challenge in the MS Test project here: https://github.com/discord-csharp/challenges/tree/main/src/Robot%20Gravity/RobotGravityTests.cs ```cs public double DetermineGravity(Robot robot) From 3ebc9149d5653651f721d5ad5bd15d80c0dd0f43 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 17:47:00 -0500 Subject: [PATCH 08/10] oof --- src/Robot Gravity/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Robot Gravity/README.md b/src/Robot Gravity/README.md index 8ab3837..628128d 100644 --- a/src/Robot Gravity/README.md +++ b/src/Robot Gravity/README.md @@ -23,7 +23,7 @@ The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + - `g` is acceleration due to gravity - `s` is time - `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) -- `i` is initial vertical (in this case i is always 0) +- `i` is initial vertical position (in this case i is always 0) @@ -101,7 +101,7 @@ The equation for the vertical position of a projectile is `p = -.5*g*s^2 + v*s + - `g` is acceleration due to gravity - `s` is time - `v` is initial velocity (in this case initial velocity is the parameter to `robot.Jump` divided by `robot.Mass`) -- `i` is initial vertical (in this case i is always 0) +- `i` is initial vertical position (in this case i is always 0) From 2378afa577dd232d9bcfe45588f18772b5cc2139 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 19:36:53 -0500 Subject: [PATCH 09/10] seperate unit tests for each plant's gravity --- src/Robot Gravity/RobotGravityTests.cs | 226 ++++++++++++------------- 1 file changed, 111 insertions(+), 115 deletions(-) diff --git a/src/Robot Gravity/RobotGravityTests.cs b/src/Robot Gravity/RobotGravityTests.cs index e0e99c8..3a8be96 100644 --- a/src/Robot Gravity/RobotGravityTests.cs +++ b/src/Robot Gravity/RobotGravityTests.cs @@ -4,127 +4,123 @@ namespace Space_Robot { - [TestClass] - public class RobotGravityTests - { - public double DetermineGravity(Robot robot) - { - // ----------------------- - // Complete challenge here. - // - // You are making a robot to explore other planets. Your robot has a gravity sensor but you want to - // have a back up way to determine the current gravity in case the sensor is damaged.Your robot has - // the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. - // Use those features to determine the current acceleration due to gravity as a positive number - // within 0.1 units. - // - // - robot.Jump - // - robot.MaxJumpForce - // - robot.HeightSensor - // - robot.Mass - // - // ----------------------- - throw new NotImplementedException(); // <- delete - } + [TestClass] + public class RobotGravityTests + { + public static double DetermineGravity(Robot robot) + { + // ----------------------- + // Complete challenge here. + // + // You are making a robot to explore other planets. Your robot has a gravity sensor but you want to + // have a back up way to determine the current gravity in case the sensor is damaged.Your robot has + // the ability to jump and it has an height sensor that asynchronously scans about every .01 seconds. + // Use those features to determine the current acceleration due to gravity as a positive number + // within 0.1 units. + // + // - robot.Jump + // - robot.MaxJumpForce + // - robot.HeightSensor + // - robot.Mass + // + // ----------------------- + throw new NotImplementedException(); // <- delete + } - public class Robot - { - [Obsolete("You don't need to make any Robots. :P")] - public Robot(Func<(double, DateTime)> getHeight, Action jump) - { - Timer timer = new((int)HeightSensorInterval.TotalMilliseconds); - timer.Elapsed += (o, e) => - { - var (height, time) = getHeight(); - HeightSensor?.Invoke(height, time); - }; - timer.Enabled = true; - JumpCallback = jump; - } - private Action JumpCallback { get; set; } - private readonly TimeSpan HeightSensorInterval = TimeSpan.FromSeconds(.01); - public double Mass { get; private set; } = 5; - public double MaxJumpForce { get; private set; } = 50d; - public event Action? HeightSensor; - public void Jump(double force) => JumpCallback?.Invoke(force); - } + public class Robot + { + [Obsolete("You don't need to make any Robots. :P")] + public Robot(Func<(double, DateTime)> getHeight, Action jump) + { + Timer timer = new((int)HeightSensorInterval.TotalMilliseconds); + timer.Elapsed += (o, e) => + { + var (height, time) = getHeight(); + HeightSensor?.Invoke(height, time); + }; + timer.Enabled = true; + JumpCallback = jump; + } + private Action JumpCallback { get; set; } + private readonly TimeSpan HeightSensorInterval = TimeSpan.FromSeconds(.01); + public double Mass { get; private set; } = 5; + public double MaxJumpForce { get; private set; } = 50d; + public event Action? HeightSensor; + public void Jump(double force) => JumpCallback?.Invoke(force); + } - [TestMethod] - public void Test() - { - // measurements in meters per second squared (m/s²) - const double GravityOnMercury = 3.7; - const double GravityOnVenus = 8.87; - const double GravityOnEarth = 9.807; - const double GravityOnMars = 3.721; - const double GravityOnJupiter = 24.79; - const double GravityOnSaturn = 10.44; - const double GravityOnUranus = 8.87; - const double GravityOnNeptune = 11.15; - const double GravityOnPluto = 0.62; - const double GravityOnEarthMoon = 1.62; + // measurements in meters per second squared (m/s²) + const double GravityOnMercury = 3.7; + const double GravityOnVenus = 8.87; + const double GravityOnEarth = 9.807; + const double GravityOnMars = 3.721; + const double GravityOnJupiter = 24.79; + const double GravityOnSaturn = 10.44; + const double GravityOnUranus = 8.87; + const double GravityOnNeptune = 11.15; + const double GravityOnPluto = 0.62; + const double GravityOnEarthMoon = 1.62; - Robot InitializeRobot(double gravity) - { - (double InitialVelocity, DateTime Time)? jump = null; + [TestMethod] public void TestGravityOnMercury() => Test(GravityOnMercury); + [TestMethod] public void TestGravityOnVenus() => Test(GravityOnVenus); + [TestMethod] public void TestGravityOnEarth() => Test(GravityOnEarth); + [TestMethod] public void TestGravityOnMars() => Test(GravityOnMars); + [TestMethod] public void TestGravityOnJupiter() => Test(GravityOnJupiter); + [TestMethod] public void TestGravityOnSaturn() => Test(GravityOnSaturn); + [TestMethod] public void TestGravityOnUranus() => Test(GravityOnUranus); + [TestMethod] public void TestGravityOnNeptune() => Test(GravityOnNeptune); + [TestMethod] public void TestGravityOnPluto() => Test(GravityOnPluto); + [TestMethod] public void TestGravityOnEarthMoon() => Test(GravityOnEarthMoon); - Robot robot = default!; -#pragma warning disable CS0618 // Type or member is obsolete - robot = new( - getHeight: GetHeight, - jump: force => - { - if (GetHeight().Height is 0) - { - jump = (force / robot.Mass, DateTime.Now); - } - }); -#pragma warning restore CS0618 // Type or member is obsolete + static void Test(double gravity) + { + try + { + Robot robot = InitializeRobot(gravity); + double determinedGravity = Math.Abs(DetermineGravity(robot)); + Assert.IsTrue(Math.Abs(determinedGravity - gravity) <= 0.1, $"expected {gravity} but got {determinedGravity}"); + } + catch (NotImplementedException) + { + Assert.Inconclusive(); + } + } - (double Height, DateTime Time) GetHeight() - { - DateTime now = DateTime.Now; - if (jump is null) - { - return (0, now); - } - double seconds = (now - jump.Value.Time).TotalSeconds; - double height = -.5 * gravity * seconds * seconds + jump.Value.InitialVelocity * seconds; - if (height < 0) - { - jump = null; - height = 0; - } - return (height, now); - } + static Robot InitializeRobot(double gravity) + { + (double InitialVelocity, DateTime Time)? jump = null; - return robot; - } + Robot robot = default!; +#pragma warning disable CS0618 // Type or member is obsolete + robot = new( + getHeight: GetHeight, + jump: force => + { + if (GetHeight().Height is 0) + { + jump = (force / robot.Mass, DateTime.Now); + } + }); +#pragma warning restore CS0618 // Type or member is obsolete - void Test(double gravity) - { - try - { - Robot robot = InitializeRobot(gravity); - double determinedGravity = Math.Abs(DetermineGravity(robot)); - Assert.IsTrue(Math.Abs(determinedGravity - gravity) <= 0.1, $"expected {gravity} but got {determinedGravity}"); - } - catch (NotImplementedException) - { - Assert.Inconclusive(); - } - } + (double Height, DateTime Time) GetHeight() + { + DateTime now = DateTime.Now; + if (jump is null) + { + return (0, now); + } + double seconds = (now - jump.Value.Time).TotalSeconds; + double height = -.5 * gravity * seconds * seconds + jump.Value.InitialVelocity * seconds; + if (height < 0) + { + jump = null; + height = 0; + } + return (height, now); + } - Test(GravityOnMercury); - Test(GravityOnVenus); - Test(GravityOnEarth); - Test(GravityOnMars); - Test(GravityOnJupiter); - Test(GravityOnSaturn); - Test(GravityOnUranus); - Test(GravityOnNeptune); - Test(GravityOnPluto); - Test(GravityOnEarthMoon); - } - } + return robot; + } + } } \ No newline at end of file From c86592103acc3b377fb10e04efa5b81d71526628 Mon Sep 17 00:00:00 2001 From: Zachary Patten Date: Tue, 23 Nov 2021 19:40:15 -0500 Subject: [PATCH 10/10] removed const's --- src/Robot Gravity/RobotGravityTests.cs | 31 +++++++++----------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/Robot Gravity/RobotGravityTests.cs b/src/Robot Gravity/RobotGravityTests.cs index 3a8be96..83ebf05 100644 --- a/src/Robot Gravity/RobotGravityTests.cs +++ b/src/Robot Gravity/RobotGravityTests.cs @@ -50,27 +50,16 @@ public Robot(Func<(double, DateTime)> getHeight, Action jump) } // measurements in meters per second squared (m/s²) - const double GravityOnMercury = 3.7; - const double GravityOnVenus = 8.87; - const double GravityOnEarth = 9.807; - const double GravityOnMars = 3.721; - const double GravityOnJupiter = 24.79; - const double GravityOnSaturn = 10.44; - const double GravityOnUranus = 8.87; - const double GravityOnNeptune = 11.15; - const double GravityOnPluto = 0.62; - const double GravityOnEarthMoon = 1.62; - - [TestMethod] public void TestGravityOnMercury() => Test(GravityOnMercury); - [TestMethod] public void TestGravityOnVenus() => Test(GravityOnVenus); - [TestMethod] public void TestGravityOnEarth() => Test(GravityOnEarth); - [TestMethod] public void TestGravityOnMars() => Test(GravityOnMars); - [TestMethod] public void TestGravityOnJupiter() => Test(GravityOnJupiter); - [TestMethod] public void TestGravityOnSaturn() => Test(GravityOnSaturn); - [TestMethod] public void TestGravityOnUranus() => Test(GravityOnUranus); - [TestMethod] public void TestGravityOnNeptune() => Test(GravityOnNeptune); - [TestMethod] public void TestGravityOnPluto() => Test(GravityOnPluto); - [TestMethod] public void TestGravityOnEarthMoon() => Test(GravityOnEarthMoon); + [TestMethod] public void TestGravityOnMercury() => Test(3.7); + [TestMethod] public void TestGravityOnVenus() => Test(8.87); + [TestMethod] public void TestGravityOnEarth() => Test(9.807); + [TestMethod] public void TestGravityOnMars() => Test(3.721); + [TestMethod] public void TestGravityOnJupiter() => Test(24.79); + [TestMethod] public void TestGravityOnSaturn() => Test(10.44); + [TestMethod] public void TestGravityOnUranus() => Test(8.87); + [TestMethod] public void TestGravityOnNeptune() => Test(11.15); + [TestMethod] public void TestGravityOnPluto() => Test(0.62); + [TestMethod] public void TestGravityOnEarthMoon() => Test(1.62); static void Test(double gravity) {