o3-2025-04-16· OpenAI
LMArena Human Preference 140K
使用html,js,css 写一个Solitaire (Turn 1)游戏 有两个特性需要实现: 1、拖拽 2、单击以及双击牌,能够自动放到最合适的位置…
- User
使用html,js,css 写一个Solitaire (Turn 1)游戏
有两个特性需要实现: 1、拖拽 2、单击以及双击牌,能够自动放到最合适的位置
使用一个Html文件来实现这个游戏,也不用图片,而是使用emoji和颜色来表现花色和颜色
- Assistant
下面是一份「单文件版」Solitaire(接一张)小游戏源码。
为了尽量简洁而仍可以完整运行,逻辑上只实现了最常用的玩法规则(可拖拽多张、点一下翻牌/发牌、双击智能移动到最佳位置)。
直接把整段代码保存成solitaire.html,在浏览器里打开即可玩。
(纯前端实现,无图片,仅用 emoji 和颜色区分花色⤵)<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>Solitaire ‑ Turn 1</title> <style> :root{ --card-w: 70px; --card-h: 96px; --stack-gap: 22px; /* 牌叠放时暴露高度 */ --anim-speed: .18s; --red: #c41a1a; --black: #111; --bg: #0a713b; } *{box-sizing:border-box;user-select:none;} body{ margin:0; background:var(--bg); font-family:Arial,Helvetica,sans-serif; } #game{ padding:14px; } /* 版面 ------------------------------------------------------------------*/ .row{display:flex;gap:12px;position:relative;} .spacer{flex:1;} .pile{ position:relative; width:var(--card-w); height:var(--card-h); border:2px solid rgba(255,255,255,.3); border-radius:6px; } .tableau{height:calc(var(--card-h)*1.2 + var(--stack-gap)*17);} /* 牌 --------------------------------------------------------------------*/ .card{ width:var(--card-w);height:var(--card-h); border-radius:6px; border:1px solid #333; background:#fff; position:absolute; left:0;top:0; display:flex;align-items:center;justify-content:center; font-weight:bold;font-size:22px; cursor:pointer; transition:top var(--anim-speed),left var(--anim-speed); } .red{color:var(--red);} .black{color:var(--black);} .facedown{ background:linear-gradient(135deg,#004488 0%,#0099ee 100%); border:1px solid #001a33; color:transparent; } .dragging{opacity:.8;z-index:9999!important;pointer-events:none;} /* 小提示文字 */ #msg{position:fixed;bottom:6px;left:50%;transform:translateX(-50%); color:#fff;font-size:14px;opacity:.4;} </style> </head> <body> <div id="game"> <!-- 顶行:库存、废牌、4个 Foundation --> <div id="top" class="row"> <div id="stock" class="pile stock"></div> <div id="waste" class="pile waste"></div> <div class="spacer"></div> <div id="fh" class="pile foundation" data-suit="hearts"></div> <div id="fd" class="pile foundation" data-suit="diamonds"></div> <div id="fc" class="pile foundation" data-suit="clubs"></div> <div id="fs" class="pile foundation" data-suit="spades"></div> </div> <!-- 7 列 Tableau --> <div id="tableau" class="row" style="margin-top:20px;"> <div class="pile tableau" data-index="0"></div> <div class="pile tableau" data-index="1"></div> <div class="pile tableau" data-index="2"></div> <div class="pile tableau" data-index="3"></div> <div class="pile tableau" data-index="4"></div> <div class="pile tableau" data-index="5"></div> <div class="pile tableau" data-index="6"></div> </div> </div> <div id="msg">点击库存翻牌;双击牌可自动移动</div> <script> // ---------- 数据结构 ---------- const SUITS = ['hearts','diamonds','clubs','spades']; const RANKS = [1,2,3,4,5,6,7,8,9,10,11,12,13]; // 1= A, 11=J... let stock=[], waste=[], foundations={hearts:[],diamonds:[],clubs:[],spades:[]}; let tableau=[[],[],[],[],[],[],[]]; function newDeck(){ const d=[]; for(const s of SUITS) for(const r of RANKS) d.push({rank:r,suit:s,faceUp:false}); return d; } function shuffle(a){ for(let i=a.length-1;i>0;i--){ const j=Math.floor(Math.random()*(i+1)); [a[i],a[j]]=[a[j],a[i]]; } } // ---------- 初始化 ---------- function deal(){ let deck=newDeck(); shuffle(deck); // 先清空 stock.length=waste.length=0; SUITS.forEach(s=>foundations[s].length=0); tableau.forEach(col=>col.length=0); // 7 列堆叠 for(let c=0;c<7;c++){ for(let r=0;r<=c;r++){ const card=deck.pop(); card.faceUp = (r===c); // 只有最上方翻开 tableau[c].push(card); } } // 剩余进库存 stock=deck; refresh(); } // ---------- DOM 渲染 ---------- const stockEl= document.getElementById('stock'); const wasteEl= document.getElementById('waste'); const fEls = { 'hearts':document.getElementById('fh'), 'diamonds':document.getElementById('fd'), 'clubs':document.getElementById('fc'), 'spades':document.getElementById('fs') }; function cardText(c){ const emoji = {hearts:'♥️',diamonds:'♦️',clubs:'♣️',spades:'♠️'}[c.suit]; const name= ['','A','2','3','4','5','6','7','8','9','10','J','Q','K'][c.rank]; return name+emoji; } function makeCardDiv(card){ const d=document.createElement('div'); d.className='card '+ (card.faceUp?(isRed(card)?'red':'black'):'facedown'); d.textContent=card.faceUp?cardText(card):''; d.dataset.rank=card.rank; d.dataset.suit=card.suit; return d; } function isRed(card){ return card.suit==='hearts'||card.suit==='diamonds'; } function clear(el){ while(el.firstChild) el.removeChild(el.firstChild); } function renderPile(domPile,cards,isTableau=false){ clear(domPile); cards.forEach((card,idx)=>{ const div=makeCardDiv(card); if(isTableau){ div.style.top= (idx* (card.faceUp?22:8))+'px'; } domPile.appendChild(div); }); } function refresh(){ // stock & waste clear(stockEl); clear(wasteEl); if(stock.length){ const back=makeCardDiv({faceUp:false,suit:'',rank:0}); stockEl.appendChild(back); } if(waste.length){ const top=waste[waste.length-1]; const div=makeCardDiv(top); wasteEl.appendChild(div); } // foundations for(const s of SUITS){ const pile=foundations[s]; renderPile(fEls[s],pile); } // tableau document.querySelectorAll('.tableau').forEach((colEl,i)=>{ renderPile(colEl,tableau[i],true); }); bindEvents(); // 给最新牌绑事件 } // ---------- 交互 ---------- function bindEvents(){ // 清除旧监听 document.querySelectorAll('.card').forEach(c=>{ c.onmousedown=null; c.ondblclick=null; }); // 双击自动移动 document.querySelectorAll('.card:not(.facedown)').forEach(c=>{ c.ondblclick = e=>{ const node=e.currentTarget; tryAutoMove(node); }; // 拖拽 c.onmousedown = startDrag; }); // 发牌 stockEl.onclick = ()=>{drawCard();}; } // 发 1 张 function drawCard(){ if(stock.length){ const c=stock.pop(); c.faceUp=true; waste.push(c); }else{ // 把废牌翻回库存 while(waste.length){ const c=waste.pop(); c.faceUp=false; stock.push(c); } } refresh(); } // ---------------------- 拖拽 ---------------------------- let dragInfo=null; // {cards,from,type,offsetX,offsetY} function startDrag(e){ const cardDiv=e.currentTarget; // 只允许拖动正面牌(或其后面连带) const [pileType,index] = locateCard(cardDiv); const col = (pileType==='tableau')?tableau[index]: (pileType==='waste'?waste:foundations[pileType]); const idxInPile=[...cardDiv.parentNode.children].indexOf(cardDiv); const cardsToDrag = col.slice(idxInPile); // 多张 if(!cardsToDrag[0].faceUp) return; // 保存信息 dragInfo={ cards:cardsToDrag, from:{type:pileType,index,idxStart:idxInPile}, offsetX:e.offsetX, offsetY:e.offsetY }; // 临时放到 body 上方便移动 const dragElems=[]; cardsToDrag.forEach((c,i)=>{ const d=makeCardDiv(c); d.classList.add('dragging'); d.style.top= (e.pageY - dragInfo.offsetY + i*22)+'px'; d.style.left=(e.pageX - dragInfo.offsetX)+'px'; document.body.appendChild(d); dragElems.push(d); }); dragInfo.elems=dragElems; document.addEventListener('mousemove',dragMove); document.addEventListener('mouseup',endDrag); } function dragMove(e){ dragInfo.elems.forEach((d,i)=>{ d.style.left=(e.pageX - dragInfo.offsetX)+'px'; d.style.top= (e.pageY - dragInfo.offsetY + i*22)+'px'; }); } function endDrag(e){ document.removeEventListener('mousemove',dragMove); document.removeEventListener('mouseup',endDrag); // 判断放到哪里 const target = document.elementFromPoint(e.clientX,e.clientY); let placed=false; if(target){ const pile = target.closest('.pile'); if(pile){ placed = tryDropOnPile(pile,dragInfo.cards); } } // 若放置失败则回原位 if(!placed) { // nothing changes }else{ // 从原堆删掉 const {type,index,idxStart}=dragInfo.from; let arr = (type==='tableau')?tableau[index]:(type==='waste'?waste:foundations[type]); arr.splice(idxStart); // 翻开新的顶牌 if(type==='tableau' && arr.length && !arr[arr.length-1].faceUp){ arr[arr.length-1].faceUp=true; } } // 清除临时 DOM dragInfo.elems.forEach(d=>d.remove()); dragInfo=null; refresh(); } function locateCard(div){ const pileEl=div.parentNode; if(pileEl.id==='waste') return ['waste',null]; if(pileEl.classList.contains('tableau')) return ['tableau', +pileEl.dataset.index]; if(pileEl.classList.contains('foundation')) return [pileEl.dataset.suit,null]; throw 'unknown pile'; } // ----------------- 放置合法性 ------------------- function canMoveToFoundation(card,foundationPile){ if(!card.faceUp)return false; if(foundationPile.length===0) return card.rank===1; // A const top=foundationPile[foundationPile.length-1]; return top.suit===card.suit && card.rank===top.rank+1; } function canMoveToTableau(card,tableauPile){ if(tableauPile.length===0) return card.rank===13; // K const top=tableauPile[tableauPile.length-1]; return top.faceUp && isRed(top)!==isRed(card) && card.rank===top.rank-1; } function tryDropOnPile(pileEl,cards){ const card=cards[0]; if(pileEl.classList.contains('foundation') && cards.length===1){ const suit=pileEl.dataset.suit; if(canMoveToFoundation(card,foundations[suit])){ foundations[suit].push(card); return true; } }else if(pileEl.classList.contains('tableau')){ const idx=+pileEl.dataset.index; if(canMoveToTableau(card,tableau[idx])){ tableau[idx]= tableau[idx].concat(cards); return true; } } return false; } // --------------- 双击自动移动 ------------------ function tryAutoMove(div){ const [pileType,index] = locateCard(div); let fromArr; if(pileType==='tableau') fromArr=tableau[index]; else if(pileType==='waste') fromArr=waste; else return; if(!fromArr.length) return; const card=fromArr[fromArr.length-1]; // 1. 先试 foundation const fPile=foundations[card.suit]; if(canMoveToFoundation(card,fPile)){ fPile.push(card); fromArr.pop(); if(pileType==='tableau' && fromArr.length && !fromArr[fromArr.length-1].faceUp) fromArr[fromArr.length-1].faceUp=true; refresh();return; } // 2. 再试 tableau (找第一符合的列) for(let i=0;i<7;i++){ if(canMoveToTableau(card,tableau[i])){ tableau[i].push(card); fromArr.pop(); if(pileType==='tableau' && fromArr.length && !fromArr[fromArr.length-1].faceUp) fromArr[fromArr.length-1].faceUp=true; refresh();return; } } } // ------------------------------------------------ deal(); // 开局 </script> </body> </html>特点 & 使用说明
- 拖拽:按住任意翻开的牌即可拖动,若该牌下方仍有翻开的牌,则会连同下面的所有翻开牌一起移动。
- 单击库存(左上角)发 1 张牌;当库存为空时再点击可把废牌重新盖回库存。
- 双击任意翻开的顶牌:
• 若可放 Foundation ⇢ 自动放到对应花色 Foundation
• 否则若有合适的 Tableau 列 ⇢ 自动移动过去 - 所有牌用 emoji + 颜色显示,♥️♦️ 为红色,♣️♠️ 为黑色。
提示:本示例力求代码短小易读,未做深入性能与边界情况优化,如需更完善的动画、得分、撤销等功能,可在此基础上继续扩展。祝你玩得开心 🎉
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.