|
43 | 43 | <button onclick="drawCards()">抽牌</button>
|
44 | 44 | <button onclick="cal()" style="display: none;">算牌</button>
|
45 | 45 | <button onclick="showResult(true)">显示结果</button>
|
| 46 | + <!-- 显示四个按钮,代表四则运算,点击后赋值selectedMethord 0:+,1:-,2:*,3:/--> |
| 47 | + <button onclick="selectedMethord(0)">+</button> |
| 48 | + <button onclick="selectedMethod(1)">-</button> |
| 49 | + <button onclick="selectedMethod(2)">*</button> |
| 50 | + <button onclick="selectedMethod(3)">/</button> |
46 | 51 |
|
47 | 52 | </div>
|
48 | 53 |
|
|
60 | 65 | const suits = ['♠', '♥', '♣', '♦'];
|
61 | 66 | const values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
|
62 | 67 | const deck = [];
|
63 |
| - |
| 68 | + const method = ['+', '-', '*', '/']; |
| 69 | + |
| 70 | + function selectedMethod(i) { |
| 71 | + selMethord = i; |
| 72 | + if(selectedCards.length == 2){ |
| 73 | + var cd = calc(cardToNumber(selectedCards[0]), selMethord, cardToNumber(selectedCards[1])); |
| 74 | + document.getElementById(`card${selectedCards[0]}`).display = "none"; |
| 75 | + document.getElementById(`card${selectedCards[1]}`).display = "none"; |
| 76 | + selectedCards = []; |
| 77 | + selMethord = -1; |
| 78 | + addCard(cd+" ", "#007f0e",cards.length); |
| 79 | + } |
| 80 | + } |
64 | 81 | // 创建扑克牌数组
|
65 | 82 | for (let suit of suits) {
|
66 | 83 | for (let value of values) {
|
|
88 | 105 | // 显示抽出的牌
|
89 | 106 | const cardContainer = document.getElementById('cardContainer');
|
90 | 107 | cardContainer.innerHTML = ''; // 清空容器
|
91 |
| - |
| 108 | + var index = 0; |
92 | 109 | drawnCards.forEach(card => {
|
93 | 110 | const color = getColorForSuit(card.slice(-1)); // 根据花色获取颜色
|
94 | 111 | // 使用SVG绘制圆角矩形并显示牌
|
95 |
| - const svg = ` |
| 112 | + addCard(card, color,index); |
| 113 | + index++; |
| 114 | + |
| 115 | + }); |
| 116 | + cards = drawnCards; |
| 117 | + cal(); |
| 118 | + } |
| 119 | + var selectedCards = []; |
| 120 | + function clcikCard(index){ |
| 121 | + if(selectedCards.length <2){ |
| 122 | + selectedCards.push(index); |
| 123 | + document.getElementById(`card${index}`).style.border = "2px solid blue"; |
| 124 | + }else if(selectedCards.length == 2){ |
| 125 | + if(selMethord != -1){ |
| 126 | + var cd = calc(cardToNumber(selectedCards[0]), selMethord, cardToNumber(selectedCards[1])); |
| 127 | + document.getElementById(`card${selectedCards[0]}`).display = "none"; |
| 128 | + document.getElementById(`card${selectedCards[1]}`).display = "none"; |
| 129 | + selMethord = -1; |
| 130 | + addCard(cd+" ", "#007f0e",cards.length); |
| 131 | + } |
| 132 | + document.getElementById(`card${selectedCards[0]}`).style.border = ""; |
| 133 | + document.getElementById(`card${selectedCards[1]}`).style.border = ""; |
| 134 | + selectedCards = []; |
| 135 | + } |
| 136 | + } |
| 137 | + function addCard(card, color,index) { |
| 138 | + const svg = ` |
96 | 139 | <svg width="100" height="150">
|
97 | 140 | <rect width="100" height="150" rx="15" ry="15" style="fill:#f0f0f0;stroke:${color};stroke-width:2" />
|
98 | 141 | <text x="50%" y="50%" alignment-baseline="middle" fill="${color}" text-anchor="middle" font-size="24" font-weight="bold">${card}</text>
|
99 | 142 | </svg>
|
100 | 143 | `;
|
101 | 144 | const div = document.createElement('div');
|
102 | 145 | div.className = 'card';
|
| 146 | + div.id = `card${index}`; |
| 147 | + div.setAttribute('onclick', `clcikCard(${index})`); |
103 | 148 | div.innerHTML = svg;
|
104 | 149 | cardContainer.appendChild(div);
|
105 |
| - }); |
106 |
| - cards = drawnCards; |
107 |
| - cal(); |
108 | 150 | }
|
109 | 151 | function getColorForSuit(suit) {
|
110 | 152 | switch (suit) {
|
|
198 | 240 | document.getElementById("inputs").innerHTML = str1 + "<br>";
|
199 | 241 | // display the inputs
|
200 | 242 | }
|
| 243 | + function calc(num1, op1, num2) { |
| 244 | + var num3 = 0.0; |
| 245 | + switch (op1) { |
| 246 | + case 0: |
| 247 | + num3 = num1 + num2; |
| 248 | + break; |
| 249 | + case 1: |
| 250 | + num3 = num1 - num2; |
| 251 | + break; |
| 252 | + case 2: |
| 253 | + num3 = num1 * num2; |
| 254 | + break; |
| 255 | + case 3: |
| 256 | + num3 = num1 / num2; |
| 257 | + break; |
| 258 | + } |
| 259 | + if (Math.abs(num3 - Math.round(num3)) < ep) |
| 260 | + return (Math.round(num3)); |
| 261 | + else |
| 262 | + return (num3); |
| 263 | + } |
201 | 264 | </script>
|
202 | 265 | </body>
|
203 | 266 |
|
|
0 commit comments