Ad
Fundamentals
Games
Code
Diff
  • 'use strict';
    
    const ELEMENT_ROCK = "Rock";
    const ELEMENT_PAPER = "Paper";
    const ELEMENT_SCISSORS = "Scissors";
    
    const ELEMENTS = [ELEMENT_ROCK, ELEMENT_PAPER, ELEMENT_SCISSORS];
    
    const PLAYER_HUMAN = "Player";
    const PLAYER_CPU = "CPU";
    
    // in codewars testable function
    const getWinningElement = function(element, otherElement) {
      if (element === otherElement)
        // no winning element -> tie
        return null;
      
      switch (element) {
        case ELEMENT_ROCK:
          if (otherElement === ELEMENT_PAPER)
            return otherElement;
          if (otherElement === ELEMENT_SCISSORS)
            return element;
    
          break;
        case ELEMENT_PAPER:
          if (otherElement === ELEMENT_ROCK)
            return element;
          if (otherElement === ELEMENT_SCISSORS)
            return otherElement;
          
          break;
        case ELEMENT_SCISSORS:
          if (otherElement === ELEMENT_ROCK)
            return otherElement;
          if (otherElement === ELEMENT_PAPER)
            return element;
    
          break;
      }  
    }
    
    // in codewars testable function
    const getWinnerOfRound = function(playerElement, cpuElement) {
      let winningElement = getWinningElement(playerElement, cpuElement);
      
      let winner;  
      switch (winningElement) {
        case playerElement:
            winner = PLAYER_HUMAN;
    
          break;
        case cpuElement:
            winner = PLAYER_CPU;
          
          break;
        default:
          // no winner -> tie
          winner = null;
    
          break;
      }
      
      return winner;
    }
    
    // in codewars testable function
    const describeRound = function(winner, winnersElement, losersElement) {
      let verbForElement = {};
      verbForElement[ELEMENT_ROCK] = 'crushes';
      verbForElement[ELEMENT_PAPER] = 'covers';
      verbForElement[ELEMENT_SCISSORS] = 'cuts';
      
      let verb = verbForElement[winnersElement];
      
      let message;
      
      switch(winner) {
        case PLAYER_HUMAN:
          message = `Your ${winnersElement} ${verb} CPU's ${losersElement}: YOU WIN!!`;
          break;
        
        case PLAYER_CPU:
          message = `CPU's ${winnersElement} ${verb} your ${losersElement}: YOU LOSE!!!`;
          break;
        
        default:
          message = `Both players choose ${winnersElement}: ITS A TIE...!`;
      }
    
      
      return message;
    }
    
    // function contains user interaction (prompt, alert) -> NOT unit-testable
    // BUT: most of the initial code got refactored out into testable functions above
    const rpsInteractiveGame = function () {
      let playerElement = prompt(`Rock Paper Scissors Game, which shape you go with:`);
      let cpuElement = ELEMENTS[ Math.trunc(Math.random() * 3) ];
    
      if (!ELEMENTS.includes(playerElement))
        return alert(`Wrong! Please choose from Rock, Paper or Scissors`);
      
      let winner = getWinnerOfRound(playerElement, cpuElement);
      let winnersElement = getWinningElement(playerElement, cpuElement);
      let losersElement = null;
      
      if (winner != null && winner === PLAYER_HUMAN)
        losersElement = cpuElement;
      else
        losersElement = playerElement;   
        
      alert(describeRound(winner, winnersElement, losersElement));
    };
    
    • let player;
    • let cpu;
    • 'use strict';
    • const ELEMENT_ROCK = "Rock";
    • const ELEMENT_PAPER = "Paper";
    • const ELEMENT_SCISSORS = "Scissors";
    • const ELEMENTS = [ELEMENT_ROCK, ELEMENT_PAPER, ELEMENT_SCISSORS];
    • const PLAYER_HUMAN = "Player";
    • const PLAYER_CPU = "CPU";
    • // in codewars testable function
    • const getWinningElement = function(element, otherElement) {
    • if (element === otherElement)
    • // no winning element -> tie
    • return null;
    • switch (element) {
    • case ELEMENT_ROCK:
    • if (otherElement === ELEMENT_PAPER)
    • return otherElement;
    • if (otherElement === ELEMENT_SCISSORS)
    • return element;
    • break;
    • case ELEMENT_PAPER:
    • if (otherElement === ELEMENT_ROCK)
    • return element;
    • if (otherElement === ELEMENT_SCISSORS)
    • return otherElement;
    • break;
    • case ELEMENT_SCISSORS:
    • if (otherElement === ELEMENT_ROCK)
    • return otherElement;
    • if (otherElement === ELEMENT_PAPER)
    • return element;
    • break;
    • }
    • }
    • // in codewars testable function
    • const getWinnerOfRound = function(playerElement, cpuElement) {
    • let winningElement = getWinningElement(playerElement, cpuElement);
    • let winner;
    • switch (winningElement) {
    • case playerElement:
    • winner = PLAYER_HUMAN;
    • const rpsGame = function (player, cpu) {
    • player = prompt(`Rock Paper Scissors Game, which shape you go with:`);
    • cpu = Math.trunc(Math.random() * 3 + 1);
    • switch (player) {
    • case 'Rock':
    • if (cpu === 2)
    • return alert(`CPU's Paper covers your ${player}: YOU LOOSE!!!`);
    • if (cpu === 3)
    • return alert(`Your ${player} crushes CPU's Scissors: YOU WIN!!`);
    • if (cpu === 1)
    • return alert(`Both players choose ${player}: ITS A TIE...!`);
    • break;
    • case 'Paper':
    • if (cpu === 1)
    • return alert(`Your ${player} covers CPU's Rock: YOU WIN!!`);
    • if (cpu === 2)
    • return alert(`Both players choose ${player}: ITS A TIE...!`);
    • if (cpu === 3)
    • return alert(`CPU's Scissors cuts your ${player}: YOU LOOSE!!!`);
    • break;
    • case 'Scissors':
    • if (cpu === 1)
    • return alert(`CPU's Rock crushes your ${player}: YOU LOOSE!!!`);
    • if (cpu === 2)
    • return alert(`Your ${player} cuts CPU's Paper: YOU WIN...!!`);
    • if (cpu === 3)
    • return alert(`Both players choose ${player}: ITS A TIE...!`);
    • break;
    • case cpuElement:
    • winner = PLAYER_CPU;
    • break;
    • default:
    • return alert(`Wrong! Please choose from Rock, Paper or Scissors`);
    • // no winner -> tie
    • winner = null;
    • break;
    • }
    • return winner;
    • }
    • // in codewars testable function
    • const describeRound = function(winner, winnersElement, losersElement) {
    • let verbForElement = {};
    • verbForElement[ELEMENT_ROCK] = 'crushes';
    • verbForElement[ELEMENT_PAPER] = 'covers';
    • verbForElement[ELEMENT_SCISSORS] = 'cuts';
    • let verb = verbForElement[winnersElement];
    • let message;
    • switch(winner) {
    • case PLAYER_HUMAN:
    • message = `Your ${winnersElement} ${verb} CPU's ${losersElement}: YOU WIN!!`;
    • break;
    • case PLAYER_CPU:
    • message = `CPU's ${winnersElement} ${verb} your ${losersElement}: YOU LOSE!!!`;
    • break;
    • default:
    • message = `Both players choose ${winnersElement}: ITS A TIE...!`;
    • }
    • };
    • return message;
    • }
    • // function contains user interaction (prompt, alert) -> NOT unit-testable
    • // BUT: most of the initial code got refactored out into testable functions above
    • const rpsInteractiveGame = function () {
    • let playerElement = prompt(`Rock Paper Scissors Game, which shape you go with:`);
    • let cpuElement = ELEMENTS[ Math.trunc(Math.random() * 3) ];
    • if (!ELEMENTS.includes(playerElement))
    • return alert(`Wrong! Please choose from Rock, Paper or Scissors`);
    • let winner = getWinnerOfRound(playerElement, cpuElement);
    • let winnersElement = getWinningElement(playerElement, cpuElement);
    • let losersElement = null;
    • if (winner != null && winner === PLAYER_HUMAN)
    • losersElement = cpuElement;
    • else
    • losersElement = playerElement;
    • alert(describeRound(winner, winnersElement, losersElement));
    • };