📄 igogame.js
字号:
}
if (whiteToken == null)
{
whiteToken = IgoGame_WhiteStone;
}
var idx = IgoGame_getIndexer(pos);
if (pos.stone == "B")
{
return this.board.substring(0,idx) + blackToken + this.board.substring(idx+1, this.board.length);
}
else if (pos.stone == "W")
{
return this.board.substring(0,idx) + whiteToken + this.board.substring(idx+1, this.board.length);
}
else
{
return this.board.substring(0,idx) + pos.stone + this.board.substring(idx+1, this.board.length);
}
}
function IgoGame_RemoveStep(stepStr) //撤销当前走子
{
var pos = IgoGame_getPosition(stepStr);
return this.removeStone(pos);
}
function IgoGame_RemoveStone(pos) //提起棋子
{
var idx = IgoGame_getIndexer(pos);
if (pos.stone == "B" || pos.stone == "W")
{
return this.board.substring(0,idx) + IgoGame_InitBoard().charAt(idx) + this.board.substring(idx+1, this.board.length);
}
}
function IgoGame_getBoardStone(pos) //将当前位置的棋子设成和棋盘上的棋子颜色相同
{
if(pos != null)
{
var idx = IgoGame_getIndexer(pos);
var st = this.board.charAt(idx);
pos.stone = IgoGame_toStone(st);
}
return pos;
}
function IgoGame_TestTake(pos) //提子判断
{
var pos_above = this.getStone(pos.above());
var pos_nether = this.getStone(pos.nether());
var pos_left = this.getStone(pos.leftside());
var pos_right = this.getStone(pos.rightside());
var isTake = false;
if (pos_above != null && pos_above.stone != null && pos_above.stone != pos.stone)
{
if (this.canTake(pos_above))
{
this.take(pos_above);
isTake = true;
}
}
if (pos_nether != null && pos_nether.stone != null && pos_nether.stone != pos.stone)
{
if (this.canTake(pos_nether))
{
this.take(pos_nether);
isTake = true;
}
}
if (pos_left != null && pos_left.stone != null && pos_left.stone != pos.stone)
{
if (this.canTake(pos_left))
{
this.take(pos_left);
isTake = true;
}
}
if (pos_right != null && pos_right.stone != null && pos_right.stone != pos.stone)
{
if (this.canTake(pos_right))
{
this.take(pos_right);
isTake = true;
}
}
if (this.canTake(pos))
{
this.takeSelf = true;
this.take(pos);
isTake = true;
}
return this.isTake;
}
function IgoGame_Take(pos) //提子
{
var pos_left = this.getStone(pos.leftside());
var pos_right = this.getStone(pos.rightside());
var pos_above = this.getStone(pos.above());
var pos_nether = this.getStone(pos.nether());
this.board = this.removeStone(pos);
if (this.stonesTake == null)
{
this.stonesTake = new Label(this.currentStep, pos);
}
else
{
this.stonesTake.append(new Label(this.currentStep, pos));
}
if (pos_above != null && pos_above.stone == pos.stone)
{
this.take(pos_above);
}
if (pos_nether != null && pos_nether.stone == pos.stone)
{
this.take(pos_nether);
}
if (pos_left != null && pos_left.stone == pos.stone)
{
this.take(pos_left);
}
if (pos_right != null && pos_right.stone == pos.stone)
{
this.take(pos_right);
}
}
function IgoGame_CanTake(pos)
{
var canTake = true;
var pos_left = this.getStone(pos.leftside());
var pos_right = this.getStone(pos.rightside());
var pos_above = this.getStone(pos.above());
var pos_nether = this.getStone(pos.nether());
if (pos_left != null && pos_left.stone == null
|| pos_right != null && pos_right.stone == null
|| pos_above != null && pos_above.stone == null
|| pos_nether != null && pos_nether.stone == null)
{
return false;
}
if (canTake && pos_left != null && pos_left.stone == pos.stone)
{
this.board = this.putStone(pos.reverse());
canTake = this.canTake(pos_left);
this.board = this.putStone(pos.reverse());
}
if (canTake && pos_right != null && pos_right.stone == pos.stone)
{
this.board = this.putStone(pos.reverse());
canTake = this.canTake(pos_right);
this.board = this.putStone(pos.reverse());
}
if (canTake && pos_above != null && pos_above.stone == pos.stone)
{
this.board = this.putStone(pos.reverse());
canTake = this.canTake(pos_above);
this.board = this.putStone(pos.reverse());
}
if (canTake && pos_nether != null && pos_nether.stone == pos.stone)
{
this.board = this.putStone(pos.reverse());
canTake = this.canTake(pos_nether);
this.board = this.putStone(pos.reverse());
}
return canTake;
}
/***********************************************************************************************\
* Static Internal Functions —— Please do not use them outside of this file *
\***********************************************************************************************/
var IgoGame_BlackStone = "●";
var IgoGame_WhiteStone = "○";
var IgoGame_NewBlackStone = "◆";
var IgoGame_NewWhiteStone = "◇";
var IgoGame_None = null;
function IgoGame_InitBoard()
{
var _board = "19 ┌┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┬┐\n"+
"18 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"17 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"16 ├┼┼╋┼┼┼┼┼╋┼┼┼┼┼╋┼┼┤\n"+
"15 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"14 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"13 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"12 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"11 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
"10 ├┼┼╋┼┼┼┼┼╋┼┼┼┼┼╋┼┼┤\n"+
" 9 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 8 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 7 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 6 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 5 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 4 ├┼┼╋┼┼┼┼┼╋┼┼┼┼┼╋┼┼┤\n"+
" 3 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 2 ├┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┤\n"+
" 1 └┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┴┘\n"+
" A B C D E F G H J K L M N O P Q R S T";
return _board;
}
function IgoGame_toStone(chr) //将标记转换成相应的黑白子
{
if (chr == IgoGame_BlackStone || chr == IgoGame_NewBlackStone)
{
return 'B';
}
else if (chr == IgoGame_WhiteStone || chr==IgoGame_NewWhiteStone)
{
return 'W';
}
else
{
return null;
}
}
function IgoGame_isStone(chr) //判断当前标记是否是棋子
{
if (chr == IgoGame_BlackStone || chr == IgoGame_WhiteStone || chr == IgoGame_NewBlackStone || chr == IgoGame_NewWhiteStone)
{
return true;
}
else
{
return false;
}
}
function IgoGame_getPosition(stepStr) //将SGF指令转换为对应的棋盘状态
{
var pos = new BoardPosition(-1, -1, IgoGame_None);
var i = stepStr.indexOf('[');
if (i != -1)
{
pos.stone = stepStr.substring(0, i);
pos.X = (stepStr.charCodeAt(i+1) - "a".charCodeAt(0));
pos.Y = (stepStr.charCodeAt(i+2) - "a".charCodeAt(0));
}
return pos;
}
function IgoGame_getIndexer(pos) //从棋盘状态获得对应的棋盘字符串索引
{
return 3 + pos.X + 23 * pos.Y;
}
/**********************************************************************************************\
* Class BoardPosition *
\**********************************************************************************************/
function BoardPosition(x, y, st)
{
this.X = x;
this.Y = y;
this.stone = st;
this.above = BoardPosition_Above;
this.nether = BoardPosition_Nether;
this.leftside = BoardPosition_LeftSide;
this.rightside = BoardPosition_RightSide;
this.reverse = BoardPosition_Reverse;
}
var IgoGame_DefaultBoardSize = 19;
function BoardPosition_Reverse() //将棋子颜色取反
{
if (this.stone == 'W')
{
this.stone = 'B';
}
else if (this.stone == 'B')
{
this.stone = 'W';
}
return this;
}
function BoardPosition_Above() //获得上方相邻棋子的位置
{
if (this.Y > 0)
{
return new BoardPosition(this.X, this.Y - 1);
}
return null;
}
function BoardPosition_Nether() //获得下方相邻的棋子的位置
{
if (this.Y < IgoGame_DefaultBoardSize - 1)
{
return new BoardPosition(this.X, this.Y + 1);
}
return null;
}
function BoardPosition_LeftSide() //获得左面相邻的棋子的位置
{
if (this.X > 0)
{
return new BoardPosition(this.X - 1, this.Y);
}
return null;
}
function BoardPosition_RightSide() //获得右面相邻的棋子的位置
{
if (this.X < IgoGame_DefaultBoardSize - 1)
{
return new BoardPosition(this.X + 1, this.Y);
}
return null;
}
/*****************************************************************************\
* Class Comment *
\*****************************************************************************/
function Comment(_step, _text)
{
this.step = _step;
this.text = _text;
this.next = null;
this.append = Comment_Append;
}
function Comment_Append(comm)
{
var current = this;
while (current.next != null)
{
current = current.next;
}
current.next = comm;
}
/*****************************************************************************\
* Class Label *
\*****************************************************************************/
function Label(_step, _pos)
{
this.step = _step;
this.pos = _pos;
this.next = null;
this.append = Label_Append;
}
function Label_Append(lab)
{
var current = this;
while (current.next != null)
{
current = current.next;
}
current.next = lab;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -