//パズルクラス
//2002/06/20 22:12 by Tsukasa Jitoh
function TOZPuzzle()
{
this.infoFieldName = null;
this.komaFieldName = null;
this.boardFieldName = null;
this.map = null;
this.boards = null;
this.handCount = 0;
this.bornKoma = 0;
this.systemMode = 0;
this.waitCount = 0;
this.activeElement = null;
this.activeElementColor = "";
this.activeElementBgColor = "";
//static data
/*
this.displayChars = new Array(" ","巣","水","餌","山","谷");
this.displayChars[10] = "一";
this.displayChars[20] = "二";
this.displayChars[30] = "三";
this.displayChars[40] = "四";
this.displayChars[50] = "五";
this.displayChars[60] = "六";
this.displayChars[91] = "一";
this.displayChars[92] = "二";
this.displayChars[93] = "三";
this.displayChars[94] = "四";
this.displayChars[95] = "五";
this.displayChars[96] = "六";
*/
this.displayColors = new Array(
"white","#ddaa44","#0099FF","#009900","#996600","#996600");
this.displayColors[10] = "#CC00FF";
this.displayColors[20] = "orange";
this.displayColors[30] = "#CCFF00";
this.displayColors[40] = "#00FF33";
this.displayColors[50] = "#66FFFF";
this.displayColors[60] = "#55CCFF";
this.displayColors[91] = "rightgray";
this.displayColors[92] = "rightgray";
this.displayColors[93] = "rightgray";
this.displayColors[94] = "rightgray";
this.displayColors[95] = "rightgray";
this.displayColors[96] = "rightgray";
this.displayImages = new Array(
"./none.png","./nest.png","./water.png",
"./food.png","./mountain.png","./libber.png");
this.displayImages[10] = "./1.png";
this.displayImages[20] = "./2.png";
this.displayImages[30] = "./3.png";
this.displayImages[40] = "./4.png";
this.displayImages[50] = "./5.png";
this.displayImages[60] = "./6.png";
this.displayImages[91] = "./d1.png";
this.displayImages[92] = "./d2.png";
this.displayImages[93] = "./d3.png";
this.displayImages[94] = "./d4.png";
this.displayImages[95] = "./d5.png";
this.displayImages[96] = "./d6.png";
this.displayTitle = new Array("空き","巣","水","餌","山","谷");
this.displayTitle[10] = "竜1";
this.displayTitle[20] = "竜2";
this.displayTitle[30] = "竜3";
this.displayTitle[40] = "竜4";
this.displayTitle[50] = "竜5";
this.displayTitle[60] = "竜6";
this.displayTitle[91] = "ダーク竜1";
this.displayTitle[92] = "ダーク竜2";
this.displayTitle[93] = "ダーク竜3";
this.displayTitle[94] = "ダーク竜4";
this.displayTitle[95] = "ダーク竜5";
this.displayTitle[96] = "ダーク竜6";
//フィールド名設定
this.setDrawfieldName = TOZPuzzle_setDrawfieldName;
function TOZPuzzle_setDrawfieldName(infoFld, komaFld, boardFld)
{
this.infoFieldName = infoFld;
this.komaFieldName = komaFld;
this.boardFieldName = boardFld;
}
//マップデータをイニシャライズ
this.setMapData = TOZPuzzle_setMapData;
function TOZPuzzle_setMapData(table)
{
this.map = table;
this.handCount = 0;
this.bornKoma = 0;
this.systemMode = 0;
this.boards = new Array();
this.boards[this.handCount] = this.copyBoard(this.map.m);
}
//マップを現在のボードに複写
this.copyBoard = TOZPuzzle_copyBoard;
function TOZPuzzle_copyBoard(map)
{
var board = new Array();
for(var y = 0; y < map.length; y++)
{
board[y] = new Array();
for(var x = 0; x < map[y].length; x++)
board[y][x] = map[y][x];
}
return board;
}
//パズル全体の描画
this.draw = TOZPuzzle_draw;
function TOZPuzzle_draw(infoFld, komaFld, boardFld)
{
this.drawInfo(infoFld);
this.drawKoma(komaFld, this.displayChars, this.displayColors,
this.displayImages, this.displayTitle);
this.drawBoard(boardFld, this.displayChars, this.displayColors,
this.displayImages, this.displayTitle);
}
//パズル情報描画
this.drawInfo = TOZPuzzle_drawInfo;
function TOZPuzzle_drawInfo(infoFld)
{
if(!infoFld)
infoFld = this.infoFieldName;
var s = "
map情報: " + this.map.info + "
" +
"設問: " + this.map.setsumon + "
";
writeStr2Html(document, infoFld, s);
}
//駒情報描画
this.drawKoma = TOZPuzzle_drawKoma;
function TOZPuzzle_drawKoma(komaFld, displayChars, displayColors,
displayImages, displayTitle)
{
if(!komaFld)
komaFld = this.komaFieldName;
var s = "";
//完成判定
var board = this.boards[this.handCount];
var cntArr = new Array();
for(var i = 0; i < this.map.successPtn.length; i++)
cntArr[i] = 0;
for(var y = 0; y < board.length; y++)
{
for(var x = 0; x < board[y].length; x++)
{
for(var i = 0; i < this.map.successPtn.length; i++)
{
if(board[y][x] == this.map.successPtn[i][0])
cntArr[i]++;
}
}
}
var j = 0;
for(var i = 0; i < this.map.successPtn.length; i++)
{
if(cntArr[i] >= this.map.successPtn[i][1])
j++;
}
if(j == this.map.successPtn.length)
s += "
おめでとう! 完成です!!!";
else if(this.handCount >= this.map.kaito.length)
{
s += this.handCount + "手で駒がなくなりました。";
if(this.systemMode == 9)
s += "
残念、失敗しました。";
}
else
{
s += (this.handCount + 1) + "手目です。駒を置いてください!
持ち駒:";
for(var i = this.handCount; i < this.map.kaito.length; i++)
{
/* 文字からimageに変更
s += "
" +
displayChars[this.map.kaito[i][0]] + "";
*/
s += "
" +
"![" + displayTitle[this.map.kaito[i][0]] +
"](" + displayImages[this.map.kaito[i][0]] +
")
";
}
}
s += "
";
writeStr2Html(document, komaFld, s);
}
//棋譜情報描画
this.drawBoard = TOZPuzzle_drawBoard;
function TOZPuzzle_drawBoard(boardFld, displayChars, displayColors,
displayImages, displayTitle)
{
if(!boardFld)
boardFld = this.boardFieldName;
var s = "";
for(var y = 0; y < this.map.m.length; y++)
{
s += "\r\n";
for(var x = 0; x < this.map.m[y].length; x++)
{
/* 文字からimageに変更
s += "| " +
"" +
displayChars[this.boards[this.handCount][y][x]] +
" | ";
*/
s += "" +
"" +
"![" +
displayTitle[this.boards[this.handCount][y][x]] +
"](" +
displayImages[this.boards[this.handCount][y][x]] +
") | ";
}
s += "\r\n
\r\n";
}
writeStr2Html(document, boardFld, s);
}
//駒を置く
this.putKoma = TOZPuzzle_putKoma;
function TOZPuzzle_putKoma(y, x)
{
//駒がないと何も出来ない
if((this.systemMode == 0 || this.systemMode == 2) &&
this.handCount < this.map.kaito.length)
{
//再エントリをブロック
this.systemMode = 1;
//次のボードを作り、駒を置く
var hand = this.handCount;
this.boards[hand + 1] = this.copyBoard(this.boards[hand]);
var beforKoma = this.boards[hand + 1][y][x];
var newKoma = this.map.kaito[hand][0];
if(isBurstKoma(this.boards[hand + 1][y][x], true))
{
var i = this.boards[hand + 1][y][x];
if(newKoma == 1 && i == 1)
newKoma = 4;
else if((newKoma == 1 || newKoma > 2) && i > 2)
newKoma = 1;
this.boards[hand + 1][y][x] = newKoma;
}
//爆発
if(beforKoma > 2 && (newKoma == 1 || newKoma == 3))
{
if(y-1 >= 0)
{
if(isBurstKoma(this.boards[hand + 1][y-1][x]))
this.boards[hand + 1][y-1][x] = 1;
}
if(x-1 >= 0)
{
if(isBurstKoma(this.boards[hand + 1][y][x-1]))
this.boards[hand + 1][y][x-1] = 1;
}
if(x+1 < this.boards[hand + 1][y].length)
{
if(isBurstKoma(this.boards[hand + 1][y][x+1]))
this.boards[hand + 1][y][x+1] = 1;
}
if(y+1 < this.boards[hand + 1].length)
{
if(isBurstKoma(this.boards[hand + 1][y+1][x]))
this.boards[hand + 1][y+1][x] = 1;
}
}
//手を進める
this.handCount++;
//描画
this.draw();
//インターバル時間等リセット
this.waitCount = 0;
//判定描画へ
this.systemMode = 2;
}
//Burstできる駒か判定
function isBurstKoma(koma, centerFlg)
{
return !((koma == 0 && !centerFlg) ||
koma == 4 || (koma >= 91 && koma <= 96));
}
}
//判定描画
this.computeBoard = TOZPuzzle_computeBoard;
function TOZPuzzle_computeBoard()
{
this.systemMode++;
var flag = false;
var i;
var board = this.boards[this.handCount];
var newBoard = this.copyBoard(board);
var yLim = board.length;
//ダーク反転
for(var y = 0; y < yLim; y++)
{
var xLim = board[y].length;
for(var x = 0; x < xLim; x++)
{
i = board[y][x];
if(i >= 91 && i <= 96 && this.bornKoma == (i - 90) * 10)
{
flag = true;
newBoard[y][x] = (i - 90) * 10;
}
}
}
//通常駒成長
if(!flag)
{
for(var y = 0; y < yLim; y++)
{
var xLim = board[y].length;
for(var x = 0; x < xLim; x++)
{
if(board[y][x] == 1)
{
var data = new Array(0, 0, 0, 0);
var koma = 0;
//四方の駒を取得
if(y-1 >= 0)
data[0] = board[y-1][x];
if(x-1 >= 0)
data[1] = board[y][x-1];
if(x+1 < xLim)
data[2] = board[y][x+1];
if(y+1 < yLim)
data[3] = board[y+1][x];
//化けるかチェック
if(TOZPuzzle_getSameKoma(data, 50) > 1)
koma = 60;
else if((i = TOZPuzzle_getSameKoma(data, 40)) > 1)
koma = 50 + (i - 2) * 10;
else if((i = TOZPuzzle_getSameKoma(data, 30)) > 1)
koma = 40 + (i - 2) * 10;
else if((i = TOZPuzzle_getSameKoma(data, 20)) > 1)
koma = 30 + (i - 2) * 10;
else if((i = TOZPuzzle_getSameKoma(data, 10)) > 1)
koma = 20 + (i - 2) * 10;
else if(TOZPuzzle_getSameKoma(data, 2) > 0 &&
TOZPuzzle_getSameKoma(data, 3) > 0)
koma = 10;
//転写
if(koma > 1)
{
flag = true;
this.bornKoma = (koma > 60? 60 :koma);
newBoard[y][x] = this.bornKoma;
}
}
}
}
}
if(flag)
{
//ボード更新
this.boards[this.handCount] = newBoard;
this.draw();
this.systemMode--;
}
else
{
//手終了。モード戻す、またはゲームを終わらせる。
this.systemMode =
(this.handCount < this.map.kaito.length? 0: 9);
this.draw();
//再描画後に色が更新されない対策
window.resizeBy(0, -1);
window.resizeBy(0, 1);
}
//同じ駒の個数取得
function TOZPuzzle_getSameKoma(data, code)
{
var rc = 0;
for(var i = 0; i < 4; i++)
{
if(data[i] == code)
rc++
}
return rc;
}
}
//タイマー
this.timerEvent = TOZPuzzle_timerEvent;
function TOZPuzzle_timerEvent()
{
this.waitCount++;
//インターバル時間の10倍待つ
if(this.waitCount < 10)
return;
switch(this.systemMode)
{
case 2:
this.computeBoard();
break;
}
//インターバル時間をリセット
this.waitCount = 0;
}
//onmouseover event
this.overItem = TOZPuzzle_overItem;
function TOZPuzzle_overItem(elm, y, x)
{
if(this.systemMode != 0)
return;
var no = getAgentNo();
if(no == 1 || no == 2)
{
this.activeElement = elm;
this.activeElementBgColor = elm.style.backgroundColor;
this.activeElementColor = elm.style.color;
elm.style.backgroundColor = "lightgreen";
elm.style.color = "white";
}
}
//onmouseout event
this.blurItem = TOZPuzzle_blurItem;
function TOZPuzzle_blurItem(elm, y, x)
{
if(this.systemMode != 0 || this.activeElement != elm)
return;
var no = getAgentNo();
if(no == 1 || no == 2)
{
elm.style.backgroundColor = this.activeElementBgColor;
elm.style.color = this.activeElementColor;
}
}
}