我正在研究一個使用JS的記憶遊戲。我設法做了一些切換工作,在兩張卡片面之間來回改變班級。我現在試圖獲得一個功能,檢查兩張遊戲卡是否匹配,如果圖像保持翻轉,如果它們不是圖像還原到卡面。如何編寫一個函數來檢查兩個類是否相同
// variable declarations for game elements
let gameTiles = document.getElementsByClassName('game_tile');
let cardFace = document.getElementsByClassName('card_face');
let cardMatchCheck = [];
let cardArray =
[ "one", "one", "two", "two", "three", "three",
"four", "four", "five", "five", "six", "six",
"seven", "seven", "eight", "eight", "nine", "nine",
"ten", "ten", "eleven", "eleven", "twelve", "twelve" ];
// div class assignment for loop from the cardArray
// .className += allows for adding to an already assigned class
// in this case "game_tile" (our button class)
for (let i = 0; i < gameTiles.length; i++) {
for (let j = 0; j < cardArray.length; j++) {
gameTiles[i].setAttribute("class", cardArray[j]);
}
}
// click response (working)
let gameTileClick = document.getElementsByTagName('button');
for (let i = 0; i < gameTileClick.length; i++) {
gameTileClick[i].addEventListener('click', changeCardImage);
}
// testing grounds:
// this function runs through all card classes, and switches
// between both card image states (back, front, back)
// need to work on implementing the pair recognition
function changeCardImage(cardArray) {
if (cardArray.className == 'one') {
cardArray.className = 'card_one_img'
cardMatchCheck.push(cardArray.className);
cardMatch();
cardMismatch();
} else if (cardArray.className == 'card_one_img') {
cardArray.className = "one"
} else if (cardArray.className == 'two') {
cardArray.className = 'card_two_img'
cardMatchCheck.push(cardArray.className);
cardMatch();
cardMismatch();
} else if (cardArray.className == 'card_two_img') {
cardArray.className = 'two'
} else if (cardArray.className == 'three') {
cardArray.className = 'card_three_img'
} else if (cardArray.className == 'card_three_img') {
cardArray.className = 'three'
} else if (cardArray.className == 'four') {
cardArray.className = 'card_four_img'
} else if (cardArray.className == 'card_four_img') {
cardArray.className = 'four'
} else if (cardArray.className == 'five') {
cardArray.className = 'card_five_img'
} else if (cardArray.className == 'card_five_img') {
cardArray.className = 'five'
} else if (cardArray.className == 'six') {
cardArray.className = 'card_six_img'
} else if (cardArray.className == 'card_six_img') {
cardArray.className = 'six'
} else if (cardArray.className == 'seven') {
cardArray.className = 'card_seven_img'
} else if (cardArray.className == 'card_seven_img') {
cardArray.className = 'seven'
} else if (cardArray.className == 'eight') {
cardArray.className = 'card_eight_img'
} else if (cardArray.className == 'card_eight_img') {
cardArray.className = 'eight'
} else if (cardArray.className == 'nine') {
cardArray.className = 'card_nine_img'
} else if (cardArray.className == 'card_nine_img') {
cardArray.className = 'nine'
} else if (cardArray.className == 'ten') {
cardArray.className = 'card_ten_img'
} else if (cardArray.className == 'card_ten_img') {
cardArray.className = 'ten'
} else if (cardArray.className == 'eleven') {
cardArray.className = 'card_eleven_img'
} else if (cardArray.className == 'card_eleven_img') {
cardArray.className = 'eleven'
} else if (cardArray.className == 'twelve') {
cardArray.className = 'card_twelve_img'
} else if (cardArray.className == 'card_twelve_img') {
cardArray.className = 'twelve'
}
}
console.log(cardMatchCheck);
// function for checking card match/mismatch
function cardMatch() {
if (cardArray.className == cardArray.className) {
console.log('match');
}
}
function cardMismatch() {
if (cardArray.className != cardArray.className) {
cardArray.className = 'card_face';
}
}
道歉,如果任何格式是時髦 - 這是我的第一個SO帖子。任何方向都非常感謝。預先感謝您的時間!
編輯:這裏是我到目前爲止有:https://codepen.io/woolsox/pen/mMRPJK
你的問題是什麼? 請將代碼放在http://codepen.io,以便人們可以看到它的功能。 –
抱歉,只是添加了codepen。我試圖弄清楚如何編寫一個函數,讓遊戲識別兩張牌是否與他們保持透露的相同,如果沒有,他們會翻轉回來。 – woolsox
整個方法容易出錯,並且有大量不必要的重複代碼。您應該使用數組來存儲卡片,並使用負值表示卡片已翻轉。這會將那些「其他」如果減少到總共兩到三行,並允許您在任何時候添加更多圖像,而無需添加更多的複製麪食代碼。 –