forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'webapp_interface' of https://github.com/Arquisoft/wiq_es6c
- Loading branch information
Showing
7 changed files
with
176 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { shuffleArray, secureRandomNumber } from '../Util'; | ||
import { Container, Typography, Box, LinearProgress} from '@mui/material'; | ||
import { Footer } from '../footer/Footer'; | ||
import { Nav } from '../nav/Nav'; | ||
|
||
const Calculator = () => { | ||
|
||
const [question, setQuestion] = useState(null); | ||
|
||
useEffect(() => { | ||
const newQuestion = generateQuestion(); | ||
setQuestion(newQuestion); | ||
}, []); | ||
|
||
|
||
function generateQuestion() { | ||
const num1 = secureRandomNumber(10) + 1; | ||
const num2 = secureRandomNumber(10) + 1; | ||
const operator = ['+', '-', 'x', '÷'][secureRandomNumber(3)]; | ||
let correctAnswer; | ||
|
||
switch (operator) { | ||
case '+': | ||
correctAnswer = num1 + num2; | ||
break; | ||
case '-': | ||
correctAnswer = num1 - num2; | ||
break; | ||
case 'x': | ||
correctAnswer = num1 * num2; | ||
break; | ||
case '÷': | ||
correctAnswer = Math.round(num1 / num2); | ||
break; | ||
} | ||
|
||
const options = [correctAnswer]; | ||
while (options.length < 4) { | ||
const option = secureRandomNumber(100) + 1; | ||
if (!options.includes(option)) { | ||
options.push(option); | ||
} | ||
} | ||
|
||
shuffleArray(options); | ||
return { | ||
q: `${num1} ${operator} ${num2}`, | ||
options: options, | ||
correctAnswer: correctAnswer | ||
}; | ||
} | ||
|
||
|
||
//CAMBIAR ESTO EN FUNCIÓN DE CÓMO QUERAMOS QUE SEA EL JUEGO | ||
const handleOptionClick = (selectedAnswer) => { | ||
if (selectedAnswer === question.correctAnswer) { | ||
alert('¡Respuesta correcta!'); | ||
} else { | ||
alert('Respuesta incorrecta. ¡Inténtalo de nuevo!'); | ||
} | ||
const newQuestion = generateQuestion(); | ||
setQuestion(newQuestion); | ||
}; | ||
|
||
|
||
return ( | ||
<> | ||
<Nav /> | ||
<Container component="main" maxWidth="xl" sx={{ marginTop: 4 }}> | ||
|
||
<div className="questionStructure"> | ||
|
||
<div class="questionCalculator"> | ||
|
||
<Typography class="questionText" component="h1" variant="h5" sx={{ textAlign: 'center' }}> | ||
{setQuestion(generateQuestion())} | ||
{question.q} | ||
</Typography> | ||
|
||
</div> | ||
|
||
<div class="allAnswers"> | ||
{question.options.map((option, index) => ( | ||
<div key={index} > | ||
<Button | ||
id={`option-${index}`} | ||
value={option} | ||
onClick={() => handleOptionClick(option)} | ||
text={option} | ||
/> | ||
</div> | ||
) | ||
)} | ||
</div> | ||
</div> | ||
|
||
{/* | ||
<Box sx={{ | ||
width: '100%', | ||
padding: 3}}> | ||
<LinearProgress id='progress'color="secondary" variant={"determinate"} value={remTime} /> | ||
</Box> | ||
*/} | ||
|
||
</Container> | ||
<Footer /> | ||
</> | ||
); | ||
}; | ||
|
||
export default Calculator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { ContextFun } from '../Context'; | ||
import axios from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { BrowserRouter as Router } from 'react-router-dom'; | ||
import Calculator from './Calculator'; | ||
|
||
const mockAxios = new MockAdapter(axios); | ||
|
||
describe("Calculator game", () => { | ||
|
||
beforeEach(() => { | ||
mockAxios.reset(); | ||
}); | ||
|
||
test("renders Calculator",async () => { | ||
render( | ||
<ContextFun> | ||
<Router> | ||
<Calculator/> | ||
</Router> | ||
</ContextFun> | ||
); | ||
|
||
// Comprobamos que el número de elementos sea 3 | ||
let operation = document.getElementsByClassName('questionStructure')[0][0]; | ||
const separatedText = operation.split(' '); | ||
expect(separatedText.length).toBeGreaterThan(3); | ||
// Comprobamos que el número de respuestas posibles sea 4 | ||
let answers = document.getElementsByClassName('questionStructure')[1]; | ||
expect(answers).toHaveLength(4); | ||
// Tratamos de hacer la operación | ||
let number1 = separatedText[0]; | ||
let number2 = separatedText[2]; | ||
let op = separatedText[1]; | ||
let result; | ||
switch (op) { | ||
case '+': result = number1 + number2; break; | ||
case '-': result = number1 - number2; break; | ||
case 'x': result = number1 * number2; break; | ||
case '÷': result = Math.round(number1 / number2); break; | ||
} | ||
expect(screen.getByText(result)).toBeInTheDocument(); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,6 @@ code { | |
flex-wrap: wrap; | ||
} | ||
|
||
#root{ | ||
|
||
} | ||
|
||
#root > main > h1{ | ||
text-align: center; | ||
} | ||
|