2
\$\begingroup\$

I built a simple console version of the rock paper scissors game. I would love to get some constructive criticism!

Questions

  1. How could I code this in an object-oriented way?
  2. Did my code follow the DRY principle?

Link to code

Repl: https://repl.it/@antgotfan/CrowdedGreenFlatassembler

        var moves, result, images, relativePath, playerSelection, computerSelection;

    moves = {
        rock: 0, 
        paper: 1, 
        scissors: 2,
    };

    function init() {

        result = {
            player: 0,
            tie: 0,
            computer: 0
        };
    }

    init();

    function convertMoves() {
        // when any rock paper scissor button is clicked 

        // then return that string into the moves object 

        // return a number corresponding to the handsign.
        playerSelection = prompt("Please choose rock, paper, or scissors!");
        return moves[playerSelection];    
    }

    function computerPlay() {
        var movesValues = Object.values(moves);
        var random = Math.floor(Math.random() * movesValues.length);
        return movesValues[random];
    } 

    function playRound(playerSelection, computerSelection) {
        computerSelection = computerPlay();
        playerSelection = convertMoves();

        var processResult = (3 + computerSelection - playerSelection) % 3;

        if (!processResult) {
            ++result.tie;
            console.log('tie');
        } else if (1 === processResult) {
            ++result.computer;
            console.log('Computer won');
        } else {
            ++result.player;
            console.log('Player won');
        }

        return result;
    }

    function game() {
        for (var perRound = 1; perRound < 5; perRound++) {
            playRound(playerSelection, computerSelection);
        }
    }

    game();
    console.log(playRound(playerSelection, computerSelection));
\$\endgroup\$
1
\$\begingroup\$

This doesn't quite answer your questions, but still hopefully helpful.

var perRound = 1; perRound < 5; perRound++ only executes four times; the fifth execution is coming from the last line of your program. It's customary to use i when simply doing something multiple times, and to start with 0 such as: var i = 0; i < 5; i++. Then remove that extra playRound at the end so you still have five rounds.

I would rename convertMoves to playerMove to better describe what it does instead of how it does it.

playRound isn't using its arguments. So you can change the first few lines to:

    function playRound() {
        var computerSelection = computerPlay();
        var playerSelection = convertMoves();

You might play with changing moves to an array and see if you like it better as moves = ['rock', 'paper', 'scissors'];. This will require several other changes to how you use it but I think it'll read easier in the end.

Just as a matter of style, put the ++ after the variable except for the rare times when you really need the other behavior.

Again, hope this helps.

Edit: I see now why you have the extra playRound at the end. But result is already global so you can simply end with console.log(result) instead of returning it from playRound each time.

\$\endgroup\$
  • \$\begingroup\$ This was really helpful! Thank you Nathan. \$\endgroup\$ – Anthony Gedeon Aug 26 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.