@Override public void restartBoard() { if (mHomeView == null) return; List<Integer> cardVals = new ArrayList<>(); cardVals.clear(); cardsList.clear(); for (int i = 0; i < pairs; i++) { cardVals.add(i); cardVals.add(i); } Collections.shuffle(cardVals); for (int val : cardVals) { CardModel c = new CardModel(); c.setId(val); cardsList.add(c); } c1 = null; c2 = null; bAllowClick = true; iUserScore = 0; mHomeView.updateScore(iUserScore); changeVisibilityCards(); flipBackAllCards(); }
@Override public void doTurn(ImageView imageBoard, int position) { if (mHomeView == null) return; if (bAllowClick) { if (!cardsList.get(position).isMatched()) if (c1 == null && !cardsList.get(position).isFlipped()) { cardsList.get(position).setFlipped(true); c1 = cardsList.get(position); mHomeView.flipCard(imageBoard, c1.getId()); } else if (c2 == null && !cardsList.get(position).isFlipped()) { cardsList.get(position).setFlipped(true); c2 = cardsList.get(position); mHomeView.flipCard(imageBoard, c2.getId()); bAllowClick = false; startTimer(1000); } } }
@Override public void checkCards() { if (mHomeView == null) return; if (c1.getId() == c2.getId()) { c1.setMatched(true); c2.setMatched(true); iUserScore += iCorrectPair; mHomeView.updateScore(iUserScore); c1 = null; c2 = null; changeVisibilityCards(); if (isGameWon()) mHomeView.onGameFinished(iUserScore); } else { iUserScore -= iInCorrectPair; mHomeView.updateScore(iUserScore); flipBackAllCards(); } bAllowClick = true; }