-
Ai coding
the following code is for AI of my game, the computer will select in a certain order number and if that number is taken the AI will check the next one so on. I made this so much complicated than it suppose to be but it works. Is there anyway to fix this and make it simpler.
Code:
if (contain("7",select)==1) {
myglobal.compsele.push("7");
select=select.removeValue(7);
cho7.mouseEnabled=false;
com8.x=319;
com8.y=492;
turn--;
trace(myglobal.compsele);
trace(select);
} else if (contain("7", select)==-1 && contain("6", select)==1 ) {
myglobal.compsele.push("6");
select=select.removeValue(6);
cho6.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=265;
com8.y=492;
turn--;
} else if (turn==6) {
com7.x=265;
com7.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==1 ) {
myglobal.compsele.push("8");
select=select.removeValue(8);
cho8.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=375;
com8.y=492;
turn--;
} else if (turn==6) {
com7.x=375;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=375;
com6.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==1 ) {
myglobal.compsele.push("5");
select=select.removeValue(5);
cho5.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=210;
com8.y=492;
turn--;
} else if (turn==6) {
com7.x=210;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=210;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=210;
com5.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==1) {
myglobal.compsele.push("9");
select=select.removeValue(9);
cho9.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=428;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=428;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=428;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=428;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=428;
com4.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==1) {
myglobal.compsele.push("4");
select=select.removeValue(4);
cho4.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=157;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=157;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=157;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=157;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=157;
com4.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==1) {
myglobal.compsele.push("10");
select=select.removeValue(10);
cho10.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=486;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=486;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=486;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=486;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=486;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=486;
com3.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==-1&&contain("3", select)==1) {
myglobal.compsele.push("3");
select=select.removeValue(3);
cho3.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=97;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=97;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=97;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=97;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=97;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=97;
com3.y=492;
turn--;
} else if (turn==1) {
com2.x=97;
com2.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==-1&&contain("3", select)==-1&&
contain("11", select)==1) {
myglobal.compsele.push("11");
select=select.removeValue(11);
cho11.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=545;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=545;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=545;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=545;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=545;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=545;
com3.y=492;
turn--;
} else if (turn==1) {
com2.x=545;
com2.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==-1&&contain("3", select)==-1&&
contain("11", select)==-1&&contain("2", select)==1) {
myglobal.compsele.push("2");
select=select.removeValue(2);
cho2.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=51;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=51;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=51;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=51;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=51;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=51;
com3.y=492;
turn--;
} else if (turn==1) {
com2.x=51;
com2.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==-1&&contain("3", select)==-1&&
contain("11", select)==-1&&contain("2", select)==-1
&&contain("12", select)==1) {
myglobal.compsele.push("12");
select=select.removeValue(12);
cho12.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=600;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=600;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=600;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=600;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=600;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=600;
com3.y=492;
turn--;
} else if (turn==1) {
com2.x=600;
com2.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==-1&&contain("3", select)==-1&&
contain("11", select)==-1&&contain("2", select)==-1
&&contain("12", select)==-1&&contain("1", select)==1) {
myglobal.compsele.push("1");
select=select.removeValue(1);
cho1.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=10;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=10;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=10;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=10;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=10;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=10;
com3.y=492;
turn--;
} else if (turn==1) {
com2.x=10;
com2.y=492;
turn--;
}
} else if (contain("7", select)==-1 && contain("6", select)==-1
&&contain("8", select)==-1 &&contain("5", select)==-1
&&contain("9", select)==-1&&contain("4", select)==-1
&&contain("10", select)==-1&&contain("3", select)==-1&&
contain("11", select)==-1&&contain("2", select)==-1
&&contain("12", select)==-1&&contain("1", select)==-1
&&contain("13", select)==1) {
myglobal.compsele.push("13");
select=select.removeValue(13);
cho13.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=656;
com8.y=492;
turn--;
}
if (turn==6) {
com7.x=656;
com7.y=492;
turn--;
} else if (turn==5) {
com6.x=656;
com6.y=492;
turn--;
} else if (turn==4) {
com5.x=656;
com5.y=492;
turn--;
} else if (turn==3) {
com4.x=656;
com4.y=492;
turn--;
} else if (turn==2) {
com3.x=656;
com3.y=492;
turn--;
} else if (turn==1) {
com2.x=656;
com2.y=492;
turn--;
}
} else {
myglobal.compsele.push("14");
select=select.removeValue(14);
cho14.mouseEnabled=false;
trace(myglobal.compsele);
trace(select);
if (turn==7) {
com8.x=717;
com8.y=503;
turn--;
}
if (turn==6) {
com7.x=717;
com7.y=503;
turn--;
} else if (turn==5) {
com6.x=717;
com6.y=503;
turn--;
} else if (turn==4) {
com5.x=717;
com5.y=503;
turn--;
} else if (turn==3) {
com4.x=717;
com4.y=503;
turn--;
} else if (turn==2) {
com3.x=717;
com3.y=503;
turn--;
} else if (turn==1) {
com2.x=717;
com2.y=503;
turn--;
}
}
-
Forget the code for the moment, and describe your problem in more detail.
There are several things in your code which are unexplained. What are:
contain,
select,
myglobal,
myglobal.compsele
the chos
turn
Why do you sometimes use Strings for your numbers and sometimes use numbers? What are the x and y values and how are they determined?
Basically, you just vomited up a big mess of ugly code and asked us to fix it without really telling us what it is you're trying to do.
I suspect that your answer will lie in judicious use of arrays instead of craploads of copy-pasted code.
-
contain-use to check if the string is in the array or not
select-the array that contain all the numbers
myglobal- the package that contain all global variable(useless now, all my code are in package now instead on frame)
myglobal.compsele- the number AI selected
the chos- mc of position where characters are going to be placed
turn- the game is turn based therefore it checks for turn