-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
63 lines (57 loc) · 2.77 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
import * as React from 'react';
import { Container, Button, Grid, Typography } from '@mui/material';
import CssBaseline from '@mui/material/CssBaseline';
import questions from "../data/__questions.json";
const Game = () => {
const [questionData, setQuestionData] = React.useState(null);
const [buttonStates, setButtonStates] = React.useState([]);
React.useEffect(() => {
const randomIndex = Math.floor(Math.random() * questions.length);
setQuestionData(questions[randomIndex]);
setButtonStates(new Array(questions[randomIndex].options.length).fill(null));
}, []);
const selectResponse = (index, response) => {
const newButtonStates = [...buttonStates];
if (response === questionData.correctAnswer) {
for (let i = 0; i < questionData.options.length; i++) {
if (i === index) {
newButtonStates[i] = "success";
} else {
newButtonStates[i] = "failure";
}
}
} else {
newButtonStates[index] = "failure"
}
setButtonStates(newButtonStates);
};
if (!questionData) {
return <div>Cargando...</div>; // Muestra un mensaje de carga mientras se obtienen los datos
}
return (
<Container sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', flex:'1' }}>
<CssBaseline />
<Grid container spacing={4}>
<Grid item xs={12}>
<Typography variant="h5" align="center">{ questionData.question }</Typography>
</Grid>
{questionData.options.map((option, index) => (
<Grid item xs={6} key={index}>
<Button
variant="contained"
onClick={() => selectResponse(index, option)}
disabled={buttonStates[index] !== null} // Disables the buttom once it has been stablished its state
sx={{ height: "15vh", width: "100%", backgroundColor: buttonStates[index] === "success" ? "green" : buttonStates[index] === "failure" ? "red" : null,
"&:disabled": { // Aplica el color al botón deshabilitado
backgroundColor: buttonStates[index] === "success" ? "green" : buttonStates[index] === "failure" ? "red" : null,
color: "white" }
}}
>
{`${String.fromCharCode(65 + index)}) ${option}`} {/* Convierte el índice a letra (A, B, C, ...) */}
</Button>
</Grid>
))}
</Grid>
</Container>
);
};