Java 制作具有可选择位置的甲板的最佳方法是什么?
我有24张卡片,你可以从中选择6张。如果你选择了一张卡,你有6个不同的位置来放置该卡(每个可选择的卡对应一个) 您将如何在Java 制作具有可选择位置的甲板的最佳方法是什么?,java,android,android-studio,Java,Android,Android Studio,我有24张卡片,你可以从中选择6张。如果你选择了一张卡,你有6个不同的位置来放置该卡(每个可选择的卡对应一个) 您将如何在java中为此编写代码,以便: 没有一张牌可以选两次 如果该卡已在某个位置,单击该位置即可进入该卡的信息活动 我尝试使用数组列表,在列表中我将这些点的状态作为数字,以及哪些牌已经被选中(作为数字)。 然而,这得到了很多代码,正因为如此,应用程序非常慢 因此,我的问题是:实现这样一个套牌选择应用程序的最佳和有效的方法是什么?我试图记住oop,这可能是实现套牌逻辑的方法,看起来有
java
中为此编写代码,以便:
因此,我的问题是:实现这样一个套牌选择应用程序的最佳和有效的方法是什么?我试图记住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
}
}
//我不知道你的游戏中应该有什么牌,所以它是空的
公共静态类卡{}
公共静态类甲板{
//卡是私有的,因为直接访问卡槽会违背卡组的目的
私人最终卡槽[]卡;
公共甲板(卡片…卡片){
this.cards=新卡槽[cards.length];
对于(int i=0;i=卡片长度){
抛出新的IndexOutOfBoundsException(“您必须从该牌组中取出卡”);
}
如果(卡[pos]。已获取){
抛出新的非法访问异常(“卡已被占用”);
}
卡片[pos]。已取=真;
退货卡[pos].卡;
}
专用静态类卡槽{
布尔值;
卡片;
公共卡槽(卡槽){
这张卡=卡片;
}
}
}
公共静态类手{
//在比赛中手的大小不应该改变(或者应该取决于你的比赛,所以决赛是可选的)
公共最终整数大小;
//再次暴露牌会破坏手牌课的目的
私人卡[]卡;
公共人手(整数){
这个。大小=大小;
}
//与甲板相同,我们使用例外情况
public void addCard(int pos,Card Card)抛出IndexOutOfBoundsException、IllegalAccessException{
如果(位置<0 | |位置>=卡片长度){
抛出新的IndexOutOfBoundsException(“你必须把卡片放在手里”);
}
如果(卡片[pos]==null){
抛出新的IllegalAccessException(“卡槽,如果已经占用”);
}
卡[pos]=卡;
}
//试着实现这个
公共卡getCard(内部pos){
返回null;
}
}
公共静态void main(字符串[]args){
卡片组=新卡片组(新卡片(),/*在此处枚举所有卡片*/新卡片(),新卡片());
手牌=新牌(6);
//虽然避免筑巢,但如果可能的话,尝试捕捉
试一试{
卡片=卡片组。takeCard(3);
试一试{
手动添加卡片(1张,卡片);
}捕获(非法访问例外e){
//处理异常,要求重新插入卡
}catch(IndexOutOfBoundsException e){
//这里也一样
}
}捕获(非法访问例外e){
//处理异常,请再次索取卡片
}catch(IndexOutOfBoundsException e){
//这里也一样
}
}
请分享您迄今为止尝试过的代码。这很不幸,但基本上应该与clash royal类似,如果您知道该游戏,而不是您认为导致问题的全部代码,您可以再解释一下您在这里做了什么吗?您懂语法吗?比如80%的语法,只是有些事情不是原因。在一些关键部件之后,你能说明你在这一行做什么吗?喜欢在每节课之前就已经做了,但在那节课里面也做了吗?