We all liked that feeling of nostalgia for the games and games we played in childhood and adolescence. Several consoles and computer games marked this era. One of the great games was the Minesweeper of the Windows Operating System, which is shown in Figure 1.
In this game, we aimed to select a location that did not have a bomb. Otherwise, we lost the game.
Therefore, it was thinking about the concept of this game, that we created this project with the objective of bringing back a game that is well known to all: the minefield.
Figure 1 - Minesweeper of the Windows Operating System.
Our project consists of a simple game with excellent dynamics, with the option of being played by two people.
Your main objective is to choose an empty square where there is no bomb. If there is a bomb in place, the game is over. Otherwise, the game continues. Based on this, each location will be represented by a button connected to the Arduino.
Therefore, in this article, you will learn the following concepts:
1. Develop the minefield game for Arduino;
2. Learn to use the random and randomseed function.
So, next, we will start the development of the minefield game with Arduino for you to have fun with your friends.
Minesweeper Game Development with Arduino
Based on this operating principle, the following circuit in Figure 1 was developed.
Figure 2 - Electronic Schematic in the Breadboard.
As we can see, this circuit is composed of an Arduino UNO, which is responsible for processing the logic of the game, buttons that simulate the locations, and the LEDs and buzzer, to indicate victory and defeat in the game through light and audible signals.
From now on we will cover the circuit's operation and the logic implemented in the circuit.
Minesweeper with Arduino
The main objective of the game is to find an empty space where there is no bomb. Otherwise, if the user presses the button where the pump is, the system will generate an alarm signaling that the user has lost the game.
For this, we will use buttons to simulate each square. We will use programming logic to draw the digital pin number of one of the buttons. After the draw, the mine will be assigned to this respective button.
In this way, we will now present the code of the developed project.
The code is shown below.
int numero; int estado; int buzzer = 2; void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(buzzer,OUTPUT); pinMode(8, INPUT); pinMode(9, INPUT); pinMode(10, INPUT); pinMode(11, INPUT); pinMode(12, INPUT); pinMode(13, INPUT); randomSeed(analogRead(A0)); numero = random(8,14); } void loop() { digitalWrite(3, HIGH); estado = digitalRead(numero); while(estado == 1) { digitalWrite(4, HIGH); tone(buzzer,1500); delay(100); digitalWrite(3, LOW); noTone(buzzer); digitalWrite(4, LOW); tone(buzzer,1500); delay(100); digitalWrite(3, HIGH); noTone(buzzer); delay(100); while(estado == 0) { estado = 1; } } }
As you can see, a variable was first declared for the digital pins connected to the buttons. In addition, we will create a variable to check the status of these buttons, that is, if they are in high or low logic state.
Finally, we declare a variable for the buzzer and assign a digital port to that variable.
int numero; // Variável referente aos pinos digitais conectados aos botões // int estado; // Variável para verificar o estado dos botôes, se estão em nivel lógico alto ou baixo. // int buzzer = 2; // Váriavel atribuida ao pino digital 7, referente ao buzzer.//
Next, we have the setup function. In this function, we configure the I / O pins for connection of the buttons as input and the LED's and Buzzer pins as outputs.
In addition, we use the randomSeed function. This function uses as a parameter the value read on a disconnected analog input to generate a seed value. Because it is known that a disconnected analog pin will generate random values and, thus, we have a really random effect on the...
Read more »