⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 igogame.js

📁 《JavaScript王者归来》examples.rar
💻 JS
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************\
 *   IgoElf Javascript Parser Ver 1.00                                    *
 *   Author akira_cn@msn.com                                              *   
 *   Copyright (c) 2004 all rights reserved                               *
\**************************************************************************/

/**************************************************************************\
 *   Class Igo Game                                                       *
\**************************************************************************/

function IgoGame(steps, panel, startIdx)
{
	if (startIdx == null)
	{
		this.startIndex = 0;
	}
	else
	{
		this.startIndex = startIdx;
	}
	//public
	this.initBoard = IgoGame_Initialize;	   		//初始化棋盘
	this.moveForwards = IgoGame_MoveForwards;		//下一手
	this.moveBackwards = IgoGame_MoveBackwards;		//前一手
	this.nextComment = IgoGame_MoveNextComment;     //下一解说
	this.prevComment = IgoGame_MovePrevComment;     //前一解说
	this.goStep = IgoGame_GoStep;					//到指定步
	this.setComment = IgoGame_setComment;			//设置解说
	this.setLabel = IgoGame_setLabel;               //设置标记

	//private
	this.Steps = steps;								//棋谱序列
	this.panel = panel;								//显示棋谱的面板对象
	this.currentStep = 0;							//当前步数

	this.board = IgoGame_InitBoard(); 		        //当前步棋盘状态

	this.applyStep = IgoGame_ApplyStep;	             //绘制单步棋谱
	this.toStep = IgoGame_ParseStep;		         //解析棋谱文本
	this.removeStep = IgoGame_RemoveStep;	         //反绘制单步棋谱
	this.removeStone = IgoGame_RemoveStone;			 //提起棋子
	this.draw = IgoGame_DrawStep;                    //将当前棋谱绘制到面板对象
	this.testTake = IgoGame_TestTake;                //提子判断
	this.getStone = IgoGame_getBoardStone;           //获取棋子颜色
	this.canTake = IgoGame_CanTake;					 //判断是否能够提子
	this.take = IgoGame_Take;						 //提子
	this.putStone = IgoGame_PutStone;                //落子
	this.comment = null;							  //解说
	this.appendComment = IgoGame_AppendComment;       //添加注释
	this.takeSelf = false;                            //判断自杀
	this.label = null;                                //标记
	this.appendLabel = IgoGame_AppendLabel;			  //添加标记

	this.isShowLab = true;                            //设定是否显示文字标记(不包括添加黑白子)
	this.isTake = IgoGame_isTake;                     //判断是否提子
	this.stonesTake = null;                           //被提的棋子
}

function IgoGame_setLabel(label) //设置标记 格式:step,mark[xy];step,mark[xy];
{
	var index = 0;
	
	while(index != -1)
	{
		index = label.indexOf(';');
		if (index >= 0)
		{
			var aLabel = label.substring(0, index);
			var separator = aLabel.indexOf(',');
			if (separator >= 0)
			{
				var step = aLabel.substring(0, separator);
				var mark = aLabel.substring(separator+1, aLabel.length);
				this.appendLabel(new Label(step, IgoGame_getPosition(mark)));
			}
			label = label.substring(index+1, label.length);
		}
	}
}

function IgoGame_setComment(comm) //设置解说 格式:step,comment;step,comment;......
{
	var index = 0;
	
	while(index != -1)
	{
		index = comm.indexOf(';');
		if (index >= 0)
		{
			var aComment = comm.substring(0, index);
			var separator = aComment.indexOf(',');
			if (separator >= 0)
			{
				var step = aComment.substring(0, separator);
				var text = aComment.substring(separator+1, aComment.length);
				this.appendComment(new Comment(step, text));
			}
			comm = comm.substring(index+1, comm.length);
		}
	}
}

function IgoGame_AppendLabel(lab)  //添加标记
{
	if (this.label == null)
	{
		this.label = lab;
	}
	else
	{
		this.label.append(lab);
	}
}

function IgoGame_AppendComment(comm) //添加解说
{
	if (this.comment == null)
	{
		this.comment = comm;
	}
	else
	{
		this.comment.append(comm);
	}
}

function IgoGame_Initialize()   //初始化棋盘
{
	this.board = IgoGame_InitBoard();
	this.takeSelf = false;
	this.stonesTake = null;
	this.draw();
}

function IgoGame_MoveForwards() //下一步
{
	var st = this.toStep(++this.currentStep + this.startIndex);
	if (st != null)
	{
		this.board = this.applyStep(st)
		this.testTake(IgoGame_getPosition(st));
		this.draw();
	}
	else
	{
		--this.currentStep;
	}
}

