action script3.0五子棋

stop();
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.system.fscommand;
import flash.events.Event;

recommandText.visible = false;
Name1txt.border = true;
Name2txt.border = true;
Name1txt.visible = true;
Name2txt.visible =true;
baiqizi.visible = false;
heiqizi.visible = false;
baiwin.visible=false;
heiwin.visible=false;

var information: TextField = new TextField();
information.autoSize = “center”
var i, j: int; //存放下子坐标
var count: int = 0; //总棋子数
var runGame: Boolean = false; //表示游戏运行状态
var board: Array = new Array(); //初始化棋盘
for (var m: int = 1; m <= 15; m++)
{
board[m] = new Array();
for (var n: int = 1; n <= 15; n++)
{
board[m][n] = 0;
}
}

startButton.buttonMode = true;
endButton.buttonMode = true;

startButton.addEventListener(MouseEvent.CLICK, start);
endButton.addEventListener(MouseEvent.CLICK, quitGame);
clickArea.addEventListener(MouseEvent.CLICK, xiazi);

/清盘函数/

/返回游戏/

function quitGame(e: MouseEvent): void
{
fscommand(“quit”);
}

function start(e: MouseEvent)
{ //游戏开始
if (runGame == false)
{
runGame = true;
clickArea.buttonMode = true;

	showInformation();
	heiqizi.visible = true;
}
else
{
	
	
	recommandText.visible = true;
	
}

}

function showInformation(): void
{
information.defaultTextFormat = new TextFormat(“黑体”, 22, 0x000000);
information.width = 130;
information.height = 30;
information.x = 653.65
information.y = 70
information.border = true;
information.background = true;
information.backgroundColor = 0xffffff;

stage.addChild(information);

}

function end(e: Event)
{
if (runGame == false)
{
fscommand(“quit”);
}
else
{

	recommandText.visible = true;
	
}

}

/创建落子事件和动画/
function xiazi(e: Event)
{
for (i = 1; i <= 15; i++)
{ //定位函数,当前点击点为(i,j)
for (j = 1; j <= 15; j++)
{
if (stage.mouseX <= 55 + 30 * i &&
stage.mouseX >= 25 + 30 * i &&
stage.mouseY <= 71 + 30 * j &&
stage.mouseY >= 41 + 30 * j)
{
trace(“i” + ‘=’ + i, “j” + ‘=’ + j)
if (runGame)
{
var temp_black: heiqi = new heiqi();
var temp_white: baiqi = new baiqi();
temp_black.x = 33 + 30 * i;
temp_black.y = 45 + 29.5 * j;
temp_white.x = 33 + 30 * i;
temp_white.y = 45 + 29.5 * j;
if (count % 2 == 0 && board[i][j] == 0)
{
stage.addChildAt(temp_black, 1);
board[i][j] = 1;
count++;

					heiqizi.visible = false;
					baiqizi.visible = true;
					trace( runGame + '**' + i + ',' + j + '=' + board[i][j])
				}
				else if (count % 2 == 1 && board[i][j] == 0)
				{
					stage.addChildAt(temp_white, 1);
					count++;
					board[i][j] = 2;
					
					heiqizi.visible = true;
					baiqizi.visible = false;
					trace( runGame + '**' + i + ',' + j + '=' + board[i][j])
				}
				trace("当前棋子总数" + count);
				if (judge(board, i, j) == 1)
				{
					runGame = false;
					
					heiwin.visible=true;
				}
				else if (judge(board, i, j) == 2)
				{
					runGame = false;
					
					baiwin.visible=true;
				}
				else if (judge(board, i, j) == 3)
				{
					runGame = false;
					information.text = '和棋!'
					trace("和棋")
				}
			}
		}
	}
}

}

