2016-11-11 67 views
0

我有一個任務,使撲克遊戲。這是它的作用:它產生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); 

} 

}

回答

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