一款强大的人机五子棋,代码全开放
部分代码:—————— <script type="text/javascript">
var divCanvas = document.getElementById("divCanvas");
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var locations = new Array(15); //保存棋盘各点状态 [0空,1白子,2黑子]
var imgComputer = new Image();
imgComputer.src = "../images/white.png"; //黑棋子 -玩家
var imgPlayer = new Image();
imgPlayer.src = "../images/black.png"; //白棋子 --电脑
var pointX, pointY; //落子X,Y坐标
var isComputerRunning = false; //电脑是否正在下
canvas.addEventListener("click", OnClick, false);
var locationX, locationY; //落子所在数组下标
var directionLRCount = 0, directionUDCount = 0, directionLURDCount = 0, directionLDRUCount = 0; //分别 表示左右\上下\左上右下\左下右上方向棋子数
var count = 0; //某方向上棋子数
var computer = 2; //电脑值 用于存储在locations中
var player = 1; //玩家值 用于存储在locations中
//以下属性用于计算电脑落子处
var maxCount = 0; //最大棋子数
var directionLRPointXL, directionLRPointY, directionLRPointXR; //左右方向最左坐标,最右坐标
var directionUDPointX, directionUDPointYU, directionUDPointYD; //上下方向最上面坐标,最下坐标
var directionLURDPointXL, directionLURDPointYU, directionLURDPointXR, directionLURDPointYD; //左上右下方向左上坐标右下坐标
var directionLDRUPointXL, directionLDRUPointYD, directionLDRUPointXR, directionLDRUPointYU; //左下右上方向
var horizontalLineNum = 15, verticalLineNum = 15; //横线数量,纵线数量
//玩家落子
function OnClick(evt) {
if (isComputerRunning) {
alert("请稍等,电脑正在想怎么下!");
return;
}
locationX = parseInt((evt.layerX - 9) / 40);
locationY = parseInt((evt.layerY - 9) / 40);
if (locations[locationX][locationY] == 0) {
locations[locationX][locationY] = player;
//画棋子
DrawImg(locationX, locationY, player);
}
if (IsWin(locationX, locationY, player)) {
DrawWin(player);
alert("您赢了");
return;
//结束
}
评论0
最新资源