五子棋结算算法
编程思想 每次用户下棋子是, 对当前棋子的 左右, 上下, 斜线进行判断, 看是否有成了5个的, 有则结算, 没有则继续游戏
-
前提说明
- 当前五子棋棋盘大小为 DISK_SIZE = 15; // 棋盘的大小 即为15*15的棋盘
- 用户下棋时, 会将棋子的位置传出 (pos_x, pox_y) 横轴为x, 竖轴为y, 以及当前棋子的类型(黑或白)
- 因为 js 没有真正的二维数组, 所有我用的是一个一维数组存储 chess_disk(默认全部为0), 表示方法 为 chess_disk[pos_y * 15 + pos_x]
- chess_disk中的的值 0表示该位置没有棋子, 1表示黑棋, 2表示白旗
代码展示
对代码分析, 以左到右为例子
left_to_right(block_x: number, block_y: number, chess_type: number) {
let count = 1; // 表示棋子连成了几个, 5个则结算 return 1
let pos_x = block_x;
while(1) { // 首先判断当前棋子的左边是否有相同颜色的棋子, 有则count++ , 没有则break 退出循环
pos_x --;
if(pos_x < 0) break;
if(this.chess_disk[block_y * 15 + pos_x] == chess_type) count ++;
else break;
}
pos_x = block_x;
while(1) { // 在判断当前棋子右边是否有相同颜色的棋子
pos_x ++;
if(pos_x > 14) break;
if(this.chess_disk[block_y * 15 + pos_x] == chess_type) count ++
else break;
}
if(count >= 5) return 1;
return -1;
}
// 检查 左右边的 // block_y * 15 + block_x;
left_to_right(block_x: number, block_y: number, chess_type: number) {
let count = 1;
let pos_x = block_x;
while(1) {
pos_x --;
if(pos_x < 0) break;
if(this.chess_disk[block_y * 15 + pos_x] == chess_type) count ++;
else break;
}
pos_x = block_x;
while(1) {
pos_x ++;
if(pos_x > 14) break;
if(this.chess_disk[block_y * 15 + pos_x] == chess_type) count ++
else break;
}
if(count >= 5) return 1;
return -1;
}
// 检查 上下边的
top_to_bottom(block_x: number, block_y: number, chess_type: number) {
let count = 1;
let pos_y = block_y;
while(1) {
pos_y --;
if(pos_y < 0) break;
if(this.chess_disk[pos_y * 15 + block_x] == chess_type) count ++;
else break;
}
pos_y = block_x;
while(1) {
pos_y ++;
if(pos_y > 14) break;
if(this.chess_disk[pos_y * 15 + block_x] == chess_type) count ++
else break;
}
if(count >= 5) return 1;
return -1;
}
// 检查左下 到 右上
lb_to_rt(block_x: number, block_y: number, chess_type: number) {
let count = 1;
let pos_x = block_x;
let pos_y = block_y;
while(1) {
pos_x ++;
pos_y ++;
if(pos_x > 14) break;
if(pos_y > 14) break;
if(this.chess_disk[pos_y * 15 + pos_x] == chess_type) count ++;
else break;
}
pos_x = block_x;
pos_y = block_y;
while(1) {
pos_x --;
pos_y --;
if(pos_x < 0) break;
if(pos_y < 0) break;
if(this.chess_disk[pos_y * 15 + pos_x] == chess_type) count ++;
else break;
}
if(count >= 5) return 1;
return -1;
}
// 检查左上 到 右下
lt_to_rb(block_x: number, block_y: number, chess_type: number) {
let count = 1;
let pos_x = block_x;
let pos_y = block_y;
while(1) {
pos_x ++;
pos_y --;
if(pos_x > 14) break;
if(pos_y < 0) break;
if(this.chess_disk[pos_y * 15 + pos_x] == chess_type) count ++;
else break;
}
pos_x = block_x;
pos_y = block_y;
while(1) {
pos_x --;
pos_y ++;
if(pos_x < 0) break;
if(pos_y > 14) break;
if(this.chess_disk[pos_y * 15 + pos_x] == chess_type) count ++;
else break;
}
if(count >= 5) return 1;
return -1;
}
结算判断
// 返回值1表示游戏结算, 0表示继续游戏, 2表示平局
check_game_over(block_x: number, block_y: number, chess_type: number) {
if(this.left_to_right(block_x, block_y, chess_type) == 1 ||
this.top_to_bottom(block_x, block_y, chess_type) == 1 ||
this.lt_to_rb(block_x, block_y, chess_type) == 1 ||
this.lb_to_rt(block_x, block_y, chess_type) == 1) {
return 1;
}
for(let i=0; i<this.DISK_SIZE * this.DISK_SIZE; i++) {
if(this.chess_disk[i] == ChessType.NONE) {
return 0;
}
}
return 2;
}