-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatividade07.js
More file actions
97 lines (84 loc) · 2.29 KB
/
atividade07.js
File metadata and controls
97 lines (84 loc) · 2.29 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
// var A = 100003;
// var B = 300004;
// var Tempo = 0;
// while (A < B) {
// A = A * 1.03;
// B = B * 1.015;
// Tempo++;
// }
// console.log(
// `A quantidade de anos até a cidade A ultrapassar a cidade B é de ${Tempo} anos`);
//Criar um "Jogo da velha" que mostre as informações dentro da imagem abaixo. Inserir as informações de forma randomica. Informar ao final quem ganhou X ou O
// console.log(`___|___|___`);
// console.log(`___|___|___`);
// console.log(` | | `);
// Estrutura do Tabuleiro
const tabuleiro = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "],
];
let jogadorAtual = "X";
function jogar(jogador) {
let linha, coluna;
do {
linha = Math.floor(Math.random() * 3);
coluna = Math.floor(Math.random() * 3);
} while (tabuleiro[linha][coluna] !== " ");
tabuleiro[linha][coluna] = jogador;
jogadorAtual = jogadorAtual === "X" ? "O" : "X";
}
function verificaVencedor() {
for (let i = 0; i < 3; i++) {
if (
//Verifica coluna
tabuleiro[0][i] === tabuleiro[1][i] &&
tabuleiro[1][i] === tabuleiro[2][i] &&
tabuleiro[1][i] !== " "
) {
console.log(`O jogador ${tabuleiro[1][i]} ganhou`);
return tabuleiro[1][i];
} else if (
//Verifica linha
tabuleiro[i][0] === tabuleiro[i][1] &&
tabuleiro[i][1] === tabuleiro[i][2] &&
tabuleiro[i][1] !== " "
) {
console.log(`O jogador ${tabuleiro[i][1]} ganhou`);
return tabuleiro[i][1];
}
}
if (
// Vefifica diagonal
tabuleiro[0][0] === tabuleiro[1][1] &&
tabuleiro[1][1] === tabuleiro[2][2] &&
tabuleiro[1][1] !== " "
) {
console.log(`O jogador ${tabuleiro[1][1]} ganhou`);
return tabuleiro[1][1];
} else if (
// Vefifica diagonal
tabuleiro[0][2] === tabuleiro[1][1] &&
tabuleiro[1][1] === tabuleiro[2][0] &&
tabuleiro[1][1] !== " "
) {
console.log(`O jogador ${tabuleiro[1][1]} ganhou`);
return tabuleiro[1][1];
}
if (!tabuleiro.some((event) => event.includes(" "))) {
console.log("O jogo empatou");
return "Empate";
}
return "";
}
let vencedor = "";
while (!vencedor) {
jogar(jogadorAtual);
console.table(tabuleiro);
vencedor = verificaVencedor();
if (!vencedor) {
jogar(jogadorAtual);
console.table(tabuleiro);
vencedor = verificaVencedor();
}
}