<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WiseCodes &#187; Game</title>
	<atom:link href="http://www.wisecodes.com/tag/game/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wisecodes.com</link>
	<description>Bringing You The Power of Code !!</description>
	<lastBuildDate>Wed, 01 Sep 2010 19:33:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Javascript: Tic Tac Toe</title>
		<link>http://www.wisecodes.com/2009/08/javascript-tic-tac-toe/</link>
		<comments>http://www.wisecodes.com/2009/08/javascript-tic-tac-toe/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 15:17:33 +0000</pubDate>
		<dc:creator>Venu Thomas</dc:creator>
				<category><![CDATA[JAVASCRIPT]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[OpenSource]]></category>

		<guid isPermaLink="false">http://www.wisecodes.com/?p=741</guid>
		<description><![CDATA[View Demo Download Code [3.29 kb] Playing The computer always plays with circles ( &#8220;O&#8221;) and you with blades ( &#8220;X&#8221;). Start your. To put an X have to click on the button the bottom panel that corresponds to the position you want to put it. You can also hand-write it, always with capital letters [...]]]></description>
			<content:encoded><![CDATA[<h2><a href="http://wisecodes.com/Code/TicTacToeJavascript/TicTacToe.html" target="_blank"><img class="alignnone size-full wp-image-18" title="live_preview" src="http://www.wisecodes.com/wp-content/uploads/2009/02/live_preview.png" alt="live_preview" width="32" height="32" /> View Demo</a> <a href="http://wisecodes.com/Code/TicTacToeJavascript/TicTacToe.rar"><img class="alignnone size-full wp-image-29" title="download" src="http://www.wisecodes.com/wp-content/uploads/2009/02/download.gif" alt="download" width="32" height="35" />Download Code</a> [3.29 kb]</h2>
<h2>Playing</h2>
<p>The computer always plays with <em> circles </em> ( &#8220;O&#8221;) and you with <em> blades </em><br />
( &#8220;X&#8221;). Start your. To put an X <em> </em> have to click on the button<br />
the bottom panel that corresponds to the position you want to put it. You can<br />
also hand-write it, always with capital letters (without cheating, because the<br />
computer trusts you).</p>
<p>The computer will make its <em> redondel </em> every time you put your <em> X </em>.<br />
Never make a mistake, and if you neglect (and you do not cheat), you win. If you cheat, do not<br />
work correctly.</p>
<p>When the game ends, the computer shows the result and increase the counters<br />
lower. To play another game, press the button again <strong> Game </strong>, which<br />
Clean the panel game. To clear the counters, click the Clear button <strong>Clear</strong>.</p>
<p>This < head> between and <strong><</strong>/head>:</p>
<pre class="brush: xml;">
&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;
// Tic Tac Toe
// Version: 1 (10/08/2009)
// Venu Thomas
// This script and many others can
// download online for free
// in the code: [url = http://www.wisecodes.com] www.wisecodes.com [/ url] 

// To understand the explanations:
// Win - and win puts
// Game - and has put in a two-line 'O' and an empty box
// Defend - keep winning the next move in the opposite
// It makes no sense at first occupy the center if there is victory or play
// There must defend first and attack later. First look for the victory, then
// search and then defends move 

// global variable
var strPlaying = true   

//ROLE PLAY CHECK------------------------------------------------
function wc_fnCheckPlay(grid, record) {
    // check that it is possible to stripe 3 in a row, column or diagonal
     // returns the position where you have to put the record to do so and -1 if there is no gamble   

    var x, y   

    for(x = 0; x &lt; 9; x += 3) {      //Check the rows
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 1].value == record))
            if (grid.elements[x + 2].value == &quot;&quot;)
                return (x + 2)
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 2].value == record))
            if (grid.elements[x + 1].value == &quot;&quot;)
                return (x + 1)
        if ((grid.elements[x + 1].value == record) &amp;&amp; (grid.elements[x + 2].value == record))
            if (grid.elements[x].value == &quot;&quot;)
                return (x)
    }   

    for(x = 0; x &lt; 3; x++) {     //Check the columns
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 3].value == record))
            if (grid.elements[x + 6].value == &quot;&quot;)
                return (x + 6)
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 6].value == record))
            if (grid.elements[x + 3].value == &quot;&quot;)
                return (x + 3)
        if ((grid.elements[x + 3].value == record) &amp;&amp; (grid.elements[x + 6].value == record))
            if (grid.elements[x].value == &quot;&quot;)
                return (x)
    }   

    //check the diagonals
    if ((grid.elements[2].value == record) &amp;&amp; (grid.elements[4].value == record) &amp;&amp; (grid.elements[6].value == &quot;&quot;))
        return (6)
    if ((grid.elements[2].value == record) &amp;&amp; (grid.elements[4].value == &quot;&quot;) &amp;&amp; (grid.elements[6].value == record))
        return (4)
    if ((grid.elements[2].value == &quot;&quot;) &amp;&amp; (grid.elements[4].value == record) &amp;&amp; (grid.elements[6].value == record))
        return (2)
    if ((grid.elements[0].value == record) &amp;&amp; (grid.elements[4].value == record) &amp;&amp; (grid.elements[8].value == &quot;&quot;))
        return (8)
    if ((grid.elements[0].value == record) &amp;&amp; (grid.elements[4].value == &quot;&quot;) &amp;&amp; (grid.elements[8].value == record))
        return (4)
    if ((grid.elements[0].value == &quot;&quot;) &amp;&amp; (grid.elements[4].value == record) &amp;&amp; (grid.elements[8].value == record))
        return (0)   

    return -1
}   

