From d98deab22eecd423d9cd64d5a43908205aadbe7c Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Sat, 13 Oct 2018 14:53:06 -0500 Subject: [PATCH 1/2] Now with extra modulo --- 01week/rockPaperScissors.js | 69 ++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index a04cc01aa..da593f0ae 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -8,13 +8,80 @@ const rl = readline.createInterface({ output: process.stdout }); +//hand 1 enters move already done for us +//hand 2 enters move already done for us +//check to see if moves are valid +//if the moves are invalid, redo moves +//check who won function rockPaperScissors(hand1, hand2) { - // Write code here + if (checkMove(hand1) && checkMove(hand2)){ + return checkWin(hand1, hand2); + }else{ + return "Player has invalid move"; + } } + +const checkMove = (hand) =>{ + if (hand.toLowerCase().trim() == "scissors" + || hand.toLowerCase().trim() == "rock" + || hand.toLowerCase().trim() == "paper"){ + return true; + } +} + +const checkWin = (hand1,hand2) =>{ + + +let modulaHack = 0; + +if(hand1==hand2){ + return "It's a tie!"; +} + + +const tempHand1 = moveToNumber(hand1); +const tempHand2 = moveToNumber(hand2); + + +//turning moves into values to do math on bellow + +modulaHack = (tempHand1 - tempHand2) % 3; +if (modulaHack<0){ + modulaHack+=3; +} +//the way the math should work is anything that adds together and modulos with 3 to 1, the first player +//wins. Sadly, modulo of negatives in javascript yields negative modulo, which is unlike how +//normal mathamatitians think of negative modulo. But if you just add back in the modulo number, +//it yields the deseried value. +//I learned this modulo trick back in computer science school for this game, sadly +//the javascript implimentation for modulo of negative numbers makes it a bit more combersome to +//pull off. Still, it beats a big ol long if statement! + +if (modulaHack==1) { + return "Player 1 Wins!"; +} +return "Player 2 Wins!"; + +} + + + +const moveToNumber = (move) =>{ + switch(move.toLowerCase()) { + case "rock": + return 0; + case "paper": + return 1; + case "scissors": + return 2; + } +} + + function getPrompt() { rl.question('hand1: ', (answer1) => { rl.question('hand2: ', (answer2) => { From b14d3174a8b78edda47b29089418468a0213ec1c Mon Sep 17 00:00:00 2001 From: Christopher Willis Date: Tue, 23 Oct 2018 20:21:02 -0500 Subject: [PATCH 2/2] Redid the indenting --- 01week/rockPaperScissors.js | 52 ++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index da593f0ae..e72bc856e 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -34,37 +34,31 @@ const checkMove = (hand) =>{ } const checkWin = (hand1,hand2) =>{ + let modulaHack = 0; + if(hand1==hand2){ + return "It's a tie!"; + } + const tempHand1 = moveToNumber(hand1); + const tempHand2 = moveToNumber(hand2); + //turning moves into values to do math on bellow -let modulaHack = 0; - -if(hand1==hand2){ - return "It's a tie!"; -} - - -const tempHand1 = moveToNumber(hand1); -const tempHand2 = moveToNumber(hand2); - - -//turning moves into values to do math on bellow - -modulaHack = (tempHand1 - tempHand2) % 3; -if (modulaHack<0){ - modulaHack+=3; -} -//the way the math should work is anything that adds together and modulos with 3 to 1, the first player -//wins. Sadly, modulo of negatives in javascript yields negative modulo, which is unlike how -//normal mathamatitians think of negative modulo. But if you just add back in the modulo number, -//it yields the deseried value. -//I learned this modulo trick back in computer science school for this game, sadly -//the javascript implimentation for modulo of negative numbers makes it a bit more combersome to -//pull off. Still, it beats a big ol long if statement! - -if (modulaHack==1) { - return "Player 1 Wins!"; -} -return "Player 2 Wins!"; + modulaHack = (tempHand1 - tempHand2) % 3; + if (modulaHack<0){ + modulaHack+=3; + } + //the way the math should work is anything that adds together and modulos with 3 to 1, the first player + //wins. Sadly, modulo of negatives in javascript yields negative modulo, which is unlike how + //normal mathamatitians think of negative modulo. But if you just add back in the modulo number, + //it yields the deseried value. + //I learned this modulo trick back in computer science school for this game, sadly + //the javascript implimentation for modulo of negative numbers makes it a bit more combersome to + //pull off. Still, it beats a big ol long if statement! + + if (modulaHack==1) { + return "Player 1 Wins!"; + } + return "Player 2 Wins!"; }