2017-11-30 7 views
2

このゲームのカードを元の位置に戻すにはどうしたらいいですか?

var cards = [ 
 
    { 
 
    rank: "Queen", 
 
    suit: "Hearts", 
 
    cardImage: "images/queen-of-hearts.png", 
 
    id: 0, 
 
    }, 
 
    { 
 
    rank: "Queen", 
 
    suit: "Diamonds", 
 
    cardImage: "images/queen-of-diamonds.png", 
 
    id: 1, 
 
    }, 
 
    { 
 
    rank: "King", 
 
    suit: "Hearts", 
 
    cardImage: "images/king-of-hearts.png", 
 
    id: 2, 
 
    }, 
 
    { 
 
    rank: "King", 
 
    suit: "Diamonds", 
 
    cardImage: "images/king-of-diamonds.png", 
 
    id: 3 
 
    } 
 
]; 
 
//1 
 
function createBoard() { 
 
    for (var i = 0; i < cards.length; i++) { 
 
    var cardElement = document.createElement('img'); 
 
    // console.log(cardElement); 
 
    cardElement.setAttribute('src', 'images/back.png'); 
 
    cardElement.setAttribute('data-id', i); 
 
    document.getElementById('game-board').appendChild(cardElement); 
 
    cardElement.addEventListener('click', flipCard); 
 
    cardElement.style.width = '210px'; 
 

 
    } 
 
} 
 
createBoard(); 
 
//2 
 
function flipCard() { 
 
    var cardId = this.getAttribute('data-id'); 
 
    cardsInPlay.push(cards[cardId].rank); 
 
    cardsInPlay.push(cards[cardId].id); 
 
    this.setAttribute('src', cards[cardId].cardImage); 
 

 
// CHECK FOR MATCH HERE => 
 
    if (cardsInPlay.length === 2) { 
 
     if (cardsInPlay[0] === cardsInPlay[1]) { 
 
     alert("You found a match!"); 
 
     } 
 
     else { 
 
     alert("Sorry, try again."); 
 
     console.log(cardsInPlay); 
 
     cardsInPlay[0].setAttribute('src', 'images/back.png'); // this doesnt work 
 
     cardsInPlay[1].setAttribute('src', 'images/back.png'); // this doesnt work 
 
     } 
 
    } 
 
} 
 
var cardsInPlay = [];
body{ 
 
    text-align: center; 
 
    margin: 0; 
 

 
} 
 

 
h1 { 
 
    font-family: "Raleway", sans-serif; 
 
    font-weight: 400; 
 
    color: #0d2c40; 
 
    font-size: 45px; 
 
    letter-spacing: 1px; 
 
    margin: 0; 
 
    color: white; 
 
} 
 

 
p { 
 
    font-family: "Droid Serif", serif; 
 
    line-height: 26px; 
 
    font-size: 18px; 
 
} 
 

 
a { 
 
    font-family: raleway; 
 
    text-decoration: none; 
 
    color: #F15B31; 
 
    letter-spacing: 1px; 
 
    font-weight: 600; 
 
    font-size: 18px; 
 

 
} 
 

 
h2 { 
 
    font-family: raleway; 
 
    font-size: 20px; 
 
    color: #0d2c40; 
 
    letter-spacing: 1px; 
 
    font-weight: 600; 
 
} 
 

 
header { 
 
    background-color: #F15B31; 
 
    padding: 30px 20px 30px 20px; 
 
} 
 

 
main { 
 
    width: 850px; 
 
    margin: 35px auto 
 
} 
 

 
a { 
 
    margin: 0 20px; 
 
    color: white; 
 
} 
 

 
nav a:hover { 
 
    border-bottom: 2px solid white; 
 
} 
 

 
nav { 
 
    background-color: #00A6B3; 
 
    padding: 20px 0; 
 
} 
 

 
img { 
 
    margin: 40px 8px 0 8px; 
 
} 
 

 
footer { 
 
    text-transform: uppercase; 
 
    padding: 0 20px; 
 
    background-color: #0D2C40; 
 
    color: white; 
 
    letter-spacing: .08em; 
 
    font-weight: 500; 
 
} 
 

 
.copyright { 
 
    float: left; 
 
} 
 

 
.message { 
 
    float: right; 
 
} 
 

 
.clearfix:after { 
 
    visibility: hidden; 
 
    display: block; 
 
    content: " "; 
 
    clear: both; 
 
    height: 0; 
 
    font-size: 0; 
 
} 
 

 
.name { 
 
    color: #F15B31; 
 
    font-weight: 700; 
 
} 
 
