Guess It in FreePascal
{ Guess program.  public domain as of august 13th 2007. Mystikshadows }

Program GuessingGame;

Uses
     Crt;

Const
     RandomMinimum = 1;
     RandomMaximum = 100;

Var
     RandomNumber:   Integer;
     UserGuess:      Integer;
     UserAnswer:     Char;
     RoundNumber:    Integer;
     CanExit:        Integer;
     CancelGuessing: Integer;
Begin

     Randomize;
     CanExit := 0;
     While CanExit = 0 Do
     Begin
           RandomNumber := Random(RandomMaximum) + 1;
           WriteLn('Ok, I just thought of a number between ', RandomMinimum, ' and ', RandomMaximum);
           WriteLn('And you can try to guess it in the least amount of tries.');
           WriteLn;
           WriteLn('After each guess I will give you hints as to if my number is');
           WriteLn('higher or lower than your guess again or enter 0 to exit the');
           WriteLn('current guessing.');
           WriteLn;
           RoundNumber    := 1;
           CancelGuessing := 0;
           UserGuess      := 0;
           UserAnswer     := #32;
           While (RandomNumber <> UserGuess) And (CancelGuessing = 0) Do
           Begin
                 Write('Round # ', RoundNumber, ': ');
                 ReadLn(UserGuess);
                 If (UserGuess = 0) Then
                    CancelGuessing := 1;
                 If (UserGuess > RandomNumber) Then
                    WriteLn('You are too high, guess lower.');
                 If (UserGuess < RandomNumber) Then
                    WriteLn('You are too low, guess higher.');
                 RoundNumber := RoundNumber + 1;
           End;
           If RandomNumber = UserGuess Then
           Begin
              WriteLn;
              WriteLn('You guess the number in ', RoundNumber - 1, ' guesses.');
           End;
           While (UserAnswer <> 'y') And (UserAnswer <> 'Y') And (UserAnswer <> 'n') And (UserAnswer <> 'N') Do
           Begin
                 WriteLn;
                 Write('Do you want to try another round (Y for Yes, N for No)?');
                 ReadLn(UserAnswer);
           End;
           If (UserAnswer = 'n') Or (UserAnswer = 'N') Then
              CanExit := 1;
     End;

End.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.