how to make neural logic to train the snake in the game
up vote
-4
down vote
favorite
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game=setInterval(game,1000/1);
}
gs=15;
grx=80;
gry=40;
tc=40;
b=1;
px=Math.floor(Math.random()*grx);
py=Math.floor(Math.random()*grx);
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
x1=x2=x3=x4=x5=x6=0;
w1=Math.random();
w2=Math.random();
w3=Math.random();
w4=Math.random();
w5=Math.random();
w6=Math.random();
xv=yv=0;
trail=;
tail = 5;
lastpress=0;
right=0;
left=0;
straight=0;
decision=0;
reward=0;
function moveai(decision)
{
switch (decision)
{
case 1:
if (lastpress!=39)
{
xv=-1;yv=0;
}
lastpress=37; //left
break;
case 2:
if (lastpress!=40)
{
xv=0;yv=-1;
}
lastpress=38; //up
break;
case 3:
if (lastpress!=37)
{
xv=1;yv=0;
}
lastpress=39; //right
break;
case 4:
if (lastpress!=38)
{
xv=0;yv=1;
}
lastpress=40; //down
break;
}
}
function directionoffood(direction)
{
right=left=straight=0;
switch (lastpress)
{
case 37:
if (py<ay)
{
left=1;
}
else if (py==ay && ax<px)
{
straight=1;
}
else
{
right=1;
}
break;
case 38:
if (px<ax)
{
right=1;
}
else if (px==ax && ay<py)
{
straight=1;
}
else
{
left=1;
}
break;
case 39:
if (py<ay)
{
right=1;
}
else if (py==ay && ax>px)
{
straight=1;
}
else
{
left=1;
}
break;
case 40:
if (px<ax)
{
left=1;
}
else if (px==ax && py<ay)
{
straight=1;
}
else
{
right=1;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function colission(direction)
{
straight=right=left=1;
switch (lastpress)
{
case 37:
if (px==0)
{
straight=0;
}
if (py==(gry-1))
{
left=0;
}
if (py==0)
{
right=0;
}
break;
case 38:
if (py==0)
{
straight=0;
}
if (px==(grx-1))
{
right=0;
}
if (px==0)
{
left=0;
}
break;
case 39:
if (px==(grx-1))
{
straight=0;
}
if (py==(gry-1))
{
right=0;
}
if (py==0)
{
left=0;
}
break;
case 40:
if (py==(gry-1))
{
straight=0;
}
if (px==0)
{
right=0;
}
if (px==(grx-1))
{
left=0;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function ai()
{
x1=directionoffood("straight");
x2=directionoffood("left");
x3=directionoffood("right");
x4=colission("straight");
x5=colission("left");
x6=colission("right");
add=(x1*w1)+(x2*w2)+(x3*w3)+(x4*w4)+(x5*w5)+(x6*w6)+b;
y = 1 / (1 + Math.exp(-add));
//console.log("right" + directionoffood(right));
//console.log("left" + directionoffood(left));
//console.log("straight" + directionoffood(straight));
//console.log("right" + colission(right));
//console.log("left" + colission(left));
//console.log("straight" + colission(straight));
console.log(reward);
}
function nextfood()
{
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
}
function game() {
diff=((px-ax)**2)+((py-ay)**2);
ai();
decision=0;
px+=xv;
py+=yv;
if(px<0) {
clearInterval(game);
}
if(px>grx-1) {
clearInterval(game);
}
if(py<0) {
clearInterval(game);
}
if(py>gry-1) {
clearInterval(game);
}
ctx.fillStyle="white";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) { //gameover
// clearInterval(game);
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) { //growth
tail++;
reward+=10;
nextfood();
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
if (((px-ax)**2)+((py-ay)**2)<diff)
{
reward++;
}
else
{
reward-=1.5;
}
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if (lastpress!=39 && 37)
{
lastpress=37;
xv=-1;yv=0;
}
break;
case 38:
if (lastpress!=40 && 38)
{
lastpress=38;
xv=0;yv=-1;
}
break;
case 39:
if (lastpress!=37 && 39)
{
lastpress=39;
xv=1;yv=0;
}
break;
case 40:
if (lastpress!=38 && 40)
{
lastpress=40;
xv=0;yv=1;
}
break;
}
}
<canvas style="border: 1px solid blue" id="gc" width="1200px" height="600px"></canvas>
So I have created a snake game(where snake eat food and grow bigger)
snippet attached of the full game...
Now I am trying to make the snake an AI so it can eat his food itself...
here's my network
now I have an answer to every question at the left side (in 1 or 0) and I know how to do the right part if I have the answers...
I even have a reward function which increases the reward if the step is taken towards the food and decrease it if the step is taken in opposite direction to food.
but I don't know how to do the second part????? <---- this is my question
for now i had created the sigmoid(x1.w1+x2.w2+x3.w3+x4.w4+x5.w5)
where x's are the answer of left side questions and w's are just random no between 1 and 0... the sigmoid gives me either a figure close to 1 or 0.... but what to do now??? plz help cause i am very new to machine learing...
for any other inquiry plz comment...
machine-learning artificial-intelligence
add a comment |
up vote
-4
down vote
favorite
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game=setInterval(game,1000/1);
}
gs=15;
grx=80;
gry=40;
tc=40;
b=1;
px=Math.floor(Math.random()*grx);
py=Math.floor(Math.random()*grx);
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
x1=x2=x3=x4=x5=x6=0;
w1=Math.random();
w2=Math.random();
w3=Math.random();
w4=Math.random();
w5=Math.random();
w6=Math.random();
xv=yv=0;
trail=;
tail = 5;
lastpress=0;
right=0;
left=0;
straight=0;
decision=0;
reward=0;
function moveai(decision)
{
switch (decision)
{
case 1:
if (lastpress!=39)
{
xv=-1;yv=0;
}
lastpress=37; //left
break;
case 2:
if (lastpress!=40)
{
xv=0;yv=-1;
}
lastpress=38; //up
break;
case 3:
if (lastpress!=37)
{
xv=1;yv=0;
}
lastpress=39; //right
break;
case 4:
if (lastpress!=38)
{
xv=0;yv=1;
}
lastpress=40; //down
break;
}
}
function directionoffood(direction)
{
right=left=straight=0;
switch (lastpress)
{
case 37:
if (py<ay)
{
left=1;
}
else if (py==ay && ax<px)
{
straight=1;
}
else
{
right=1;
}
break;
case 38:
if (px<ax)
{
right=1;
}
else if (px==ax && ay<py)
{
straight=1;
}
else
{
left=1;
}
break;
case 39:
if (py<ay)
{
right=1;
}
else if (py==ay && ax>px)
{
straight=1;
}
else
{
left=1;
}
break;
case 40:
if (px<ax)
{
left=1;
}
else if (px==ax && py<ay)
{
straight=1;
}
else
{
right=1;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function colission(direction)
{
straight=right=left=1;
switch (lastpress)
{
case 37:
if (px==0)
{
straight=0;
}
if (py==(gry-1))
{
left=0;
}
if (py==0)
{
right=0;
}
break;
case 38:
if (py==0)
{
straight=0;
}
if (px==(grx-1))
{
right=0;
}
if (px==0)
{
left=0;
}
break;
case 39:
if (px==(grx-1))
{
straight=0;
}
if (py==(gry-1))
{
right=0;
}
if (py==0)
{
left=0;
}
break;
case 40:
if (py==(gry-1))
{
straight=0;
}
if (px==0)
{
right=0;
}
if (px==(grx-1))
{
left=0;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function ai()
{
x1=directionoffood("straight");
x2=directionoffood("left");
x3=directionoffood("right");
x4=colission("straight");
x5=colission("left");
x6=colission("right");
add=(x1*w1)+(x2*w2)+(x3*w3)+(x4*w4)+(x5*w5)+(x6*w6)+b;
y = 1 / (1 + Math.exp(-add));
//console.log("right" + directionoffood(right));
//console.log("left" + directionoffood(left));
//console.log("straight" + directionoffood(straight));
//console.log("right" + colission(right));
//console.log("left" + colission(left));
//console.log("straight" + colission(straight));
console.log(reward);
}
function nextfood()
{
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
}
function game() {
diff=((px-ax)**2)+((py-ay)**2);
ai();
decision=0;
px+=xv;
py+=yv;
if(px<0) {
clearInterval(game);
}
if(px>grx-1) {
clearInterval(game);
}
if(py<0) {
clearInterval(game);
}
if(py>gry-1) {
clearInterval(game);
}
ctx.fillStyle="white";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) { //gameover
// clearInterval(game);
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) { //growth
tail++;
reward+=10;
nextfood();
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
if (((px-ax)**2)+((py-ay)**2)<diff)
{
reward++;
}
else
{
reward-=1.5;
}
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if (lastpress!=39 && 37)
{
lastpress=37;
xv=-1;yv=0;
}
break;
case 38:
if (lastpress!=40 && 38)
{
lastpress=38;
xv=0;yv=-1;
}
break;
case 39:
if (lastpress!=37 && 39)
{
lastpress=39;
xv=1;yv=0;
}
break;
case 40:
if (lastpress!=38 && 40)
{
lastpress=40;
xv=0;yv=1;
}
break;
}
}
<canvas style="border: 1px solid blue" id="gc" width="1200px" height="600px"></canvas>
So I have created a snake game(where snake eat food and grow bigger)
snippet attached of the full game...
Now I am trying to make the snake an AI so it can eat his food itself...
here's my network
now I have an answer to every question at the left side (in 1 or 0) and I know how to do the right part if I have the answers...
I even have a reward function which increases the reward if the step is taken towards the food and decrease it if the step is taken in opposite direction to food.
but I don't know how to do the second part????? <---- this is my question
for now i had created the sigmoid(x1.w1+x2.w2+x3.w3+x4.w4+x5.w5)
where x's are the answer of left side questions and w's are just random no between 1 and 0... the sigmoid gives me either a figure close to 1 or 0.... but what to do now??? plz help cause i am very new to machine learing...
for any other inquiry plz comment...
machine-learning artificial-intelligence
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.
– Prune
Nov 20 at 18:32
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game=setInterval(game,1000/1);
}
gs=15;
grx=80;
gry=40;
tc=40;
b=1;
px=Math.floor(Math.random()*grx);
py=Math.floor(Math.random()*grx);
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
x1=x2=x3=x4=x5=x6=0;
w1=Math.random();
w2=Math.random();
w3=Math.random();
w4=Math.random();
w5=Math.random();
w6=Math.random();
xv=yv=0;
trail=;
tail = 5;
lastpress=0;
right=0;
left=0;
straight=0;
decision=0;
reward=0;
function moveai(decision)
{
switch (decision)
{
case 1:
if (lastpress!=39)
{
xv=-1;yv=0;
}
lastpress=37; //left
break;
case 2:
if (lastpress!=40)
{
xv=0;yv=-1;
}
lastpress=38; //up
break;
case 3:
if (lastpress!=37)
{
xv=1;yv=0;
}
lastpress=39; //right
break;
case 4:
if (lastpress!=38)
{
xv=0;yv=1;
}
lastpress=40; //down
break;
}
}
function directionoffood(direction)
{
right=left=straight=0;
switch (lastpress)
{
case 37:
if (py<ay)
{
left=1;
}
else if (py==ay && ax<px)
{
straight=1;
}
else
{
right=1;
}
break;
case 38:
if (px<ax)
{
right=1;
}
else if (px==ax && ay<py)
{
straight=1;
}
else
{
left=1;
}
break;
case 39:
if (py<ay)
{
right=1;
}
else if (py==ay && ax>px)
{
straight=1;
}
else
{
left=1;
}
break;
case 40:
if (px<ax)
{
left=1;
}
else if (px==ax && py<ay)
{
straight=1;
}
else
{
right=1;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function colission(direction)
{
straight=right=left=1;
switch (lastpress)
{
case 37:
if (px==0)
{
straight=0;
}
if (py==(gry-1))
{
left=0;
}
if (py==0)
{
right=0;
}
break;
case 38:
if (py==0)
{
straight=0;
}
if (px==(grx-1))
{
right=0;
}
if (px==0)
{
left=0;
}
break;
case 39:
if (px==(grx-1))
{
straight=0;
}
if (py==(gry-1))
{
right=0;
}
if (py==0)
{
left=0;
}
break;
case 40:
if (py==(gry-1))
{
straight=0;
}
if (px==0)
{
right=0;
}
if (px==(grx-1))
{
left=0;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function ai()
{
x1=directionoffood("straight");
x2=directionoffood("left");
x3=directionoffood("right");
x4=colission("straight");
x5=colission("left");
x6=colission("right");
add=(x1*w1)+(x2*w2)+(x3*w3)+(x4*w4)+(x5*w5)+(x6*w6)+b;
y = 1 / (1 + Math.exp(-add));
//console.log("right" + directionoffood(right));
//console.log("left" + directionoffood(left));
//console.log("straight" + directionoffood(straight));
//console.log("right" + colission(right));
//console.log("left" + colission(left));
//console.log("straight" + colission(straight));
console.log(reward);
}
function nextfood()
{
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
}
function game() {
diff=((px-ax)**2)+((py-ay)**2);
ai();
decision=0;
px+=xv;
py+=yv;
if(px<0) {
clearInterval(game);
}
if(px>grx-1) {
clearInterval(game);
}
if(py<0) {
clearInterval(game);
}
if(py>gry-1) {
clearInterval(game);
}
ctx.fillStyle="white";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) { //gameover
// clearInterval(game);
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) { //growth
tail++;
reward+=10;
nextfood();
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
if (((px-ax)**2)+((py-ay)**2)<diff)
{
reward++;
}
else
{
reward-=1.5;
}
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if (lastpress!=39 && 37)
{
lastpress=37;
xv=-1;yv=0;
}
break;
case 38:
if (lastpress!=40 && 38)
{
lastpress=38;
xv=0;yv=-1;
}
break;
case 39:
if (lastpress!=37 && 39)
{
lastpress=39;
xv=1;yv=0;
}
break;
case 40:
if (lastpress!=38 && 40)
{
lastpress=40;
xv=0;yv=1;
}
break;
}
}
<canvas style="border: 1px solid blue" id="gc" width="1200px" height="600px"></canvas>
So I have created a snake game(where snake eat food and grow bigger)
snippet attached of the full game...
Now I am trying to make the snake an AI so it can eat his food itself...
here's my network
now I have an answer to every question at the left side (in 1 or 0) and I know how to do the right part if I have the answers...
I even have a reward function which increases the reward if the step is taken towards the food and decrease it if the step is taken in opposite direction to food.
but I don't know how to do the second part????? <---- this is my question
for now i had created the sigmoid(x1.w1+x2.w2+x3.w3+x4.w4+x5.w5)
where x's are the answer of left side questions and w's are just random no between 1 and 0... the sigmoid gives me either a figure close to 1 or 0.... but what to do now??? plz help cause i am very new to machine learing...
for any other inquiry plz comment...
machine-learning artificial-intelligence
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game=setInterval(game,1000/1);
}
gs=15;
grx=80;
gry=40;
tc=40;
b=1;
px=Math.floor(Math.random()*grx);
py=Math.floor(Math.random()*grx);
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
x1=x2=x3=x4=x5=x6=0;
w1=Math.random();
w2=Math.random();
w3=Math.random();
w4=Math.random();
w5=Math.random();
w6=Math.random();
xv=yv=0;
trail=;
tail = 5;
lastpress=0;
right=0;
left=0;
straight=0;
decision=0;
reward=0;
function moveai(decision)
{
switch (decision)
{
case 1:
if (lastpress!=39)
{
xv=-1;yv=0;
}
lastpress=37; //left
break;
case 2:
if (lastpress!=40)
{
xv=0;yv=-1;
}
lastpress=38; //up
break;
case 3:
if (lastpress!=37)
{
xv=1;yv=0;
}
lastpress=39; //right
break;
case 4:
if (lastpress!=38)
{
xv=0;yv=1;
}
lastpress=40; //down
break;
}
}
function directionoffood(direction)
{
right=left=straight=0;
switch (lastpress)
{
case 37:
if (py<ay)
{
left=1;
}
else if (py==ay && ax<px)
{
straight=1;
}
else
{
right=1;
}
break;
case 38:
if (px<ax)
{
right=1;
}
else if (px==ax && ay<py)
{
straight=1;
}
else
{
left=1;
}
break;
case 39:
if (py<ay)
{
right=1;
}
else if (py==ay && ax>px)
{
straight=1;
}
else
{
left=1;
}
break;
case 40:
if (px<ax)
{
left=1;
}
else if (px==ax && py<ay)
{
straight=1;
}
else
{
right=1;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function colission(direction)
{
straight=right=left=1;
switch (lastpress)
{
case 37:
if (px==0)
{
straight=0;
}
if (py==(gry-1))
{
left=0;
}
if (py==0)
{
right=0;
}
break;
case 38:
if (py==0)
{
straight=0;
}
if (px==(grx-1))
{
right=0;
}
if (px==0)
{
left=0;
}
break;
case 39:
if (px==(grx-1))
{
straight=0;
}
if (py==(gry-1))
{
right=0;
}
if (py==0)
{
left=0;
}
break;
case 40:
if (py==(gry-1))
{
straight=0;
}
if (px==0)
{
right=0;
}
if (px==(grx-1))
{
left=0;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function ai()
{
x1=directionoffood("straight");
x2=directionoffood("left");
x3=directionoffood("right");
x4=colission("straight");
x5=colission("left");
x6=colission("right");
add=(x1*w1)+(x2*w2)+(x3*w3)+(x4*w4)+(x5*w5)+(x6*w6)+b;
y = 1 / (1 + Math.exp(-add));
//console.log("right" + directionoffood(right));
//console.log("left" + directionoffood(left));
//console.log("straight" + directionoffood(straight));
//console.log("right" + colission(right));
//console.log("left" + colission(left));
//console.log("straight" + colission(straight));
console.log(reward);
}
function nextfood()
{
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
}
function game() {
diff=((px-ax)**2)+((py-ay)**2);
ai();
decision=0;
px+=xv;
py+=yv;
if(px<0) {
clearInterval(game);
}
if(px>grx-1) {
clearInterval(game);
}
if(py<0) {
clearInterval(game);
}
if(py>gry-1) {
clearInterval(game);
}
ctx.fillStyle="white";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) { //gameover
// clearInterval(game);
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) { //growth
tail++;
reward+=10;
nextfood();
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
if (((px-ax)**2)+((py-ay)**2)<diff)
{
reward++;
}
else
{
reward-=1.5;
}
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if (lastpress!=39 && 37)
{
lastpress=37;
xv=-1;yv=0;
}
break;
case 38:
if (lastpress!=40 && 38)
{
lastpress=38;
xv=0;yv=-1;
}
break;
case 39:
if (lastpress!=37 && 39)
{
lastpress=39;
xv=1;yv=0;
}
break;
case 40:
if (lastpress!=38 && 40)
{
lastpress=40;
xv=0;yv=1;
}
break;
}
}
<canvas style="border: 1px solid blue" id="gc" width="1200px" height="600px"></canvas>
So I have created a snake game(where snake eat food and grow bigger)
snippet attached of the full game...
Now I am trying to make the snake an AI so it can eat his food itself...
here's my network
now I have an answer to every question at the left side (in 1 or 0) and I know how to do the right part if I have the answers...
I even have a reward function which increases the reward if the step is taken towards the food and decrease it if the step is taken in opposite direction to food.
but I don't know how to do the second part????? <---- this is my question
for now i had created the sigmoid(x1.w1+x2.w2+x3.w3+x4.w4+x5.w5)
where x's are the answer of left side questions and w's are just random no between 1 and 0... the sigmoid gives me either a figure close to 1 or 0.... but what to do now??? plz help cause i am very new to machine learing...
for any other inquiry plz comment...
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game=setInterval(game,1000/1);
}
gs=15;
grx=80;
gry=40;
tc=40;
b=1;
px=Math.floor(Math.random()*grx);
py=Math.floor(Math.random()*grx);
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
x1=x2=x3=x4=x5=x6=0;
w1=Math.random();
w2=Math.random();
w3=Math.random();
w4=Math.random();
w5=Math.random();
w6=Math.random();
xv=yv=0;
trail=;
tail = 5;
lastpress=0;
right=0;
left=0;
straight=0;
decision=0;
reward=0;
function moveai(decision)
{
switch (decision)
{
case 1:
if (lastpress!=39)
{
xv=-1;yv=0;
}
lastpress=37; //left
break;
case 2:
if (lastpress!=40)
{
xv=0;yv=-1;
}
lastpress=38; //up
break;
case 3:
if (lastpress!=37)
{
xv=1;yv=0;
}
lastpress=39; //right
break;
case 4:
if (lastpress!=38)
{
xv=0;yv=1;
}
lastpress=40; //down
break;
}
}
function directionoffood(direction)
{
right=left=straight=0;
switch (lastpress)
{
case 37:
if (py<ay)
{
left=1;
}
else if (py==ay && ax<px)
{
straight=1;
}
else
{
right=1;
}
break;
case 38:
if (px<ax)
{
right=1;
}
else if (px==ax && ay<py)
{
straight=1;
}
else
{
left=1;
}
break;
case 39:
if (py<ay)
{
right=1;
}
else if (py==ay && ax>px)
{
straight=1;
}
else
{
left=1;
}
break;
case 40:
if (px<ax)
{
left=1;
}
else if (px==ax && py<ay)
{
straight=1;
}
else
{
right=1;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function colission(direction)
{
straight=right=left=1;
switch (lastpress)
{
case 37:
if (px==0)
{
straight=0;
}
if (py==(gry-1))
{
left=0;
}
if (py==0)
{
right=0;
}
break;
case 38:
if (py==0)
{
straight=0;
}
if (px==(grx-1))
{
right=0;
}
if (px==0)
{
left=0;
}
break;
case 39:
if (px==(grx-1))
{
straight=0;
}
if (py==(gry-1))
{
right=0;
}
if (py==0)
{
left=0;
}
break;
case 40:
if (py==(gry-1))
{
straight=0;
}
if (px==0)
{
right=0;
}
if (px==(grx-1))
{
left=0;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function ai()
{
x1=directionoffood("straight");
x2=directionoffood("left");
x3=directionoffood("right");
x4=colission("straight");
x5=colission("left");
x6=colission("right");
add=(x1*w1)+(x2*w2)+(x3*w3)+(x4*w4)+(x5*w5)+(x6*w6)+b;
y = 1 / (1 + Math.exp(-add));
//console.log("right" + directionoffood(right));
//console.log("left" + directionoffood(left));
//console.log("straight" + directionoffood(straight));
//console.log("right" + colission(right));
//console.log("left" + colission(left));
//console.log("straight" + colission(straight));
console.log(reward);
}
function nextfood()
{
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
}
function game() {
diff=((px-ax)**2)+((py-ay)**2);
ai();
decision=0;
px+=xv;
py+=yv;
if(px<0) {
clearInterval(game);
}
if(px>grx-1) {
clearInterval(game);
}
if(py<0) {
clearInterval(game);
}
if(py>gry-1) {
clearInterval(game);
}
ctx.fillStyle="white";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) { //gameover
// clearInterval(game);
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) { //growth
tail++;
reward+=10;
nextfood();
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
if (((px-ax)**2)+((py-ay)**2)<diff)
{
reward++;
}
else
{
reward-=1.5;
}
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if (lastpress!=39 && 37)
{
lastpress=37;
xv=-1;yv=0;
}
break;
case 38:
if (lastpress!=40 && 38)
{
lastpress=38;
xv=0;yv=-1;
}
break;
case 39:
if (lastpress!=37 && 39)
{
lastpress=39;
xv=1;yv=0;
}
break;
case 40:
if (lastpress!=38 && 40)
{
lastpress=40;
xv=0;yv=1;
}
break;
}
}
<canvas style="border: 1px solid blue" id="gc" width="1200px" height="600px"></canvas>
window.onload=function() {
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
game=setInterval(game,1000/1);
}
gs=15;
grx=80;
gry=40;
tc=40;
b=1;
px=Math.floor(Math.random()*grx);
py=Math.floor(Math.random()*grx);
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
x1=x2=x3=x4=x5=x6=0;
w1=Math.random();
w2=Math.random();
w3=Math.random();
w4=Math.random();
w5=Math.random();
w6=Math.random();
xv=yv=0;
trail=;
tail = 5;
lastpress=0;
right=0;
left=0;
straight=0;
decision=0;
reward=0;
function moveai(decision)
{
switch (decision)
{
case 1:
if (lastpress!=39)
{
xv=-1;yv=0;
}
lastpress=37; //left
break;
case 2:
if (lastpress!=40)
{
xv=0;yv=-1;
}
lastpress=38; //up
break;
case 3:
if (lastpress!=37)
{
xv=1;yv=0;
}
lastpress=39; //right
break;
case 4:
if (lastpress!=38)
{
xv=0;yv=1;
}
lastpress=40; //down
break;
}
}
function directionoffood(direction)
{
right=left=straight=0;
switch (lastpress)
{
case 37:
if (py<ay)
{
left=1;
}
else if (py==ay && ax<px)
{
straight=1;
}
else
{
right=1;
}
break;
case 38:
if (px<ax)
{
right=1;
}
else if (px==ax && ay<py)
{
straight=1;
}
else
{
left=1;
}
break;
case 39:
if (py<ay)
{
right=1;
}
else if (py==ay && ax>px)
{
straight=1;
}
else
{
left=1;
}
break;
case 40:
if (px<ax)
{
left=1;
}
else if (px==ax && py<ay)
{
straight=1;
}
else
{
right=1;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function colission(direction)
{
straight=right=left=1;
switch (lastpress)
{
case 37:
if (px==0)
{
straight=0;
}
if (py==(gry-1))
{
left=0;
}
if (py==0)
{
right=0;
}
break;
case 38:
if (py==0)
{
straight=0;
}
if (px==(grx-1))
{
right=0;
}
if (px==0)
{
left=0;
}
break;
case 39:
if (px==(grx-1))
{
straight=0;
}
if (py==(gry-1))
{
right=0;
}
if (py==0)
{
left=0;
}
break;
case 40:
if (py==(gry-1))
{
straight=0;
}
if (px==0)
{
right=0;
}
if (px==(grx-1))
{
left=0;
}
break;
}
switch (direction)
{
case "right":
return right;
break;
case "left":
return left;
break;
case "straight":
return straight;
break;
}
}
function ai()
{
x1=directionoffood("straight");
x2=directionoffood("left");
x3=directionoffood("right");
x4=colission("straight");
x5=colission("left");
x6=colission("right");
add=(x1*w1)+(x2*w2)+(x3*w3)+(x4*w4)+(x5*w5)+(x6*w6)+b;
y = 1 / (1 + Math.exp(-add));
//console.log("right" + directionoffood(right));
//console.log("left" + directionoffood(left));
//console.log("straight" + directionoffood(straight));
//console.log("right" + colission(right));
//console.log("left" + colission(left));
//console.log("straight" + colission(straight));
console.log(reward);
}
function nextfood()
{
ax=Math.floor(Math.random()*grx);
ay=Math.floor(Math.random()*gry);
}
function game() {
diff=((px-ax)**2)+((py-ay)**2);
ai();
decision=0;
px+=xv;
py+=yv;
if(px<0) {
clearInterval(game);
}
if(px>grx-1) {
clearInterval(game);
}
if(py<0) {
clearInterval(game);
}
if(py>gry-1) {
clearInterval(game);
}
ctx.fillStyle="white";
ctx.fillRect(0,0,canv.width,canv.height);
ctx.fillStyle="lime";
for(var i=0;i<trail.length;i++) {
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
if(trail[i].x==px && trail[i].y==py) { //gameover
// clearInterval(game);
}
}
trail.push({x:px,y:py});
while(trail.length>tail) {
trail.shift();
}
if(ax==px && ay==py) { //growth
tail++;
reward+=10;
nextfood();
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
if (((px-ax)**2)+((py-ay)**2)<diff)
{
reward++;
}
else
{
reward-=1.5;
}
}
function keyPush(evt) {
switch(evt.keyCode) {
case 37:
if (lastpress!=39 && 37)
{
lastpress=37;
xv=-1;yv=0;
}
break;
case 38:
if (lastpress!=40 && 38)
{
lastpress=38;
xv=0;yv=-1;
}
break;
case 39:
if (lastpress!=37 && 39)
{
lastpress=39;
xv=1;yv=0;
}
break;
case 40:
if (lastpress!=38 && 40)
{
lastpress=40;
xv=0;yv=1;
}
break;
}
}
<canvas style="border: 1px solid blue" id="gc" width="1200px" height="600px"></canvas>
machine-learning artificial-intelligence
machine-learning artificial-intelligence
asked Nov 20 at 7:45
user10633681
84
84
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.
– Prune
Nov 20 at 18:32
add a comment |
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.
– Prune
Nov 20 at 18:32
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.
– Prune
Nov 20 at 18:32
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.
– Prune
Nov 20 at 18:32
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388358%2fhow-to-make-neural-logic-to-train-the-snake-in-the-game%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.
– Prune
Nov 20 at 18:32