#game-board{ 
 

 
    width: 1200px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <link href="css/style.css" rel="stylesheet" type="text/css"> 
 

 
     <title>Memory card game</title> 
 
    </head> 
 
    <body> 
 
     <header> 
 
     <h1>Memory Game</h1> 
 
     </header> 
 
     <nav> 
 
     <p><a href="#">INSTRUCTIONS</a><a href="#"> GAME</a></p> 
 
     </nav> 
 
     <main> 
 
     <h2>INSTRUCTIONS</h2> 
 
     <p>Concentration, also known as Match Match, Memory, Pelmanism, Shinkei-suijaku, Pexeso or simply Pairs, is a card game in which all of the cards are laid face down on a surface and two cards are flipped face up over each turn. The object of the game is to turn over pairs of matching cards.</p> 
 
     <div id="game-board" class="board clearfix"></div> 
 
     </main> 
 
     <footer> 
 
      <div class="clearfix"> 
 
      <p class="copyright">Copyright 2017</p> 
 
      <p class="message">Created with &hearts; by <span class="name">GA</span></p> 
 
       </div> 
 
     </footer> 
 
    <script src="js/main.js"></script> 
 
    </body> 
 
</html>

私は元の位置に一致していませんでした両方のカードを元に戻すことができる方法を知りたいです。試合がある場合は、あなたが勝利したという警告が表示されます。それ以外の場合はもう一度試してください。しかし、2枚のカードが一致しなかった場合は元の位置に戻してください。カードは1枚だけ元の位置に戻っています(これを持っていますが、これは両方を指すと思いました)カードの画像はここにはありません。誰か助けてもらえますか?

var cards = [ 
 
    { 
 
    rank: "Queen", 
 
    suit: "Hearts", 
 
    cardImage: "images/queen-of-hearts.png", 
 
    id: 0, 
 
    }, 
 
    { 
 
    rank: "Queen", 
 
    suit: "Diamonds", 
 
    cardImage: "images/queen-of-diamonds.png", 
 
    id: 1, 
 
    }, 
 
    { 
 
    rank: "King", 
 
    suit: "Hearts", 
 
    cardImage: "images/king-of-hearts.png", 
 
    id: 2, 
 
    }, 
 
    { 
 
    rank: "King", 
 
    suit: "Diamonds", 
 
    cardImage: "images/king-of-diamonds.png", 
 
    id: 3 
 
    } 
 
]; 
 
//1 
 
function createBoard() { 
 
    for (var i = 0; i < cards.length; i++) { 
 
    var cardElement = document.createElement('img'); 
 
    // console.log(cardElement); 
 
    cardElement.setAttribute('src', 'images/back.png'); 
 
    cardElement.setAttribute('data-id', i); 
 
    document.getElementById('game-board').appendChild(cardElement); 
 
    cardElement.addEventListener('click', flipCard); 
 
    cardElement.style.width = '210px'; 
 

 
    } 
 
} 
 
createBoard(); 
 
//2 
 
function flipCard() { 
 
    var cardId = this.getAttribute('data-id'); 
 
    cardsInPlay.push(cards[cardId].rank); 
 
    cardsInPlay.push(cards[cardId].id); 
 
    this.setAttribute('src', cards[cardId].cardImage); 
 

 
// CHECK FOR MATCH HERE => 
 
    if (cardsInPlay.length === 2) { 
 
     if (cardsInPlay[0] === cardsInPlay[1]) { 
 
     alert("You found a match!"); 
 
     } 
 
     else { 
 
     alert("Sorry, try again."); 
 
     console.log(cardsInPlay); 
 
     cardsInPlay[0].setAttribute('src', 'images/back.png'); // this doesnt work 
 
     cardsInPlay[1].setAttribute('src', 'images/back.png'); // this doesnt work 
 
     } 
 
    } 
 
} 
 
