使用AI可以完成简单程序(如:五子棋),甚至都不要调试即可以运行,但逻辑规则复杂的程序就需要反复的调整,修改运行BUG,优化运行性能。(如:中国象棋,支持提示目标落子位置,并要求使用AI算法自动对弈)。
下面是经过反复调整后(N多次),得到的中国象棋游戏的js代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>中国象棋</title>
<style>
.board {
display: grid;
grid-template-columns: repeat(9, 50px);
grid-template-rows: repeat(10, 50px);
gap: 1px;
background-color: #f0d9b5;
width: fit-content;
margin: auto;
}
.cell {
width: 50px;
height: 50px;
background-color: #b58863;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
cursor: pointer;
}
.piece {
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid;
background-color: white;
}
.red {
color: red;
border-color: red;
}
.black {
color: black;
border-color: black;
}
.possible-move {
position: relative;
z-index: 1000;
}
.possible-move::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 45px;
height: 45px;
border: 2px dashed white;
border-radius: 50%;
z-index: 1000;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
.blink {
animation: blink 1s linear infinite;
}
</style>
</head>
<body>
<h1 align="center">中国象棋</h1>
<div class="board" id="board"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const board = document.getElementById('board');
const pieces = {
'red': ['车_red', '马_red', '相_red', '仕_red', '帅_red', '仕_red', '相_red', '马_red', '车_red', '炮_red', '炮_red', '兵_red', '兵_red', '兵_red', '兵_red', '兵_red'],
'black': ['车_black', '马_black', '象_black', '士_black', '将_black', '士_black', '象_black', '马_black', '车_black', '炮_black', '炮_black', '卒_black', '卒_black', '卒_black', '卒_black', '卒_black']
};
const initialPositions = {
'red': [
[0, 1, 2, 3, 4, 5, 6, 7, 8],
[null, null, null, null, null, null, null, null, null],
[null, 9, null, null, null, null, null, 10, null],
[11, null, 12, null, 13, null, 14, null, 15],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null]
],
'black': [
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[11, null, 12, null, 13, null, 14, null, 15],
[null, 9, null, null, null, null, null, 10, null],
[null, null, null, null, null, null, null, null, null],
[0, 1, 2, 3, 4, 5, 6, 7, 8]
]
};
let selectedPiece = null;
function createBoard() {
for (let row = 0; row < 10; row++) {
for (let col = 0; col < 9; col++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = row;
cell.dataset.col = col;
board.appendChild(cell);
const redPieceIndex = initialPositions.red[row]?.[col];
const blackPieceIndex = initialPositions.black[row]?.[col];
if (redPieceIndex !== null &&am