Guess demo improvements.

This commit is contained in:
José Pedro 2017-12-10 01:31:21 +00:00
parent 8bed32957e
commit 7befa1eb3c
No known key found for this signature in database
GPG key ID: B8247B9301707B83
2 changed files with 40 additions and 33 deletions

View file

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Runtime.InteropServices; using Cosmos.Debug.Kernel;
using System.Text;
//using Cosmos.Debug.Kernel;
using Sys = Cosmos.System; using Sys = Cosmos.System;
/* /*
@ -13,55 +11,61 @@ namespace Guess
{ {
public class GuessOS : Sys.Kernel public class GuessOS : Sys.Kernel
{ {
protected int mCount = 0; private Random mRandom;
private int mCount;
protected int mMagicNo = 22; private int mNumber;
public GuessOS()
{
// Didnt check if tickcount is working yet.. can change this later
//var xRandom = new Random(234243534);
//mMagicNo = xRandom.Next(1, 100);
}
protected override void BeforeRun() protected override void BeforeRun()
{ {
//Cosmos.Core.HMI.Init(); mRandom = new Random();
mCount = 0;
mNumber = mRandom.Next(1, 100);
Console.Clear(); Console.Clear();
Console.WriteLine("Guess Demo"); Console.WriteLine("Guess Demo");
Console.WriteLine("----------");
Console.WriteLine();
Console.WriteLine("Please guess a number from 1 to 100."); Console.WriteLine("Please guess a number from 1 to 100.");
Console.WriteLine();
} }
protected override void Run() protected override void Run()
{ {
mCount++; mCount++;
//mDebugger.Send(""); Console.Write($"Guess #{mCount}: ");
//mDebugger.SendMessage("Kernel", "New iteration");
Console.WriteLine(); var xGuess = Int32.Parse(Console.ReadLine());
Console.WriteLine("Guess #" + mCount); mDebugger.Send($"Guess#{mCount}: {xGuess}");
Console.Write("Please enter a guess: ");
string xInputStr = Console.ReadLine(); if (xGuess < mNumber)
Console.Write("Input length: ");
Console.WriteLine(xInputStr.Length.ToString());
int xGuess = int.Parse(xInputStr);
Console.WriteLine("Your guess was " + xGuess);
if (xGuess < mMagicNo)
{ {
Console.WriteLine("Too low."); Console.WriteLine("Too low.");
} }
else if (xGuess > mMagicNo) else if (xGuess > mNumber)
{ {
Console.WriteLine("Too high."); Console.WriteLine("Too high.");
} }
else else
{ {
Console.WriteLine("You guessed it!"); Console.WriteLine("You guessed it!");
Console.WriteLine("Press any key to end Guess Demo. Thanks for playing!"); Console.WriteLine("Press s to stop or any other key to play again. Thanks for playing!");
Console.ReadKey();
Stop(); if (Console.ReadKey().Key == ConsoleKey.S)
{
Stop();
}
mNumber = mRandom.Next(1, 100);
mCount = 0;
} }
} }
protected override void AfterRun()
{
Sys.Power.Shutdown();
}
} }
} }

View file

@ -5,7 +5,7 @@ using Cosmos.HAL;
namespace Cosmos.System_Plugs.System namespace Cosmos.System_Plugs.System
{ {
[Plug(Target = typeof(Random))] [Plug(Target = typeof(Random))]
public class RandomImpl public static class RandomImpl
{ {
public static void Ctor(Random aThis) public static void Ctor(Random aThis)
{ {
@ -17,6 +17,9 @@ namespace Cosmos.System_Plugs.System
//empty ATM //empty ATM
} }
// TODO: improve this
public static int GenerateGlobalSeed() => RTC.Second;
public static int Next(Random aThis, int maxValue) public static int Next(Random aThis, int maxValue)
{ {
return (int)(GetUniform() * maxValue); return (int)(GetUniform() * maxValue);