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

Free-Mail

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

Learn to code a simple game in Zig

Learn to code a simple game in Zig: Learn to code a simple game in Zig Moshe Zadka Mon, 01/30/2023 – 03:00

Writing the same application in multiple languages is a great way to learn new ways to program. Most programming languages have certain things in common, such as:

  • Variables
  • Expressions
  • Statements

These concepts are the basis of most programming languages. Once you understand them, you can take the time you need to figure out the rest.

Furthermore, programming languages usually share some similarities. Once you know one programming language, you can learn the basics of another by recognizing its differences.

A good tool for learning a new language is by 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 1 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.

Guess the number in Zig basic

Zig is still in the alpha stage, and subject to change. This article is correct as of zig version 0.11. Install zig by going to the downloads directory and downloading the appropriate version for your operating system and architecture:

 const std = @import("std");  fn ask_user() !i64 {     const stdin = std.io.getStdIn().reader();     const stdout = std.io.getStdOut().writer();      var buf: [10]u8 = undefined;      try stdout.print("Guess a number between 1 and 100: ", .{});      if (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| {         return std.fmt.parseInt(i64, user_input, 10);     } else {         return error.InvalidParam;     } }  pub fn main() !void {     const stdout = std.io.getStdOut().writer();     var prng = std.rand.DefaultPrng.init(blk: {         var seed: u64 = undefined;         try std.os.getrandom(std.mem.asBytes(&seed));         break :blk seed;     });     const value = prng.random().intRangeAtMost(i64, 1, 100);     while (true) {         const guess = try ask_user();         if (guess == value) {             break;         }         const message =             if (guess < value)             "low"         else             "high";         try stdout.print("Too {s}\n", .{message});     }     try stdout.print("That's right\n", .{}); }

The first line const std = @import("std"); imports the Zig standard library.
Almost all programs will need it.

The ask_user function in Zig

The function ask_user() returns a 64-bit integer or an error. This is what the ! (exclamation mark) notes. This means if there is an I/O issue or the user enters an invalid input, the function returns an error.

The try operator calls a function and return its value. If it returns an error, it immediately returns from the calling function with an error. This allows explicit, but easy, error propagation. The first two lines in ask_user alias contains some constants from std.
This makes the following I/O code simpler.

This line prints the prompt:

 try stdout.print("Guess a number between 1 and 100: ", .{});

It automatically returns a failure if the print fails (for example, writing to a closed terminal).

This line defines the buffer into which user input is read:

 var buf: [10]u8 = undefined;

The expression inside the if clause reads user input into the buffer:

 (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input|

The expression returns the slice of the buffer that was read into. This is assigned to the variable user_input, which is only valid inside the if block.

The function std.fmt.parseInt returns an error if the number cannot be parsed.
This error is propagated to the caller. If no bytes have been read, the function immediately returns an error.

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

The main function

The function begins by getting a random number. It uses std.rand.DefaultPrng.

The function initializes the random number generator with std.os.getrandom. It then uses the generator to get a number in the range of 1 to 100.

The while loop continues while true is true, which is forever. The only way out is with the break, which happens when the guess is equal to the random value.

When the guess is not equal, the if statement returns the string low or high depending on the guess. This is interpolated into the message to the user.

Note that main is defined as !void, which means it can also return an error. This allows using the try operator inside main.

Sample output

An example run, after putting the program in main.zig:

 $   zig run main.zig  Guess a number between 1 and 100: 50 Too high Guess a number between 1 and 100: 25 Too low Guess a number between 1 and 100: 37 Too low Guess a number between 1 and 100: 42 Too high Guess a number between 1 and 100: 40 That's right

Summary

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!

Practice programming in Zig by writing a "guess the number" game.

Woman sitting in front of her computer
Image by:

Ray Smith

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: Sailing in Africa ?

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 30 January 2023 by Alan Category: SoftwareTag: Code, Game, Learn, Simple

Gandhi’s glasses found ‘hanging out of letterbox’ to fetch thousands at auction

South Africa in the news: Gandhi’s glasses found ‘hanging out of letterbox’ to fetch thousands at …

Read moreGandhi’s glasses found ‘hanging out of letterbox’ to fetch thousands at auction

South Africa reintroduces alcohol ban as coronavirus cases surge

South Africa in the news: South Africa reintroduces alcohol ban as coronavirus cases surge: The move …

Read moreSouth Africa reintroduces alcohol ban as coronavirus cases surge

Cool Ajax Save Post Screenshot #3 image

A cool WordPress Plugin image: Ajax Save Post Screenshot #3 Image by Bull3t This is a screenshot of …

Read moreCool Ajax Save Post Screenshot #3 image

About Alan

Previous Post:Fedhealth Platinum Trail Run celebrates a decade of spectacular trail running in 2023!
Next Post:Au Sénégal, le non-resepct du code de la route et la vétusté du parc automobile sont les principales causes d’accidents

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