gpt4 book ai didi

javascript - 将数组值分配给表格单元格

转载 作者:行者123 更新时间:2023-12-03 03:46:39 25 4
gpt4 key购买 nike

我正在尝试使用 Javascript 创建一个简单的内存游戏,并且我已经创建了一个字母数组来匹配游戏,但是,我无法弄清楚如何将单个字母实际分配给游戏的每个单元格。 table 充当董事会。

<html>
<head>
<style>
td {
background-color: red;
width:100px;
height:100px;
border:1px solid black
}
</style>
<table>
<tr>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
</tr>
<tr>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
</tr>
<tr>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
</tr>
<tr>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
<td id = "tile" onClick = "revealLetter()"></td>
</tr>
</table>
<script>
var letters = ['A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H'];
var square_values = [];
var square_ids = [];
var i = letters.length, j, temp;
while(--i > 0) {
j = Math.floor(Math.random() * (i+1));
temp = letters[j];
letters[j] = letters[i];
letters[i] = temp;
}

</script>
</head>
<body>

</body>
</html>

最佳答案

我建议采取稍微不同的方法。我没有构建表格然后放入字母,而是编写了一个基于随机数组构建网格的函数。我借用了this question的函数使数组随机化。然后,我添加了事件监听器,通过将卡片的值更改为其 data-letter 属性来“翻转”卡片。

我还没有编写其余的游戏逻辑 - 我不想踩到你的脚趾!显然你想要做到这一点,这样如果你翻了两个不匹配的,他们会返回并再试一次,但如果他们这样做了,他们会继续显示 - 我相信你可以处理逻辑并且修改我的函数,添加事件监听器。

buildGrid();

//global
var a, b = '';


function buildGrid() {
var letters = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F', 'G', 'G', 'H', 'H'];
var randomised = shuffle(letters);
var num_letters = randomised.length;
var str_tbl = '';

while (num_letters >= 4) {
str_tbl += '<tr>';
for (var i = 0; i < 4; i++) {
str_tbl += '<td data-letter="' + randomised[i] + '">?</td>';
}
str_tbl += '</tr>';
ranomdised = randomised.splice(0, 4);
num_letters -= 4;
}

var tbl = document.createElement('table');
tbl.innerHTML = str_tbl;
document.getElementsByClassName('grid')[0].appendChild(tbl);
addEventListeners();
}

function addEventListeners(){

var tds = document.getElementsByTagName( 'td' );

for( var i = 0; i < tds.length; i++ ){
tds[i].addEventListener( 'click', function(){
this.innerHTML = this.getAttribute( 'data-letter');
this.className += ' chosen';
});
}

}


// borrowed from https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}
td {
background-color: #cccccc;
text-align: centre;
padding: 20px;
height: 100px;
width: 100px;
color: #333333;
font-family: helvetica, arial, sans-serif;
font-size: 30px;
font-weight: bold;
cursor: pointer;
}

td:hover {
background-color: #dddddd;
}

td.chosen {
background-color: lightblue;
}
<div class="grid"></div>

关于javascript - 将数组值分配给表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45362420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com