function IgoGame_MoveNextComment() //下一解说
{
	var comm = this.comment;
	if (comm == null)
	{
		alert("本棋谱没有解说!");
		return;
	}
	else
	{
		while (comm != null)
		{
			var st = comm.step;
			if (st > this.currentStep)
			{
				this.goStep(st);
				break;
			}
			comm = comm.next;
		}
	}
}
function IgoGame_MovePrevComment() //前一解说
{
	var comm = this.comment;
	if (comm == null)
	{
		alert("本棋谱没有解说!");
		return;
	}
	else
	{
		while (comm != null)
		{
			var st = comm.step;
			if (st < this.currentStep && (comm.next == null || comm.next.step >= this.currentStep))
			{
				this.goStep(st);
				break;
			}
			comm = comm.next;
		}
	}
}
function IgoGame_GoStep(step) //到指定步数
{
	var i = 1;
	var st = this.toStep(i);
	this.initBoard();
	this.currentStep = step;

	if (step < 1)
	{
		self.status = self.defaultStatus;
		step = 0;
	}

	step = (step - 0) + (this.startIndex - 0);
	while(i <= step && st != null)
	{
		this.currentStep = i - 1 - this.startIndex;
		this.board = this.applyStep(st)
		this.testTake(IgoGame_getPosition(st));
		i ++;
		st = this.toStep(i);
	}
	this.currentStep = i - 1 - this.startIndex;
	this.draw();
}

function IgoGame_MoveBackwards() //上一步
{
	if (this.currentStep <= 0)
	{
		return;
	}
	if (this.isTake() != null)
	{
		var take = this.isTake();
		while(take != null)
		{
			this.board = this.putStone(take.pos);
			take = take.next;
		}
		if (this.stonesTake == this.isTake())
		{
			this.stonesTake = null;
		}
		else
		{
			take = this.stonesTake;
			while(take != null)
			{
				if (take.next == this.isTake())
				{
					take.next = null;
					break;
				}
				take = take.next;
			}
		}
		//this.goStep(--this.currentStep);
	}
	this.board = this.removeStep(this.toStep((this.currentStep - 0) + (this.startIndex - 0)));
	this.currentStep--;
	this.draw();
}

function IgoGame_isTake() //判断是否提子
{
	var take = this.stonesTake;
	
	while (take != null)
	{
		if (take.step >= this.currentStep)
		{
			break;
		}
		take = take.next;
	}
	return take;
}
function IgoGame_ParseStep(step) //解析棋谱
{
	if (step <= 0)
	{
		step = 0;
		self.status = self.defaultStatus;
		return;
	}
	
	var stepList = this.Steps;
	var index = 0;
	var st = 0;
	var stepString = null;

	while (index != -1)
	{
		index = stepList.indexOf(';');
		st ++;
		if (index >= 0 && st >= step)
		{
			stepString = stepList.substring(0, index);
			break;
		}
		stepList = stepList.substring(index+1, stepList.length);
	}
	if (step == this.currentStep && StatusText == StatusText1)
	{
		self.status = StatusText + ":" + stepString;
	}
	return stepString;
}	

function IgoGame_DrawStep() //将棋谱绘制到指定对象中去
{
	var tmp_board = this.board
	
	eval(this.panel).step.value = this.currentStep;
	
	var lab = this.label;

	while(lab != null) //显示标记
	{
		if (lab.step == (this.currentStep - 0) + (this.startIndex - 0) || lab.step < (this.currentStep - 0) + (this.startIndex - 0) && (lab.pos.stone == 'W' || lab.pos.stone == 'B'))
		{
			if (lab.pos.stone == 'W' || lab.pos.stone == 'B')
			{
				tmp_board = this.putStone(lab.pos);
			}
			if (this.isShowLab)
			{
				this.board = this.putStone(lab.pos);
			}
		}
		lab = lab.next;
	}

	if (this.currentStep > 0 && !this.takeSelf)
	{
		this.board =this.applyStep(this.toStep((this.currentStep - 0) + (this.startIndex - 0)), IgoGame_NewBlackStone, IgoGame_NewWhiteStone);
	}

	eval(this.panel).board.value = this.board;
		
	var comm = this.comment;
	eval(this.panel).comment.value = "";
	
	while(comm != null)   //显示解说
	{
		if (comm.step == (this.currentStep - 0) + (this.startIndex - 0))
		{
			eval(this.panel).comment.value = comm.text;
		}
		comm = comm.next;
	}
	
	this.board = tmp_board;
}

function IgoGame_ApplyStep(stepStr, blackToken, whiteToken) //解析单步并绘制棋谱
{
	if (blackToken == null)
	{
		blackToken = IgoGame_BlackStone;
	}
	if (whiteToken == null)
	{
		whiteToken = IgoGame_WhiteStone;
	}

	if (stepStr != null)
	{
		var pos = IgoGame_getPosition(stepStr);
		return this.putStone(pos, blackToken, whiteToken); 
	}
	else
	{
		return this.board;
	}
}

function IgoGame_PutStone(pos, blackToken, whiteToken)  //落子
{
	if (blackToken == null)
	{
		blackToken = IgoGame_BlackStone;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -