首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于记忆文本的游戏

基于记忆文本的游戏
EN

Code Review用户
提问于 2014-09-12 01:12:40
回答 3查看 5.3K关注 0票数 6

有谁愿意检查一下我的内存内存代码吗?

代码语言:javascript
复制
import java.util.*;

public class Cards {

    public boolean gameInProgress = true;

    public void shuffleCards() {

        Scanner userInput = new Scanner(System.in);
        List cardList = new LinkedList();
        List matchedCards = new LinkedList();

        //this is the list of cards available to pick from
        String[] cards = {"a", "b", "c", "d", "e", "a", "b", "c", "d", "e"};

        //this shuffles the cards each game
        Collections.shuffle(Arrays.asList(cards));

        //this moves the cards array into a LinkedList
        Collections.addAll(cardList, cards);

        //main game loop. stops when all cards are matched
        while (gameInProgress) {

            //Stores the users card picks in variables card1 and card2
            System.out.println("pick a card (0-9)\n");

            int card1 = userInput.nextInt();

            System.out.println("First card picked is the letter " + cardList.get(card1));

            System.out.println("pick a second card.\n");

            int card2 = userInput.nextInt();

while(card1 == card2){
            System.out.println("You cannot pick the same card twice. Pick a different card.");
            card2 = userInput.nextInt();
        }

            System.out.println("second card picked is the letter " + cardList.get(card2));


            //checks if a card has been picked already
            while(matchedCards.contains(cardList.get(card1))) {
                System.out.println("First card already picked. Pick again: ");
                card1 = userInput.nextInt();
            }

            while(matchedCards.contains(cardList.get(card2))){
                System.out.println("Second card already picked. Pick again: ");
                card2 = userInput.nextInt();
            }

                //copies the matched cards to a new linked list
                if (cardList.get(card1) == cardList.get(card2)) {
                    System.out.println("You got a match!\n");

                    matchedCards.add(cardList.get(card1));
                    matchedCards.add(cardList.get(card2));

                    System.out.println("You have collected " + matchedCards.size() + "/10 cards");

                    System.out.println(matchedCards + " are in the matched pile\n");

                    //stops the game once all cards have been matched
                    if (matchedCards.size() == 10) {
                        System.out.println("You Win!!!");
                        gameInProgress = false;
                    }
                }
                else {
                    System.out.println("Not a match\n");

                }
        }
    }
}



    public class Game {


    public static void main(String[] args) {


        Cards card = new Cards();

        card.shuffleCards();



    }
}
EN

回答 3

Code Review用户

回答已采纳

发布于 2014-09-12 12:32:09

回顾:

代码语言:javascript
复制
public void shuffleCards()

只应该洗牌,因为它是这样说的。创建新方法void playGame() int getFirstPick()int getSecondPick()boolean isAlreadyMatched(int card1, int card2)boolean areAllCardsMatched()以及可能的其他方法。

将这些方法分配给它们应该属于的类。

代码语言:javascript
复制
Game
    void playGame()        
    int getFirstPick()
    int getSecondPick()
    boolean verifyFirstPick(int firstPick)
    boolean verifySecondPick(int firstPick, int secondPick)

Cards
    void shuffleCards()
    boolean isAlreadyMatched(int card)
    boolean areAllCardsMatched()

为什么?

因为现在你已经有了一段很长的代码,它可以完成所有的事情,它会让你困惑并导致错误。通过将其分开,可以更容易地识别您需要做什么,以及代码是否正在执行。

如果你做得对,你就会得到这样的代码:

代码语言:javascript
复制
public void playGame(){
    //printInstructions(); //an idea, perhaps?
    setupGame();
    while(!isGameWon()){
        doTurn();
    }
    printGameWon();
}

太抽象了,皮姆。

好的,让我们再深入一点,在setupGame中:

代码语言:javascript
复制
public void setupGame(){
    cards = new Cards();
    cards.shuffle();
    userInput = new Scanner(System.in);
}

这似乎建立了竞争环境和投入。

下一个方法是什么?isGameWon()..。

代码语言:javascript
复制
public boolean isGameWon(){
    return (cards != null && cards.areAllCardsMatched());
}

这只是检查所有卡片是否匹配而已。(这里有一个鬼鬼祟祟的快捷方式;使用cards != null,我检查游戏是否已经初始化。)

好吧,那doTurn()怎么办?那个要大一点。

