patternjavascriptMinor
Simple number theory game
Viewed 0 times
theorynumbergamesimple
Problem
I just started JavaScript about a week ago. For my first project, my goal was to use my knowledge of basic JavaScript to create somewhat of a "thinking computer brain," so to speak. I wasn't focused on the looks of it, as seen by my only using the basic prompts and alerts. I just wanted to get used to writing coherently in JavaScript.
The game "works" but I feel that the code is overly verbose and could be much more succinct. But since I'm new to JS, I'm having trouble figuring out how to go about it. I understand if this is asking too much, but maybe some of the math people might find the game at least a little interesting.
`var welcome = alert("Welcome to 2-to-1. Both you and the computer will start the game off with the number '2'. Whoever gets to '1' first wins! But of course, there's a catch.");
var rules = alert("This is a turn based game. You must first choose a factor of the computer's number. This can be any factor EXCEPT the number 1. 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.");
var rules2 = alert("A few extra rules: When 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. Also, if your score ever reaches 0, this results in an automatic loss and the game ends.");
var rules3 = alert('Lastly, instead of choosing a factor from the computers score, you can choose to simply add 1 to your current score. To do this, type "+1" without the quotes. Good Luck!');
var yourDiv;
var compDiv;
var operator;
var yourNumber = 2;
var compNumber = 2;
function getFactors(integer) {
var factors = [],
quotient = 0;
for(var i = 1; i
The game "works" but I feel that the code is overly verbose and could be much more succinct. But since I'm new to JS, I'm having trouble figuring out how to go about it. I understand if this is asking too much, but maybe some of the math people might find the game at least a little interesting.
`var welcome = alert("Welcome to 2-to-1. Both you and the computer will start the game off with the number '2'. Whoever gets to '1' first wins! But of course, there's a catch.");
var rules = alert("This is a turn based game. You must first choose a factor of the computer's number. This can be any factor EXCEPT the number 1. 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.");
var rules2 = alert("A few extra rules: When 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. Also, if your score ever reaches 0, this results in an automatic loss and the game ends.");
var rules3 = alert('Lastly, instead of choosing a factor from the computers score, you can choose to simply add 1 to your current score. To do this, type "+1" without the quotes. Good Luck!');
var yourDiv;
var compDiv;
var operator;
var yourNumber = 2;
var compNumber = 2;
function getFactors(integer) {
var factors = [],
quotient = 0;
for(var i = 1; i
Solution
You've written the program as a single function. The overall structure can be made more apparent and the code easier to follow if you define helper functions which encapsulate smaller steps of the full program.
For instance, if you take the 30,000 ft view of your program, it looks like this (in pseudo-code):
Each of these steps can be put into their own helper function which are also called "subroutines". The will make the body of the while loop a lot more readable.
Here is an example of a subroutine to get a valid number from the player (again, pseudo-code):
Note how the while loop avoids the duplication of code that you have in your program.
Here is another idea for a subroutine - one that updates a player's score and returns how to report it. Either the computer's or player's score can be updated with this:
Finally, you should put all of the computer's decision logic in a separate function, e.g.:
With these helper functions this is what the while loop could look like:
For instance, if you take the 30,000 ft view of your program, it looks like this (in pseudo-code):
while (neither player has won) {
get the player's number
update the player's score
check if player has won or lost
determine the computer's number
update the computer's score
check if the computer has won or lost
}Each of these steps can be put into their own helper function which are also called "subroutines". The will make the body of the while loop a lot more readable.
Here is an example of a subroutine to get a valid number from the player (again, pseudo-code):
function getPlayersDivisor(computersNumber) {
while (true) {
...prompt for a number, put into var yourDiv...
if (yourDiv is a divisor of computersNumber or is "+1") {
return yourDiv;
}
alert("Not a valid entry");
}
}Note how the while loop avoids the duplication of code that you have in your program.
Here is another idea for a subroutine - one that updates a player's score and returns how to report it. Either the computer's or player's score can be updated with this:
function updateNumber(oldNumber, divisorChosen) {
var newNumber;
var message;
if (divisorChosen == 1 || divisorChosen % 2 == 0) {
newNumber = oldNumber + divisorChosen
message = divisorChosen + " was added to";
} else {
newNumber = oldNumber - divisorChosen;
if (newNumber < 0) {
newNumber = - newNumber;
}
message = divisorChosen + " was subtracted from";
}
return [ newNumber, message ]; # returns multiple values
}Finally, you should put all of the computer's decision logic in a separate function, e.g.:
function chooseForComputer(playerNumber) {
# logic for choosing a divisor for the computer goes here
...
return compDiv;
}With these helper functions this is what the while loop could look like:
// Assume these variables are defined:
// player - the player's number
// computer - the computer's number
//
// and that neither player has won.
while (true) {
playersDiv = getPlayersDivisor(computer);
var result = updateNumber(player, playersDiv);
player = result[0];
var message = result[1];
alert(message + " your score. Your number is now " + player)
if (player == 0) { alert ("You lost."); break }
if (player == 1) { alert ("You won.!"); break }
var computerDiv = chooseForComputer(player);
var result = updateNumber(computer, computerDiv);
computer = result[0]
alert("Computer choose " + computerDiv +" and now has score " + computer);
if (computer == 0) { alert("Computer lost."); break }
if (computer == 1) { alert("Computer won!"); break }
}Code Snippets
while (neither player has won) {
get the player's number
update the player's score
check if player has won or lost
determine the computer's number
update the computer's score
check if the computer has won or lost
}function getPlayersDivisor(computersNumber) {
while (true) {
...prompt for a number, put into var yourDiv...
if (yourDiv is a divisor of computersNumber or is "+1") {
return yourDiv;
}
alert("Not a valid entry");
}
}function updateNumber(oldNumber, divisorChosen) {
var newNumber;
var message;
if (divisorChosen == 1 || divisorChosen % 2 == 0) {
newNumber = oldNumber + divisorChosen
message = divisorChosen + " was added to";
} else {
newNumber = oldNumber - divisorChosen;
if (newNumber < 0) {
newNumber = - newNumber;
}
message = divisorChosen + " was subtracted from";
}
return [ newNumber, message ]; # returns multiple values
}function chooseForComputer(playerNumber) {
# logic for choosing a divisor for the computer goes here
...
return compDiv;
}// Assume these variables are defined:
// player - the player's number
// computer - the computer's number
//
// and that neither player has won.
while (true) {
playersDiv = getPlayersDivisor(computer);
var result = updateNumber(player, playersDiv);
player = result[0];
var message = result[1];
alert(message + " your score. Your number is now " + player)
if (player == 0) { alert ("You lost."); break }
if (player == 1) { alert ("You won.!"); break }
var computerDiv = chooseForComputer(player);
var result = updateNumber(computer, computerDiv);
computer = result[0]
alert("Computer choose " + computerDiv +" and now has score " + computer);
if (computer == 0) { alert("Computer lost."); break }
if (computer == 1) { alert("Computer won!"); break }
}Context
StackExchange Code Review Q#102425, answer score: 6
Revisions (0)
No revisions yet.