程式碼生成範例
這個範例展示了如何使用 DeepSeek 來生成程式碼,包括如何描述需求、生成程式碼和解釋程式碼的實作細節。
任務
生成一個完整的 HTML 五子棋遊戲,包含遊戲邏輯和介面樣式。
提示詞
請幫我用 HTML 生成一個五子棋遊戲,所有程式碼都儲存在一個 HTML 中。
回答範例
當然!以下是一個簡單的五子棋遊戲的 HTML 程式碼。這個程式碼包含了一個基本的五子棋棋盤,玩家可以透過點擊棋盤上的格子來下棋。遊戲會檢測是否有玩家獲勝。
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>五子棋遊戲</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
#board {
display: grid;
grid-template-columns: repeat(15, 30px);
grid-template-rows: repeat(15, 30px);
gap: 1px;
background-color: #d18b47;
padding: 10px;
border-radius: 5px;
}
.cell {
width: 30px;
height: 30px;
background-color: #ffce9e;
border: 1px solid #d18b47;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.cell.black::before, .cell.white::before {
content: '';
width: 20px;
height: 20px;
border-radius: 50%;
}
.cell.black::before {
background-color: black;
}
.cell.white::before {
background-color: white;
}
#message {
margin-top: 20px;
font-size: 24px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="board"></div>
<div id="message"></div>
<script>
const boardSize = 15;
let currentPlayer = 'black';
let board = Array.from({ length: boardSize }, () => Array(boardSize).fill(null));
function createBoard() {
const boardElement = document.getElementById('board');
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', handleCellClick);
boardElement.appendChild(cell);
}
}
}
function handleCellClick(event) {
const row = parseInt(event.target.dataset.row);
const col = parseInt(event.target.dataset.col);
if (board[row][col] !== null) return;
board[row][col] = currentPlayer;
event.target.classList.add(currentPlayer);
if (checkWin(row, col, currentPlayer)) {
document.getElementById('message').textContent = `${currentPlayer === 'black' ? '黑棋' : '白棋'}獲勝!`;
document.querySelectorAll('.cell').forEach(cell => cell.removeEventListener('click', handleCellClick));
return;
}
currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
}
function checkWin(row, col, player) {
const directions = [
{ dr: 0, dc: 1 }, // 水平
{ dr: 1, dc: 0 }, // 垂直
{ dr: 1, dc: 1 }, // 主對角線
{ dr: 1, dc: -1 } // 副對角線
];
for (const { dr, dc } of directions) {
let count = 1;
// 檢查一個方向
for (let i = 1; i < 5; i++) {
const r = row + dr * i;
const c = col + dc * i;
if (r < 0 || r >= boardSize || c < 0 || c >= boardSize || board[r][c] !== player) break;
count++;
}
// 檢查相反方向
for (let i = 1; i < 5; i++) {
const r = row - dr * i;
const c = col - dc * i;
if (r < 0 || r >= boardSize || c < 0 || c >= boardSize || board[r][c] !== player) break;
count++;
}
if (count >= 5) return true;
}
return false;
}
createBoard();
</script>
</body>
</html>
程式碼說明
-
HTML 結構:
#board
是棋盤的容器,使用grid
佈局來建立 15x15 的格子.cell
是每個格子的樣式,點擊時會觸發handleCellClick
事件#message
用於顯示遊戲結果
-
CSS 樣式:
- 棋盤和格子的樣式,包括背景色、邊框、棋子的樣式等
- 使用 CSS Grid 佈局實現棋盤的網格結構
- 使用偽元素實現棋子的顯示
-
JavaScript 邏輯:
createBoard
函數用於生成棋盤handleCellClick
函數處理格子的點擊事件,更新棋盤狀態並檢查是否獲勝checkWin
函數檢查當前玩家是否在點擊的位置上連成五子
使用說明
- 將程式碼儲存為
.html
檔案 - 在瀏覽器中開啟該檔案
- 黑白雙方輪流點擊棋盤上的格子下棋
- 當一方連成五子時,遊戲結束並顯示獲勝訊息
功能特點
- 支援黑白雙方輪流下棋
- 自動判斷勝負
- 防止在已有棋子的位置重複落子
- 遊戲結束後禁止繼續下棋
- 簡潔美觀的介面設計
程式碼生成範例
from openai import OpenAI
client = OpenAI(
base_url="https://api.deepseek.com/",
api_key="<YOUR_API_KEY>"
)
completion = client.chat.completions.create(
model="deepseek-chat",
messages=[
{
"role": "user",
"content": "請幫我用 HTML 生成一個五子棋遊戲,所有程式碼都儲存在一個 HTML 中。"
}
]
)
print(completion.choices[0].message.content)