扑克游戏,更改数组值

时间:2016-11-11 02:48:34

标签: java arrays return

我有一个玩扑克游戏的任务。它的作用如下:它生成5个数字(0到52之间)。这五个数字将根据其值转换为符号和实际卡片值。我需要允许用户更改最多4张牌,而不是两次给他相同的牌。我的问题是:当他改变第一张牌时它会顺利进行,但如果他决定改变第二张牌,那么第一张牌也会改变。以下是代码,并提前感谢您的帮助(如果您不理解,请注释法语请告诉我)(同时忽略未使用的方法,因为我还没有完成):

   package pkgtp2;

import java.util.*;

 public class TP2 {

static int[] cards = new int[5];
static int[] sortes = new int[5];
static int balance = 100;
static int h = 0;

public static void Initialiser(boolean[] pack) {
    pack = new boolean[52];

}

public static void Regles() {
    System.out.println("**************            " + " AUCUNE COMBINAISON = -10$");
    System.out.println("*Jeu de Poker*            " + " 1 PAIRE = 0$");
    System.out.println("**************            " + " 2 PAIRES = 20$");
    System.out.println("                          " + " BRELAN<3> = 35$");
    System.out.println("                          " + " SUITE = 50$");
    System.out.println("                          " + " FULL = 75$");
    System.out.println("                          " + " COULEUR = 100$");
    System.out.println("                          " + " CARRÉ = 150$");
    System.out.println("                          " + " QUINTE ROYALE = 500$");
    System.out.println("         Vous avez :" + balance + " $");

}

public static void findcards(int[] cards, boolean[] pack) {
    System.out.println();
    char symboles[] = {'♥', '♦', '♣', '♠'};
    String valeurs[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    for (int i = 0; i < cards.length; i++) {
        switch (cards[i] / 13) {
            case 0:

                System.out.print(symboles[0]);
                break;
            case 1:

                System.out.print(symboles[1]);
                break;
            case 2:

                System.out.print(symboles[2]);
                break;
            case 3:

                System.out.print(symboles[3]);
                break;
        }

        switch (cards[i] % 13 + 1) {
            case 1:
                System.out.print(valeurs[0]);
                break;
            case 2:
                System.out.print(valeurs[1]);
                break;
            case 3:
                System.out.print(valeurs[2]);
                break;
            case 4:
                System.out.print(valeurs[3]);
                break;
            case 5:
                System.out.print(valeurs[4]);
                break;
            case 6:
                System.out.print(valeurs[5]);
                break;
            case 7:
                System.out.print(valeurs[6]);
                break;
            case 8:
                System.out.print(valeurs[7]);
                break;
            case 9:
                System.out.print(valeurs[8]);
                break;
            case 10:
                System.out.print(valeurs[9]);
                break;
            case 11:
                System.out.print(valeurs[10]);
                break;
            case 12:
                System.out.print(valeurs[11]);
                break;
            case 13:
                System.out.print(valeurs[12]);
                break;

        }
        System.out.println();
    }

}

public static void inicartes(int[] cards, boolean[] pack) {

    Random give = new Random();

    for (int i = 0; i < cards.length; i++) {
        do {
            cards[i] = give.nextInt(52);
        } while (pack[cards[i]]);
        pack[cards[i]] = true;
        System.out.println(cards[i]);
    }

}

public static void Mélanger() {

}

public static void Afficher5cartes() {

}

public static void changecards(int[] cards, boolean[] pack) {
    Scanner read = new Scanner(System.in);
    int cardchange;
    Random give = new Random();
    System.out.println("Veuillez entrer un chiffre de 1 a 5 correspondant aux"
            + "cartes que vous voulez changer (vous pouvez changer au plus 4 cartes). Si vous voulez conserver vos cartes, entrez '0' ");
    cardchange = read.nextInt();
    do {
        if (cardchange < 0 || cardchange > 5) {
            do {
                System.out.println("Erreur, veuillez entrer un chiffre de 1 a 5 correspondant aux "
                        + " cartes que vous voulez changer (vous pouvez changer au plus 4 cartes). Entrez '0' lorsque vos désirez garder vos cartes ");
                cardchange = read.nextInt();
            } while (cardchange < 0 || cardchange > 5);
        } else if (cardchange > 0 && cardchange <= 5) {
            System.out.println("\nVoici votre nouvelle main");

            for (int i = 0; i < cards.length; i++) {
                do {
                    cards[cardchange - 1] = give.nextInt(52);
                } while (pack[cards[cardchange - 1]]);
                pack[cards[i]] = true;
                System.out.println(cards[i]);
            }

            h++;
            System.out.println("");
            cardchange = read.nextInt();

        }

    } while (h < 4 && cardchange <= 5 && cardchange > 0);
}

public static void EvalJeu() {

}

public static void Afficher$() {

}

public static void main(String[] args) {
    boolean pack[] = new boolean[52];
    Regles();
    inicartes(cards, pack);
    findcards(cards, pack);
    changecards(cards, pack);

}

}

1 个答案:

答案 0 :(得分:0)

问题是你不必要地循环。您的要求是只改变某个位置。所以只改变那个。请参阅下面的代码。

 public static void changecards(int[] cards, boolean[] pack) {
    Scanner read = new Scanner(System.in);
    int cardchange;
    Random give = new Random();
    System.out.println();
    cardchange = -1;
    for (int i = 0; i < 5; i++) {
      System.out.println("\n\nTurn: " + i + " - Enter the card position that you want to change?");
      while (cardchange < 0 || cardchange > 5) {
        System.out.println("Enter a card number between 1 and 5; Enter 0 if you don't want to "
            + "change anything.");
        cardchange = read.nextInt();
      }
      if (cardchange == 0) {
        System.out.println("Turn skipped nothing will be changed.");
      } else {
        System.out.print("Your card number" + cardchange + " will be changed to ");
        cards[cardchange - 1] = give.nextInt(52);
        System.out.println(cards[cardchange - 1]);
      }
      cardchange = -1;
      System.out.println("\n\nYour new cards are : ");
      for (int card : cards) {
        System.out.print(card + "\t");
      }
      findcards(cards,pack);
    }

  }

输出如下。

            **************             AUCUNE COMBINAISON = -10$
            *Jeu de Poker*             1 PAIRE = 0$
            **************             2 PAIRES = 20$
                                       BRELAN<3> = 35$
                                       SUITE = 50$
                                       FULL = 75$
                                       COULEUR = 100$
                                       CARRÉ = 150$
                                       QUINTE ROYALE = 500$
                     Vous avez :100 $
            51  3   22  34  35  
            ♠K  ♥4  ♦10 ♣9  ♣10 


            Turn: 0 - Enter the card position that you want to change?
            Enter a card number between 1 and 5; Enter 0 if you don't want to change anything.

            1
            Your card number1 will be changed to 40


            Your new cards are : 
            40  3   22  34  35  
            ♠2  ♥4  ♦10 ♣9  ♣10 

            Turn: 1 - Enter the card position that you want to change?
            Enter a card number between 1 and 5; Enter 0 if you don't want to change anything.
            0
            Turn skipped nothing will be changed.


            Your new cards are : 
            40  3   22  34  35  
            ♠2  ♥4  ♦10 ♣9  ♣10 

            Turn: 2 - Enter the card position that you want to change?
            Enter a card number between 1 and 5; Enter 0 if you don't want to change anything.
            2
            Your card number2 will be changed to 19


            Your new cards are : 
            40  19  22  34  35  
            ♠2  ♦7  ♦10 ♣9  ♣10 

            Turn: 3 - Enter the card position that you want to change?
            Enter a card number between 1 and 5; Enter 0 if you don't want to change anything.
            2
            Your card number2 will be changed to 50


            Your new cards are : 
            40  50  22  34  35  
            ♠2  ♠Q  ♦10 ♣9  ♣10 

            Turn: 4 - Enter the card position that you want to change?
            Enter a card number between 1 and 5; Enter 0 if you don't want to change anything.
            4
            Your card number4 will be changed to 48


            Your new cards are : 
            40  50  22  48  35  
            ♠2  ♠Q  ♦10 ♠10 ♣10