• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

Learn the Ada programming language by writing a simple game

Learn the Ada programming language by writing a simple game: Learn the Ada programming language by writing a simple game Moshe Zadka Mon, 01/09/2023 – 03:00

When you want to learn a new programming language, it’s good to focus on the things programming languages have in common:

  • Variables
  • Expressions
  • Statements

These concepts are the basis of most programming languages. Once you understand them, you can start figuring out the rest. Because programming languages usually share similarities, once you know one language, you can learn the basics of another by understanding its differences.

A good way to learn new languages is practicing with a standard program. This allows you to focus on the language, not the program’s logic. I’m doing that in this article series using a “guess the number” program, in which the computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the number correctly.

This program exercises several concepts in programming languages:

  • Variables
  • Input
  • Output
  • Conditional evaluation
  • Loops

It’s a great practical experiment to learn a new programming language.

Install Ada

The Ada programming language is a unique and highly structured language with a dedicated developer base. The toolchain for Ada is the GNU Ada Development Environment, better known as GNAT.

You can install GNAT on Linux using your distribution’s package manager. On Fedora, CentOS, or similar:

 $   sudo dnf install gcc-gnat

On Debian, Linux Mint, and derivatives:

 $   sudo apt install gnat

On macOS and Windows, you can download an installer from the Adacore website (choose your platform from the drop-down menu).

Guess the number in Ada

Create a file called game.adb.

The two built-in Ada libraries this program uses are Text_IO and Numerics.Discrete_Random:

 
with Ada.Text_IO;

use Ada.Text_IO;

with Ada.Numerics.Discrete_Random;

Procedure head

The name of the procedure must match the name of the file. The first part is defining the variables.

Note that the discrete_random is specialized to a specific range. In this case, the range of numbers allowed:

 
procedure Game is
   type randRange is range 1..100;
   package Rand_Int is new ada.numerics.discrete_random(randRange);
   use Rand_Int;
   gen : Generator;
   num : randRange;
   incorrect: Boolean := True;
   guess: randRange;

Programming and development
Red Hat Developers Blog
Programming cheat sheets
Try for free: Red Hat Learning Subscription
eBook: An introduction to programming with Bash
Bash shell scripting cheat sheet
eBook: Modernizing Enterprise Java
An open source developer’s guide to building applications

Procedure logic

The logic starts by reset(gen). This initializes the random number generator, ensuring the number, initialized with random(gen), will be different each time you run the program.

The next step is to run the loop:

  • Output the instructions for a guess
  • Read the line
  • Convert it to randRange
  • Check it against the number

If the number matches, incorrect is set to False, causing the next iteration of the loop to exit.

Finally, the program prints a confirmation of the guess correctness before exiting:

 
begin
   reset(gen);
   num := random(gen);
   while incorrect loop
       Put_Line ("Guess a number between 1 and 100");
       declare
          guess_str : String := Get_Line (Current_Input);
       begin
          guess := randRange'Value (guess_str);
       end;
       if guess < num then
           Put_line("Too low");
       elsif guess > num then
           Put_line("Too high");
       else
           incorrect := False;
       end if;
   end loop;
   Put_line("That's right");
end Game;

Build the program

The easiest way to compile an Ada program is to use gnatmake:

 
$ gnatmake game.adb
aarch64-linux-gnu-gcc-10 -c game.adb
aarch64-linux-gnu-gnatbind-10 -x game.ali
aarch64-linux-gnu-gnatlink-10 game.ali

This generates a binary called game.

Run the program

Each run of the program will be a little different. This is one example:

 
$ ./game 
Guess a number between 1 and 100
50
Too low
Guess a number between 1 and 100
75
Too low
Guess a number between 1 and 100
82
Too low
Guess a number between 1 and 100
90
Too high
Guess a number between 1 and 100
87
Too low
Guess a number between 1 and 100
88
That's right

Learn Ada

This “guess the number” game is a great introductory program for learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts of the languages and compare their details.

Do you have a favorite programming language? How would you write the “guess the number” game in it? Follow this article series to see examples of other programming languages that might interest you!

This “guess the number” game is a great introductory program for learning a new programming language because it exercises several common programming concepts in a pretty straightforward way.

Working from home at a laptop
Image by:

Opensource.com

Programming

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Moshe Zadka

Supporting Open Source.

Have you tried: Diving with Sharks?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 9 January 2023 by Alan Category: SoftwareTag: Game, Language, Learn, Programming, Simple, Writing

Trending Now: Faraway Downs

These are the latest trending searches on Google right now: Faraway Downs: : Daily Search Trends …

Read moreTrending Now: Faraway Downs

Moving South Africa’s Education Needle Forward

Table of Contents Toggle Financing Change Taking Ownership and Embracing Modernised Education …

Read moreMoving South Africa’s Education Needle Forward

Despite lockdown, smokers are still getting their tobacco fix albeit by very expensive means

South Africa in the news: Despite lockdown, smokers are still getting their tobacco fix albeit by …

Read moreDespite lockdown, smokers are still getting their tobacco fix albeit by very expensive means

About Alan

Previous Post:Trending Now: Arrest
Next Post:Antipsychotic withdrawal – an unrecognised and misdiagnosed problem

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top