-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
82 lines (69 loc) · 1.88 KB
/
Game.js
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
const states = {
INITIAL: 0,
INGAME: 1,
PAUSED: 2,
OVER: 3
}
const DISKS = {
RED: "red",
YELLOW: "yellow"
}
class Game {
constructor() {
this.currentPlayer = DISKS.YELLOW;
this.playerColor = DISKS.YELLOW;
this.cpuColor = DISKS.RED;
this.state = states.INITIAL;
this.shape = 7;
this.moves = 0;
}
// Generates a random number between 1 and 2
setInitialPlayer() {
// Genera un número aleatorio entre 1 y 2
const numeroAleatorio = Math.random() + 1;
this.currentPlayer = (numeroAleatorio == 1) ? DISKS.YELLOW : DISKS.RED;
}
insertDisk(index) {
/* Avoid double clicks */
if (!isPlayerTurn()) {
return false;
}
// Aquí iría la lógica para realizar la acción del jugador
matrix[index] = (this.currentPlayer == this.playerColor) ? 1 : 2;
// Luego de que el jugador actúa, se cambia el turno al siguiente jugador
this.changeTurn();
}
isPlayerTurn() {
return (this.currentPlayer === this.playerColor) ? true : false;
}
async changeTurn() {
// Verificamos si el juego ha terminado
if (this.state == 3) {
//alert("terminado");
//return;
}
// Change to user's turn
if (this.currentPlayer === this.cpuColor) {
circle.style.opacity = 1;
this.currentPlayer = this.playerColor;
}
// Computer's turn
else {
circle.style.opacity = 0;
this.currentPlayer = this.cpuColor;
game.moves++;
document.getElementById('moves').innerHTML = 'Moves: ' + game.moves;
// Vakes computer move
await sleep(1500);
var isOver = cpu()
if(isOver) {
this.state = states.OVER
}
}
}
gameOver() {
// Aquí iría la lógica para determinar si el juego ha terminado
// (por ejemplo, si un jugador ha ganado o si ya no quedan más movimientos posibles)
return (this.state == states.OVER) ? true : false
}
}