VOOZH about

URL: https://www.geeksforgeeks.org/c/dice-simulator-with-sound-using-graphics/

⇱ Dice simulator with sound using graphics - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dice simulator with sound using graphics

Last Updated : 23 Jul, 2025

Graphics are defined as any sketch or a drawing or a special network that pictorially represents some meaningful information. Computer Graphics is used where a set of images needs to be manipulated or the creation of the image in the form of pixels and is drawn on the computer. It can be used in digital photography, film, entertainment, electronic gadgets, and all other core technologies which are required.

In this article, we will discuss the simulation of the dice valued from 1 to 6 using computer graphics. To implement this simulation, graphics.h header file is used for making dice, dos.h is used to play the beep sound when dice are thrown and the stdlib.h header file is used for the random() function that generates random numbers from 1 to 6.

Approach:

  • Initialize a graphic path using the function initgraph().
  • Iterate a loop until the program is ended and perform the following steps:
    • Take character input from the user and:
      • If the character is space then generate any random number from the range [1, 6] and display that number in the square using the rectangle function.
      • If the character is 0 then break out of the loop.
      • If the character is other than O or space then display the message to press characters only 0 and space.
  • After the simulator is ended, then close the graphics using the function closegraph().

Below is the C program to illustrate the dice simulator using graphics:

Output
 

Comment