//HORIZONTAL PARTNER ROLE
function wc_fnPartnerHorizontal(grid, record) {
    // check if it's possible to put 2 in a row, the other empty position ...
     //... same row
     // returns the row to do so, or -1 if not 

    var x, y   

    for(x = 0; x &lt; 9; x += 3) {      //Check the rows
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 1].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 2].value == &quot;&quot;))
            return (x)
        if ((grid.elements[x].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 1].value == record) &amp;&amp; (grid.elements[x + 2].value == &quot;&quot;))
            return (x + 1)
        if ((grid.elements[x].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 1].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 2].value == record))
            return (x + 2)
    }   

    return -1
}   

//VERTICAL PARTNER ROLE---------------------------------------------------
function wc_fnPartnerVertical(grid, record, playHtal) {
 // check if it's possible to put 2 in a column, the other empty position ...
     //... same column and taking into account whether that position is horizontal pair
     // returns the column if you do not allow horizontal pair, or -1 if not      

    var x, y   

    for(x = 0; x &lt; 3; x++) {         //check the columns
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 3].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 6].value == &quot;&quot;))
            if (x != playHtal)        //if partner not interested in horizontal and vertical
                return (x)
        if ((grid.elements[x].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 3].value == record) &amp;&amp; (grid.elements[x + 6].value == &quot;&quot;))
            if ((x + 3) != playHtal)
                return (x + 3)
        if ((grid.elements[x].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 3].value == &quot;&quot;) &amp;&amp; (grid.elements[x + 6].value == record))
            if ((x + 6) != playHtal)
                return (x + 6)
    }   

    return -1
}   

//Get POSITION\
function wc_fnGetPosition(playHtal, playVcal) {
 // Find the position that allows you move vertically and horizontally at the same time move
     // known row and column where couples can be

    var x, y, fila, columna
    var position = 0
    matrix = new Array(3)   

    for (x = 0; x &lt; 3; x++) {        //creates a matrix that assigns a row and column position
        matrix[x] = new Array(3)
        for (y = 0; y &lt; 3; y++) {
            matrix[x][y] = position
            position ++
        }
    }   

    for (x = 0; x &lt; 3; x++) {                    //looking for the row and column
        for (y = 0; y &lt; 3; y++) {
            if (matrix[x][y] == playHtal) //find the row
                fila = x
            if (matrix[x][y] == playVcal) //I found the column
                columna = y
        }
    }   

    return (matrix[fila][columna])          //move back position
}   

