Parag’s Den

Dedicated to my adored friends…..

  • Blog Status

    • 8,357 Visits so far
  • Categories

  • Recent Posts

  • Calender

    November 2007
    M T W T F S S
    « Oct   Jun »
  • Advertisements

Tic Tac Toe

Posted by paragdongre on November 5, 2007

Hi Friends!
Now u must used to it, so need not to tell you that what you suppose to do.
Simply copy paste this javascript code in notepad and save it as “.HTML” file format.
and start playing.

[Note: Plz dont make any type of updation in the code before copying it.]

  <SCRIPT LANGUAGE=”LiveScript”>
  step = 0;
  // change board when button is clicked
        function clear_all(form) {
   step = 0;
   for (i=0;i<9; ++i) {
  // change board when button is clicked
        function clickit(field) {
  if (step == -1) {alert(“Reset to play again”); return;},2,1);
        position = ‘a’+position;
  if (field.form[position].value !=””) {alert(“Can’t go there”); return;}
        if (eval_pos(field.form)) {
   field.form.output.value=”You Win!”;
   step = -1;
        field.form.output.value=’I moved to ‘ + position.substring(1,2,1);
  if (position==””) {
   field.form.output.value=”No Winner.”;
   step = -1;

        if (eval_pos(field.form)) {
   field.form.output.value=”You Lose!”;
   step = -1;

  // see if there is a winner
        function eval_pos(form) {
     if ((form.a0.value!=”” &&
   form.a0.value==form.a3.value && form.a0.value==form.a6.value)||

   && form.a0.value==form.a1.value && form.a0.value==form.a2.value) ||
   && form.a0.value==form.a4.value && form.a0.value==form.a8.value) ||
   && form.a1.value==form.a4.value && form.a1.value==form.a7.value) ||
   && form.a2.value==form.a5.value && form.a2.value==form.a8.value) ||
   && form.a2.value==form.a4.value && form.a2.value==form.a6.value) ||
   && form.a3.value==form.a4.value && form.a3.value==form.a5.value) ||
   && form.a6.value==form.a7.value && form.a6.value==form.a8.value))
   return true;
           return false;

  function f(a) {
  if (a == “”) return “.”; else return a;

  // get position for move.
        function comp_move(form,player,weight,depth) {

            var cost;
   var bestcost=-2;
   var position;
   var newplayer;
   if (player==”X”) newplayer=”O”; else newplayer=”X”;
   if (depth==diff) return 0;

   if (eval_pos(form)) return 1;
   for (var i=0; i<9; ++i) {
    if (form[position].value != “”)
    cost = comp_move(form,newplayer, -weight, depth+1);
    if (cost > bestcost) {
     if (cost==1) i=9;
   if (bestcost==-2) bestcost=0;

  // get position for move.
        function get_move(form) {
   var cost;
   var bestcost=-2;
   // don’t think about first move.
   if (step++ == 0)
    if (form.a4.value==””)
     return “a4”;
    if (form.a0.value==””)
     return “a0”;

   for (var i=0; i<9; ++i) {
    if (form[localposition].value != “”)
    cost=comp_move(form,”X”, -1, 0);
    if (cost > bestcost) {
     if (cost==1) i=9;
   return bestmove;

  // complain if user attempts to change board
        function complain(field) {
   field.form.output.focus(); // put focus eleswhere
   alert(“Don’t change the game fields directly!”);

        // the end –>
<font face =”Georgia” ><b>Simply click on the buttons next to textbox in which u want to tic.</b></font>
 <INPUT SIZE=1 NAME=”a0″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b0″ OnClick=”clickit(this)”>
 <INPUT SIZE=1 NAME=”a1″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b1″ OnClick=”clickit(this)”>
 <INPUT SIZE=1 NAME=”a2″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b2″ OnClick=”clickit(this)”>

 <INPUT SIZE=1 NAME=”a3″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b3″ OnClick=”clickit(this)”>
 <INPUT SIZE=1 NAME=”a4″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b4″ OnClick=”clickit(this)”>
 <INPUT SIZE=1 NAME=”a5″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b5″ OnClick=”clickit(this)”>

 <INPUT SIZE=1 NAME=”a6″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b6″ OnClick=”clickit(this)”>
 <INPUT SIZE=1 NAME=”a7″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b7″ OnClick=”clickit(this)”>
 <INPUT SIZE=1 NAME=”a8″ OnFocus=”complain(this)”>
 <INPUT TYPE=”button” NAME=”b8″ OnClick=”clickit(this)”>
 Message: <INPUT NAME=”output” TYPE=”text”><BR>
 Difficulty: <SELECT NAME=”difficulty”
 <OPTION VALUE=1> Very Easy
 <OPTION VALUE=4> Hard (may think a long time)
 <BR><INPUT TYPE=”button” VALUE=”Let Computer Start First” OnClick=”
  if (!step++) this.form.a4.value=’O’;”>

 <BR><INPUT TYPE=”reset” VALUE=”Restart” OnClick=”clear_all(this.form)”>



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: