y, img_id);
board_states[lastPoint.x][lastPoint.y] = img_id;
}
}
if (!isOutside(sel_x, sel_y)) {
switch (board_states[sel_x][sel_y]) {
case id_normal:
img_id = id_normal_light;
break;
case id_flag:
img_id = id_flag_light;
break;
case id_unknown:
img_id = id_unknown_light;
break;
default:
img_id = undefined;
}
if (img_id !== undefined) {
drawMineBlock(cxt, sel_x, sel_y, img_id);
board_states[sel_x][sel_y] = img_id;
}
}
lastPoint.SetPoint(sel_x, sel_y);
})
.mouseup(function (eve) {
if (gameOver) return;
let sel_x = axis2Row(eve.offsetY - board_border);
let sel_y = axis2Row(eve.offsetX - board_border);
if (isOutside(sel_x, sel_y)) return;
switch (eve.button) {
case 0:
leftClick(cxt, sel_x, sel_y);
break;
case 2:
rightClick(cxt, sel_x, sel_y);
break;
default:
}
})
.on('contextmenu', function () {
return false;
});

$('#btn_reset').click(function () {
reset($canvas, cxt);
});
};

/**
* 左键点击
* @param cxt
* @param x0 行
* @param y0 列
*/
function leftClick(cxt, x0, y0) {
let count0;
let img_id = board_states[x0][y0];
switch (img_id) {
case id_normal_light:
case id_unknown_light:
count0 = expandByDFS(cxt, x0, y0);
isGameOver(count0);
break;
default:
// todo 是数字?
if (img_id >= id_n[1] && img_id <= id_n[8]) {
if (img_id === id_n[flagCountSurround(x0, y0)]) {
count0 = expandSurround(cxt, x0, y0);
isGameOver(count0);
} else {
drawMineBlock(cxt, x0, y0, id_sweep_error);
setTimeout(function () {
drawMineBlock(cxt, x0, y0, img_id);
}, 300);
}
}
}
}

/**
* 右键点击
* @param cxt
* @param x0 行
* @param y0 列
*/
function rightClick(cxt, x0, y0) {
let img_id;
switch (board_states[x0][y0]) {
case id_normal_light:
img_id = id_flag_light;
$left_count.text(function (i, t) {
return Number(t) - 1;
});
break;
case id_flag_light:
img_id = id_unknown_light;
$left_count.text(function (i, t) {
return Number(t) + 1;
});
break;
case id_unknown_light:
img_id = id_normal_light;
break;
default:
img_id = undefined;
}
if (img_id !== undefined) {
drawMineBlock(cxt, x0, y0, img_id);
board_states[x0][y0] = img_id;
}
}

/**
* 重置
* @param $canvas
* @param cxt
*/
function reset($canvas, cxt) {
board_row = Number($board_row.val());
board_col = Number($board_col.val());
board_total = board_row * board_col;
mine_count = Number($mine_count.val());
mine_count = Math.min(board_total, mine_count);
sweepCount = 0;
$left_count.text(mine_count);
gameOver = false;

for (let i = 0; i < board_row; i++) {
board_states[i] = [];
}
for (let i = 0; i < board_row; i++) {
mine_map[i] = [];
}
board_left = 0;
board_top = 0;
board_right = board_left + board_border + (img_width + board_border) * board_col;
board_bottom = board_top + board_border + (img_height + board_border) * board_row;

$canvas.attr('width', board_right);
$canvas.attr('height', board_bottom);
initBoard();
randomMines();
drawBoard(cxt);
}

/**
* 判断游戏是否结束
* @param count0 挖开的块的个数
* @return {boolean}
*/
function isGameOver(count0) {
if (count0 < 0) {
gameOver = true;
alert('你踩到地雷了!');
return gameOver;
}
sweepCount += count0;
if (sweepCount + mine_count === board_total) {
gameOver = true;
alert('游戏胜利!');
}
return gameOver;
}

/**
* 绘制棋盘
* @param cxt
*/
function drawBoard(cxt) {
cxt.fillStyle = border_color;
cxt.fillRect(board_left, board_top, board_right - board_left, board_bottom - board_top);
for (let i = 0; i < board_row; i++) {
for (let j = 0; j < board_col; j++) {
drawMineBlock(cxt, i, j, id_normal);
}
}
}

/**
* 绘制地雷块
* @param cxt
* @param x0 行
* @param y0 列
* @param img_id 图片索引
*/
function drawMineBlock(cxt, x0, y0, img_id) {
cxt.drawImage($imgs[img_id], board_border + row2Axis(y0), board_border + row2Axis(x0));
}

/**
* 坐标差转为行列
* @param offset0 坐标差
* @return {number}
*/
function axis2Row(offset0) {
// 暂时只以图片宽度为基准,所以图片宽高必须一致
let w = img_width + board_border;
let s = parseInt(offset0 / w);

Prev | Next
Pg.: 1 2 3


Back to home | File page

Subscribe | Register | Login | N