This project uses the Arty's switches, buttons, and LEDs to create a simple guessing game. The game chooses a number between 0 and 15 and gives the player 4 chances to guess the number. The game continues playing until the user presses the button to stop the game.
GameplayTo play the game, the user must have a terminal emulator with UART support, such as Tera Term. The terminal will display messages as the game is played. To enter a number, the player must toggle the switches to set a number in binary. The value set on the switches will be displayed on the RGB LEDs in blue at any given time for easy reading. When the players have decided on a number, they can submit the guess by pressing Button 0. If the guess is correct, the program will print a message stating this. If the guess is incorrect, the program will print a message telling the player whether the guess was too high or too low and the number of chances remaining. If the player guesses incorrectly four times, all of the LEDs and RGB LEDs, set to red, will flash three times. Then, the program prints a message revealing the correct answer. Finally, the game asks players if they would like to continue playing. Button 0 begins another game and Button 3 ends the game. Players can also end the game at any time by pressing Button 3.
HardwareAll of the hardware needed for this project is on the Arty board itself. To begin setting it up, I followed the Arty - Getting Started with MicroBlaze tutorial on the Digilent Documentation page, with the addition of the LEDs, RGB LEDs, buttons, and switches to the system before creating the HDL wrapper and bitstream. Other than these additions, my process follows the rest of the tutorial.
SoftwareAfter completing the tutorial, I modified helloworld.c
and wrote another C source file, guessingGame.c
, to implement the game's features. Before users can start a guessing game, they must initialize the inputs and outputs on the board with a call to initIO()
. To run an instance of the guessing game, users can call the guessingGame()
function. This function is written to be called in a loop so that games can start repeatedly until the user wishes to stop playing. The other functions in guessingGame.c
are helper functions that manage various parts of the game.
Some of the features implemented in the software include user-input debouncing and edge detection, LED pulse width modulation (PWM), and random number generation for the game. In the function getGuess()
, I have implemented a way of detecting button presses by the user such that a single button press will only register once. Without this system, a single button press will be detected as several presses. The other feature, PWM on the LEDs was implemented in displayGuess()
and blinkLEDS()
to dim the bright RGB LEDs. The random generator is the rand()
function from stdlib.h
. Typically, this function is only used after initializing a random seed with srand()
. A common way to do this is to use the current time as the seed. However, I was not able to get the current time on the Arty and the program runs with the default seed. This means that the sequence of numbers is always the same each time the program is run.
This guessing game happens to be built in such a way that you can almost always get to the correct answer within the given number of tries using this algorithm: start by guessing eight, then add or subtract the next lowest power of two according to the game's message. This will guarantee finding the answer unless the answer is either 0 or 1. When this happens, you will be forced to guess between 0 and 1. This algorithm can be generalized to larger guessing games to determine how many chances would be required to guarantee or almost guarantee finding the correct answer. A guessing game between 0 and 2^n will require n chances to find the answer.
Images and Videos
Comments