-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
119 lines (98 loc) · 4.58 KB
/
index.php
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
<?php
if ($_SERVER['REQUEST_METHOD'] == "GET") {
$screen = "Para começar digite um texto:";
}
if (isset($_POST['enviado'])) {
$textoEnviado = $_POST['textoEnviado'];
$tentativas = intval($_POST['tentativas']);
$textoAtual = $_POST['textoAtual'];
if ($tentativas == 0) {
$textoParaMemorizar = $textoEnviado;
$textoAtual = $textoParaMemorizar;
if (empty($_POST['palavrasSeparadas'])) {
$palavrasSeparadas = explode(" ", $textoParaMemorizar);
}
$screen = "Repita o seu texto:<p>{$textoAtual}</p>";
$tentativas++;
} else {
$textoParaMemorizar = $_POST['textoParaMemorizar'];
$palavrasSeparadas = explode(" ", $_POST['palavrasSeparadas']);
$palavrasRemovidas = explode(" ", $_POST['palavrasRemovidas']);
if ($textoEnviado != $textoParaMemorizar) {
$mensagem = "Você já errou {$tentativas} vezes!";
$screen = "Tente novamente: <br> {$textoAtual}";
$tentativas++;
}
if ($textoEnviado == $textoParaMemorizar) {
$tentativas = 1;
$chavePalavraAleatoria = array_rand($palavrasSeparadas);
array_push($palavrasRemovidas, $palavrasSeparadas[$chavePalavraAleatoria]);
$mensagem = "Voce acertou!";
$textoAtual = $_POST['textoAtual'];
$textoAtual = $_POST['textoAtual'];
$palavraRemovida = $palavrasSeparadas[$chavePalavraAleatoria];
$textoAtual = preg_replace("/\b" . preg_quote($palavraRemovida) . "\b/", "_", $textoAtual, 1);
unset($palavrasSeparadas[$chavePalavraAleatoria]);
$screen = "Digite o texto completo novamente:<p> {$textoAtual} </p>";
}
if ($textoEnviado == $textoParaMemorizar && count($palavrasSeparadas) == 0) {
$mensagem = "Parabéns, você decorou todo o texto!";
$screen = "Texto memorizado:<br> {$textoParaMemorizar}";
$tentativas = 0;
$textoParaMemorizar = "";
$palavrasSeparadas = array();
$palavrasRemovidas = array();
}
if ($tentativas >= 4) {
$screen = "Comece novamente: <br>{$textoParaMemorizar}";
$palavrasSeparadas = explode(" ", $textoParaMemorizar);
$palavrasRemovidas = array();
$textoAtual = $textoParaMemorizar;
$tentativas = 1;
}
}
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Carneiro Tec</title>
</head>
<body>
<header>
<h1>Carneiro Tec</h1>
<h2>Bem-Vindo ao Memorizador de Texto</h2>
</header>
<div class="row">
<main class="component" id="screen">
<p><?php echo $screen . PHP_EOL; ?></p>
<p><?php if (isset($mensagem)) {
echo $mensagem;
} ?></p>
</main>
<aside>
<form class="component" action="/" method="POST">
<textarea name="textoEnviado" id="textoEnviado" cols="50" rows="10"></textarea><br>
<input type="hidden" name="textoAtual" value="<?php if (isset($textoAtual)) {
echo $textoAtual;
} ?>">
<input type="hidden" name="palavrasSeparadas" value="<?php if (isset($palavrasSeparadas)) {
echo implode(" ", $palavrasSeparadas);
} ?>">
<input type="hidden" name="palavrasRemovidas" value="<?php if (isset($palavrasRemovidas)) {
echo implode(" ", $palavrasRemovidas);
} ?>">
<input type="hidden" name="textoParaMemorizar" value="<?php if (isset($textoParaMemorizar)) {
echo $textoParaMemorizar;
} ?>">
<input type="hidden" name="tentativas" value="<?php if (isset($tentativas)) {
echo $tentativas;
} ?>">
<input type="submit" name="enviado" value="Enviar">
</form>
</aside>
</div>
</body>
</html>