//FUNCION REDONDELA PUT-------------------------------------------------
function wc_fnPutRedondela(grid) {
    //the ordanador plays

    var play, playHtal, playVcal   

    //attacks and wins
    play = wc_fnCheckPlay(grid, &quot;O&quot;)
    if (play != -1) {
        grid.elements[play].value = &quot;O&quot;
        alert('I won!!!')
        document.scoreboard.missing.value++
        strPlaying = false
        return 1
    }   

    //advocates avoiding the rival wins
    play = wc_fnCheckPlay(grid, &quot;X&quot;)
    if (play != -1) {
        grid.elements[play].value = &quot;O&quot;
        return 1
    }   

    //attacks and win the next
    playHtal = wc_fnPartnerHorizontal(grid, &quot;O&quot;)
    playVcal = wc_fnPartnerVertical(grid, &quot;O&quot;, playHtal)
    if ((playHtal != -1) &amp;&amp; (playVcal != -1)) {     //you can put 2 and 2 in a row in column
        if ((playHtal != 4) || (playVcal != 4)) {       //is not the center
            play = wc_fnGetPosition(playHtal, playVcal)  //gets where it should put
            grid.elements[play].value = &quot;O&quot;
            return 1
        }
    }   

    //attacks and put two in a row
    if ((playHtal != -1) &amp;&amp; (playVcal == -1)) {
        if ((playHtal != 2) &amp;&amp; (playHtal != 5) &amp;&amp; (playHtal != 8))    //this is arbitrary
            grid.elements[playHtal + 1].value = &quot;O&quot;                        //puts the right
        else
            grid.elements[playHtal - 1].value = &quot;O&quot;                        //placed to the left
        return 1
    }   

    //attacks and put in two column
    if ((playHtal == -1) &amp;&amp; (playVcal != -1)) {
        if ((playVcal != 6) &amp;&amp; (playVcal != 7) &amp;&amp; (playVcal != 8))
            grid.elements[playVcal + 3].value = &quot;O&quot;        //put down
        else
            grid.elements[playVcal - 3].value = &quot;O&quot;        //puts up
        return 1
    }   

    //occupies the center
    if (grid.elements[4].value == &quot;&quot;) {
        grid.elements[4].value = &quot;O&quot;
        return 1
    }   

    //This took the first free
    for (x = 0; x &lt; 9; x++)
        if (grid.elements[x].value == &quot;&quot;) {
            grid.elements[x].value = &quot;O&quot;
            return 1
        }   

    alert('Draw...')
    document.scoreboard.draw.value++
    strPlaying = false
    return -1
}   

//FUNCION PUT ASPA
function wc_fnPutAspa(grid, position) {   

    //first check that this strPlaying
    if (strPlaying) {
        if (grid.elements[position].value != &quot;&quot;)     //empty box
            alert('That box is already occupied.')
        else {                                                  //empty box, you can put
            grid.elements[position].value = &quot;X&quot;  

            //Check if you have won
            if ( wc_fnNotesVictoria(grid, &quot;X&quot;) ) {
                alert('Congratulations! You win... <img src='http://www.wisecodes.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ')
                document.scoreboard.won.value++
                strPlaying = false
            } else
                wc_fnPutRedondela(grid)
        }
    } else {
        alert('To start a new line \nClick Play again.')
    }
}   

//VICTORIA FUNCTION CHECK
function wc_fnNotesVictoria(grid, record) {   

    //Check if you have earned the record player that plays with  

    var x   

    for(x = 0; x &lt; 9; x += 3) {  //Check the rows
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 1].value == record) &amp;&amp; (grid.elements[x + 2].value == record))
            return true
    }   

    for(x = 0; x &lt; 3; x++) {     //check the columns
        if ((grid.elements[x].value == record) &amp;&amp; (grid.elements[x + 3].value == record) &amp;&amp; (grid.elements[x + 6].value == record))
            return true
    }   

    //check the diagonals
    if ((grid.elements[2].value == record) &amp;&amp; (grid.elements[4].value == record) &amp;&amp; (grid.elements[6].value == record))
        return true
    if ((grid.elements[0].value == record) &amp;&amp; (grid.elements[4].value == record) &amp;&amp; (grid.elements[8].value == record))
        return true   

    return false
}   

