From 7befa1eb3c8a7d67db6109f549824a57d9d431cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Sun, 10 Dec 2017 01:31:21 +0000 Subject: [PATCH] Guess demo improvements. --- Demos/Guess/Kernel.cs | 66 ++++++++++--------- .../Cosmos.System2_Plugs/System/RandomImpl.cs | 7 +- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Demos/Guess/Kernel.cs b/Demos/Guess/Kernel.cs index 949f51234..3cea85fb5 100644 --- a/Demos/Guess/Kernel.cs +++ b/Demos/Guess/Kernel.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; -//using Cosmos.Debug.Kernel; + +using Cosmos.Debug.Kernel; using Sys = Cosmos.System; /* @@ -13,55 +11,61 @@ namespace Guess { public class GuessOS : Sys.Kernel { - protected int mCount = 0; - - protected int mMagicNo = 22; - - public GuessOS() - { - // Didnt check if tickcount is working yet.. can change this later - //var xRandom = new Random(234243534); - //mMagicNo = xRandom.Next(1, 100); - } - + private Random mRandom; + private int mCount; + private int mNumber; + protected override void BeforeRun() { - //Cosmos.Core.HMI.Init(); + mRandom = new Random(); + + mCount = 0; + mNumber = mRandom.Next(1, 100); + Console.Clear(); Console.WriteLine("Guess Demo"); + Console.WriteLine("----------"); + Console.WriteLine(); Console.WriteLine("Please guess a number from 1 to 100."); + Console.WriteLine(); } protected override void Run() { mCount++; + + Console.Write($"Guess #{mCount}: "); + + var xGuess = Int32.Parse(Console.ReadLine()); + mDebugger.Send($"Guess#{mCount}: {xGuess}"); - //mDebugger.Send(""); - //mDebugger.SendMessage("Kernel", "New iteration"); - Console.WriteLine(); - Console.WriteLine("Guess #" + mCount); - Console.Write("Please enter a guess: "); - string xInputStr = Console.ReadLine(); - Console.Write("Input length: "); - Console.WriteLine(xInputStr.Length.ToString()); - int xGuess = int.Parse(xInputStr); - Console.WriteLine("Your guess was " + xGuess); - if (xGuess < mMagicNo) + if (xGuess < mNumber) { Console.WriteLine("Too low."); } - else if (xGuess > mMagicNo) + else if (xGuess > mNumber) { Console.WriteLine("Too high."); } else { Console.WriteLine("You guessed it!"); - Console.WriteLine("Press any key to end Guess Demo. Thanks for playing!"); - Console.ReadKey(); - Stop(); + Console.WriteLine("Press s to stop or any other key to play again. Thanks for playing!"); + + if (Console.ReadKey().Key == ConsoleKey.S) + { + Stop(); + } + + mNumber = mRandom.Next(1, 100); + mCount = 0; } } + + protected override void AfterRun() + { + Sys.Power.Shutdown(); + } } } diff --git a/source/Cosmos.System2_Plugs/System/RandomImpl.cs b/source/Cosmos.System2_Plugs/System/RandomImpl.cs index 9856b912c..e49f0df6b 100644 --- a/source/Cosmos.System2_Plugs/System/RandomImpl.cs +++ b/source/Cosmos.System2_Plugs/System/RandomImpl.cs @@ -5,7 +5,7 @@ using Cosmos.HAL; namespace Cosmos.System_Plugs.System { [Plug(Target = typeof(Random))] - public class RandomImpl + public static class RandomImpl { public static void Ctor(Random aThis) { @@ -17,6 +17,9 @@ namespace Cosmos.System_Plugs.System //empty ATM } + // TODO: improve this + public static int GenerateGlobalSeed() => RTC.Second; + public static int Next(Random aThis, int maxValue) { return (int)(GetUniform() * maxValue); @@ -60,4 +63,4 @@ namespace Cosmos.System_Plugs.System return uniform; } } -} \ No newline at end of file +}