var PIECE_MOVES = '7677,3433,2627,9493,2526,8822+,8879,2231,7788,3322,3839,9594,6859,2282,651B,742B\ ,4365+,5261,7576,4352,7475,7473,7869,5443,4647,6251,4738,4222,6677,5241,5849,6463\ ,5657,7262,5766,6564,3637,822B,1819,1413,1617,7382,7968,6271,2628,6362,4858,8473\ ,2826,7381,5848,7584,3729,6475,2928,3534,3536,6665,4537,4433,2425,4544,4546,6766+\ ,6758,752N,6667,3764+,481S,462P,4647,3837,2629,6573,2324+,5765+,5748,252P,2526,4738\ ,2725,3647,3223,2736,4232,6252,511B,192R,8879,1819+,851N,4927,6251+,6272,731G\ '; var piece_moves = PIECE_MOVES.split(','); var WHITE_BINDS = 'K51,R82,B22,G61,G41,S71,S31,N81,N21,L91,L11,P93,P83,P73,P63,P53,P43,P33,P23,P13'; var BLACK_BINDS = 'K59,R28,B88,G69,G49,S79,S39,N89,N29,L99,L19,P97,P87,P77,P67,P57,P47,P37,P27,P17'; var WHITE_NAME = '後手'; var BLACK_NAME = '先手'; var TRAY_COLS = 4; var TRAY_ROWS = 6; var PICT_HTTP = 'http://owa.as.wakwak.ne.jp/zope/shogi/ZJavaScript/pict/'; var TRAY_W = 108; var TRAY_H = 128; var PIECE_TYPES = 'KRBGSNLP'; var PIECE_NAMES = '玉,飛,角,金,銀,桂,香,歩,,龍,馬,,成銀,成桂,成香,と'; var COLS_CHAR = '123456789'; var ROWS_CHAR = '一二三四五六七八九'; var white_binds = WHITE_BINDS.split(','); var black_binds = BLACK_BINDS.split(','); var piece_names = PIECE_NAMES.split(','); var current_max = piece_moves.length; var current_pos = 0; var auto_running = false; var board81 = new Array(81); var white_hand = new Array(9); var black_hand = new Array(9); ////////////////////////////////////////////////////// // for Ajax var scorelist = 'void'; var xmlhttp = false; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); xmlhttp.overrideMimeType('text/plain'); } else { try { xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { xmlhttp = false; } } } function queryScore() { var game = document.selectGame.elements[0].value; var uri = game + '/pubGetScore'; xmlhttp.open('GET', uri, true); xmlhttp.onreadystatechange = updateDiag; xmlhttp.send(null); } function updateDiag() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { scorelist = xmlhttp.responseText; piece_moves = scorelist.split(','); current_max = piece_moves.length; if (current_pos > current_max) current_pos = current_max; createDiag(); } else { alert('updateDiag: http status = ' + xmlhttp.status); } } } ////////////////////////////////////////////////////// // side tray image function getPieceString(bw, id, pc, x, y) { s = ''; return s; } function getRankString(bw, obj, len, yo) { var xo = (len > 4)? 0: (TRAY_W - 27 * len) / 2; var xd = (len > 4)? (TRAY_W - 27) / (len - 1): 27; str = '' while (len > 0) { while (obj.cnt <= 0) { ++obj.pid; if (obj.pid >= 8) break; obj.cnt = obj.hand[obj.pid]; } if (obj.pid >= 8) break; str += getPieceString(bw, len, obj.getc(), xo, yo); xo += xd; --len; --obj.cnt; } return str; } function getHandString(bw, hand, rank, len) { var obj = new Object(); obj.hand = hand; obj.pid = 0; obj.cnt = obj.hand[0]; obj.getc = function() {return PIECE_TYPES.charAt(this.pid);} var str = ''; var y = (TRAY_H - 31 * len) / 2; for (var i = 0; i < len; i++) { str += getRankString(bw, obj, rank[i], y); y += 31; } return str; } function showHandTray(bw) { var hand = bw? black_hand: white_hand; var rank = new Array(5); var cnt = 0; var n1 = hand[0]; for (var i = 1; i < 8; i += 2) { var n2 = hand[i] + hand[i + 1]; if (n1 == 0) n1 = n2; else if (n1 + n2 < 4) n1 += n2; else { rank[cnt++] = n1; n1 = n2; } } if (n1 > 0) { while ((n1 > 6) && (cnt < 3)) { var d = Math.floor(n1 / (6 - cnt)); if (d < 6) d = 6; rank[cnt++] = d; n1 -= d; } rank[cnt++] = n1; } var s = getHandString(bw, hand, rank, cnt); if (bw) document.getElementById("blackSideTray").innerHTML = s; else document.getElementById("whiteSideTray").innerHTML = s; } ////////////////////////////////////////////////////// // General function str2pos(s, n) { col = parseInt(s.charAt(n + 0)); row = parseInt(s.charAt(n + 1)); return (col - 1) + (row - 1) * 9; } function showCellPieces(x, y, s) { bw = s.charAt(0); cp = s.charAt(1); tags = document.getElementById('shogiBoard'); rank = tags.getElementsByTagName('tr').item(y); cell = rank.getElementsByTagName('td').item(x); if (bw == 'W' || bw == 'B') pict = 'url(' + PICT_HTTP + s + ')'; else pict = 'none'; cell.style.backgroundImage = pict; cell.style.backgroundColor = "transparent"; } function showHandPieces(bw) { var side = bw? 'B': 'W'; var hand = bw? black_hand: white_hand; var tags = document.getElementById(bw? 'blackHand': 'whiteHand'); for (var y = 0; y < TRAY_ROWS; y++) { for (var x = 0; x < TRAY_COLS; x++) { rank = tags.getElementsByTagName('tr').item(y); cell = rank.getElementsByTagName('td').item(x); cell.style.backgroundImage = 'none'; } } var cnt = 0; for (var n in hand) { s = hand[n]; c = PIECE_TYPES.charAt(n); for (var i = 0; i < s; i++) { y = Math.floor(cnt / TRAY_COLS); x = cnt % TRAY_COLS; pict = 'url(' + PICT_HTTP + side + c + ')'; rank = tags.getElementsByTagName('tr').item(y); cell = rank.getElementsByTagName('td').item(x); cell.style.backgroundImage = pict; ++cnt; } } } function initDiag() { for (var i = 0; i < 81; i++) board81[i] = ''; for (var i in white_binds) { s = white_binds[i]; c = s.charAt(0); n = str2pos(s, 1); board81[n] = 'W' + c; } for (var i in black_binds) { s = black_binds[i]; c = s.charAt(0); n = str2pos(s, 1); board81[n] = 'B' + c; } for (var i = 0; i < 9; i++) { white_hand[i] = 0; black_hand[i] = 0; } } function createDiag() { initDiag(); for (var i = 0; i < current_pos; i++) { s = piece_moves[i]; dst = str2pos(s, 0); c = s.charAt(3); n = PIECE_TYPES.indexOf(c); if (n >= 0) { switch (s.charAt(2)) { case '1': --black_hand[n]; board81[dst] = 'B' + c; break; case '2': --white_hand[n]; board81[dst] = 'W' + c; break; } } else { src = str2pos(s, 2); cap = board81[dst]; if (cap) { c = cap.charAt(0); n = PIECE_TYPES.indexOf(cap.charAt(1)); if (c == 'W') ++black_hand[n]; else if (c == 'B') ++white_hand[n]; } c = board81[src]; if (s.length > 4 && s.charAt(4) == '+') c += 'x'; board81[dst] = c; board81[src] = ''; } } for (var y = 0; y < 9; y++) { for (var x = 0; x < 9; x++) showCellPieces(x, y, board81[(8 - x) + y * 9]); } //showHandPieces(0); //showHandPieces(1); showHandTray(0); showHandTray(1); showScoreInfo(); showButton(); } function showScoreInfo() { d = '【' if (current_pos <= 0) d += '開始局面'; else { d += current_pos; d += '手目 ' d += (current_pos % 2)? '先手▲':'後手△'; mov = piece_moves[current_pos - 1]; col = mov.charAt(0); row = mov.charAt(1); d += COLS_CHAR.charAt(col - 1); d += ROWS_CHAR.charAt(row - 1); dst = str2pos(mov, 0); s = board81[dst]; c = PIECE_TYPES.indexOf(s.charAt(1)); if (mov.length > 4) d += piece_names[c] + '成'; else if (PIECE_TYPES.indexOf(mov.charAt(3)) >= 0) d += piece_names[c] + '打'; else if (s.length > 2) d += piece_names[c + 8]; else d += piece_names[c]; } d += '】'; e = document.getElementById("scoreInfo"); e.innerHTML = d; } ////////////////////////////////////////////////////// // event function gotoHead() { current_pos = 0; createDiag(); } function goBack10() { current_pos = (current_pos <= 10)? 0: current_pos - 10; createDiag(); } function gotoPrev() { current_pos = (current_pos <= 0)? 0: current_pos - 1; createDiag(); } function gotoNext() { if (++current_pos > current_max) { current_pos = current_max; if (auto_running) playToggle(); // stop } createDiag(); } function goFore10() { current_pos += 10; if (current_pos > current_max) current_pos = current_max; createDiag(); } function gotoTail() { current_pos = current_max; createDiag(); } function loadDiag() { location.href = location.href; } function playToggle() { if (auto_running) { clearInterval(IntervalID); auto_running = false; } else { IntervalID = setInterval('gotoNext()', 1000); auto_running = true; } createDiag(); } function showButton() { if (auto_running == true) { document.cpanel2.butt_auto.value="再生停止"; document.cpanel1.butt_head.disabled=true; document.cpanel1.butt_back.disabled=true; document.cpanel1.butt_prev.disabled=true; document.cpanel1.butt_next.disabled=true; document.cpanel1.butt_fore.disabled=true; document.cpanel1.butt_tail.disabled=true; document.cpanel2.butt_load.disabled=true; } else { document.cpanel2.butt_auto.value = "自動再生"; document.cpanel2.butt_load.disabled=false; document.cpanel1.butt_head.disabled = (current_pos <= 0)? true: false; document.cpanel1.butt_prev.disabled = (current_pos <= 0)? true: false; document.cpanel1.butt_back.disabled = (current_pos <= 0)? true: false; document.cpanel1.butt_fore.disabled = (current_pos >= current_max)? true: false; document.cpanel1.butt_next.disabled = (current_pos >= current_max)? true: false; document.cpanel1.butt_tail.disabled = (current_pos >= current_max)? true: false; } }