package com.celerysoft.ittakestwo.modules;

import java.util.ArrayList;

/**
 * Card matching game module.
 */
public class CardMatchingGame {

    private final int MATCH_BOUNS = 4;
    private final int MISMATCH_PENALTY = 2;
    private final int COST_TO_CHOOSE = 1;

    private int mScore;
    public int getScore() {
        return mScore;
    }

    private ArrayList<Card> mCards = new ArrayList<>();

    public CardMatchingGame(int cardCount, Deck usingDeck) {
        for (int i = 0; i < cardCount; i++) {
            Card card = usingDeck.drawRandomCard();
            if (card != null) {
                mCards.add(card);
            } else {

            }
        }
    }

    public void chooseCardAtIndex(int index) {
        Card card = this.cardAtIndex(index);
        if (card != null) {
            if (!card.isMatched()) {
                if (card.isChosen()) {
                    card.setChosen(false);
                } else {
                    for (Card otherCard : mCards) {
                        if (otherCard.isChosen() && !otherCard.isMatched()) {
                            int matchScore = card.match(new Card[]{otherCard});
                            if (matchScore > 0) {
                                mScore += matchScore * MATCH_BOUNS;
                                otherCard.setMatched(true);
                                card.setMatched(true);
                            } else {
                                mScore -= MISMATCH_PENALTY;
                                otherCard.setChosen(false);
                            }
                            break;
                        }
                    }
                    mScore -= COST_TO_CHOOSE;
                    card.setChosen(true);
                }
            }
        }
    }

    public Card cardAtIndex(int index) {
        return index < mCards.size() ? mCards.get(index) : null;
    }
}

@@ -37,16 +37,42 @@
 dules;%0A%0A
+import android.util.Log;%0A%0A
 import j
@@ -91,16 +91,16 @@
 ayList;%0A
-
 %0A/**%0A * 
@@ -161,16 +161,84 @@
 ngGame %7B
+%0A    private final String LOG_TAG = this.getClass().getSimpleName();
 %0A%0A    pr
@@ -600,11 +600,11 @@
 nt; 
-i
 ++
+i
 ) %7B%0A
@@ -742,17 +742,106 @@
  else %7B%0A
+                Log.w(LOG_TAG, %22Card count of game more than card count of using deck.%22);
 %0A
-
         
@@ -2010,32 +2010,32 @@
 ex(int index) %7B%0A
-
         return i
@@ -2088,11 +2088,629 @@
 l;%0A    %7D
+%0A%0A    public void restart() %7B%0A        // reset game status%0A        mScore = 0;%0A        for (Card card : mCards) %7B%0A            card.setMatched(false);%0A            card.setChosen(false);%0A        %7D%0A%0A        // shuffle cards%0A        ArrayList%3CCard%3E cards = new ArrayList%3C%3E();%0A        int cardCount = mCards.size();%0A        for (int i = 0; i %3C cardCount; ++i) %7B%0A            Card card = mCards.remove((int) (Math.random() * mCards.size()));%0A            cards.add(card);%0A        %7D%0A        if (mCards.isEmpty()) %7B%0A            mCards = cards;%0A        %7D else %7B%0A            Log.e(LOG_TAG, %22shuffle cards error.%22);%0A        %7D%0A    %7D
 %0A%7D%0A

add restart mothod for cardMatchGame model