【问题标题】:What is the best way to make a deck with choosable positions? [closed]制作具有可选位置的甲板的最佳方法是什么? [关闭]
【发布时间】:2021-02-13 15:21:55
【问题描述】:

我有 24 张卡片,您可以从中选择 6 张。如果您选择一张卡片,您有 6 个不同的位置可以放置该卡片(每张可供选择的卡片一个)。

您将如何在java 中为此编写代码,以便:

  1. 没有卡可以选择两次
  2. 如果卡片已经在某个地点,点击该地点即可进入该卡片的信息活动

我尝试使用数组列表,其中我将这些点的状态作为数字以及已经选择的卡片(作为数字)。 但是,这会得到很多代码,因此应用程序非常慢......

所以我的问题是:实现这样一个套牌选择应用程序的最佳和有效方法是什么?

【问题讨论】:

  • 请分享您目前尝试过的代码。
  • 很遗憾,但如果你知道那个游戏的话,它基本上应该像皇室战争中一样
  • 不是整个代码,你认为是导致问题的那部分

标签: java android android-studio


【解决方案1】:

我试图记住 oop,这可能是你可以如何实现取卡逻辑的方式,它看起来像很多代码,但 java 就是这样

// i don't know what Card should have in your game so it's empty
    public static class Card {}

    public static class Deck {
        // cards are private as accessing card slots directly would defy purpose of deck
        private final CardSlot[] cards;

        public Deck(Card ...cards) {
            this.cards = new CardSlot[cards.length];
            for(int i = 0; i < cards.length; i++) {
                this.cards[i] = new CardSlot(cards[i]);
            }
        }

        // there are multiple options how things can go wrong so we are throwing exceptions
        public Card takeCard(int pos) throws IndexOutOfBoundsException, IllegalAccessException {
            if(pos < 0 || pos >= cards.length) {
                throw new IndexOutOfBoundsException("you have to pull card from this deck");
            }

            if(cards[pos].taken) {
                throw new IllegalAccessException("card is already taken");
            }

            cards[pos].taken = true;

            return cards[pos].card;
        }

        private static class CardSlot {
            boolean taken;
            Card card;

            public CardSlot(Card card) {
                this.card = card;
            }
        }
    }

    public static class Hand {
        // hand size should not change during game (or should depends on your game so final is optional)
        public final int size;
        // again exposing cards would defeat purpose of Hand class
        private Card[] cards;

        public Hand(int size) {
            this.size = size;
        }

        // same as for deck we use exceptions
        public void addCard(int pos, Card card) throws IndexOutOfBoundsException, IllegalAccessException {
            if(pos < 0 || pos >= cards.length) {
                throw new IndexOutOfBoundsException("you have to put card into your hand");
            }

            if(cards[pos] == null) {
                throw new IllegalAccessException("card slot if already taken");
            }

            cards[pos] = card;
        }

        // try implement this
        public Card getCard(int pos) {
            return null;
        }
    }

    public static void main(String[] args){
        Deck deck = new Deck(new Card(),/* enumerate all cards here */ new Card(), new Card());
        Hand hand = new Hand(6);
        
        // though avoid nesting try catch if possible
        try{
            Card card = deck.takeCard(3);
            try{
                hand.addCard(1, card);
            } catch(IllegalAccessException e) {
                // handle exception, ask to insert card again
            } catch(IndexOutOfBoundsException e) {
                // same here
            }
        } catch (IllegalAccessException e){
            // handle exception, ask to takeCard again
        } catch (IndexOutOfBoundsException e) {
            // same here
        }
    }

【讨论】:

  • 你能解释一下你在这里做了什么吗?
  • 你懂语法吗?
  • 喜欢其中的 80%,只是不是某些事情的原因。你能在一些关键部分之后做//*你在这条线上做什么*吗?喜欢在每节课之前已经做过,但也在那个课内?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多