Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 3 of 3
Thread: Javascript Game homework help

02262014, 07:32 PM #1
 Join Date
 Feb 2014
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
Javascript Game homework help
Help, please!
my professor has us doing number games, and i am lost on this part of a javascript assignment.
"In the 21 game, two players start with a pile of 21 stones. The players take turns removing from one to three stones from the pile. The player that removes the last stone is the game's loser.
When you write your program, it is meant to make the first move. Write it so that it picks up from one to three stones per each turn. Use this code:
computerMove = Math.floor(Math.random()* 3 +1;
Your program should then ask the player how many stones she wishes to remove. Repeat this back and forth until all 21 stones are gone."
So, I don't even qualify as having a basic knowledge of Javascript, and I'm desperate. Can anyone help me on this?
02262014, 07:56 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,508
 Thanks
 205
 Thanked 2,581 Times in 2,559 Posts
This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.
However, I'll give you a push forward:
Code:<script type = "text/javascript"> var numstones = 21; while (numstones >0) { // keep going while there are still stones in the heap // PLAYER for (var i=1; i<2; i++) { var ans = parseInt(prompt ("How many stones do you wish to remove?", "")); if ((isNaN(ans))  (ans == "")  (ans <1)  (ans >3)) { alert ("You must enter a number 13!! "); i  ; } } // now calculate the number of stones remaining (not to be <0!). ans must not be > numstones. // if the number of stones remaining is 0 you have taken the last one and you lose. // and then let the computer choose its move with similar calculations for stones remaining.
Note that your random number code is missing a closing bracket. Should be computerMove = Math.floor(Math.random()* 3 +1);
If the computer is intelligent it will try to leave just one stone for the player to be forced to take.
In other words its choice will not be random if there are 24 stones left. The computer will take numstones  1 (leaving one for the player to take). So in this situation the computer will always win.
For further help you must write at least a good chunk of the remaining code.
Be aware that prompts and alerts are obsolete as ways of obtaining and displaying data but I expect that your professor wishes you to use them at this early stage. So prefer to use IE as your browser.
Quizmaster: Evelyn Waugh was a famous female cricketer. True or false?
Contestant: True
Last edited by Philip M; 02272014 at 09:06 AM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
03042014, 01:35 AM
#3
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,960
 Thanks
 0
 Thanked 688 Times in 677 Posts
Actually the computer's selection should never be random. That particular game is always a win to one specific player if both play a perfect game (which depends on the number you start with) so the computer response in each case could be optimised to make the perfect response to each player move and so always wil if the starting number gives the game to the computer and to win if the player makes a single mistake for those that would otherwise be a player win.
So 4 stones the first player takes three and the second has to take the last stone and so loses.
5 stones the second player always wins.
6 stones the first player takes one and wins.
7 stones the first player takes 2 and wins.
8 stones the first player takes 3 and wins
9 stones the second player wins
and so on with the second player being the winner whenever the number is a multiple of 4 plus one. So with 21 stones the second player always wins  unless they make a mistake).
As the computer goes first and removes randomly one to three stones each time all the player has to do is to subtract the number the computer removed from four and remove that many as their go in order to leave the last stone for the computer. No matter what move the computer selects starting with 21, it will only win if the player makes a mistake.
I think having the computer go second in that situation and have it set to play the correct move instead of a random one would make a far more interesting coding exercise.
Last edited by felgall; 03042014 at 01:38 AM.
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.