(window.onload = function() { var mouseX, mouseY; var s = 45; var t = 38; var c = 0; var count = 0; var XY = new Array( -1*s, -2*s*Math.sqrt(3), +1*s, -2*s*Math.sqrt(3), -2*s, -1*s*Math.sqrt(3), +0*s, -1*s*Math.sqrt(3), +2*s, -1*s*Math.sqrt(3), -3*s, +0*s*Math.sqrt(3), -1*s, +0*s*Math.sqrt(3), +1*s, +0*s*Math.sqrt(3), +3*s, +0*s*Math.sqrt(3), -2*s, +1*s*Math.sqrt(3), +0*s, +1*s*Math.sqrt(3), +2*s, +1*s*Math.sqrt(3), -1*s, +2*s*Math.sqrt(3), +1*s, +2*s*Math.sqrt(3)); var flag = new Array(0x04C8, 0x04C8, 0x04C8); draw(); function draw() { var canvas = document.getElementById('cvs'); if ( ! canvas || ! canvas.getContext ) return false; var ctx = canvas.getContext('2d'); eval('ctx.fillS'+'tyle = \'#FFFFFF\';'); ctx.fillRect(0, 0, 400, 400); for (var i=30; i!=390; i+=120) for (var j=0; j!=15; j++) if ( i*100+j == near(1-c) ) { matchstick(i, j, '#FFCC55', '#0000FF'); } else if ( Math.floor(flag[(i-30)/120]/Math.pow(2,j))%2 ) { matchstick(i, j, '#FFCC55', '#FF0000'); } var max = Math.max(Math.max(flag[0], flag[1]), flag[2]); var min = Math.min(Math.min(flag[0], flag[1]), flag[2]); var mid = flag[0] + flag[1] + flag[2] - max - min ; if ( (max == 0x04c8)*(mid == 0x04c8)*(min == 0x04c8) ) { document.getElementById('inner').innerHTML = 'マッチ棒を2本動かして正三角形を5個にせよ。'; count = 0; } else if ( (count == 2)*( (max == 0x08C8)*(mid == 0x04C8)*(min == 0x02C8) || (max == 0x04D0)*(mid == 0x04C8)*(min == 0x04C4) || (max == 0x08C8)*(mid == 0x06C8)*(min == 0x00C8) || (max == 0x0CC8)*(mid == 0x02C8)*(min == 0x00C8) || (max == 0x0CC8)*(mid == 0x04C8)*(min == 0x02C0) || (max == 0x0CC0)*(mid == 0x06C0)*(min == 0x04C8) || (max == 0x08C0)*(mid == 0x06C8)*(min == 0x04C8) || (max == 0x04D0)*(mid == 0x04CC)*(min == 0x04C0) || (max == 0x04D8)*(mid == 0x04C4)*(min == 0x04C0) || (max == 0x04CC)*(mid == 0x04C8)*(min == 0x00D0) || (max == 0x04C8)*(mid == 0x00D8)*(min == 0x00CC) || (max == 0x04D8)*(mid == 0x04C8)*(min == 0x00C4) ) ) { document.getElementById('inner').innerHTML = '更にマッチ棒を2本動かして正三角形を4個にせよ。'; } else if ( (count == 4)*( (max == 0x08C8)*(mid == 0x00E8)*(min == 0x00CA) || (max == 0x02C8)*(mid == 0x01C8)*(min == 0x00C9) || (max == 0x14C0)*(mid == 0x05C0)*(min == 0x04C4) || (max == 0x24C0)*(mid == 0x04E0)*(min == 0x04D0) ) ) { document.getElementById('inner').innerHTML = '更に更にマッチ棒を2本動かして正三角形を3個にせよ。'; } else if ( (count == 6)*( (max == 0x01C8)*(mid == 0x00E8)*(min == 0x00C3) || (max == 0x0258)*(mid == 0x018C)*(min == 0x00C9) || (max == 0x088C)*(mid == 0x00CA)*(min == 0x0078) || (max == 0x30C0)*(mid == 0x05C0)*(min == 0x04E0) || (max == 0x24C0)*(mid == 0x0C60)*(min == 0x0690) || (max == 0x14C0)*(mid == 0x0C44)*(min == 0x0780) ) ) { document.getElementById('inner').innerHTML = '更に更に更にマッチ棒を2本動かして正三角形を2個にせよ。'; } else if ( (count == 8)*( (max == 0x018C)*(mid == 0x00C3)*(min == 0x0078) || (max == 0x30C0)*(mid == 0x0C60)*(min == 0x0780) ) ) { document.getElementById('inner').innerHTML = '更に更に更に更にマッチ棒を2本動かしてというのは嘘です。'; } else if ( (c == 0)*(count%2 == 0) ) { document.getElementById('inner').innerHTML = '条件を満たせなかったようです。'; } else if ( (c == 0)*(count%2 == 1) ) { ; } function matchstick(i, j, c1, c2) { var r, a; ctx.lineWidth = 4.2; a = Math.atan(XY[j*2]/XY[j*2+1]); if ( XY[j*2+1] != 0 ) { r = XY[j*2+1]/Math.abs(XY[j*2+1])*Math.sqrt(XY[j*2]*XY[j*2]+XY[j*2+1]*XY[j*2+1]); } else { r = Math.sqrt(XY[j*2]*XY[j*2]+XY[j*2+1]*XY[j*2+1]); } eval('ctx.strokeS'+'tyle = \''+c1+'\';'); ctx.beginPath(); ctx.moveTo(200.5+r*Math.cos(a+i*Math.PI/180.0)+t*Math.cos((i-90)*Math.PI/180.0), 200.5-r*Math.sin(a+i*Math.PI/180.0)-t*Math.sin((i-90)*Math.PI/180.0)); ctx.lineTo(200.5+r*Math.cos(a+i*Math.PI/180.0)+t*Math.cos((i+90)*Math.PI/180.0), 200.5-r*Math.sin(a+i*Math.PI/180.0)-t*Math.sin((i+90)*Math.PI/180.0)); ctx.closePath(); ctx.stroke(); eval('ctx.fillS'+'tyle = \''+c2+'\';'); ctx.beginPath(); ctx.moveTo( 200.5+r*Math.cos(a+i*Math.PI/180.0)+(t-5)*Math.cos((i+87)*Math.PI/180.0), 200.5-r*Math.sin(a+i*Math.PI/180.0)-(t-5)*Math.sin((i+87)*Math.PI/180.0)); ctx.bezierCurveTo( 200.5+r*Math.cos(a+i*Math.PI/180.0)+(t+10)*Math.cos((i+80)*Math.PI/180.0), 200.5-r*Math.sin(a+i*Math.PI/180.0)-(t+10)*Math.sin((i+80)*Math.PI/180.0), 200.5+r*Math.cos(a+i*Math.PI/180.0)+(t+10)*Math.cos((i+100)*Math.PI/180.0), 200.5-r*Math.sin(a+i*Math.PI/180.0)-(t+10)*Math.sin((i+100)*Math.PI/180.0), 200.5+r*Math.cos(a+i*Math.PI/180.0)+(t-5)*Math.cos((i+93)*Math.PI/180.0), 200.5-r*Math.sin(a+i*Math.PI/180.0)-(t-5)*Math.sin((i+93)*Math.PI/180.0)); ctx.closePath(); ctx.fill(); } function near(f) { var r, a; var z = 160000; var m = 0; for (var i=30; i!=390; i+=120) for (var j=0; j!=15; j++) if ( Math.floor(flag[(i-30)/120]/Math.pow(2,j))%2 == f ) { a = Math.atan(XY[j*2]/XY[j*2+1]); if ( XY[j*2+1] != 0 ) { r = XY[j*2+1]/Math.abs(XY[j*2+1])*Math.sqrt(XY[j*2]*XY[j*2]+XY[j*2+1]*XY[j*2+1]); } else { r = Math.sqrt(XY[j*2]*XY[j*2]+XY[j*2+1]*XY[j*2+1]); } var x = mouseX; var y = mouseY; x = x - (200.5+r*Math.cos(a+i*Math.PI/180.0)); y = y - (200.5-r*Math.sin(a+i*Math.PI/180.0)); if ( Math.max(x*x+y*y,z) == z ) { z = x*x+y*y; m = i*100+j; } } return m; } canvas.onmousedown = mouseDownListner; function mouseDownListner(e) { adjustXY(e); c = 1; var n = near(c); if ( n != 0 ) { var i = Math.floor(n/100); var j = n%100; flag[(i-30)/120] -= Math.floor(Math.pow(2,j)); draw(); } } canvas.onmouseup = mouseUpListner; function mouseUpListner(e) { adjustXY(e); if ( c == 1 ) { c = 0; var n = near(c); if ( n != 0 ) { var i = Math.floor(n/100); var j = n%100; flag[(i-30)/120] += Math.floor(Math.pow(2,j)); count++; draw(); } } } canvas.onmousemove = mouseMoveListner; function mouseMoveListner(e) { adjustXY(e); draw(); } canvas.onmouseover = mouseOverListner; function mouseOverListner(e) { adjustXY(e) } canvas.onmouseout = mouseOutListner; function mouseOutListner(e) { adjustXY(e); if ( c == 1 ) { c = 0; var n = near(c); if ( n != 0 ) { var i = Math.floor(n/100); var j = n%100; flag[(i-30)/120] += Math.floor(Math.pow(2,j)); count++; draw(); } } } function adjustXY(e) { var rect = e.target.getBoundingClientRect(); mouseX = e.clientX - rect.left; mouseY = e.clientY - rect.top; } }})();