From 75e7e25a056df3fe6bf1e698e864516b6b79eaf5 Mon Sep 17 00:00:00 2001 From: epvanhouten Date: Sat, 27 Feb 2021 18:02:47 -0600 Subject: [PATCH] Introduce game class Cleanup uses of goto and encapsulate game logic. --- 51 Hurkle/csharp/src/hurkle/HurkleGame.cs | 111 ++++++++++++++++++++ 51 Hurkle/csharp/src/hurkle/Program.cs | 117 ++-------------------- 2 files changed, 118 insertions(+), 110 deletions(-) create mode 100644 51 Hurkle/csharp/src/hurkle/HurkleGame.cs diff --git a/51 Hurkle/csharp/src/hurkle/HurkleGame.cs b/51 Hurkle/csharp/src/hurkle/HurkleGame.cs new file mode 100644 index 00000000..7abeaad5 --- /dev/null +++ b/51 Hurkle/csharp/src/hurkle/HurkleGame.cs @@ -0,0 +1,111 @@ +using System; + +namespace hurkle +{ + public class HurkleGame + { + private readonly Random _random = new Random(); + private readonly int guesses; + private readonly int gridSize; + + public HurkleGame(int guesses, int gridSize) + { + this.guesses = guesses; + this.gridSize = gridSize; + } + + public void PlayGame() + { + // BASIC program was generating a float between 0 and 1 + // then multiplying by the size of the grid to to a number + // between 1 and 10. C# allows you to do that directly. + var A = _random.Next(0,gridSize); + var B = _random.Next(0,gridSize); + + /* + 310 FOR K=1 TO N + 320 PRINT "GUESS #";K; + 330 INPUT X,Y + 340 IF ABS(X-A)+ABS(Y-B)=0 THEN 500 + 350 REM PRINT INFO + 360 GOSUB 610 + 370 PRINT + 380 NEXT K + */ + for(var K=1;K<=guesses;K++) + { + Console.WriteLine($"GUESS #{K}"); + var inputLine = Console.ReadLine(); + var seperateStrings = inputLine.Split(',', 2, StringSplitOptions.TrimEntries); + var X = int.Parse(seperateStrings[0]); + var Y = int.Parse(seperateStrings[1]); + if(Math.Abs(X-A) + Math.Abs(Y-B) == 0) + { + /* + 500 REM + 510 PRINT + 520 PRINT "YOU FOUND HIM IN";K;GUESSES!" + 540 GOTO 440 + */ + Console.WriteLine(); + Console.WriteLine($"YOU FOUND HIM IN {K} GUESSES!"); + return; + } + + PrintInfo(X,Y,A,B); + } + + /* + 410 PRINT + 420 PRINT "SORRY, THAT'S;N;"GUESSES." + 430 PRINT "THE HURKLE IS AT ";A;",";B + */ + Console.WriteLine(); + Console.WriteLine($"SORRY, THAT'S {guesses} GUESSES"); + Console.WriteLine($"THE HURKLE IS AT {A},{B}"); + } + + private static void PrintInfo(int X, int Y, int A, int B) + { + + /* + 610 PRINT "GO "; + */ + Console.Write("GO "); + /* + 620 IF Y=B THEN 670 + 630 IF YB) + { + Console.Write("SOUTH"); + }else if(YA) + { + Console.Write("WEST"); + } + + Console.WriteLine(); + /* + 720 PRINT + 730 RETURN + */ + } + } +} \ No newline at end of file diff --git a/51 Hurkle/csharp/src/hurkle/Program.cs b/51 Hurkle/csharp/src/hurkle/Program.cs index 9e768b08..447fa547 100644 --- a/51 Hurkle/csharp/src/hurkle/Program.cs +++ b/51 Hurkle/csharp/src/hurkle/Program.cs @@ -39,119 +39,16 @@ namespace hurkle Console.WriteLine(@"AFTER EACH TRY, I WILL TELL YOU THE APPROXIMATE"); Console.WriteLine(@"DIRECTION TO GO TO LOOK FOR THE HURKLE."); Console.WriteLine(); - - /* - 285 A=INT(G*RND(1)) - 286 B=INT(G*RND(1)) - */ - // Get a pseudo number generator - var randomSource = new Random(); - START: - // BASIC program was generating a float between 0 and 1 - // then multiplying by the size of the grid to to a number - // between 1 and 10. C# allows you to do that directly. - var A = randomSource.Next(0,G); - var B = randomSource.Next(0,G); - - /* - 310 FOR K=1 TO N - 320 PRINT "GUESS #";K; - 330 INPUT X,Y - 340 IF ABS(X-A)+ABS(Y-B)=0 THEN 500 - 350 REM PRINT INFO - 360 GOSUB 610 - 370 PRINT - 380 NEXT K - */ - for(var K=1;K<=N;K++) - { - Console.WriteLine($"GUESS #{K}"); - var inputLine = Console.ReadLine(); - var seperateStrings = inputLine.Split(',', 2, StringSplitOptions.TrimEntries); - var X = int.Parse(seperateStrings[0]); - var Y = int.Parse(seperateStrings[1]); - if(Math.Abs(X-A) + Math.Abs(Y-B) == 0) - { - /* - 500 REM - 510 PRINT - 520 PRINT "YOU FOUND HIM IN";K;GUESSES!" - 540 GOTO 440 - */ - Console.WriteLine(); - Console.WriteLine($"YOU FOUND HIM IN {K} GUESSES!"); - goto END; - } - PrintInfo(X,Y,A,B); - } - - /* - 410 PRINT - 420 PRINT "SORRY, THAT'S;N;"GUESSES." - 430 PRINT "THE HURKLE IS AT ";A;",";B - */ - Console.WriteLine(); - Console.WriteLine($"SORRY, THAT'S {N} GUESSES"); - Console.WriteLine($"THE HURKLE IS AT {A},{B}"); - /* - 440 PRINT - 450 PRINT "LET'S PLAY AGAIN. HURKLE IS HIDING." - 460 PRINT - 470 GOTO 285 - */ - END: - Console.WriteLine(); - Console.WriteLine("LET'S PLAY AGAIN. HURKLE IS HIDING"); - Console.WriteLine(); - goto START; - - /* - 999 END - */ - } + var hurkle = new HurkleGame(N,G); + while(true) + { + hurkle.PlayGame(); - private static void PrintInfo(int X, int Y, int A, int B) - { - - /* - 610 PRINT "GO "; - */ - Console.Write("GO "); - /* - 620 IF Y=B THEN 670 - 630 IF YB) - { - Console.Write("SOUTH"); - }else if(YA) - { - Console.Write("WEST"); - } - - Console.WriteLine(); - /* - 720 PRINT - 730 RETURN - */ } } }