var x = new Array();
var y = new Array();
var dir = new Array();
var number_dir = new Array();
var x_pol = new Array();
var y_pol = new Array();
var a=new Array();
var b=new Array();
var life_a = new Array();
var life_b = new Array();
function sample(){
for (s = 1; s < 101; s++) {
a[s] = 1;
b[s] = 100;
life_a[s] = 100;
life_b[s] = 100;
}
hex();
var timer;
var delay = 100;
var loop = function () {
move();
game();
paint();
clearTimeout(timer);
timer = setTimeout(loop, delay);
}
loop();
}
function game(){
var c1=new Array();
var c2=new Array();
for(s=1;s<101;s++){
c1[s]=0;
c2[s]=0;
}
for(s=1;s<101;s++){
sx = a[s];
if(life_a[s]>50)c1[sx]=100;
sx = b[s];
if(life_b[s]>50)c2[sx]=100;
}
for(a1=1;a1<101;a1++){
h = 0;
if (c1[a1] > 50) h = h + 1;
ch = 0;
for (a2 = 1; a2 < number_dir[a1]+1 ;a2++ ){
a3=dir[a1][a2];
if(c2[a3]>50)ch=100;
}
if (ch > 50) h = h + 1;
if (h == 2) fight(a1);
}
}
function fight(a1){
mode = 1;
if (Math.random() > 0.5) mode = 2;
if (mode == 1) alpha(a1);
if (mode == 2) beta(a1);
}
function alpha(a1){
for(s=1;s<101;s++){
for (a2 = 1; a2 < number_dir[a1]+1 ;a2++ ){
a3=dir[a1][a2];
if (b[s] == a3) life_b[s] = 0;
}
}
}
function beta(a1){
for (s = 1; s < 101; s++) {
if (a[s] == a1) life_a[s] = 0;
}
}
function hex(){
for(t=1;t<101;t++){
dir[t] = new Array();
}
tx = 0;
for (s = 1; s < 6; s++) {
for (sx = 1; sx < 11; sx++) {
tx = tx + 1;
x[tx] = 2 * s - 1;
y[tx] = 2 * sx - 1;
}
}
for (s = 1; s < 6; s++) {
for (sx = 1; sx < 11; sx++) {
tx = tx + 1;
x[tx] = 2 * s ;
y[tx] = 2 * sx;
}
}
for(s=1;s<101;s++){
tx=0;
x1=x[s];
y1=y[s]+2;
sx = search(x1, y1);
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[s][tx] =sx;
x1=x[s];
y1=y[s]-2;
sx = search(x1, y1);
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[s][tx] =sx;
x1=x[s]+1;
y1=y[s]-1;
sx = search(x1, y1);
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[s][tx] =sx;
x1=x[s]+1;
y1=y[s]+1;
sx = search(x1, y1);
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[s][tx] =sx;
x1=x[s]-1;
y1=y[s]+1;
sx = search(x1, y1);
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[s][tx] =sx;
x1=x[s]-1;
y1=y[s]-1;
sx = search(x1, y1);
if (sx > 0) tx = tx + 1;
if (sx > 0) dir[s][tx] =sx;
number_dir[s] = tx;
}
}
function move(){
for (s = 1; s < 101; s++) {
sx = a[s];
var n1 = number_dir[sx] * Math.random();
var n2 = Math.floor(n1) + 1;
s1 = dir[sx][n2];
ch = 0;
for (sx = 1; sx < 101; sx++) {
h = 0;
if (life_b[sx] > 50) h = h + 1;
if (b[sx]==s1) h = h + 1;
if (h == 2) ch = 100;
}
if (ch < 50) a[s] = s1;
}
for (s = 1; s < 101; s++) {
sx = b[s];
var n1 = number_dir[sx] * Math.random();
var n2 = Math.floor(n1) + 1;
s1 = dir[sx][n2];
ch = 0;
for (sx = 1; sx < 101; sx++) {
h = 0;
if (life_a[sx] > 50) h = h + 1;
if (a[sx]==s1) h = h + 1;
if (h == 2) ch = 100;
}
if (ch < 50) b[s] = s1;
}
}
function tranx(le1,th1){
x1 = le1*Math.cos(th1 * Math.PI / 180);
return x1;
}
function trany(le1,th1){
y1 = le1*Math.sin(th1 * Math.PI / 180);
return y1;
}
function poly(col){
var str,data;
var s;
data="";
for(s=1;s<number_pol;s++){
data=data+x_pol[s]+","+y_pol[s]+",";
}
s=number_pol;
data=data+x_pol[s]+","+y_pol[s];
str="<polygon fill="+chopx(col)+" points="+chopx(data)+"/>";
return str;
}
function chopx(str){
var strx;
strx="\""+str+"\"";
return strx;
}
function paint(){
var sub=new svgpaint();
str="<svg width=\"700\" height=\"700\">";
h = trany(30,60);
for(s=1;s<101;s++){
for (sx = 1; sx < 7; sx++) {
y_pol[sx] = h * y[s] + trany(30, 60 * sx);
x_pol[sx] = 45 * x[s] + tranx(30, 60 * sx);
}
number_pol = 6;
str = str + poly("blue");
for (sx = 1; sx < 7; sx++) {
yy1 = h * y[s] + trany(30, 60 * sx);
xx1 = 45 * x[s] + tranx(30, 60 * sx);
yy2 = h * y[s] + trany(30, 60 * sx+60);
xx2 = 45 * x[s] + tranx(30, 60 * sx+60);
str = str + sub.line(xx1, yy1, xx2, yy2, 2, "black");
}
}
for(tx=1;tx<101;tx++){
s = a[tx];
for (sx = 1; sx < 7; sx++) {
y_pol[sx] = h * y[s] + trany(30, 60 * sx);
x_pol[sx] = 45 * x[s] + tranx(30, 60 * sx);
}
number_pol = 6;
if(life_a[tx]>50)str = str + poly("red");
}
for(tx=1;tx<101;tx++){
s = b[tx];
for (sx = 1; sx < 7; sx++) {
y_pol[sx] = h * y[s] + trany(30, 60 * sx);
x_pol[sx] = 45 * x[s] + tranx(30, 60 * sx);
}
number_pol = 6;
if(life_b[tx]>50)str = str + poly("yellow");
}
str = str + "</svg>";
$("#memo").html(str);
}
function search(x1,y1){
var s,sx,h;
sx=0;
for (s = 1; s < 101; s++) {
h = 0;
if (x[s] == x1) h = h + 1;
if (y[s] == y1) h = h + 1;
if (h == 2) sx = s;
}
return sx;
}
最終更新:2015年06月18日 10:43