How to resolve the algorithm Guess the number/With feedback step by step in the Genie 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 Genie 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 Genie programming language

Source code in the genie programming language

[indent=4]
/*
   Number guessing with feedback, in Genie
   from https://wiki.gnome.org/Projects/Genie/AdvancedSample

   valac numberGuessing.gs
   ./numberGuessing
*/
class NumberGuessing

    prop min:int
    prop max:int
    
    construct(m:int, n:int)
        self.min = m
        self.max = n
    
    def start()
        try_count:int = 0
        number:int = Random.int_range(min, max)
        
        stdout.printf("Welcome to Number Guessing!\n\n")
        stdout.printf("I have thought up a number between %d and %d\n", min, max)
        stdout.printf("which you have to guess. I will give hints as we go.\n\n")
    
        while true
            stdout.printf("Try #%d, ", ++try_count)
            stdout.printf("please enter a number between %d and %d: ", min, max)
            line:string? = stdin.read_line()
            if line is null
                stdout.printf("bailing...\n")
                break

            input:int64 = 0
            unparsed:string = ""
            converted:bool = int64.try_parse(line, out input, out unparsed)
            if not converted or line is unparsed
                stdout.printf("Sorry, input seems invalid\n")
                continue

            guess:int = (int)input
            if number is guess
                stdout.printf("Congratulations! You win.\n")
                break
            else
                stdout.printf("Try again. The number in mind is %s than %d.\n",
                   number > guess ? "greater" : "less", guess)

init
    var game = new NumberGuessing(1, 100)
    game.start()

  

You may also check:How to resolve the algorithm Shell one-liner step by step in the Perl programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the Red programming language
You may also check:How to resolve the algorithm Even or odd step by step in the ABAP programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the BQN programming language
You may also check:How to resolve the algorithm Special characters step by step in the BASIC programming language