var cardsInPlay = [];
body{ 
 
    text-align: center; 
 
    margin: 0; 
 

 
} 
 

 
h1 { 
 
    font-family: "Raleway", sans-serif; 
 
    font-weight: 400; 
 
    color: #0d2c40; 
 
    font-size: 45px; 
 
    letter-spacing: 1px; 
 
    margin: 0; 
 
    color: white; 
 
} 
 

 
p { 
 
    font-family: "Droid Serif", serif; 
 
    line-height: 26px; 
 
    font-size: 18px; 
 
} 
 

 
a { 
 
    font-family: raleway; 
 
    text-decoration: none; 
 
    color: #F15B31; 
 
    letter-spacing: 1px; 
 
    font-weight: 600; 
 
    font-size: 18px; 
 

 
} 
 

 
h2 { 
 
    font-family: raleway; 
 
    font-size: 20px; 
 
    color: #0d2c40; 
 
    letter-spacing: 1px; 
 
    font-weight: 600; 
 
} 
 

 
header { 
 
    background-color: #F15B31; 
 
    padding: 30px 20px 30px 20px; 
 
} 
 

 
main { 
 
    width: 850px; 
 
    margin: 35px auto 
 
} 
 

 
a { 
 
    margin: 0 20px; 
 
    color: white; 
 
} 
 

 
nav a:hover { 
 
    border-bottom: 2px solid white; 
 
} 
 

 
nav { 
 
    background-color: #00A6B3; 
 
    padding: 20px 0; 
 
} 
 

 
img { 
 
    margin: 40px 8px 0 8px; 
 
} 
 

 
footer { 
 
    text-transform: uppercase; 
 
    padding: 0 20px; 
 
    background-color: #0D2C40; 
 
    color: white; 
 
    letter-spacing: .08em; 
 
    font-weight: 500; 
 
} 
 

 
.copyright { 
 
    float: left; 
 
} 
 

 
.message { 
 
    float: right; 
 
} 
 

 
.clearfix:after { 
 
    visibility: hidden; 
 
    display: block; 
 
    content: " "; 
 
    clear: both; 
 
    height: 0; 
 
    font-size: 0; 
 
} 
 

 
.name { 
 
    color: #F15B31; 
 
    font-weight: 700; 
 
} 
 
#game-board{ 
 

 
    width: 1200px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <link href="css/style.css" rel="stylesheet" type="text/css"> 
 
     
 
     <title>Memory card game</title> 
 
    </head> 
 
    <body> 
 
     <header> 
 
     <h1>Memory Game</h1> 
 
     </header> 
 
     <nav> 
 
     <p><a href="#">INSTRUCTIONS</a><a href="#"> GAME</a></p> 
 
     </nav> 
 
     <main> 
 
     <h2>INSTRUCTIONS</h2> 
 
     <p>Concentration, also known as Match Match, Memory, Pelmanism, Shinkei-suijaku, Pexeso or simply Pairs, is a card game in which all of the cards are laid face down on a surface and two cards are flipped face up over each turn. The object of the game is to turn over pairs of matching cards.</p> 
 
     <div id="game-board" class="board clearfix"></div> 
 
     </main> 
 
     <footer> 
 
      <div class="clearfix"> 
 
      <p class="copyright">Copyright 2017</p> 
 
      <p class="message">Created with &hearts; by <span class="name">GA</span></p> 
 
       </div> 
 
     </footer> 
 
    <script src="js/main.js"></script> 
 
    </body> 
 
</html>

+0

@AuxTaco AuxTacoあなたは再び私を助けてくださいできますか? :) – fran

+0

あなたは1つの答えに満足していますか?あなたはそれを受け入れ、あなたを助けるために私たちに報酬を与えることができます! – RaphaMex

答えて

0

EDIT: your problem with this is about closure in javascript. I recommend you use IIFE (for EcmaScript5) or let keyword (for EcmaScript6). Read more here .

の代わりにこれをやって:

