AI的出现,提升了人们的工作效率,尤其是给大家提供了更多接触其他行业的机会。今天,利用#deepseek#html版的五子棋游戏。
首先,进入deepseek官网(免费的),输入如下指令:
用html生成一个五子棋游戏,实现人机对战功能。
deepseek深入思考后,提供了第一版程序:
五子棋人机对战
轮到你了
<script>
const canvas = document.getElementById('board');
const ctx = canvas.getContext('2d');
const SIZE = 15;
const CELL_SIZE = 30;
let board = Array(SIZE).fill().map(() => Array(SIZE).fill(0));
let gameOver = false;
// 初始化棋盘
function initBoard() {
ctx.strokeStyle = '#000';
for (let i = 0; i < SIZE; i++) {
ctx.beginPath();
ctx.moveTo(i * CELL_SIZE + CELL_SIZE/2, CELL_SIZE/2);
ctx.lineTo(i * CELL_SIZE + CELL_SIZE/2, canvas.height - CELL_SIZE/2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(CELL_SIZE/2, i * CELL_SIZE + CELL_SIZE/2);
ctx.lineTo(canvas.width - CELL_SIZE/2, i * CELL_SIZE + CELL_SIZE/2);
ctx.stroke();
}
}
// 绘制棋子
function drawPiece(x, y, color) {
ctx.beginPath();
ctx.arc(x * CELL_SIZE + CELL_SIZE/2, y * CELL_SIZE + CELL_SIZE/2, 13, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
}
// 判断胜利
function checkWin(x, y, player) {
const directions = [[1,0], [0,1], [1,1], [1,-1]];
for (let [dx, dy] of directions) {
let count = 1;
for (let i = 1; i < 5; i++) {
if (board[x + i*dx]?.[y + i*dy] === player) count++;
else break;
}
for (let i = 1; i < 5 i if boardx - idx.y - idy='== player)' count else break if count>= 5) return true;
}
return false;
}
// 简单AI
function computerMove() {
let maxScore = -1;
let bestMoves = [];
for (let i = 0; i < SIZE; i++) {
for (let j = 0; j < size j if boardij='== 0)' let score='evaluatePosition(i,' j 2 if score> maxScore) {
maxScore = score;
bestMoves = [[i, j]];
} else if (score === maxScore) {
bestMoves.push([i, j]);
}
}
}
}
let [x, y] = bestMoves[Math.floor(Math.random() * bestMoves.length)];
board[x][y] = 2;
drawPiece(x, y, 'white');
if (checkWin(x, y, 2)) {
gameOver = true;
document.getElementById('status').textContent = '电脑获胜!';
}
}
// 位置评估函数
function evaluatePosition(x, y, player) {
let score = 0;
const directions = [[1,0], [0,1], [1,1], [1,-1]];
for (let [dx, dy] of directions) {
let line = [player];
for (let i = 1; i < 5; i++) {
const cell = board[x + i*dx]?.[y + i*dy] || 3;
line.push(cell === 0 ? player : cell);
}
for (let i = 1; i < 5 i const cell='board[x' - idx.y - idy 3 line.unshiftcell='== 0' player : cell const str='line.join('');' if str.includes22222 score if str.includes211112 score if str.includes2111 score if str.includes211 score if str.includes11 score return score canvas.addeventlistenerclick> {
if (gameOver) return;
const rect = canvas.getBoundingClientRect();
const x = Math.round((e.clientX - rect.left - CELL_SIZE/2) / CELL_SIZE);
const y = Math.round((e.clientY - rect.top - CELL_SIZE/2) / CELL_SIZE);
if (x >= 0 && x < size y>= 0 && y < SIZE && board[x][y] === 0) {
board[x][y] = 1;
drawPiece(x, y, 'black');
if (checkWin(x, y, 1)) {
gameOver = true;
document.getElementById('status').textContent = '你赢了!';
return;
}
document.getElementById('status').textContent = '电脑思考中...';
setTimeout(computerMove, 500);
}
});
initBoard();
</script>
并给出如下提示:
同时给出优化建议:
运行效果如下:
测试发现白棋看不清,另外第一次运行时我下了第两个黑子,AI才下一个白子(我不知道是不是自己没看到白棋)。于是提醒deepseek进行程序优化:
修改建议,讲棋子改为黑色和黄色,以便于区分。
增加重新开始和记录比分功能。
优化人机对弈,上个版本中AI算法存在问题,用户已经落第二子后,AI才落下第一子
deepseek很快给出新版本程序,代码如下:
五子棋人机对战
轮到你了
比分 玩家: 0 - 电脑: 0
<script>
const canvas = document.getElementById('board');
const ctx = canvas.getContext('2d');
const SIZE = 15;
const CELL_SIZE = 30;
let board = Array(SIZE).fill().map(() => Array(SIZE).fill(0));
let gameOver = false;
let playerScore = 0;
let computerScore = 0;
// 初始化游戏
function initGame() {
board = Array(SIZE).fill().map(() => Array(SIZE).fill(0));
gameOver = false;
ctx.clearRect(0, 0, canvas.width, canvas.height);
initBoard();
document.getElementById('status').textContent = '轮到你了';
}
// 初始化棋盘
function initBoard() {
ctx.strokeStyle = '#000';
for (let i = 0; i < SIZE; i++) {
ctx.beginPath();
ctx.moveTo(i * CELL_SIZE + CELL_SIZE/2, CELL_SIZE/2);
ctx.lineTo(i * CELL_SIZE + CELL_SIZE/2, canvas.height - CELL_SIZE/2);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(CELL_SIZE/2, i * CELL_SIZE + CELL_SIZE/2);
ctx.lineTo(canvas.width - CELL_SIZE/2, i * CELL_SIZE + CELL_SIZE/2);
ctx.stroke();
}
}
// 绘制棋子
function drawPiece(x, y, color) {
ctx.beginPath();
ctx.arc(x * CELL_SIZE + CELL_SIZE/2, y * CELL_SIZE + CELL_SIZE/2, 13, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.strokeStyle = '#333';
ctx.lineWidth = 1;
ctx.fill();
ctx.stroke();
}
// 判断胜利
function checkWin(x, y, player) {
const directions = [[1,0], [0,1], [1,1], [1,-1]];
for (let [dx, dy] of directions) {
let count = 1;
for (let i = 1; i < 5; i++) {
if (board[x + i*dx]?.[y + i*dy] === player) count++;
else break;
}
for (let i = 1; i < 5 i if boardx - idx.y - idy='== player)' count else break if count>= 5) return true;
}
return false;
}
// 优化后的AI
function computerMove() {
let maxScore = -Infinity;
let bestMoves = [];
// 优先检查胜利机会
for (let i = 0; i < SIZE; i++) {
for (let j = 0; j < size j if boardij continue boardij='2;' if checkwini j 2 bestmoves='[[i,' j boardij='0;' break boardij='0;' if bestmoves.length> 0) break;
}
// 没有立即获胜机会时进行常规评估
if (bestMoves.length === 0) {
for (let i = 0; i < SIZE; i++) {
for (let j = 0; j < size j if boardij continue let score='evaluatePosition(i,' j 2 1.2 score j 1 if score> maxScore) {
maxScore = score;
bestMoves = [[i, j]];
} else if (score === maxScore) {
bestMoves.push([i, j]);
}
}
}
}
let [x, y] = bestMoves[Math.floor(Math.random() * bestMoves.length)];
board[x][y] = 2;
drawPiece(x, y, '#ffd700');
if (checkWin(x, y, 2)) {
gameOver = true;
computerScore++;
updateScore();
document.getElementById('status').textContent = '电脑获胜!';
} else {
document.getElementById('status').textContent = '轮到你了';
}
}
// 增强版评估函数
function evaluatePosition(x, y, player) {
let score = 0;
const directions = [[1,0], [0,1], [1,1], [1,-1]];
for (let [dx, dy] of directions) {
let line = [];
for (let i = -4; i <= 4 i const xi='x' idx const yi='y' idy if xi>= 0 && xi < size yi>= 0 && yi < SIZE) {
line.push(board[xi][yi]);
} else {
line.push(-1);
}
}
// 分析棋型
for (let i = 0; i <= 4; i++) {
const segment = line.slice(i, i+5);
let playerCount = 0;
let emptyCount = 0;
for (let cell of segment) {
if (cell === player) playerCount++;
else if (cell === 0) emptyCount++;
else playerCount = -Infinity; // 遇到障碍
}
if (playerCount < 0 continue switchplayercount case 5: score break case 4: if emptycount='== 1)' score break case 3: if emptycount='== 2)' score break case 2: if emptycount='== 3)' score break return score function updatescore document.getelementbyidscore.textcontent='`比分' : playerscore - : computerscore function restartgame initgame canvas.addeventlistenerclick> {
if (gameOver) return;
const rect = canvas.getBoundingClientRect();
const x = Math.round((e.clientX - rect.left - CELL_SIZE/2) / CELL_SIZE);
const y = Math.round((e.clientY - rect.top - CELL_SIZE/2) / CELL_SIZE);
if (x >= 0 && x < size y>= 0 && y < SIZE && board[x][y] === 0) {
board[x][y] = 1;
drawPiece(x, y, '#000');
if (checkWin(x, y, 1)) {
gameOver = true;
playerScore++;
updateScore();
document.getElementById('status').textContent = '你赢了!';
return;
}
document.getElementById('status').textContent = '电脑思考中...';
setTimeout(computerMove, 100); // 缩短AI响应时间
}
});
initGame();
</script>
运行效果:
完美运行,说实话,一不小心就玩不过了。
同时,deepseek还给出了优化建议:
前后用时不到五分钟,得到一个完美小程序,以后摸鱼就靠它了。