Hi @mtrtmk ,
first of all thank you for taking the time to answer.
I was confused about the terms, so thank you for clarifying the differences between âargumentâ, âparameterâ, and âfunction as an argumentâ.
Ok, I guess I got it. Hereâs the full code:
// All code should be written in this file.
// Player One moves and values
let playerOneMoveOneType = undefined;
let playerOneMoveOneValue = undefined;
let playerOneMoveTwoType = undefined;
let playerOneMoveTwoValue = undefined;
let playerOneMoveThreeType = undefined;
let playerOneMoveThreeValue = undefined;
// Player Two moves and values
let playerTwoMoveOneType = undefined;
let playerTwoMoveOneValue = undefined;
let playerTwoMoveTwoType = undefined;
let playerTwoMoveTwoValue = undefined;
let playerTwoMoveThreeType = undefined;
let playerTwoMoveThreeValue = undefined;
let playerOneWins;
let playerTwoWins;
// Funzione per impostare le mosse dei giocatori
const setPlayerMoves = (player, moveOneType, moveOneValue, moveTwoType, moveTwoValue, moveThreeType, moveThreeValue) => {
// testfail return se le variabili non sono presenti
if (!moveOneType || !moveOneValue || !moveTwoType || !moveTwoValue || !moveThreeType || !moveThreeValue) {
return;
}
// Verifica se il tipo di mossa è valido
if (!isValidMoveType(moveOneType) || !isValidMoveType(moveTwoType) || !isValidMoveType(moveThreeType)) {
return;
}
// Verifica se il valore è valido
if (!isValidMoveValue(moveOneValue) || !isValidMoveValue(moveTwoValue) || !isValidMoveValue(moveThreeValue)) {
return;
}
// Verifica se la somma dei valori è maggiore di 99
if (moveOneValue + moveTwoValue + moveThreeValue > 99) {
return;
}
if (player === 'Player One') {
playerOneMoveOneType = moveOneType;
playerOneMoveOneValue = moveOneValue;
playerOneMoveTwoType = moveTwoType;
playerOneMoveTwoValue = moveTwoValue;
playerOneMoveThreeType = moveThreeType;
playerOneMoveThreeValue = moveThreeValue;
} else if (player === 'Player Two') {
playerTwoMoveOneType = moveOneType;
playerTwoMoveOneValue = moveOneValue;
playerTwoMoveTwoType = moveTwoType;
playerTwoMoveTwoValue = moveTwoValue;
playerTwoMoveThreeType = moveThreeType;
playerTwoMoveThreeValue = moveThreeValue;
}
}
// Helper function isValidMoveType() per validare rock, paper o scissor
function isValidMoveType(moveType) {
return (moveType === 'rock') || (moveType === 'paper') || (moveType === 'scissors');
}
// Helper function per verificare i valori
function isValidMoveValue(moveValue) {
return (moveValue >= 1) && (moveValue <= 99);
}
function getRoundWinner(roundNumber) {
switch (roundNumber) {
case 1:
return getMoveWinner(playerOneMoveOneType, playerOneMoveOneValue, playerTwoMoveOneType, playerTwoMoveOneValue);
case 2:
return getMoveWinner(playerOneMoveTwoType, playerOneMoveTwoValue, playerTwoMoveTwoType, playerTwoMoveTwoValue);
case 3:
return getMoveWinner(playerOneMoveThreeType, playerOneMoveThreeValue, playerTwoMoveThreeType, playerTwoMoveThreeValue);
default:
return null;
}
}
function getMoveWinner(playerOneMoveType, playerOneMoveValue, playerTwoMoveType, playerTwoMoveValue) {
if (!playerOneMoveType || !playerOneMoveValue || !playerTwoMoveType || !playerTwoMoveValue ) {
return null;
}
if (playerOneMoveType === playerTwoMoveType) {
if (playerOneMoveValue > playerTwoMoveValue) {
return 'Player One';
} else if (playerOneMoveValue < playerTwoMoveValue) {
return 'Player Two';
} else {
return 'Tie';
}
}
if (playerOneMoveType === 'rock') {
if (playerTwoMoveType === 'scissors') {
return 'Player One';
} else {
return 'Player Two';
}
} else if (playerOneMoveType === 'paper') {
if (playerTwoMoveType === 'rock') {
return 'Player One';
} else {
return 'Player Two';
}
} else {
if (playerTwoMoveType === 'paper') {
return 'Player One';
} else {
return 'Player Two';
}
}
}
// A function called getGameWinner, which compares both playerâs move types and values for the whole game and returns the appropriate winner ('Player One', 'Player Two', or 'Tie')
function getGameWinner() {
if(!playerOneMoveOneType || !playerOneMoveTwoType || !playerOneMoveTwoType || !playerOneMoveOneValue || !playerOneMoveTwoValue || !playerOneMoveThreeValue || !playerTwoMoveOneType || !playerTwoMoveTwoType || !playerTwoMoveTwoType || !playerTwoMoveOneValue || !playerTwoMoveTwoValue || !playerTwoMoveThreeValue) {
return null;
}
playerOneWins = 0;
playerTwoWins = 0;
const roundOneWinner = getRoundWinner(1);
const roundTwoWinner = getRoundWinner(2);
const roundThreeWinner = getRoundWinner(3);
addWin(roundOneWinner);
addWin(roundTwoWinner);
addWin(roundThreeWinner);
if (playerOneWins > playerTwoWins) {
return 'Player One';
} else if (playerOneWins < playerTwoWins) {
return 'Player Two';
} else {
return 'Tie';
}
}
function addWin(winner) {
if (winner === 'Player One') {
playerOneWins = (playerOneWins + 1) || 1;
} else if (winner === 'Player Two') {
playerTwoWins = (playerTwoWins + 1) || 1;
}
}
In this case the addWin function is called for each round:
addWin(roundOneWinner);
addWin(roundTwoWinner);
addWin(roundThreeWinner);
The value of the âwinnerâ parameter for each case is defined here:
const roundOneWinner = getRoundWinner(1);
const roundTwoWinner = getRoundWinner(2);
const roundThreeWinner = getRoundWinner(3);
Which is basically the result of the execution of the following function for round 1, 2 and 3:
function getRoundWinner(roundNumber) {
switch (roundNumber) {
case 1:
return getMoveWinner(playerOneMoveOneType, playerOneMoveOneValue, playerTwoMoveOneType, playerTwoMoveOneValue);
case 2:
return getMoveWinner(playerOneMoveTwoType, playerOneMoveTwoValue, playerTwoMoveTwoType, playerTwoMoveTwoValue);
case 3:
return getMoveWinner(playerOneMoveThreeType, playerOneMoveThreeValue, playerTwoMoveThreeType, playerTwoMoveThreeValue);
default:
return null;
}
}
Could you please confirm if my analysis is correct?
Thank you