Search code examples
javaarraysreturn

Poker Game, changing arrays values


I have an assignment to make a poker game. Here is what it does : it generates 5 numbers (between 0 and 52). Those five numbers are converted into symbols and actual cards values based on their values. I need to allow the user to change up to 4 cards and not give him the same cards twice. My problem is : When he changes the first card it goes well, but then if he decides to change the second one, the first will also change. Here is the code and thanks in advance for the help (comments are in French if you don't understand please let me know) (Also ignore the unused methods as i'm not finished yet) :

   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);

}

}


Solution

  • The problem is you are looping unnecessarily. Your requirement is to only change a certain position. So change only that. See the code below.

     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);
        }
    
      }
    

    The output is as follows.

                **************             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