
- 积分
- 4618
- 魅力
- 0
- 金币
- 8
- 注册时间
- 2022-1-2

|
发表于 2025-2-27 22:30:45
|
显示全部楼层
只能讲见人讲人话,见鬼讲鬼话
是时候改抛弃对AI的成见了,AI的某项能力超过人类后,人类就再也赶不上了,目前短短不到一个月的轻度使用是非常震撼的,最震撼的是用AI实现一个小时候下的叫六子棋的棋类游戏,仅仅修改几次提示词AI就把人机对战的网页程序。而且我再也下不过电脑给的算法了。。只花了十来分钟。
这是AI生成的代码,有兴趣可以把代码用记事本保存,把.txt改成.html用浏览其打开试试。
- <!DOCTYPE html>
- <html>
- <head>
- <title>六子棋</title>
- <style>
- body {
- font-family: sans-serif;
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- #board {
- display: grid;
- grid-template-columns: repeat(4, 80px);
- grid-template-rows: repeat(4, 80px);
- border: 2px solid black;
- margin-bottom: 20px;
- }
- .cell {
- width: 80px;
- height: 80px;
- border: 1px solid lightgray;
- position: relative;
- }
- .piece {
- width: 70px;
- height: 70px;
- border-radius: 50%;
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- cursor: pointer;
- transition: transform 0.3s ease, opacity 0.3s ease;
- }
- .red {
- background-color: red;
- }
- .black {
- background-color: black;
- }
- .selected {
- transform: translate(-50%, -50%) scale(1.2);
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
- }
- .fading {
- opacity: 0;
- }
- button {
- margin: 5px;
- padding: 8px 15px;
- border: none;
- background-color: #4CAF50;
- color: white;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 14px;
- cursor: pointer;
- border-radius: 5px;
- }
- #message {
- margin-top: 10px;
- font-weight: bold;
- }
- </style>
- </head>
- <body>
- <h1>六子棋</h1>
- <div id="board"></div>
- <button id="aiFirstButton">AI 先手</button>
- <button id="restartButton">重新开始</button>
- <div id="message"></div>
- <script>
- const boardSize = 4;
- let board = Array(boardSize).fill(null).map(() => Array(boardSize).fill(null));
- let currentPlayer = "black";
- let selectedPiece = null;
- let gameOver = false;
- let aiIsFirst = false;
- const aiDepth = 10; // Set the AI search depth
- const redPiecesInitial = [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 3]];
- const blackPiecesInitial = [[2, 0], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]];
- function initializeBoard(aiFirst = false) {
- board = Array(boardSize).fill(null).map(() => Array(boardSize).fill(null));
- gameOver = false;
- currentPlayer = "black";
- selectedPiece = null;
- aiIsFirst = aiFirst;
- redPiecesInitial.forEach(coord => board[coord[0]][coord[1]] = "red");
- blackPiecesInitial.forEach(coord => board[coord[0]][coord[1]] = "black");
- if (aiFirst) {
- currentPlayer = 'red';
- aiMove();
- }
- renderBoard();
- updateMessage();
- }
- function renderBoard() {
- const boardElement = document.getElementById("board");
- boardElement.innerHTML = "";
- for (let row = 0; row < boardSize; row++) {
- for (let col = 0; col < boardSize; col++) {
- const cell = document.createElement("div");
- cell.classList.add("cell");
- cell.dataset.row = row;
- cell.dataset.col = col;
- cell.addEventListener("click", handleCellClick);
- if (board[row][col]) {
- const piece = document.createElement("div");
- piece.classList.add("piece", board[row][col]);
- piece.dataset.row = row;
- piece.dataset.col = col;
- piece.addEventListener("click", handlePieceClick); // Add click listener to piece
- cell.appendChild(piece);
- }
- boardElement.appendChild(cell);
- }
- }
- }
- function handlePieceClick(event) {
- event.stopPropagation(); // Prevent cell click from firing
- const piece = event.target;
- const row = parseInt(piece.dataset.row);
- const col = parseInt(piece.dataset.col);
- if (gameOver) return;
- if (board[row][col] !== currentPlayer) return;
- // Deselect if clicking the already selected piece
- if (selectedPiece && selectedPiece.row === row && selectedPiece.col === col) {
- deselectPiece();
- return;
- }
- selectPiece(row, col);
- }
- function handleCellClick(event) {
- if (gameOver) return;
- const row = parseInt(event.target.dataset.row);
- const col = parseInt(event.target.dataset.col);
- if (selectedPiece) {
- movePiece(selectedPiece.row, selectedPiece.col, row, col);
- }
- }
- function selectPiece(row, col) {
- deselectPiece(); // Deselect any previously selected piece
- selectedPiece = { row: row, col: col };
- const pieceElement = document.querySelector(`.piece[data-row="${row}"][data-col="${col}"]`);
- if (pieceElement) {
- pieceElement.classList.add("selected");
- }
- }
- function deselectPiece() {
- if (selectedPiece) {
- const pieceElement = document.querySelector(`.piece[data-row="${selectedPiece.row}"][data-col="${selectedPiece.col}"]`);
- if (pieceElement) {
- pieceElement.classList.remove("selected");
- }
- selectedPiece = null;
- }
- }
- function isValidMove(startRow, startCol, endRow, endCol) {
- if (endRow < 0 || endRow >= boardSize || endCol < 0 || endCol >= boardSize) return false;
- if (board[endRow][endCol] !== null) return false;
- const rowDiff = Math.abs(endRow - startRow);
- const colDiff = Math.abs(endCol - startCol);
- return (rowDiff === 1 && colDiff === 0) || (rowDiff === 0 && colDiff === 1);
- }
- function movePiece(startRow, startCol, endRow, endCol) {
- if (!isValidMove(startRow, startCol, endRow, endCol)) {
- deselectPiece();
- return;
- }
- board[endRow][endCol] = board[startRow][startCol];
- board[startRow][startCol] = null;
- const pieceElement = document.querySelector(`.piece[data-row="${startRow}"][data-col="${startCol}"]`);
- if (pieceElement) {
- pieceElement.dataset.row = endRow;
- pieceElement.dataset.col = endCol;
- }
- renderBoard(); //Re-render to move the element.
- deselectPiece();
- handleCaptures(endRow, endCol);
- if (gameOver) return;
- currentPlayer = currentPlayer === "black" ? "red" : "black";
- updateMessage();
- if (currentPlayer === "red") {
- setTimeout(aiMove, 500); // Delay the AI move slightly
- }
- }
- function handleCaptures(row, col) {
- const opponent = currentPlayer === "black" ? "red" : "black";
- let captures = [];
- // Check horizontal captures
- const horizontal = [board[row][0], board[row][1], board[row][2], board[row][3]];
- // Check [ 空格, 我方, 我方, 敌方 ]
- if (horizontal[0] === null && horizontal[1] === currentPlayer && horizontal[2] === currentPlayer && horizontal[3] === opponent) {
- captures.push([row, 3]);
- }
- // Check [ 敌方, 我方, 我方, 空格 ]
- if (horizontal[0] === opponent && horizontal[1] === currentPlayer && horizontal[2] === currentPlayer && horizontal[3] === null) {
- captures.push([row, 0]);
- }
- // Check [ 我方, 我方, 敌方, 空格 ]
- if (horizontal[0] === currentPlayer && horizontal[1] === currentPlayer && horizontal[2] === opponent && horizontal[3] === null) {
- captures.push([row, 2]);
- }
- // Check [ 空格, 敌方, 我方, 我方 ]
- if (horizontal[0] === null && horizontal[1] === opponent && horizontal[2] === currentPlayer && horizontal[3] === currentPlayer) {
- captures.push([row, 1]);
- }
- // Check vertical captures
- const vertical = [board[0][col], board[1][col], board[2][col], board[3][col]];
- // Check [ 空格, 我方, 我方, 敌方 ]
- if (vertical[0] === null && vertical[1] === currentPlayer && vertical[2] === currentPlayer && vertical[3] === opponent) {
- captures.push([3, col]);
- }
- // Check [ 敌方, 我方, 我方, 空格 ]
- if (vertical[0] === opponent && vertical[1] === currentPlayer && vertical[2] === currentPlayer && vertical[3] === null) {
- captures.push([0, col]);
- }
- // Check [ 我方, 我方, 敌方, 空格 ]
- if (vertical[0] === currentPlayer && vertical[1] === currentPlayer && vertical[2] === opponent && vertical[3] === null) {
- captures.push([2, col]);
- }
- // Check [ 空格, 敌方, 我方, 我方 ]
- if (vertical[0] === null && vertical[1] === opponent && vertical[2] === currentPlayer && vertical[3] === currentPlayer) {
- captures.push([1, col]);
- }
- captures.forEach(capture => {
- const [captureRow, captureCol] = capture;
- removePiece(captureRow, captureCol);
- });
- if (checkWinCondition()) {
- gameOver = true;
- updateMessage();
- }
- }
- function removePiece(row, col) {
- const pieceElement = document.querySelector(`.piece[data-row="${row}"][data-col="${col}"]`);
- if (pieceElement) {
- pieceElement.classList.add("fading");
- setTimeout(() => {
- board[row][col] = null;
- renderBoard();
- }, 300); // Match the transition duration
- } else {
- board[row][col] = null;
- renderBoard();
- }
- }
- function checkWinCondition() {
- const redPieces = board.flat().filter(piece => piece === "red").length;
- const blackPieces = board.flat().filter(piece => piece === "black").length;
- if (redPieces === 0) {
- return "black";
- }
- if (blackPieces === 0) {
- return "red";
- }
- return null;
- }
- function updateMessage() {
- const messageElement = document.getElementById("message");
- if (gameOver) {
- messageElement.textContent = `游戏结束! ${checkWinCondition() === "black" ? "黑方" : "红方"} 获胜!`;
- } else {
- messageElement.textContent = `轮到 ${currentPlayer === "black" ? "黑方" : "红方"} 走棋`;
- }
- }
- // AI Logic
- function aiMove() {
- const bestMove = minimax(board, aiDepth, -Infinity, Infinity, true); // Depth of 10
- if (bestMove.move) {
- const [startRow, startCol, endRow, endCol] = bestMove.move;
- movePiece(startRow, startCol, endRow, endCol);
- } else {
- console.log("No move found");
- }
- }
- function minimax(boardState, depth, alpha, beta, maximizingPlayer) {
- const winner = checkWinConditionForState(boardState);
- if (depth === 0 || winner) {
- return { score: evaluateBoard(boardState, winner), move: null };
- }
- if (maximizingPlayer) {
- let bestScore = -Infinity;
- let bestMove = null;
- const possibleMoves = getPossibleMoves(boardState, "red");
- for (const move of possibleMoves) {
- const [startRow, startCol, endRow, endCol] = move;
- const newBoardState = getNewBoardState(boardState, startRow, startCol, endRow, endCol, "red");
- const score = minimax(newBoardState, depth - 1, alpha, beta, false).score;
- if (score > bestScore) {
- bestScore = score;
- bestMove = move;
- }
- alpha = Math.max(alpha, bestScore);
- if (beta <= alpha) {
- break; // Beta cutoff
- }
- }
- return { score: bestScore, move: bestMove };
- } else {
- let bestScore = Infinity;
- let bestMove = null;
- const possibleMoves = getPossibleMoves(boardState, "black");
- for (const move of possibleMoves) {
- const [startRow, startCol, endRow, endCol] = move;
- const newBoardState = getNewBoardState(boardState, startRow, startCol, endRow, endCol, "black");
- const score = minimax(newBoardState, depth - 1, alpha, beta, true).score;
- if (score < bestScore) {
- bestScore = score;
- bestMove = move;
- }
- beta = Math.min(beta, bestScore);
- if (beta <= alpha) {
- break; // Alpha cutoff
- }
- }
- return { score: bestScore, move: bestMove };
- }
- }
- function getPossibleMoves(boardState, player) {
- const moves = [];
- for (let row = 0; row < boardSize; row++) {
- for (let col = 0; col < boardSize; col++) {
- if (boardState[row][col] === player) {
- const possibleDestinations = [[row - 1, col], [row + 1, col], [row, col - 1], [row, col + 1]];
- for (const [endRow, endCol] of possibleDestinations) {
- if (endRow >= 0 && endRow < boardSize && endCol >= 0 && endCol < boardSize && boardState[endRow][endCol] === null) {
- moves.push([row, col, endRow, endCol]);
- }
- }
- }
- }
- }
- return moves;
- }
- function getNewBoardState(boardState, startRow, startCol, endRow, endCol, player) {
- const newBoardState = boardState.map(row => [...row]); // Deep copy
- newBoardState[endRow][endCol] = newBoardState[startRow][startCol];
- newBoardState[startRow][startCol] = null;
- const opponent = player === "black" ? "red" : "black";
- let captures = [];
- // Check horizontal captures
- const horizontal = [newBoardState[endRow][0], newBoardState[endRow][1], newBoardState[endRow][2], newBoardState[endRow][3]];
- // Check [ 空格, 我方, 我方, 敌方 ]
- if (horizontal[0] === null && horizontal[1] === player && horizontal[2] === player && horizontal[3] === opponent) {
- captures.push([endRow, 3]);
- }
- // Check [ 敌方, 我方, 我方, 空格 ]
- if (horizontal[0] === opponent && horizontal[1] === player && horizontal[2] === player && horizontal[3] === null) {
- captures.push([endRow, 0]);
- }
- // Check [ 我方, 我方, 敌方, 空格 ]
- if (horizontal[0] === player && horizontal[1] === player && horizontal[2] === opponent && horizontal[3] === null) {
- captures.push([endRow, 2]);
- }
- // Check [ 空格, 敌方, 我方, 我方 ]
- if (horizontal[0] === null && horizontal[1] === opponent && horizontal[2] === player && horizontal[3] === player) {
- captures.push([endRow, 1]);
- }
- // Check vertical captures
- const vertical = [newBoardState[0][endCol], newBoardState[1][endCol], newBoardState[2][endCol], newBoardState[3][endCol]];
- // Check [ 空格, 我方, 我方, 敌方 ]
- if (vertical[0] === null && vertical[1] === player && vertical[2] === player && vertical[3] === opponent) {
- captures.push([3, endCol]);
- }
- // Check [ 敌方, 我方, 我方, 空格 ]
- if (vertical[0] === opponent && vertical[1] === player && vertical[2] === player && vertical[3] === null) {
- captures.push([0, endCol]);
- }
- // Check [ 我方, 我方, 敌方, 空格 ]
- if (vertical[0] === player && vertical[1] === player && vertical[2] === opponent && vertical[3] === null) {
- captures.push([2, endCol]);
- }
- // Check [ 空格, 敌方, 我方, 我方 ]
- if (vertical[0] === null && vertical[1] === opponent && vertical[2] === player && vertical[3] === player) {
- captures.push([1, endCol]);
- }
- captures.forEach(capture => {
- const [captureRow, captureCol] = capture;
- newBoardState[captureRow][captureCol] = null;
- });
- return newBoardState;
- }
- function checkWinConditionForState(boardState) {
- const redPieces = boardState.flat().filter(piece => piece === "red").length;
- const blackPieces = boardState.flat().filter(piece => piece === "black").length;
- if (redPieces === 0) {
- return "black";
- }
- if (blackPieces === 0) {
- return "red";
- }
- return null;
- }
- function evaluateBoard(boardState, winner) {
- if (winner === "red") {
- return 100;
- } else if (winner === "black") {
- return -100;
- } else {
- // Heuristic evaluation - consider piece count (red wants more, black wants less)
- const redPieces = boardState.flat().filter(piece => piece === "red").length;
- const blackPieces = boardState.flat().filter(piece => piece === "black").length;
- return redPieces - blackPieces; // Simple piece difference
- }
- }
- // Button Event Listeners
- document.getElementById("aiFirstButton").addEventListener("click", () => {
- initializeBoard(true);
- });
- document.getElementById("restartButton").addEventListener("click", () => {
- initializeBoard(false);
- });
- initializeBoard();
- </script>
- </body>
- </html>
复制代码
|
-
|