代码语言:javascript
复制
public void doTurn(){
    int firstPick = -1;        
    do {
        firstPick = getFirstPick();
    } while(!verifyFirstPick(firstPick));
    printFirstPick(firstPick);

    int secondPick = -1;
    do {
        secondPick = getSecondPick();
    } while(!verifySecondPick(firstPick, secondPick));
    printSecondPick(secondPick);

    checkMatch(firstPick, secondPick);
}

它处理第一张和第二张拾取的卡片,然后把它们传递给检查是否匹配。

让我们来看看checkMatch函数。

代码语言:javascript
复制
public void checkMatch(int firstPick, int secondPick){
    if(cards.match(firstPick, secondPick)){
        printMatch();
        printRemainingMatches();
    } else {
        printNoMatch();
    }
}

我们还没看到任何真正的代码在做什么。我现在向您展示的所有实际操作都是setupGame方法。剩下的只是构建游戏流程。

函数现在列出:

代码语言:javascript
复制
Game
    void setupGame();
    void playGame();
    void doTurn();
    void checkMatch(int firstPick, int secondPick);     
    int getFirstPick();
    int getSecondPick();
    boolean verifyFirstPick(int firstPick);
    boolean verifySecondPick(int firstPick, int secondPick);
    boolean isGameWon();

    void printFirstPick(int firstPick);
    void printSecondPick(int secondPick);
    void printMatch();
    void printNoMatch();
    void printRemainingMatches();
    void printGameWon();

Cards
    void shuffleCards();
    boolean match(int card1, int card2);
    boolean isAlreadyMatched(int card);
    boolean areAllCardsMatched();

您的任务是实现我尚未展示的内容。您还必须移动一些变量,使它们成为类成员,而不仅仅是在函数中声明(提示:cardsuserInput是其中的两个,我不知道是否还有更多的变量)。

给定函数的名称,应该很容易猜出它们是做什么的。

shuffleCards洗牌。match测试两张卡片是否匹配。等等..。

票数 8
EN

Code Review用户

发布于 2014-09-12 11:47:21

臭虫:

匹配一对(假设它们是0和1)。选0作为你的第一张牌,选2作为你的第二张牌。作为你的第一张卡。我们已经绕过了双挑支票。然后我们就有了匹配。

我会通过重新排序语句来解决这个问题:

代码语言:javascript
复制
        System.out.println("pick a card (0-9)\n");

        int card1 = userInput.nextInt();

        //checks if a card has been picked already
        while(matchedCards.contains(cardList.get(card1))) {
            System.out.println("First card already picked. Pick again: ");
            card1 = userInput.nextInt();
        }

        System.out.println("First card picked is the letter " + cardList.get(card1));

        System.out.println("pick a second card.\n");

        int card2 = userInput.nextInt();

        while(matchedCards.contains(cardList.get(card2) || card1 == card2)){
            System.out.println("Second card already picked. Pick again: ");
            card2 = userInput.nextInt();
        }

        System.out.println("second card picked is the letter " + cardList.get(card2));

我合并了“第二张卡已经被选中”和“不能选择同一张卡”的信息,因为从技术上讲,你选择的是一张已经被选中的卡。

票数 6
EN

Code Review用户

发布于 2014-09-12 23:11:02

除了Pimgd所指出的,我还有一些话要说:

首先,您的代码有点混乱,因为缩进很远,间距也是一样的。如果您正在使用IDE (我希望是这样),请使用IDE自动更正这些内容。或者看看Pimgd的代码,看看它是如何完成的。

在您的设置中,您首先创建一个列表,然后创建一个数组,然后从数组中添加一个列表,然后将您的数组添加到第一个列表中。这可以做得更简单。

此外,您的数组是不可维护的,因为很容易意外地将其中一个字母切换到一个完全不同的字母,这样就不再有对了。

另外,您没有对列表使用泛型,我建议使用ArrayList而不是LinkedList,因为相对于remove方法,它会更快。

一个更好的设置是:

代码语言:javascript
复制
List<String> cards = Arrays.asList(new String[] { "a", "b", "c", "d", "e" });
List<String> cardList = new ArrayList<String>();
cardList.addAll(cards);
cardList.addAll(cards);
Collections.shuffle(cards);

在这两行中:

代码语言:javascript
复制
System.out.println("You have collected " + matchedCards.size() + "/10 cards");

if (matchedCards.size() == 10) {

你指的是数字10?但你为什么要说数字10呢?真的,为什么是数字10?啊,因为那是你的牌号?那你为什么不看看你有多少张牌呢!?如果你再加几张牌呢?

代码语言:javascript
复制
System.out.println("You have collected " + matchedCards.size() + "/" + cards.size() + " cards");

if (matchedCards.size() == cards.size()) {
票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/62695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档