How to resolve the algorithm Guess the number/With feedback step by step in the Objeck programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Guess the number/With feedback step by step in the Objeck programming language

Table of Contents

Problem Statement

Write a game (computer program) that follows the following rules:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number/With feedback step by step in the Objeck programming language

Source code in the objeck programming language

use IO;

bundle Default {
   class GuessNumber {
      function : Main(args : String[]) ~ Nil {
         Guess();
      }
      function : native : Guess() ~ Nil {
         done := false;
         "Guess the number which is between 1 and 10 or 'n' to quite: "->PrintLine();
         rand_num := (Float->Random() * 10.0)->As(Int) + 1;
         while(done = false) {
            guess := Console->ReadString();
            number := guess->ToInt();
            if(number <> 0) {
               if(number <> rand_num) {
                  Console->Print("Your guess was too ")
                     ->Print(number < rand_num ? "low" : "high")
                     ->Print(".\nGuess again: ");
               }
               else {
                  "Hurray! You guessed correctly!"->PrintLine();
                  done := true;
               };
            }
            else {
               if(guess->StartsWith("q") | guess->StartsWith("Q")) {
                  done := true;
               };
            };
         };
      }
   }
}

  

You may also check:How to resolve the algorithm Vigenère cipher/Cryptanalysis step by step in the Ada programming language
You may also check:How to resolve the algorithm Sockets step by step in the Fantom programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Excel programming language
You may also check:How to resolve the algorithm Anti-primes step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Quine step by step in the ABAP programming language