&lt;/script&gt;
</pre>
<p>And this from <strong><</strong>body> and <strong><</strong>/body> :</p>
<pre class="brush: xml;">
&lt;!-- To display the grid, and Accountants --&gt;
&lt;form name=&quot;grid&quot;&gt;
  &lt;div align=&quot;center&quot;&gt;&lt;center&gt;
  &lt;table border=&quot;0&quot; width=&quot;62%&quot; cellpadding=&quot;5&quot;&gt;
    &lt;tr&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T0&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T1&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T2&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot; align=&quot;right&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T3&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T4&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T5&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot; align=&quot;right&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T6&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T7&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;T8&quot; size=&quot;3&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot; align=&quot;right&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B0&quot; onClick=&quot;wc_fnPutAspa(grid, 0)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B1&quot; onClick=&quot;wc_fnPutAspa(grid, 1)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B2&quot; onClick=&quot;wc_fnPutAspa(grid, 2)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot;&gt;&lt;input type=&quot;reset&quot; value=&quot;New game&quot; name=&quot;new&quot; onClick=&quot;strPlaying=true&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=&quot;18%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B3&quot; onClick=&quot;wc_fnPutAspa(grid, 3)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;17%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B4&quot; onClick=&quot;wc_fnPutAspa(grid, 4)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;18%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B5&quot; onClick=&quot;wc_fnPutAspa(grid, 5)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td width=&quot;18%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B6&quot; onClick=&quot;wc_fnPutAspa(grid, 6)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;17%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B7&quot; onClick=&quot;wc_fnPutAspa(grid, 7)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;18%&quot;&gt;&lt;input type=&quot;button&quot; value=&quot; X &quot; name=&quot;B8&quot; onClick=&quot;wc_fnPutAspa(grid, 8)&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot; align=&quot;right&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/center&gt;&lt;/div&gt;
&lt;/form&gt;

&lt;form name=&quot;scoreboard&quot;&gt;
  &lt;div align=&quot;center&quot;&gt;&lt;center&gt;
  &lt;table border=&quot;0&quot; width=&quot;62%&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot;&gt;
    &lt;tr&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;small&gt;draw:&lt;br&gt;
      &lt;/small&gt;&lt;input type=&quot;text&quot; name=&quot;draw&quot; value=&quot;0&quot; size=&quot;6&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;small&gt;missing:&lt;br&gt;
      &lt;/small&gt;&lt;input type=&quot;text&quot; name=&quot;missing&quot; value=&quot;0&quot; size=&quot;6&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;20%&quot;&gt;&lt;small&gt;won:&lt;br&gt;
      &lt;/small&gt;&lt;input type=&quot;text&quot; name=&quot;won&quot; value=&quot;0&quot; size=&quot;6&quot;&gt;&lt;/td&gt;
      &lt;td width=&quot;85%&quot;&gt; &lt;br&gt;&lt;input type=&quot;reset&quot; value=&quot;Clear&quot; name=&quot;delete&quot; class=&quot;metal&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/center&gt;&lt;/div&gt;
&lt;/form&gt;

&lt;h2&gt;Playing&lt;/h2&gt;
&lt;p&gt;The computer always plays with &lt;em&gt; circles &lt;/em&gt; ( &quot;O&quot;) and you with &lt;em&gt; blades &lt;/em&gt;
( &quot;X&quot;). Start your. To put an X &lt;em&gt; &lt;/em&gt; have to click on the button
the bottom panel that corresponds to the position you want to put it. You can
also hand-write it, always with capital letters (without cheating, because the
computer trusts you).&lt;/p&gt;
&lt;p&gt;The computer will make its &lt;em&gt; redondel &lt;/em&gt; every time you put your &lt;em&gt; X &lt;/em&gt;.
Never make a mistake, and if you neglect (and you do not cheat), you win. If you cheat, do not
work correctly.&lt;/p&gt;
&lt;p&gt;When the game ends, the computer shows the result and increase the counters
lower. To play another game, press the button again &lt;strong&gt; Game &lt;/strong&gt;, which
Clean the panel game. To clear the counters, click the Clear button &lt;strong&gt;Clear&lt;/strong&gt;.&lt;/p&gt;
</pre>
<p>Happy Programming!!! <img src='http://www.wisecodes.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://www.wisecodes.com/2009/08/javascript-tic-tac-toe/" target="_blank"><img src="http://www.wisecodes.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://www.wisecodes.com/2009/08/javascript-tic-tac-toe/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wisecodes.com/2009/08/javascript-tic-tac-toe/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

