编写一个五子棋html小游戏
时间: 2024-01-27 19:02:28 浏览: 104
以下是一个简单的五子棋 HTML 小游戏,你可以参考一下。
```html
<!DOCTYPE html>
<html>
<head>
<title>五子棋小游戏</title>
<style type="text/css">
body {
background-color: #eee;
}
#board {
margin: auto;
margin-top: 50px;
border: 1px solid #333;
width: 500px;
height: 500px;
position: relative;
}
.piece {
position: absolute;
border-radius: 50%;
box-shadow: 1px 1px 3px rgba(0,0,0,0.5);
}
.black {
background-color: #000;
}
.white {
background-color: #fff;
}
.win {
background-color: #f00;
opacity: 0.5;
}
</style>
</head>
<body>
<div id="board"></div>
<script type="text/javascript">
var board = document.getElementById('board');
var context = board.getContext('2d');
var me = true; // 是否我方下棋
var over = false; // 是否结束游戏
var chessBoard = []; // 棋盘
var wins = []; // 赢法数组
var myWin = []; // 我方赢法统计数组
var computerWin = []; // 电脑赢法统计数组
// 初始化棋盘
for (var i = 0; i < 15; i++) {
chessBoard[i] = [];
for (var j = 0; j < 15; j++) {
chessBoard[i][j] = 0;
}
}
// 初始化赢法数组
for (var i = 0; i < 15; i++) {
wins[i] = [];
for (var j = 0; j < 15; j++) {
wins[i][j] = [];
}
}
// 统计所有赢法
var count = 0;
for (var i = 0; i < 15; i++) {
for (var j = 0; j < 11; j++) {
for (var k = 0; k < 5; k++) {
wins[i][j+k][count] = true;
}
count++;
}
}
for (var i = 0; i < 15; i++) {
for (var j = 0; j < 11; j++) {
for (var k = 0; k < 5; k++) {
wins[j+k][i][count] = true;
}
count++;
}
}
for (var i = 0; i < 11; i++) {
for (var j = 0; j < 11; j++) {
for (var k = 0; k < 5; k++) {
wins[i+k][j+k][count] = true;
}
count++;
}
}
for (var i = 0; i < 11; i++) {
for (var j = 14; j > 3; j--) {
for (var k = 0; k < 5; k++) {
wins[i+k][j-k][count] = true;
}
count++;
}
}
// 初始化赢法统计数组
for (var i = 0; i < count; i++) {
myWin[i] = 0;
computerWin[i] = 0;
}
// 绘制棋盘
function drawBoard() {
context.strokeStyle = '#333';
for (var i = 0; i < 15; i++) {
context.moveTo(15 + i * 30, 15);
context.lineTo(15 + i * 30, 465);
context.stroke();
context.moveTo(15, 15 + i * 30);
context.lineTo(465, 15 + i * 30);
context.stroke();
}
}
// 绘制棋子
function drawPiece(i, j, me) {
context.beginPath();
context.arc(15 + i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);
context.closePath();
var gradient = context.createRadialGradient(15 + i * 30 + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 - 2, 0);
if (me) {
gradient.addColorStop(0, '#0a0a0a');
gradient.addColorStop(1, '#636766');
} else {
gradient.addColorStop(0, '#d1d1d1');
gradient.addColorStop(1, '#f9f9f9');
}
context.fillStyle = gradient;
context.fill();
}
// 下棋
board.onclick = function(e) {
if (over || !me) {
return;
}
var x = e.offsetX;
var y = e.offsetY;
var i = Math.floor(x / 30);
var j = Math.floor(y / 30);
if (chessBoard[i][j] == 0) {
drawPiece(i, j, me);
chessBoard[i][j] = 1;
for (var k = 0; k < count; k++) {
if (wins[i][j][k]) {
myWin[k]++;
computerWin[k] = 6;
if (myWin[k] == 5) {
alert('你赢了!');
over = true;
}
}
}
if (!over) {
me = !me;
computerAI();
}
}
}
// 电脑下棋
function computerAI() {
var myScore = [];
var computerScore = [];
var max = 0;
var u = 0;
var v = 0;
for (var i = 0; i < 15; i++) {
myScore[i] = [];
computerScore[i] = [];
for (var j = 0; j < 15; j++) {
myScore[i][j] = 0;
computerScore[i][j] = 0;
}
}
for (var i = 0; i < 15; i++) {
for (var j = 0; j < 15; j++) {
if (chessBoard[i][j] == 0) {
for (var k = 0; k < count; k++) {
if (wins[i][j][k]) {
if (myWin[k] == 1) {
myScore[i][j] += 200;
} else if (myWin[k] == 2) {
myScore[i][j] += 400;
} else if (myWin[k] == 3) {
myScore[i][j] += 2000;
} else if (myWin[k] == 4) {
myScore[i][j] += 10000;
}
if (computerWin[k] == 1) {
computerScore[i][j] += 220;
} else if (computerWin[k] == 2) {
computerScore[i][j] += 420;
} else if (computerWin[k] == 3) {
computerScore[i][j] += 2100;
} else if (computerWin[k] == 4) {
computerScore[i][j] += 20000;
}
}
}
if (myScore[i][j] > max) {
max = myScore[i][j];
u = i;
v = j;
} else if (myScore[i][j] == max) {
if (computerScore[i][j] > computerScore[u][v]) {
u = i;
v = j;
}
}
if (computerScore[i][j] > max) {
max = computerScore[i][j];
u = i;
v = j;
} else if (computerScore[i][j] == max) {
if (myScore[i][j] > myScore[u][v]) {
u = i;
v = j;
}
}
}
}
}
drawPiece(u, v, false);
chessBoard[u][v] = 2;
for (var k = 0; k < count; k++) {
if (wins[u][v][k]) {
computerWin[k]++;
myWin[k] = 6;
if (computerWin[k] == 5) {
alert('电脑赢了!');
over = true;
}
}
}
if (!over) {
me = !me;
}
}
drawBoard();
</script>
</body>
</html>
```
在浏览器中打开该 HTML 文件,即可开始游戏。
阅读全文