/    Sign up×
Community /Pin to ProfileBookmark

Pick A Number game help please

Anyone please help.

I am trying to build a game in Javascript and being new to JS I am a bit stuck.
Using the [B]prompt[/B] dialogue box and a [B]while[/B] loop statement where someone chooses a number between 1 and 100 inclusive.
On clicking the OK button an Alert box comes up to tell the user after each guess if the number is less than or greater than their guess.

It has also to keep track of the number of times the user guesses the number.

So that if the user cancels of takes more than 7 guesses to guess the number the script should send out an Alert box telling them that they have lost the game. It then resets the counter to 0.

If the user guesses the number within 7 tries the Alert box says Congratulations and resets the counter to 0.

All Help Much Appreciated.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@oleragFeb 27.2004 — Here's some sample code. If you like, convert the "Hide1"

object to a hidden so the random number is not displayed.
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Number Guess Demo</title>
<script type="text/javascript">
<!--
var maxPicks = 7;
var picks = 0;

function checkNumber(field) {
var num = document.forms[0].hide1.value;
if (!checkValue(field.value)) {
alert("Only integers between 1 and 100 may be entered.");
field.focus();
}
else {
var val = parseInt(field.value);
var msg = "";
if (val == num) {
alert("Congratulations - you win.");
resetGame();
}
else {
if (val < num) {
msg = "Your selection is less than the number.";
}
else {
msg = "Your selection is greater than the number.";
}
picks++;

if (picks == maxPicks) {
msg += "Sorry - you lose.";
alert(msg);
resetGame();
}
else {
document.forms[0].field2.value = maxPicks-picks;
msg += "You have " + (maxPicks-picks) + " selections remaining.";
alert(msg);
field.value = "";
field.focus();
}
}
}
}

function checkValue(val) {
if (!isNaN(val)) {
if (val > 0 && val <= 100) {
return(true);
}
}
return(false);
}

function resetGame() {
var num = parseInt(Math.round(Math.random()*1000)/10);
picks = 0;
document.forms[0].field1.value = "";
document.forms[0].field2.value = maxPicks-picks;
document.forms[0].hide1.value = num;
document.forms[0].field1.focus();
}
// -->
</script>

<div style="text-align: center;">
<strong>Number Guess Demo</strong>
</div>

<form action="#">
<p>
Pick your number...<input type="text" name="field1" size="10">
Remaining picks...<input type="text" name="field2" size="5" disabled>
</p>

<p style="text-align: center;">
<input type="button" name="btn1" value="Guess" onClick="checkNumber(document.forms[0].field1)">
<input type="button" name="btn2" value="New Game" onClick="resetGame()">
</p>

<p>
<input type="text" name="hide1" size="5" disabled>
</p>
</form>

<script type="text/javascript">
resetGame();
</script>
[/code]
Copy linkTweet thisAlerts:
@charterauthorFeb 27.2004 — Hi

I have already done it in a similar way.

What I want is for it to work through a pop-up [B]prompt dialogue box[/B] using the [B]prompt[/B] statement.

The reason I want it this way is so that some one can play the game whilst another page is loading or the user is downloading a file.
Copy linkTweet thisAlerts:
@oleragFeb 27.2004 — Well, if you must...
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Number Guess Demo</title>
<script type="text/javascript">
<!--
var maxPicks = 7;
var picks = 0;

function checkNumber(valIn) {
if (!checkValue(valIn)) {
alert("Only integers between 1 and 100 may be entered.");
}
else {
var num = parseInt(document.forms[0].hide1.value);
var val = parseInt(valIn);
var msg = "";
if (val == num) {
document.forms[0].field1.value = "WINNER";
alert("Congratulations - you win.");
return(false);
}
else {
if (val < num) {
msg = "Your selection is less than the number.";
}
else {
msg = "Your selection is greater than the number.";
}
picks++;

if (picks == maxPicks) {
document.forms[0].field1.value = "Try Again";
msg += "Sorry - you lose.";
alert(msg);
return(false);
}
else {
document.forms[0].field1.value = maxPicks-picks;
msg += "You have " + (maxPicks-picks) + " selections remaining.";
alert(msg);
}
}
}
return(true);
}

function checkValue(val) {
if (!isNaN(val)) {
if (val > 0 && val <= 100) {
return(true);
}
}
return(false);
}

function startGame() {
var num = parseInt(Math.round(Math.random()*1000)/10);
picks = 0;
document.forms[0].field1.value = maxPicks-picks;
document.forms[0].hide1.value = num;
var val="";
do {
val = window.prompt("Enter a number between 1 and 100."," ");
}
while(checkNumber(val));
}
// -->
</script>

<div style="text-align: center;">
<strong>Number Guess Demo</strong>
</div>

<form action="#">
<p>
Remaining picks...<input type="text" name="field1" size="10" disabled>
</p>

<p style="text-align: center;">
<input type="button" name="btn1" value="New Game" onClick="startGame()">
</p>

<p>
<input type="text" name="hide1" size="5" disabled>
</p>
</form>

[/code]
Copy linkTweet thisAlerts:
@js_student66Mar 14.2004 — I am doing something similar --

using a high and low number, with no maximum guesses on what the random number is

then i have to output how many guesses it took to find that random number

I'm not very good at looping (setting up a counter)

any pointers would be great.

thanks in advance
Copy linkTweet thisAlerts:
@oleragMar 14.2004 — You can use the previous code sample with three minor

alterations to meet all of your requirements.

  • 1. Assign the "maxPicks" global at some ridiculously high

    number, such as 10000.

  • 2. Change the "alert()" function to read...

    alert("Congratulations - you won in " + picks + " picks.");

  • 3. Make the text object "field1" a hidden object.


  • Your high and low numbers right now are 1 to 100. If you

    make these enterable fields, you''ll need to...

    a) Add a little html to add the text objects and make your

    default assignments.

    b) Validate/handle the object values before each game

    begins if the user changes these values. This would mean

    altering the "checkValues()" function and the handling

    alert message found in the checkNumber() function.

    The silly thing about "number search" games is that if you

    are permitted more than 7 picks to find an integer value

    between 1 and 1000, you can really never lose.
    Copy linkTweet thisAlerts:
    @neil9999Mar 14.2004 — I've made a shorter version below with a few extra features. Change var maxnum to the maximum number that can be picked. var tries is the amount of tries someone can have (set to -1 for unlimited):

    [code=php]
    <script type="text/javascript">
    function startgame(){
    var maxnum=50;
    var num=parseInt(Math.round(Math.random()*maxnum));
    var val;
    var tries=7;
    alert(num);
    alert("Guess the number")
    while(val!=num){
    if(tries==0){
    alert("Sorry! You lose! Answer: "+num);
    break;
    }
    val=window.prompt("Enter a number between 1 and 100. Type QUIT to exit.","");
    if(val=="QUIT"){
    alert("Sorry! You lose! Answer: "+num);
    break;
    }
    if(val>0 && val<maxnum){
    if(val==num){
    alert("Correct! You win!");
    }
    else if(val<num){
    tries=tries-1;
    alert("The number is higher. "+tries+" tries remaining.")
    }
    else{
    tries=tries-1;
    alert("The number is lower. "+tries+" tries remaining.")
    }
    }
    else{
    alert("Invalid entry. Choose a number between 1 and "+maxnum)
    }
    }
    }
    </script>
    </head>

    <body>

    <input type="button" value="New Game" onclick="startgame()">
    [/code]


    Neil
    ×

    Success!

    Help @charter spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.3,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...