因此,对于这个方法,我应该从玩家手中删除刚刚打出的牌,同时接受玩家(玩家)和牌(索引)作为参数。
我知道,如果我要在playerHand[0][0]处移除玩家1的第一张牌,我需要将playerHand[0][1]移动到playerHand[0][0]位置并移动任何玩家的其他卡牌也减少了一个索引,但我不确定如何在我的代码中准确地解决这个问题。
public static int nPlayers;
public static int currentPlayer;
public static UnoCard playerHand[][];
public static UnoCard currentCard;
public static final int CARDS_IN_DECK = 112;
public static final int START_HAND = 7;
public static boolean direction = true; // true for regular direction, false for reverse direction
public static int winner;
public static void removeCardFromHand(int p, int idx) {
}
最佳答案
使用数组,你可以做这样的事情
public static void removeCardFromHand(int p, int idx) {
// get the card to remove
UnoCard removed = playerHand[p][idx];
// reorder all cards
for (int i = idx; i <= playerHand.length - 2; i++) {
playerHand[p][i] = playerHand[p][i + 1];
}
// last card is now the second last card, so we dont need it
playerHand[p][playerHand.length - 1] = null;
}
我建议使用ArrayList
,它有一个remove
方法并且更容易使用。
关于java - 我正在努力在 Uno 游戏中实现 removeCardsFromHand 方法,请问我可以提供一些关于如何实现该方法的建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58401455/