function createBoard() { 
    for (var i = 0; i < cards.length; i++) { 
    var cardElement = document.createElement('img'); 
    [..] 
    cardElement.addEventListener('click', flipCard); << 'this' will refer to last cardElement at the end of the loop 

この操作を行います。今、あなたが戻って簡単にカードを反転させることができ

function createBoard() { 
    for (var i = 0; i < cards.length; i++) { 
    var cardElement = document.createElement('img'); 
    [..] 
    cardElement.addEventListener('click', (function(x) {return function() {flipCard(x)}})(i)); // 'i' is immediately evaluated to the correct value 
    cards[i].element = cardElement; // Keep association with DOM here 

を。

function flipCard (i) { 
    cardsInPlay.push(i); 
    // Flip played card 
    cards[i].element.setAttribute('src', cards[i].cardImage); 

    if (cardsInPlay.length === 1) 
    return; // First card: no game resolution yet 

    // Second card: give user 1s to see it flipped before flipping back 
    setTimeout(function(){ 
    if (cards[cardsInPlay[0]].rank === cards[cardsInPlay[1]].rank) 
     alert("You found a match!"); 
    else 
     alert("Sorry, try again."); 
    cardsInPlay.forEach(function(i) { 
     cards[i].element.setAttribute('src', 'images/back.png'); 
    }); 
    cardsInPlay.length = 0; 
    }, 1000); 
} 
+0

私は2枚のカードをクリックするとconsole.logで最後にクリックしたカードしか見ることができません。私はこれがなぜ私がなぜdoesntの仕事を得るかをクリックしたものを指していると思った。しかし、大変ありがとうございます> :) – fran

+0

さらに簡単に言うと、 'cardsInPlay'はカードのインデックス(すなわちID)を保存することができます。だから、あなたのオブジェクトを渡す必要はありません。 – RaphaMex

+0

最後に、配列 'cardsInPlay'ですべてのプッシュを更新しました。コードを簡単にします。 – RaphaMex

0

あなたが戻って両方のカードを反転しようとしている場合、このコードはのみのプレイで最後のカードを反転させます「cardsInPlay [1]」:あなたが欲しい

this.setAttribute('src', 'images/back.png'); 

は「cardsInPlay [両方を反転することです0]」と "cardsInPlay [1]" ので、多分このようsomethign:

else { 
    alert("Sorry, try again."); 
    console.log(cardsInPlay); 
    cardsInPlay[0].setAttribute('src', 'images/back.png'); 
    cardsInPlay[1].setAttribute('src', 'images/back.png'); 
    } 
+0

これは表示されません> TypeError:cardsInPlay [0]。setAttributeは関数ではありません 私はこれまでに試したことがあり、setAttributeをこれに適用すると動作します。 私はそのことを言ったように、 – fran

+0

ジョエルクルーズと私は新しいですが、これは最後にクリックしたカードを指していますか? – fran

+0

@fran最初に "cardElement.addEventListener( 'click'、flipCard);を実行すると、それは通過し、if文(cardInPlay.length === 2)を引き起こすことは決してありません。そして、それは2回目になり、現在のカード上で実行されます。最善のことはRaphaMexが答えた通りにカードのIDを保管することです。 –

0

以下のスニペットは、必要な機能を実現するために変更されました!あなたは確認していただけますか?

var cards = [ 
 
    { 
 
    rank: "Queen", 
 
    suit: "Hearts", 
 
    cardImage: "http://via.placeholder.com/350x150?text=QueenHeartsfront" 
 
    }, 
 
    { 
 
    rank: "Queen", 
 
    suit: "Diamonds", 
 
    cardImage: "http://via.placeholder.com/350x150?text=QueenDiamondsfront" 
 
    }, 
 
    { 
 
    rank: "King", 
 
    suit: "Hearts", 
 
    cardImage: "http://via.placeholder.com/350x150?text=KingHeartsfront" 
 
    }, 
 
    { 
 
    rank: "King", 
 
    suit: "Diamonds", 
 
    cardImage: "http://via.placeholder.com/350x150?text=KingDiamondsfront" 
 
    } 
 
]; 
 
//1 CREATE BOARD 
 
function createBoard() { 
 
    for (var i = 0; i < cards.length; i++) { 
 
    var cardElement = document.createElement('img'); 
 
    // console.log(cardElement); 
 
    cardElement.setAttribute('src', 'http://via.placeholder.com/350x150?text=back'); 
 
    cardElement.setAttribute('data-id', i); 
 
    document.getElementById('game-board').appendChild(cardElement); 
 
    cardElement.addEventListener('click', flipCard); 
 
    cardElement.style.width = '210px'; 
 
    } 
 
} 
 
createBoard(); 
 
var prev = ""; 
 
//2 FLIPCARD 
 
function flipCard() { 
 
    var cardId = this.getAttribute('data-id'); 
 
    cardsInPlay.push(cards[cardId].rank); 
 
    this.setAttribute('src', cards[cardId].cardImage); 
 
    console.log(cardsInPlay[0]); 
 
    console.log(cardsInPlay[1]); 
 
    if (cardsInPlay.length === 2) { 
 
     if (cardsInPlay[0] === cardsInPlay[1]) { 
 
     alert("You found a match!"); 
 
     cardsInPlay = []; 
 
     } 
 
     else { 
 
     alert("Sorry, try again."); 
 
     cardsInPlay = []; 
 
     // cardsInPlay.pop(); 
 
     // cardsInPlay.pop(); 
 
     // console.log(cardsInPlay); 
 
     try{ 
 
     prev.setAttribute('src', 'http://via.placeholder.com/350x150?text=back'); 
 
     }catch(e){} 
 
     this.setAttribute('src', 'http://via.placeholder.com/350x150?text=back'); 
 
     } 
 
    } 
 
    prev = this; 
 
} 
 
var cardsInPlay = [];
body{ 
 
    text-align: center; 
 
    margin: 0; 
 

 
} 
 

 
h1 { 
 
    font-family: "Raleway", sans-serif; 
 
    font-weight: 400; 
 
    color: #0d2c40; 
 
    font-size: 45px; 
 
    letter-spacing: 1px; 
 
    margin: 0; 
 
    color: white; 
 
} 
 

 
p { 
 
    font-family: "Droid Serif", serif; 
 
    line-height: 26px; 
 
    font-size: 18px; 
 
} 
 

 
a { 
 
    font-family: raleway; 
 
    text-decoration: none; 
 
    color: #F15B31; 
 
    letter-spacing: 1px; 
 
    font-weight: 600; 
 
    font-size: 18px; 
 

 
} 
 

 
h2 { 
 
    font-family: raleway; 
 
    font-size: 20px; 
 
    color: #0d2c40; 
 
    letter-spacing: 1px; 
 
    font-weight: 600; 
 
} 
 

 
header { 
 
    background-color: #F15B31; 
 
    padding: 30px 20px 30px 20px; 
 
} 
 

 
main { 
 
    width: 850px; 
 
    margin: 35px auto 
 
} 
 

 
a { 
 
    margin: 0 20px; 
 
    color: white; 
 
} 
 

 
nav a:hover { 
 
    border-bottom: 2px solid white; 
 
} 
 

 
nav { 
 
    background-color: #00A6B3; 
 
    padding: 20px 0; 
 
} 
 

 
img { 
 
    margin: 40px 8px 0 8px; 
 
} 
 

 
footer { 
 
    text-transform: uppercase; 
 
    padding: 0 20px; 
 
    background-color: #0D2C40; 
 
    color: white; 
 
    letter-spacing: .08em; 
 
    font-weight: 500; 
 
} 
 

 
.copyright { 
 
    float: left; 
 
} 
 

 
.message { 
 
    float: right; 
 
} 
 

 
.clearfix:after { 
 
    visibility: hidden; 
 
    display: block; 
 
    content: " "; 
 
    clear: both; 
 
    height: 0; 
 
    font-size: 0; 
 
} 
 

 
.name { 
 
    color: #F15B31; 
 
    font-weight: 700; 
 
} 
 
#game-board{ 
 

 
    width: 1200px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <link href="css/style.css" rel="stylesheet" type="text/css"> 
 
     
 
     <title>Memory card game</title> 
 
    </head> 
 
    <body> 
 
     <header> 
 
     <h1>Memory Game</h1> 
 
     </header> 
 
     <nav> 
 
     <p><a href="#">INSTRUCTIONS</a><a href="#"> GAME</a></p> 
 
     </nav> 
 
     <main> 
 
     <h2>INSTRUCTIONS</h2> 
 
     <p>Concentration, also known as Match Match, Memory, Pelmanism, Shinkei-suijaku, Pexeso or simply Pairs, is a card game in which all of the cards are laid face down on a surface and two cards are flipped face up over each turn. The object of the game is to turn over pairs of matching cards.</p> 
 
     <div id="game-board" class="board clearfix"></div> 
 
     </main> 
 
     <footer> 
 
      <div class="clearfix"> 
 
      <p class="copyright">Copyright 2017</p> 
 
      <p class="message">Created with &hearts; by <span class="name">GA</span></p> 
 
       </div> 
 
     </footer> 
 
    <script src="js/main.js"></script> 
 
    </body> 
 
</html>

関連する問題