Posted in Uncategorized

JS Post 3 – AI Gaming (*〇□〇)……!

So I was searching places to code an AI game and the Google served me with this code (JS code for playing pong). I am was trying to decipher it enough to find a pattern to code my snake game.And I failed…miserably.

( -̩̩̩-̩̩̩-̩̩̩- _ -̩̩̩-̩̩̩-̩̩̩- )

And I wasted my time with no fruits whatsoever. But I did code something simpler and it is pretty stupid also. I am gonna sit on it tomorrow (after I take a nap today finishing what I have planned for today for my exam) and tweak the code a little maybe.And if I succeed to make it run without glitching I’ll put it up here as an update or something.

Some video I was referring to for learning AI using JS – Link

Well until then enjoy the randomly moving Blocky Snake powered by Random Color generator. Hopefully, it works … The logic behind it is to loop a block over a set of randomly generated color until the length of the worm is passed finishing with a transparent color and to place a new head position randomly. I have always liked randomness … I think? I guess it is kinda hard to read with everything flying like everywhere?

( ´⍪⍪`)

Uhm… this ended pretty soon? I am heading back to study so … ? Wow am I confused with the way of my life…

₍₍ ᕕ(´◓⌓◔)ᕗ⁾⁾

function getRandomColor() { var letters = ‘0123456789ABCDEF’.split(”); var color = ‘#’; for (var i = 0; i < 6; i++ ) { color += letters[Math.floor(Math.random() * 16)]; } return color; } // <![CDATA[ var colours=new Array("#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff"); // select colours to fade from background colour to worm colour var border="#000000"; // colour of border var speed=50; // speed of animation, lower is faster var worm=20; // length of worm var size=50; // size of blocks var swide, shigh; var blox=new Array(); var bloy=new Array(); var bloc=new Array(); var blok=new Array(); var blot=new Array(); var dx, dy; var x, y; var c=0; var pete=speed*worm/colours.length; var boddie; if (typeof('addRVLoadEvent')!='function') function addRVLoadEvent(funky) { var oldonload=window.onload; if (typeof(oldonload)!='function') window.onload=funky; else window.onload=function() { if (oldonload) oldonload(); funky(); } } addRVLoadEvent(blocky_the_worm); function blocky_the_worm() { if (document.getElementById) { var i, d, s; boddie=document.createElement("div");"fixed";"0px";"0px";"1px";"1px";"visible";"transparent"; document.body.appendChild(boddie); size+=4; set_width(); x=4+size*Math.floor(swide/(size*2)); y=4+size*Math.floor(shigh/(size*2)); for (i=0; i<worm; i++) { d=document.createElement("div");; s.width=(size-4)+"px"; s.height=(size-4)+"px"; s.overflow="hidden"; s.position="absolute"; s.border="1px solid "+border; blok[i]=s; boddie.appendChild(d); bloc[i]=-1; blox[i]=x; bloy[i]=y; blot[i]=false; } set_dir(true); setInterval('mainblock()', speed); }} function set_dir(hardth) { if (hardth) { dx=size*(Math.floor(3*Math.random())-1); dy=size*(Math.floor(3*Math.random())-1); } else { if (dx && dy) { if (Math.random()<0.5) dx=0; else dy=0; } else if (!dy) dy=(Math.random()<0.5)?size:-size; else dx=(Math.random()swide || y>shigh) { if (x>swide) dx=-size; if (y>shigh) dy=-size; } else while (x+dx>swide || x+dxshigh || y+dy<0 || Math.random()<.1 || (t=in_array(x+dx, y+dy, c))) set_dir(t); blok[c].left=(blox[c]=(x+=dx))+"px"; blok[c].top=(bloy[c]=(y+=dy))+"px"; bloc[c]=colours.length; if (blot[c]) clearTimeout(blot[c]); subblock(c); c=++c%worm; } function in_array(xtest, ytest, pos) { var i, j; for (i=pos-3; i<pos; i++) { j=(i-1) { blok[r].backgroundColor=getRandomColor(); blot[r]=setTimeout(“subblock(“+r+”)”, pete); } else blot[r]=false; } window.onresize=set_width; function set_width() { var sw_min=999999; var sh_min=999999; if (document.documentElement && document.documentElement.clientWidth) { if (document.documentElement.clientWidth>0) sw_min=document.documentElement.clientWidth; if (document.documentElement.clientHeight>0) sh_min=document.documentElement.clientHeight; } if (typeof(self.innerWidth)!=”undefined” && self.innerWidth) { if (self.innerWidth>0 && self.innerWidth0 && self.innerHeight0 && document.body.clientWidth0 && document.body.clientHeight


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