Skip to content

Instantly share code, notes, and snippets.

@taishi41228
Last active September 10, 2016 14:19
Show Gist options
  • Save taishi41228/e661e39ba0fd31c4b0f3ab27cc971f28 to your computer and use it in GitHub Desktop.
Save taishi41228/e661e39ba0fd31c4b0f3ab27cc971f28 to your computer and use it in GitHub Desktop.
requirebin sketch
class Go {
constructor(numoflines){
this.ctx = document.getElementById('gocanv').getContext('2d');
this.numoflines = numoflines;
this.padding = 10;
this.go_pad = 20;
this.go_width = this.go_width = 500 - (this.padding + this.go_pad) * 2;
this.space = this.go_width / (this.numoflines - 1);
}
star(x, y){
}
draw(){
let ctx = this.ctx;
let padding = this.padding;
let go_pad = this.go_pad;
let go_width = this.go_width;
let numoflines = this.numoflines;
let space = this.space;
ctx.globalCompositeOperation = 'go';
ctx.fillStyle = fillcolor;
ctx.fillRect(padding, padding, 500 - padding * 2, 500 - padding * 2);
ctx.strokeRect(padding, padding, 500 - padding * 2, 500 - padding * 2);
ctx.lineWidth = 1.5;
ctx.beginPath();
for (var i = 0; i < numoflines; i++) {
ctx.moveTo(padding + go_pad + space * i, padding + go_pad);
ctx.lineTo(padding + go_pad + space * i, 500 - (padding + go_pad));
}
ctx.stroke();
ctx.beginPath();
for (var i = 0; i < numoflines; i++) {
ctx.moveTo(padding + go_pad, padding + go_pad + space * i);
ctx.lineTo(500 - (padding + go_pad), padding + go_pad + space * i);
}
ctx.stroke();
ctx.lineWidth = 1;
if (numoflines == 9) {
star(5, 5);
} else if (numoflines == 13) {
star(4, 4);
star(10, 4);
star(7, 7);
star(4, 10);
star(10, 10);
} else {
star(4, 4);
star(10, 4);
star(16, 4);
star(4, 10);
star(10, 10);
star(16, 10);
star(4, 16);
star(10, 16);
star(16, 16);
}
};
update(){
};
}
class Stone {
constructor(type, x, y){
this.type = type;
this.x = x;
this.y = y;
}
draw(ctx){
};
}
String.prototype.toIndexCase = function () {
var str = 'abcdefghijklmnopqrs';
var res = '';
res += (str.indexOf(this) + 1).toString();
return res;
}
/* 各種変数の宣言 */
var selfile = document.getElementById("selfile");
var file = null;
var reader = new FileReader(); /* ファイルを扱うオブジェクト */
var sgf = ''; /* ファイル内の全テキスト */
var fillcolor = '#E7C972';
var index = 0;
var movings = [];
var board = [
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
];
var checkboard = [
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
];
for (var i = 0; i < 21; i++) {
for (var j = 0; j < 21; j++) {
checkboard[i][j] = 0;
}
}
function parseCanvas(i) {
return padding + go_pad + space * i;
}
function star(i, j) {
ctx.globalCompositeOperation = 'go';
var x = parseCanvas(i - 1);
var y = parseCanvas(j - 1);
ctx.beginPath();
ctx.fillStyle = '#000000';
ctx.arc(x, y, space / 6, 0, Math.PI * 2.0, false);
ctx.fill();
}
function goMove(i, j) {
ctx.globalCompositeOperation = 'stones';
var x = parseCanvas(j - 1);
var y = parseCanvas(i - 1);
ctx.beginPath();
if (board[i][j] == 0) {
ctx.fillStyle = '#000000';
} else if (board[i][j] == 1) {
ctx.fillStyle = '#ffffff';
} else {
return;
}
ctx.arc(x, y, space / 2 - 0.5, 0, Math.PI * 2.0, false);
ctx.fill();
ctx.stroke();
}
function playtohtml(move) {
return '<td>' + move.bw + ' ' + move.x + ' ' + move.y + '</td>';
}
function checkDead(bw, x, y) {
if (checkboard[y][x] == 1) {
return 1;
}
checkboard[y][x] = 1;
if (board[y][x] == -1) {
return 0;
}
console.log(x + ' ' + y);
if (board[y][x] == bw) {
if (y > 1) {
if (checkDead(bw, x, y - 1) == 0) return 0;
}
if (x > 1) {
if (checkDead(bw, x - 1, y) == 0) return 0;
}
if (y < 19) {
if (checkDead(bw, x, y + 1) == 0) return 0;
}
if (x < 19) {
if (checkDead(bw, x + 1, y) == 0) return 0;
}
}
return 1;
}
function doRemoveStone(bw, x, y) {
if (board[y][x] == bw) {
board[y][x] = -1;
if (y > 1) {
doRemoveStone(bw, x, y - 1);
}
if (x > 1) {
doRemoveStone(bw, x - 1, y);
}
if (y < 19) {
doRemoveStone(bw, x, y + 1);
}
if (x < 19) {
doRemoveStone(bw, x + 1, y);
}
}
return 1;
}
function removeStones(bw, x, y) {
if (board[y]) {
if (board[y][x] == bw) {
return 0;
}
if (board[y][x] == -1) {
return 0;
}
} else {
return 0;
}
for (var i = 0; i < 21; i++) {
for (var j = 0; j < 21; j++) {
checkboard[i][j] = 0;
}
}
if (checkDead(board[y][x], x, y) == 1) {
return doRemoveStone(board[y][x], x, y);
}
return 0;
}
function clear() {
ctx.globalCompositeOperation = 'stones';
ctx.clearRect(0, 0, 500, 500);
}
function init() {
draw();
}
function draw() {
for (var i = 0; i < 21; i++) {
for (var j = 0; j < 21; j++) {
checkboard[i][j] = 0;
}
}
for (var i = 0; i < index; i++) {
board[movings[i].y][movings[i].x] = movings[i].bw;
}
for (var i = 1; i < 20; i++) {
for (var j = 1; j < 20; j++) {
goMove(i, j);
}
}
}
var go = new Go(9);
go.draw();
setTimeout(function(){
;class Go {
constructor(numoflines){
this.ctx = document.getElementById('gocanv').getContext('2d');
this.numoflines = numoflines;
this.padding = 10;
this.go_pad = 20;
this.go_width = this.go_width = 500 - (this.padding + this.go_pad) * 2;
this.space = this.go_width / (this.numoflines - 1);
}
star(x, y){
}
draw(){
let ctx = this.ctx;
let padding = this.padding;
let go_pad = this.go_pad;
let go_width = this.go_width;
let numoflines = this.numoflines;
let space = this.space;
ctx.globalCompositeOperation = 'go';
ctx.fillStyle = fillcolor;
ctx.fillRect(padding, padding, 500 - padding * 2, 500 - padding * 2);
ctx.strokeRect(padding, padding, 500 - padding * 2, 500 - padding * 2);
ctx.lineWidth = 1.5;
ctx.beginPath();
for (var i = 0; i < numoflines; i++) {
ctx.moveTo(padding + go_pad + space * i, padding + go_pad);
ctx.lineTo(padding + go_pad + space * i, 500 - (padding + go_pad));
}
ctx.stroke();
ctx.beginPath();
for (var i = 0; i < numoflines; i++) {
ctx.moveTo(padding + go_pad, padding + go_pad + space * i);
ctx.lineTo(500 - (padding + go_pad), padding + go_pad + space * i);
}
ctx.stroke();
ctx.lineWidth = 1;
if (numoflines == 9) {
star(5, 5);
} else if (numoflines == 13) {
star(4, 4);
star(10, 4);
star(7, 7);
star(4, 10);
star(10, 10);
} else {
star(4, 4);
star(10, 4);
star(16, 4);
star(4, 10);
star(10, 10);
star(16, 10);
star(4, 16);
star(10, 16);
star(16, 16);
}
};
update(){
};
}
class Stone {
constructor(type, x, y){
this.type = type;
this.x = x;
this.y = y;
}
draw(ctx){
};
}
String.prototype.toIndexCase = function () {
var str = 'abcdefghijklmnopqrs';
var res = '';
res += (str.indexOf(this) + 1).toString();
return res;
}
/* 各種変数の宣言 */
var selfile = document.getElementById("selfile");
var file = null;
var reader = new FileReader(); /* ファイルを扱うオブジェクト */
var sgf = ''; /* ファイル内の全テキスト */
var fillcolor = '#E7C972';
var index = 0;
var movings = [];
var board = [
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
];
var checkboard = [
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
];
for (var i = 0; i < 21; i++) {
for (var j = 0; j < 21; j++) {
checkboard[i][j] = 0;
}
}
function parseCanvas(i) {
return padding + go_pad + space * i;
}
function goMove(i, j) {
ctx.globalCompositeOperation = 'stones';
var x = parseCanvas(j - 1);
var y = parseCanvas(i - 1);
ctx.beginPath();
if (board[i][j] == 0) {
ctx.fillStyle = '#000000';
} else if (board[i][j] == 1) {
ctx.fillStyle = '#ffffff';
} else {
return;
}
ctx.arc(x, y, space / 2 - 0.5, 0, Math.PI * 2.0, false);
ctx.fill();
ctx.stroke();
}
function playtohtml(move) {
return '<td>' + move.bw + ' ' + move.x + ' ' + move.y + '</td>';
}
function checkDead(bw, x, y) {
if (checkboard[y][x] == 1) {
return 1;
}
checkboard[y][x] = 1;
if (board[y][x] == -1) {
return 0;
}
console.log(x + ' ' + y);
if (board[y][x] == bw) {
if (y > 1) {
if (checkDead(bw, x, y - 1) == 0) return 0;
}
if (x > 1) {
if (checkDead(bw, x - 1, y) == 0) return 0;
}
if (y < 19) {
if (checkDead(bw, x, y + 1) == 0) return 0;
}
if (x < 19) {
if (checkDead(bw, x + 1, y) == 0) return 0;
}
}
return 1;
}
function doRemoveStone(bw, x, y) {
if (board[y][x] == bw) {
board[y][x] = -1;
if (y > 1) {
doRemoveStone(bw, x, y - 1);
}
if (x > 1) {
doRemoveStone(bw, x - 1, y);
}
if (y < 19) {
doRemoveStone(bw, x, y + 1);
}
if (x < 19) {
doRemoveStone(bw, x + 1, y);
}
}
return 1;
}
function removeStones(bw, x, y) {
if (board[y]) {
if (board[y][x] == bw) {
return 0;
}
if (board[y][x] == -1) {
return 0;
}
} else {
return 0;
}
for (var i = 0; i < 21; i++) {
for (var j = 0; j < 21; j++) {
checkboard[i][j] = 0;
}
}
if (checkDead(board[y][x], x, y) == 1) {
return doRemoveStone(board[y][x], x, y);
}
return 0;
}
function clear() {
ctx.globalCompositeOperation = 'stones';
ctx.clearRect(0, 0, 500, 500);
}
function init() {
draw();
}
function draw() {
for (var i = 0; i < 21; i++) {
for (var j = 0; j < 21; j++) {
checkboard[i][j] = 0;
}
}
for (var i = 0; i < index; i++) {
board[movings[i].y][movings[i].x] = movings[i].bw;
}
for (var i = 1; i < 20; i++) {
for (var j = 1; j < 20; j++) {
goMove(i, j);
}
}
}
var go = new Go(9);
go.draw();
;}, 0)
{
"name": "requirebin-sketch",
"version": "1.0.0"
}
<!-- contents of this file will be placed inside the <body> -->
<canvas id="gocanv" width="500" height="500"></canvas>
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment