我想创建一个扫雷小游戏
我想创建一个扫雷小游戏
好的,请问您对这个扫雷游戏有什么具体的要求吗?比如:
请您尽可能详细地描述您的想法,我会根据您的需求来设计和构建这个扫雷游戏。
好的,我了解了您的需求。我将为您创建一个卡通风格的扫雷游戏,具体规格如下:
目的和功能:
视觉设计要求:
交互元素和行为:
数据处理:
具体要求或约束:
接下来,我将使用这些规范来创建扫雷游戏。
<div id="app">
<div class="header">
<div class="difficulty">
<button id="easy">Easy</button>
<button id="medium">Medium</button>
<button id="hard">Hard</button>
</div>
<div class="stats">
<div class="steps">Steps: <span id="step-count">0</span></div>
<div class="mines">Mines: <span id="mine-count">0</span></div>
</div>
<button id="reset">Reset</button>
</div>
<div id="board"></div>
</div>
#app {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
width: 100%;
max-width: 500px;
margin: 0 auto;
user-select: none;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
padding: 10px;
background-color: #f0f0f0;
border-radius: 5px;
}
.difficulty button {
padding: 8px 12px;
margin: 0 5px;
border: 1px solid #ccc;
border-radius: 4px;
background-color: #fff;
cursor: pointer;
}
.difficulty button:hover, #reset:hover {
background-color:#ADD8E6
}
.difficulty button:focus, #reset:focus {
outline: none;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
}
.stats {
display: flex;
gap: 20px;
}
#board {
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(10, 1fr);
gap: 2px;
border: 2px solid #777;
background-color: #777;
width: 100%;
aspect-ratio: 1 / 1;
}
.cell {
width: 100%;
height: 100%;
background-color: #bbb;
border: 2px outset #fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em;
font-weight: bold;
cursor: pointer;
}
.cell.revealed {
border: 1px solid #777;
background-color: #ddd;
cursor: default;
}
.cell.flagged {
color: red;
}
.cell.mine {
background-color: red;
}
.cell.exploded {
background-color: orange;
}
#reset {
padding: 8px 12px;
border: 1px solid #ccc;
border-radius: 4px;
background-color: #fff;
cursor: pointer;
}
.hidden {
display: none;
}
// Your complete JavaScript here
const boardElement = document.getElementById('board');
const stepCountElement = document.getElementById('step-count');
const mineCountElement = document.getElementById('mine-count');
const easyButton = document.getElementById('easy');
const mediumButton = document.getElementById('medium');
const hardButton = document.getElementById('hard');
const resetButton = document.getElementById('reset');
let boardSize = 10;
let mines = 10;
let steps = 0;
let board = [];
let revealedCells = 0;
let flaggedCells = 0;
let gameRunning = false;
function createBoard() {
board = [];
revealedCells = 0;
flaggedCells = 0;
steps = 0;
stepCountElement.textContent = steps;
mineCountElement.textContent = mines;
boardElement.innerHTML = '';
gameRunning = true;
// Create cells
for (let row = 0; row < boardSize; row++) {
let rowArray = [];
for (let col = 0; col < boardSize; col++) {
const cell = {
row,
col,
isMine: false,
isRevealed: false,
isFlagged: false,
neighborMines: 0
};
rowArray.push(cell);
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.addEventListener('click', () => handleCellClick(cell));
cellElement.addEventListener('contextmenu', (e) => {
e.preventDefault();
handleCellRightClick(cell);
});
boardElement.appendChild(cellElement);
}
board.push(rowArray);
}
// Place mines randomly
let minesPlaced = 0;
while (minesPlaced < mines) {
const randomRow = Math.floor(Math.random() * boardSize);
const randomCol = Math.floor(Math.random() * boardSize);
if (!board[randomRow][randomCol].isMine) {
board[randomRow][randomCol].isMine = true;
minesPlaced++;
}
}
// Calculate neighbor mines
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (!board[row][col].isMine) {
board[row][col].neighborMines = countNeighborMines(row, col);
}
}
}
}
function countNeighborMines(row, col) {
let count = 0;
for (let r = Math.max(0, row - 1); r <= Math.min(boardSize - 1, row + 1); r++) {
for (let c = Math.max(0, col - 1); c <= Math.min(boardSize - 1, col + 1); c++) {
if (board[r][c].isMine) {
count++;
}
}
}
return count;
}
function handleCellClick(cell) {
if (!gameRunning || cell.isRevealed || cell.isFlagged) {
return;
}
if(!cell.isRevealed) {
steps++;
stepCountElement.textContent = steps;
}
if (cell.isMine) {
revealAllMines();
cell.isRevealed = true;
const cellElement = boardElement.children[cell.row * boardSize + cell.col];
cellElement.classList.add('exploded');
gameOver();
} else {
revealCell(cell);
if (revealedCells === boardSize * boardSize - mines) {
gameWin();
}
}
}
function handleCellRightClick(cell) {
if (!gameRunning || cell.isRevealed) {
return;
}
if (!cell.isFlagged) {
if(flaggedCells < mines) {
cell.isFlagged = true;
flaggedCells++;
const cellElement = boardElement.children[cell.row * boardSize + cell.col];
cellElement.classList.add('flagged');
cellElement.textContent = "🚩";
}
}
else {
cell.isFlagged = false;
flaggedCells--;
const cellElement = boardElement.children[cell.row * boardSize + cell.col];
cellElement.classList.remove('flagged');
cellElement.textContent = "";
}
mineCountElement.textContent = mines - flaggedCells;
}
function revealCell(cell) {
if (cell.isRevealed || cell.isFlagged) {
return;
}
cell.isRevealed = true;
revealedCells++;
const cellElement = boardElement.children[cell.row * boardSize + cell.col];
cellElement.classList.add('revealed');
if (cell.neighborMines > 0) {
cellElement.textContent = cell.neighborMines;
} else {
// Reveal neighboring cells recursively
for (let r = Math.max(0, cell.row - 1); r <= Math.min(boardSize - 1, cell.row + 1); r++) {
for (let c = Math.max(0, cell.col - 1); c <= Math.min(boardSize - 1, cell.col + 1); c++) {
revealCell(board[r][c]);
}
}
}
}
function revealAllMines() {
for (let row = 0; row < boardSize; row++) {
for (let col = 0; col < boardSize; col++) {
if (board[row][col].isMine) {
const cellElement = boardElement.children[row * boardSize + col];
cellElement.classList.add('mine');
cellElement.textContent = '💣';
}
}
}
}
function gameOver() {
gameRunning = false;
alert("Game Over!");
}
function gameWin() {
gameRunning = false;
alert(`You Win! Steps: ${steps}`);
}
function setDifficulty(difficulty) {
switch (difficulty) {
case 'easy':
mines = 10;
break;
case 'medium':
mines = 20;
break;
case 'hard':
mines = 30;
break;
}
createBoard();
}
easyButton.addEventListener('click', () => setDifficulty('easy'));
mediumButton.addEventListener('click', () => setDifficulty('medium'));
hardButton.addEventListener('click', () => setDifficulty('hard'));
resetButton.addEventListener('click', createBoard);
// Initialize game
createBoard();