topical media & game development

talk show tell print

game-javascript-casual-random-number-game.htm / htm



  
  <!-- TWO STEPS TO INSTALL RANDOM NUMBER:
  
     1.  Paste the first code in the HEAD of your HTML document
     2.  Add the last coding to the BODY of your HTML document  -->
  
  <!-- STEP ONE: Paste the first code in the HEAD of your HTML document  -->
  
  <HEAD>
  
  <SCRIPT LANGUAGE="JavaScript">
  
  <!-- This script and many more are available free online at -->
  <!-- The JavaScript Source!! http://javascript.internet.com -->
  
  <!-- Begin
  var RNumber;
  var RTries;
  limit = 50;
  function Random() {
  today = new Date();
  num = today.getTime();
  num = Math.round(Math.abs(Math.sin(num) * 1000000)) % limit;
  return num;
  }
  function Init(){
  RNumber = Random();
  RTries = 0;
  document.FGame.Output.value='I am thinking of a number between 0 and ' + (limit-1) +' Guess it....';
  document.FGame.Tries.value=RTries;
  document.FGame.HighLow.value='';
  document.FGame.Input.value='';
  }
  function Game(Number) {
  if(Number==RNumber) {
  RTries++;
  document.FGame.Output.value='You guessed it in ' + RTries + ' tries! It was ' + RNumber + '! Hit Restart to play again.';
  document.FGame.HighLow.value='Got It!';
  } 
  else {
  RTries++;
  document.FGame.Output.value='Nope, ' + Number + ' is not the number I am thinking about!';
  document.FGame.HighLow.value=(RNumber > Number) ? 'Higher!' : 'Lower!';
  document.FGame.Tries.value=RTries;
     }
  }
  // End -->
  </SCRIPT>
  
  <!-- STEP TWO: Add the last coding to the BODY of your HTML document  -->
  
  <BODY>
  
  <center>
  <FORM NAME="FGame">
  <INPUT TYPE="txt" NAME="Output" VALUE="" Size="70"><br>
  <INPUT TYPE="txt" NAME="Input" VALUE='' Size="20">
  The number is:
  <INPUT TYPE="txt" NAME="HighLow" VALUE='' Size="20">
  You guessed:
  <INPUT TYPE="txt" NAME="Tries"  VALUE="0" SIZE="3"> Times.<br>
  <INPUT TYPE="button" NAME="one"  VALUE="  1  " OnClick="FGame.Input.value += 1">
  <INPUT TYPE="button" NAME="one"  VALUE="  2  " OnClick="FGame.Input.value += 2">
  <INPUT TYPE="button" NAME="one"  VALUE="  3  " OnClick="FGame.Input.value += 3"><br>
  <INPUT TYPE="button" NAME="one"  VALUE="  4  " OnClick="FGame.Input.value += 4">
  <INPUT TYPE="button" NAME="one"  VALUE="  5  " OnClick="FGame.Input.value += 5">
  <INPUT TYPE="button" NAME="one"  VALUE="  6  " OnClick="FGame.Input.value += 6"><br>
  <INPUT TYPE="button" NAME="one"  VALUE="  7  " OnClick="FGame.Input.value += 7">
  <INPUT TYPE="button" NAME="one"  VALUE="  8  " OnClick="FGame.Input.value += 8">
  <INPUT TYPE="button" NAME="one"  VALUE="  9  " OnClick="FGame.Input.value += 9"><br>
  <INPUT TYPE="button" NAME="one"  VALUE="  0  " OnClick="FGame.Input.value += 0">
  <INPUT TYPE="button" NAME="DoIt" VALUE=" Try it!  " OnClick="Game(FGame.Input.value); FGame.Input.value=''"><br>
  <INPUT TYPE="button" NAME="init" VALUE="      Start/Restart     " OnClick="Init()">  
  </FORM>
  </center>
  
  <p><center>
  <font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
  by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
  </center><p>
  
  <!-- Script Size:  2.75 KB  -->
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.