const ASSIGNMENT = 'assignment'; const ANSWER = 'answer'; const ALGORITHM_CHECKBOX = 'algorithm'; const ERROR_MESSAGE_NO_SUCH_CHARACTER_IN_RPS = "Non existant character in rock paper scissor game selected: "; const ERROR_MESSAGE_TOO_FEW_CHARACTERS_IN_LINE = "Too few characters in line: "; const NEWLINE_CHARACTER = '\n'; const OPTION_LOSE = 'X'; const OPTION_TIE = 'Y'; const OPTION_WIN = 'Z'; const RPS = Object.freeze({ ROCK: 1, PAPER: 2, SCISSORS: 3, /** * Select winning hand for given hand. * @param hand the chosen hand * @return RPS the winning hand */ winning_hand: function(hand) { hand++; if(hand > RPS.SCISSORS) { hand = RPS.ROCK; } return hand; }, /** * Select losing hand for given hand. * @param hand the chosen hand * @return RPS the losing hand */ losing_hand: function(hand) { hand--; if(hand < RPS.ROCK) { hand = RPS.SCISSORS; } return hand; } }); const RPS_ALIAS_MAP = Object.freeze({ "A": RPS.ROCK, "B": RPS.PAPER, "C": RPS.SCISSORS, "X": RPS.ROCK, "Y": RPS.PAPER, "Z": RPS.SCISSORS, }) const SCORE_LOSE = 0; const SCORE_TIE = 3; const SCORE_WIN = 6; /** * Main function */ window.onload = function() { document.getElementById(ASSIGNMENT).addEventListener("input", calculateAnswer); document.getElementById(ALGORITHM_CHECKBOX).addEventListener("click", calculateAnswer); } /** * Listener function for input in assignment field. * @param event the onInput event */ function calculateAnswer(event) { console.info("Calculating answer for input..."); let assignment = document.getElementById(ASSIGNMENT).value; let interpret_as_rps = document.getElementById(ALGORITHM_CHECKBOX).checked; let answer = algorithm(assignment, interpret_as_rps); document.getElementById(ANSWER).innerText = answer; } /** * Calculate the answer to assignment. * @param assignment the input from the assignment. * @return string the answer */ function algorithm(assignment, interpret_as_rps) { let errors = 0; let lines = assignment.trim().split(NEWLINE_CHARACTER); let score = 0; console.info("Linecount:" + lines.length); for(let i=0; i 0) { return errors.toString() + " Error(s) in input, see console for details." } return "Player score: " + score; } /** * Interpret given input for player as a rock paper scissor character. * @param char input the input for hand character (A,B,C,X,Y,Z) * @return the player hand character or -1 if invalid. */ function interpret_player_hand_as_rps_input(input) { return map_hand_character(input); } /** * Interpret given input for player as a match option. * @param char input the input for match option (X,Y,Z) * @return the player hand character or -1 if invalid. */ function interpret_player_hand_as_match_input(input, opponent_hand) { let player_hand = -1; switch (input) { case OPTION_LOSE: player_hand = RPS.losing_hand(opponent_hand); break; case OPTION_WIN: player_hand = RPS.winning_hand(opponent_hand); break; case OPTION_TIE: player_hand = opponent_hand; break; } return player_hand; } /** * Maps the chosen hand character to * @param hand_character the chosen character * @return RPS the rock paper scissor hand or -1 if invalid */ function map_hand_character(hand_character) { if(!(hand_character in RPS_ALIAS_MAP)) { return -1; } return RPS_ALIAS_MAP[hand_character]; } /** * Calculate the result of rock paper scissor match * @param RPS player_hand the player's choice * @param RPS opponent_hand the opponent's choice * @return score */ function rps_result(player_hand, opponent_hand) { let score = 0; if(player_hand == opponent_hand) { score = SCORE_TIE + player_hand; // TIE } else if(player_hand == RPS.winning_hand(opponent_hand)) { score = SCORE_WIN + player_hand; // WIN } else { score = SCORE_LOSE + player_hand; // LOSE } console.debug("score:" + score + ", player: " + player_hand + ", opponent: " + opponent_hand); return score; }