@@ -6,18 +6,72 @@ static void Main(){
66 Console . WriteLine ( "Created by Morasiu (morasiu2@gmail.com)" ) ;
77 Console . Write ( "Press any key to start." ) ;
88 Console . ReadKey ( ) ;
9- Console . WriteLine ( "Game Start!" ) ;
10- GetDifficultLevel ( ) ;
9+
10+ StartGame ( ) ;
11+ }
12+
13+ static void StartGame ( ) {
14+ int maxNumber = GetDifficultLevel ( ) ;
15+ Random rand = new Random ( ) ;
16+ //First number is included, but second is exluded so I needed to add 1.
17+ int randomNumber = rand . Next ( 1 , maxNumber + 1 ) ;
18+ int number = 0 ;
19+ int attempt = 0 ;
20+ Console . WriteLine ( "\n Game Start!" ) ;
21+
22+ //Start guessing loop.
23+ do {
24+ Console . Write ( "Enter valid number: " ) ;
25+ //Try parse string from Console to int.
26+ try {
27+ number = int . Parse ( Console . ReadLine ( ) ) ;
28+ } catch {
29+ Console . WriteLine ( "Not a valid number." ) ;
30+ continue ;
31+ }
32+ if ( number == randomNumber ) {
33+ attempt ++ ;
34+ Console . WriteLine ( "Hurray! You won :) Attempts: " + attempt . ToString ( ) ) ;
35+ } else if ( number > randomNumber ) {
36+ attempt ++ ;
37+ Console . WriteLine ( "Lower" ) ;
38+ } else if ( number < randomNumber ) {
39+ attempt ++ ;
40+ Console . WriteLine ( "Higher" ) ;
41+ }
42+ } while ( number != randomNumber ) ;
43+ //Ask if player want to play again.
44+ PlayAgain ( ) ;
45+ }
1146
47+ static void PlayAgain ( ) {
48+ Console . Write ( "Do you want to play again? (y/n): " ) ;
49+ string decision = Console . ReadLine ( ) ;
50+ if ( decision == "y" )
51+ StartGame ( ) ;
52+ else if ( decision == "n" ) {
53+ Console . WriteLine ( "Thanks for playing. :)" ) ;
54+ } else {
55+ Console . WriteLine ( "Wrong option" ) ;
56+ PlayAgain ( ) ;
57+ }
1258 }
1359
14- static int GetDifficultLevel ( ) {
15- Console . WriteLine ( "1 . Easy (1-10)\n 2. Medium (1-100)\n 3. Hard (1-1000)\n Pick difficult level: " ) ;
60+ static int GetDifficultLevel ( ) {
61+ Console . WriteLine ( "\n 1 . Easy (1-10)\n 2. Medium (1-100)\n 3. Hard (1-1000)\n Pick difficult level: " ) ;
1662 int maxNumber = 0 ;
1763 var difficult = Console . ReadLine ( ) ;
18- if ( difficult = "1" ) {
19-
64+ if ( difficult == "1" )
65+ maxNumber = 10 ;
66+ else if ( difficult == "2" )
67+ maxNumber = 100 ;
68+ else if ( difficult == "3" )
69+ maxNumber = 1000 ;
70+ else {
71+ Console . WriteLine ( "Wrong option." ) ;
72+ maxNumber = GetDifficultLevel ( ) ;
2073 }
21- return difficult ;
74+
75+ return maxNumber ;
2276 }
2377}
0 commit comments