/** * 获取当前提示的卡片对 * * @return 返回提示的卡片对,没有时返回null */ private PiecePair promptPair() { Piece[][] pieces = gameService.getPieces(); for (int i = 0; i < pieces.length * pieces[0].length; i++) { Piece p1 = pieces[i / pieces[0].length][i % pieces[0].length]; if (Piece.canSelect(p1)) { for (int j = i + 1; j < pieces.length * pieces[0].length; j++) { Piece p2 = pieces[j / pieces[0].length][j % pieces[0].length]; if (Piece.canSelect(p2) && p1.isSameImage(p2) && link(p1, p2) != null) { return new PiecePair(p1, p2); } } } } return null; }
/** * 点击卡片时的处理 * * @param piece 点击的卡片 */ public void click(Piece piece) { Piece curPiece = piece; if (curPiece == null) { return; } unPrompt(); if (selected == curPiece || !Piece.canSelect(curPiece)) { if (selected != null) { unCheck(); } return; } if (selected == null) { check(curPiece); } else { LinkInfo linkInfo = gameService.link(selected, curPiece); if (linkInfo == null) { unCheck(); check(curPiece); } else { handleSuccess(linkInfo, selected, curPiece); } } }
/** * 成功配对时的处理 * * @param linkInfo 连接路径信息 * @param prePiece 前一个卡片信息 * @param curPiece 当前卡片信息 */ private void handleSuccess(LinkInfo linkInfo, Piece prePiece, Piece curPiece) { unCheck(); Piece[][] pieces = gameService.getPieces(); if (prePiece.isStar()) { gameStar++; if (listener != null) { listener.onStarChanged(gameStar); } } if (curPiece.isStar()) { gameStar++; if (listener != null) { listener.onStarChanged(gameStar); } } // pieces[prePiece.getIndexY()][prePiece.getIndexX()].setEmpty(true); // pieces[curPiece.getIndexY()][curPiece.getIndexX()].setEmpty(true); pieces[prePiece.getIndexY()][prePiece.getIndexX()].setImageId(GameSettings.GroundCardValue); pieces[curPiece.getIndexY()][curPiece.getIndexX()].setImageId(GameSettings.GroundCardValue); onLinkPath(linkInfo); if (alignContext != null) { alignContext.Translate(prePiece, curPiece); if (listener != null) { listener.onTranslate(); } } gameStep++; if (listener != null) { listener.onStepChanged(gameStep); } if (listener != null) { listener.onErase(); } // 如果收集了足够的星星,游戏胜利 if (levelCfg.getStars() != 0 && gameStar == levelCfg.getStars()) { gameStatus.win(); } // 如果没有了游戏块,游戏胜利 if (!gameService.hasPieces()) { gameStatus.win(); } }