這只是一個簡單的JAVAscript和HTML
五子棋棋盤落子點對應的二維數組
判斷五子棋贏棋的算法是通過對五子棋棋盤落子點對應的二維數組的操作來實現的
判斷五子棋贏棋算法
下邊的函數可以實現判斷五子棋贏棋的算法
其中函數的參數xx
算法的思想方法是
<!DOCTYPE html>
< html xmlns="
< head>
<meta http
<title></title>
<style type="text/css">
body {
margin:
}
</style>
<script type="text/javascript">
var canvas;
var context;
var isWhite = true;//設置是否該輪到白棋
var isWell = false;//設置該局棋盤是否贏了
var img_b = new Image();
img_b
var img_w = new Image();
img_w
var chessData = new Array(
for (var x =
chessData[x] = new Array(
for (var y =
chessData[x][y] =
}
}
function drawRect() {//頁面加載完畢調用函數
canvas = document
context = canvas
for (var i =
context
context
context
context
context
context
context
context
context
context
}
}
function play(e) {//鼠標點擊時發生
var x = parseInt((e
var y = parseInt((e
if (chessData[x][y] !=
alert("你不能在這個位置下棋");
return;
}
if (isWhite) {
isWhite = false;
drawChess(
}
else {
isWhite = true;
drawChess(
}
}
function drawChess(chess
if (isWell == true) {
alert("已經結束了
return;
}
if (x >=
if (chess ==
context
chessData[x][y] =
}
else {
context
chessData[x][y] =
}
judge(x
}
}
function judge(x
var count
var count
var count
var count
//左右判斷
for (var i = x; i >=
if (chessData
[y] != chess) {
break;
}
count
}
for (var i = x +
if (chessData
[y] != chess) {
break;
}
count
}
//上下判斷
for (var i = y; i >=
if (chessData[x]
!= chess) {
break;
}
count
}
for (var i = y +
if (chessData[x]
!= chess) {
break;
}
count
}
//左上右下判斷
for (var i = x
if (chessData
[j] != chess) {
break;
}
count
}
for (var i = x +
if (chessData
[j] != chess) {
break;
}
count
}
//右上左下判斷
for (var i = x
if (chessData
[j] != chess) {
break;
}
count
}
for (var i = x +
if (chessData
[j] != chess) {
break;
}
count
}
if (count
if (chess ==
alert("白棋贏了");
}
else {
alert("黑棋贏了");
}
isWell = true;//設置該局棋盤已經贏了
}
}
</script>
< /head>
< body onload="drawRect()">
<div>
<canvas width="
</canvas>
</div>
< /body>
< /html>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20294.html