After receiving some awesome feedback in my first post yesterday, I was able to grasp the "building block" method of creating javascript in chunks and putting them together to create coherent/straightforward code.
I've done quite a bit of refactoring and am fairly pleased given my initial goals for this project, but I know that there is always room for improvement! So here's the updated code for anyone interested in taking a look. The main thing that still bugs me now is the newNumber
variable. Whereas everything else is nice and tucked away in its own function, newNumber
gets passed around in several different places. Hopefully that makes sense!
I suppose I should note that the game mechanics are a little different this time around. I realized that the previous iteration involved less strategy than I originally intended, so I mixed some things up. I also focused less on making the computer think a couple steps ahead, so the game will play more randomly now.
var welcome = alert("Welcome to Zeros or Ones! Both you and the computer will start the game off with the number '2'. Whoever gets down to '1' or '0' first wins!"),
rules = alert("This is a turn based game. You must first choose a factor from the computer's number. If the chosen factor is even, it is added to your current score; if odd, it is subtracted. After this, the computer chooses a factor from your new number. This back-and-forth continues until someone gets their number down to 1 or 0."),
rules2 = alert("A few exceptions:\n\nWhen an odd number is being subtracted from your score, if your resulting score is a negative number, the absolute value is taken, turning the negative number into a positive.\n\nNeither player is allowed to win the game on a 2. For instance if your score is 2, you cannot choose the factors 1 or 3 from the computer's score.\n\nException to the exception! In the case that you have a 2 and the computer has a 3, it is allowable to choose either factor.");
var compNumber = 2,
yourNumber = 2,
newNumber,
yourDiv,
compDiv,
operator,
youWin = "YAY! You won! :D",
compWins = "Sorry! You lost :/";
function getFactors(integer) {
var factors = [],
quotient = 0;
for(var i = 1; i <= integer; i++){
quotient = integer/i;
if(quotient === Math.floor(quotient)){
factors.push(i);
}
}
return factors;
}
function playerChoosesFactor() {
var initCompFactors = getFactors(compNumber);
while (true) {
var yourDivv = prompt("Your score: " + yourNumber + ". Computer's score: " + compNumber + ". Which factor do you choose from the computer's score?");
yourDiv = parseInt(yourDivv);
if (initCompFactors.indexOf(yourDiv) !== -1 && yourNumber === 2 && compNumber !== 3 && yourDiv !== 1 && yourDiv !== 3) {
return yourDiv;
}
if (initCompFactors.indexOf(yourDiv) !== -1 && yourNumber === 2 && compNumber === 3) {
return yourDiv;
}
if (initCompFactors.indexOf(yourDiv) !== -1 && yourNumber !== 2) {
return yourDiv;
}
alert("Not a valid entry.");
}
}
function computerChoosesFactor() {
var yourFactors = getFactors(yourNumber);
compDiv = yourFactors[Math.floor(Math.random() * yourFactors.length)];
if (compNumber === 2 && yourNumber !== 3) {
while (compDiv === 1 || compDiv === 3) {
compDiv = yourFactors[Math.floor(Math.random() * yourFactors.length)];
}
}
}
function updateScore(div, whoseScore, oldNumber) {
if (div % 2 === 0) {
newNumber = oldNumber + div;
operator = "added to";
} else {
newNumber = oldNumber - div;
if (newNumber < 0) {
newNumber = newNumber * -1;
}
operator = "subtracted from";
}
alert(div + " was " + operator + whoseScore + newNumber + ".");
return newNumber;
}
function checkForWin(number, winOrLoss) {
if (number === 1 || number === 0) {
alert(winOrLoss);
return true;
} else {
return false;
}
}
while(true) {
playerChoosesFactor();
updateScore(yourDiv, " your score. You now have ", yourNumber);
yourNumber = newNumber;
if (checkForWin(yourNumber, youWin)) {break;}
computerChoosesFactor();
updateScore(compDiv, " the computer's score. The computer now has ", compNumber);
compNumber = newNumber;
if (checkForWin(compNumber, compWins)) {break;}
}
getFactors()
? \$\endgroup\$