var x = new Array();
var y = new Array();
var pos = new Array();
var area = new Array();
var fam = new Array();
var life = new Array();
var dir = new Array();
var number_dir = new Array();
function sample(){
var s, sx, tx;
tx=0;
for(s=1;s<6;s++){
for(sx=1;sx<6;sx++){
tx=tx+1;
x[tx]=s;
y[tx]=sx;
}}
for(s=1;s<26;s++){
dir[s]=new Array();
}
for(s=1;s<26;s++){
fam[s]=100;
}
for(s=1;s<26;s++){
life[s]=new Array();
}
for(s=1;s<26;s++){
pos[s]=new Array();
}
for (s = 1; s < 26; s++) {
area[s] = s;
for (sx = 1; sx <6; sx++) {
pos[s][sx] = s;
life[s][sx] = 100;
}}
var t, s1, s2;
for(s=1;s<26;s++){
t=0;
for(s1=-1;s1<2;s1++){
for(s2=-1;s2<2;s2++){
tx=search(x[s]+s1,y[s]+s2);
if(tx==s)tx=0;
if(tx>0)t=t+1;
if(tx>0)dir[s][t]=tx;
}}
number_dir[s]=t;
}
var timer;
var delay = 1000;
var loop = function () {
for (s = 1; s < 26; s++) {
for (sx = 1; sx < 6; sx++) {
move(s, sx);
}
for (sx = 1; sx < 6; sx++) {
if(life[s][sx]>60)game(s, sx);
}
}
check();
paint();
clearTimeout(timer);
timer = setTimeout(loop, delay);
}
loop();
}
function game(s1,s2){
a1 = pos[s1][s2];
c1 = 0;
for (tx = 1; tx < 11; tx++) {
h = 0;
if (pos[s1][tx] == a1) h = h + 1;
if (life[s1][tx]>50) h = h + 1;
if (h == 2) c1 = c1 + 1;
}
c2 = 0;
for (t = 1; t < 26; t++) {
for (tx = 1; tx < 11; tx++) {
h = 0;
if (pos[t][tx] == a1) h = h + 1;
if (life[t][tx]>50) h = h + 1;
if (s1 == t) h = 0;
if (h == 2) c2 = c2 + 1;
}
}
h = 0;
if (c1 > 0) h = h + 1;
if (c2 > 0) h = h + 1;
if(h==2)fight(a1, s1, c1, c2);
}
function fight(a1,sx1,c1,c2){
m1=(c1 + c2) * Math.random();
m2 = 2;
if (m1 < c1) m2 = 1;
if (m2 == 1) alpha(a1,sx1);
if (m2 == 2) beta(a1,sx1);
}
function beta(a1, sx1) {
for (tx = 1; tx < 11; tx++) {
h = 0;
if (pos[sx1][tx] == a1) h = h + 1;
if (life[sx1][tx]>50) h = h + 1;
if (h == 2)life[sx1][tx]=0;
}
}
function alpha(a1,sx1){
var t, tx,h;
for (t = 1; t < 26; t++) {
for (tx = 1; tx < 11; tx++) {
h = 0;
if (pos[t][tx] == a1) h = h + 1;
if (life[t][tx]>50) h = h + 1;
if (sx1 == t) h = 0;
if (h == 2)life[t][tx]=0 ;
}
}
}
function search(x1,y1){
var s,sx;
sx=0;
for (s = 1; s < 26; s++) {
h = 0;
if (x[s] == x1) h = h + 1;
if (y[s] == y1) h = h + 1;
if (h == 2) sx = s;
}
return sx;
}
function paint(){
var sub=new svgpaint();
str="<svg width=\"500\" height=\"500\">";
for(t=1;t<26;t++){
str=str+sub.rect(100*x[t]-100,100*y[t]-100,100,100,"blue");
if(area[t]>0)str=str+sub.rect(100*x[t]-100,100*y[t]-100,100,100,"green");
if(area[t]>9)str=str+sub.rect(100*x[t]-100,100*y[t]-100,100,100,"pink");
}
str=str+"</svg>";
$("#memo").html(str);
}
function check(){
var s, sx, tx;
for (s = 1; s < 26; s++) {
for (sx = 1; sx < 6; sx++) {
tx = 0;
if (life[s][sx] > 50) tx = pos[s][sx];
if (life[s][sx] > 50)area[tx] = s;
}
}
}
function move(s1,n){
var t1 = pos[s1][n];
var n1 = number_dir[t1] * Math.random();
var n2 = Math.floor(n1) + 1;
pos[s1][n] = dir[t1][n2];
}
最終更新:2015年06月12日 14:26