-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd.cpp
More file actions
137 lines (112 loc) · 3.76 KB
/
Copy pathlcd.cpp
File metadata and controls
137 lines (112 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// c++
// Incluir biblioteca que permite a manipulação do LCD por funções
#include <LiquidCrystal.h>
// Dimensões do tabuleiro e do LCD
#define TABULEIRO_LIN 8
#define TABULEIRO_COL 8
#define DISPLAY_LIN 2
#define DISPLAY_COL 16
// Tabuleiro maior para simular
int tabuleiro[TABULEIRO_LIN][TABULEIRO_COL];
// Posição do glider inicial
int glider[5][2] = {{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
// LCD com os pinos do Arduino, correspondem: RS, E, D4, D5, D6 e D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Contador de populações
int populationCount = 0;
void setup() {
lcd.begin(DISPLAY_COL, DISPLAY_LIN);
// Nome do jogo no LCD na tela
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Jogo da Vida");
delay(2000);
// Inicializa o tabuleiro com células mortas
for (int i = 0; i < TABULEIRO_LIN; i++) {
for (int j = 0; j < TABULEIRO_COL; j++) {
tabuleiro[i][j] = 0;
}
}
// Glider na matriz inicial
for (int i = 0; i < 5; i++) {
int x = glider[i][0];
int y = glider[i][1];
tabuleiro[x][y] = 1; // Células vivas
}
displayTabuleiro();
delay(1000);
}
// Tabuleiro no LCD (16x2)
void displayTabuleiro() {
lcd.clear();
for (int i = 0; i < DISPLAY_LIN; i++) {
lcd.setCursor(0, i);
for (int j = 0; j < DISPLAY_COL; j++) {
// Calcula a célula correspondente no tabuleiro maior
int x = i; // Cria variáveis temporárias para comparações
int y = j;
// " " para morto e "." para vivo
// Condição ? valor_se_verdadeiro : valor_se_falso;
lcd.print(tabuleiro[x][y] == 1 ? "." : " ");
}
}
}
// Atualiza o tabuleiro com as regras do Jogo da Vida
void updateTabuleiro() {
int newTabuleiro[TABULEIRO_LIN][TABULEIRO_COL];
int vivosCount = 0; // Contador de células vivas
// Aplica as regras de Conway a cada célula
for (int i = 0; i < TABULEIRO_LIN; i++) {
for (int j = 0; j < TABULEIRO_COL; j++) {
int vizinhosVivos = countVizinhosVivos(i, j);
// Regras do Jogo da Vida
if (tabuleiro[i][j] == 1) { // Célula viva
newTabuleiro[i][j] = (vizinhosVivos == 2 || vizinhosVivos == 3) ? 1 : 0;
} else { // Célula morta
newTabuleiro[i][j] = (vizinhosVivos == 3) ? 1 : 0;
}
// Atualiza contador de células vivas
if (newTabuleiro[i][j] == 1) {
vivosCount++;
}
}
}
// Atualiza o tabuleiro
for (int i = 0; i < TABULEIRO_LIN; i++) {
for (int j = 0; j < TABULEIRO_COL; j++) {
tabuleiro[i][j] = newTabuleiro[i][j];
}
}
// Incrementa o contador caso células vivas
if (vivosCount > 0) {
populationCount++;
}
}
// Conta os vizinhos vivos de uma célula, passando por parâmetro as variáveis temporárias de comparação
int countVizinhosVivos(int x, int y) {
int vivosCount = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue; // Avança
int nx = (x + i + TABULEIRO_LIN) % TABULEIRO_LIN;
int ny = (y + j + TABULEIRO_COL) % TABULEIRO_COL;
vivosCount += tabuleiro[nx][ny];
}
}
return vivosCount;
}
void loop() {
updateTabuleiro(); // Atualiza o estado do tabuleiro
displayTabuleiro(); // Exibe o LCD
delay(1000);
// Ao final, mostre o número total de populações
// Exemplo de 20 gerações
if (populationCount > 20) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Populacoes:");
lcd.setCursor(0, 1);
lcd.print(populationCount);
while (true); // Pausa o programa
}
}