/创建判断游戏胜负函数/
function judge(temp: Array, i: int, j: int): int //返回0,1,2,3
{
var x, y: int;
var heqiFlag: Boolean = true;
/和棋/
for (x = 1; x <= 15; x++)
{
for (y = 1; y <= 15; y++)
{
if (temp[x][y] == 0)
heqiFlag = false;
}
}
if (heqiFlag)
{
return 3;
}
/遍历整列/
if (j <= 5)
{
for (y = 1; y <= j; y++)
{
if (board[i][y] == 1 &&
board[i][y + 1] == 1 &&
board[i][y + 2] == 1 &&
board[i][y + 3] == 1 &&
board[i][y + 4] == 1)
{
return 1;
}
if (board[i][y] == 2 &&
board[i][y + 1] == 2 &&
board[i][y + 2] == 2 &&
board[i][y + 3] == 2 &&
board[i][y + 4] == 2)
{
return 2;
}
}
}
else
{
for (y = j - 4; y <= 11; y++)
{
if (board[i][y] == 1 &&
board[i][y + 1] == 1 &&
board[i][y + 2] == 1 &&
board[i][y + 3] == 1 &&
board[i][y + 4] == 1)
{
return 1;
}
if (board[i][y] == 2 &&
board[i][y + 1] == 2 &&
board[i][y + 2] == 2 &&
board[i][y + 3] == 2 &&
board[i][y + 4] == 2)
{
return 2;
}
}
}

/*遍历整行*/
if (i <= 5)
{
	for (x = 1; x <= i; x++)
	{
		if (board[x][j] == 1 &&
			board[x + 1][j] == 1 &&
			board[x + 2][j] == 1 &&
			board[x + 3][j] == 1 &&
			board[x + 4][j] == 1)
		{
			return 1;
		}
		if (board[x][j] == 2 &&
			board[x + 1][j] == 2 &&
			board[x + 2][j] == 2 &&
			board[x + 3][j] == 2 &&
			board[x + 4][j] == 2)
		{
			return 2;
		}
	}
}
else
{
	for (x = i - 4; x <= 11; x++)
	{
		if (board[x][j] == 1 &&
			board[x + 1][j] == 1 &&
			board[x + 2][j] == 1 &&
			board[x + 3][j] == 1 &&
			board[x + 4][j] == 1)
		{
			return 1;
		}
		if (board[x][j] == 2 &&
			board[x + 1][j] == 2 &&
			board[x + 2][j] == 2 &&
			board[x + 3][j] == 2 &&
			board[x + 4][j] == 2)
		{
			return 2;
		}
	}
}

/*遍历整个主对角线*/
//上三角
if (i >= j)
{
	if (i - j <= 10) //去除右上角	
	{
		for (x = i - j + 1, y = 1; x <= 11; x++, y++)
		{
			if (board[x][y] == 1 &&
				board[x + 1][y + 1] == 1 &&
				board[x + 2][y + 2] == 1 &&
				board[x + 3][y + 3] == 1 &&
				board[x + 4][y + 4] == 1)
			{
				return 1;
			}
			if (board[x][y] == 2 &&
				board[x + 1][y + 1] == 2 &&
				board[x + 2][y + 2] == 2 &&
				board[x + 3][y + 3] == 2 &&
				board[x + 4][y + 4] == 2)
			{
				return 2;
			}
		}
	}
}
//下三角
if (i < j)
{
	if (j - i <= 10) //去除左下角	
	{
		for (y = 1 + j - i, x = 1; y <= 11; x++, y++)
		{
			if (board[x][y] == 1 &&
				board[x + 1][y + 1] == 1 &&
				board[x + 2][y + 2] == 1 &&
				board[x + 3][y + 3] == 1 &&
				board[x + 4][y + 4] == 1)
			{
				return 1;
			}
			if (board[x][y] == 2 &&
				board[x + 1][y + 1] == 2 &&
				board[x + 2][y + 2] == 2 &&
				board[x + 3][y + 3] == 2 &&
				board[x + 4][y + 4] == 2)
			{
				return 2;
			}
		}
	}
}

/*遍历次对角线*/
if (i + j >= 6 && i + j <= 26) //去除边角
{
	if (i + j <= 16)
	{ //上三角
		for (x = 1, y = i + j - 1; y >= 5; x++, y--)
		{
			trace(board[x][y],
				board[x + 1][y - 1],
				board[x + 2][y - 2],
				board[x + 3][y - 3],
				board[x + 4][y - 4])
			if (board[x][y] == 1 &&
				board[x + 1][y - 1] == 1 &&
				board[x + 2][y - 2] == 1 &&
				board[x + 3][y - 3] == 1 &&
				board[x + 4][y - 4] == 1)
			{
				return 1;
			}
			if (board[x][y] == 2 &&
				board[x + 1][y - 1] == 2 &&
				board[x + 2][y - 2] == 2 &&
				board[x + 3][y - 3] == 2 &&
				board[x + 4][y - 4] == 2)
			{
				return 2;
			}
		}
	}
	trace('\n')
	if (i + j > 16)
	{ //下三角
		for (x = i + j - 15, y = 15; x <= 11; x++, y--)
		{
			if (board[x][y] == 1 &&
				board[x + 1][y - 1] == 1 &&
				board[x + 2][y - 2] == 1 &&
				board[x + 3][y - 3] == 1 &&
				board[x + 4][y - 4] == 1)
			{
				return 1;
			}
			if (board[x][y] == 2 &&
				board[x + 1][y - 1] == 2 &&
				board[x + 2][y - 2] == 2 &&
				board[x + 3][y - 3] == 2 &&
				board[x + 4][y - 4] == 2)
			{
				return 2;
			}
		}
	}
}
return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值