Clubs and decks, how to hand out different hands? POO

0

I must create a program that generates me Cards (As - King) and also Clubs, these same ones will be distributed to 3 users (each user will have 6 cards) and must show a sum. The program already does all that but there is a problem that I still can not solve, it is assumed that two users can not have the same card, suppose: Player 1 has King of hearts, then Player 2 can not have this same card.

In my program it generates the cards and clubs but in the hand of 2 or even of the 3 players there is the probability that the same card will come out. Could someone give me an alternative?

Annex code:

1.- With this part I generate and distribute the letters

    public void generarCartas(){

        revolver[i]=(int)(Math.random()*rangoCartas.length);  //genera los numeros aleatorios
            for(i=1; i<totalCartas; i++){
                revolver[i]=(int)(Math.random()*rangoCartas.length);
                for(int j=0; j<i; j++){
                    if(revolver[i]==revolver[j]){  //Esta condición evita que se repitan los numeros
                        i=i-1;
                    }
                }   
            }       
    }
    public void repartirCartas(){       
        for(int k=0; k<totalCartas; k++){ 
            String cartita = cartitas[revolver[k] %13 ];

            String palito = palitos[revolver[k] / 4 ];

            switch(revolver[k]){

            case 1:
                System.out.println("Tu carta numero " + (k+1) + " es el: " + cartita + " de " + palito );
                suma = suma + revolver[k];

            break;

            default:
                System.out.println("Tu carta numero " + (k+1) + " es el: " + cartita + " de " + palito );
                suma = suma + revolver[k] + 1;

                break;

            }

        }
        System.out.println("Total suma:" + (suma+1));
    }

2.- With this one I show the card of each player.

    MazoCartas jugador1;
    jugador1 = new MazoCartas();

    MazoCartas jugador2;
    jugador2 = new MazoCartas();

    MazoCartas jugador3;
    jugador3 = new MazoCartas();


    System.out.println("Mano jugador 1:");
    jugador1.generarCartas();
    jugador1.repartirCartas();

    System.out.println("Mano jugador 2:");
    jugador2.generarCartas();
    jugador2.repartirCartas();


    System.out.println("Mano jugador 3:");
    jugador3.generarCartas();
    jugador3.repartirCartas();
    
asked by G. Tary 08.10.2017 в 20:12
source

1 answer

1

The question caused me a lot of interest, the problem may be that a deck or deck is created by each player and this can generate card collusions between the players, the only option is to have a single deck for the three players, which are drawn letters that are delivered to each player but which are removed from the deck, here I leave a solution option in order to evolve according to what each user of the community requires.

First we define the figures and numbers of the cards

public enum Figura {
  CORAZONES, DIAMANTES, PICAS, TREBOL
}

public enum Numero {
  AS, DOS, TRES, CUATRO, CINCO, SEIS, SIETE,  OCHO, NUEVE, J, Q, K   
}

Now we define a letter

public class Carta {
  private final Figura figura;
  private final Numero numero;
  public Carta(Figura f, Numero n){
    this.figura = f;
    this.numero = n;
  }
  public Figura getFigura() {
    return figura;
  }
  public Numero getNumero() {
    return numero;
  }
  @Override
  public String toString(){
    return this.numero.toString() + "-" + this.figura.toString();
  }
}

Now we define a deck or deck

public class Baraja {
  private List<Carta> baraja;
  public Baraja(){
    this.construir();
  }
  private void construir(){
    this.baraja = new ArrayList();
    for(Figura f : Figura.values()){
      for(Numero n : Numero.values()){
        this.baraja.add(new Carta(f, n));
      }
    }
  }
  public void mezclar(byte cantidad){
    int nVeces = cantidad * this.baraja.size();
    Random random = new Random();
    int index = 0;
    Carta carta;
    for(int i = 0; i < nVeces; i++){
      index = random.nextInt(this.baraja.size());
      carta = this.baraja.remove(index);
      index = random.nextInt(this.baraja.size());
      this.baraja.add(index, carta);
    }
  }
  public int getNumeroCartas(){
    return this.baraja.size();
  }
  public Carta getCarta(){
    return this.baraja.remove(0);
  }
  public boolean estaVacia(){
    return this.baraja.isEmpty();
  }
  @Override
  public String toString(){
    return this.baraja.toString();
  }
}

In every card game there is a dealer who manages the deck

public class Dealer {
  private Baraja baraja;
  public Dealer(Baraja baraja){
    this.baraja = baraja;
  }
  public void mezclarBaraja(byte cantidad){
    this.baraja.mezclar(cantidad);
  }
  public Carta entregarCarta(){
    return this.baraja.getCarta();
  }
  public String mostrarBaraja(){
    return this.baraja.toString();
  }
  public int getNumeroCartas(){
    return this.baraja.getNumeroCartas();
  }
}

There are also players

public class Jugador {
  private List<Carta> mano;
  private String nombre;
  public Jugador(String nombre){
    this.nombre = nombre;
    this.mano = new ArrayList();
  }
  public void adicionarCarta(Carta carta){
    this.mano.add(carta);
  }
  public Carta botarCarta(int index){
    return this.mano.remove(index);
  }
  @Override
  public String toString(){
    StringBuilder sb = new StringBuilder();
    sb.append("Jugador: ")
      .append(this.nombre)
      .append("\n").append(this.mano.toString());
    return sb.toString();
  }
}

And finally, we have the game

public class Juego {
  private List<Jugador> jugadores;
  private Dealer dealer;
  public Juego(){
    this.dealer = new Dealer(new Baraja());
    this.dealer.mezclarBaraja((byte)15);
    this.jugadores = new ArrayList();
  }
  public void addJugador(Jugador jugador){
    this.jugadores.add(jugador);
  }
  public void repartirCartas(byte numeroCartas){
    for(Jugador j : this.jugadores){
      for(byte i = 0; i < numeroCartas; i++){
        j.adicionarCarta(this.dealer.entregarCarta());
      } 
    }
  }
  public String mostrarBaraja(){
    return this.dealer.mostrarBaraja();
  }
  public int getNumeroCartasDealer(){
    return this.dealer.getNumeroCartas();
  }
  public List<Jugador> getJugadores(){
    return this.jugadores;
  }
  public static void main(String[] args){
    Juego juego = new Juego();
    System.out.println("Baraja Inicial de :" + juego.getNumeroCartasDealer() + " cartas." );
    System.out.println(juego.mostrarBaraja());
    juego.addJugador(new Jugador("Jugador-1"));
    juego.addJugador(new Jugador("Jugador-2"));
    juego.addJugador(new Jugador("Jugador-3"));
    juego.repartirCartas((byte)6);
    System.out.println("\n----- Cartas por Jugador ------\n");
    for(Jugador j : juego.getJugadores()){
      System.out.println(j.toString());
    }
    System.out.println("\n");
    System.out.println("Baraja Final de :" + juego.getNumeroCartasDealer() + " cartas." );
    System.out.println(juego.mostrarBaraja());
  }
}

and here an example of the exit, where we see the initial deck mixed, and then the cards of each player and the cards that the dealer left where we can see that there are no card collusions

Baraja Inicial de :48 cartas.
[SEIS-DIAMANTES, TRES-DIAMANTES, CUATRO-TREBOL, Q-TREBOL, J-TREBOL, CUATRO-CORAZONES, J-DIAMANTES, OCHO-TREBOL, OCHO-DIAMANTES, Q-PICAS, J-CORAZONES, CINCO-TREBOL, SEIS-TREBOL, SIETE-CORAZONES, TRES-CORAZONES, K-DIAMANTES, NUEVE-TREBOL, AS-PICAS, SIETE-DIAMANTES, Q-CORAZONES, NUEVE-DIAMANTES, SIETE-TREBOL, TRES-PICAS, CUATRO-PICAS, AS-CORAZONES, AS-DIAMANTES, AS-TREBOL, DOS-CORAZONES, DOS-TREBOL, TRES-TREBOL, CINCO-PICAS, Q-DIAMANTES, J-PICAS, CUATRO-DIAMANTES, SIETE-PICAS, NUEVE-PICAS, CINCO-DIAMANTES, SEIS-PICAS, OCHO-CORAZONES, DOS-PICAS, K-TREBOL, K-PICAS, CINCO-CORAZONES, DOS-DIAMANTES, SEIS-CORAZONES, OCHO-PICAS, NUEVE-CORAZONES, K-CORAZONES]
----- Cartas por Jugador ------
Jugador: Jugador-1
[SEIS-DIAMANTES, TRES-DIAMANTES, CUATRO-TREBOL, Q-TREBOL, J-TREBOL, CUATRO-CORAZONES]
Jugador: Jugador-2
[J-DIAMANTES, OCHO-TREBOL, OCHO-DIAMANTES, Q-PICAS, J-CORAZONES, CINCO-TREBOL]
Jugador: Jugador-3
[SEIS-TREBOL, SIETE-CORAZONES, TRES-CORAZONES, K-DIAMANTES, NUEVE-TREBOL, AS-PICAS]
Baraja Final de :30 cartas.
[SIETE-DIAMANTES, Q-CORAZONES, NUEVE-DIAMANTES, SIETE-TREBOL, TRES-PICAS, CUATRO-PICAS, AS-CORAZONES, AS-DIAMANTES, AS-TREBOL, DOS-CORAZONES, DOS-TREBOL, TRES-TREBOL, CINCO-PICAS, Q-DIAMANTES, J-PICAS, CUATRO-DIAMANTES, SIETE-PICAS, NUEVE-PICAS, CINCO-DIAMANTES, SEIS-PICAS, OCHO-CORAZONES, DOS-PICAS, K-TREBOL, K-PICAS, CINCO-CORAZONES, DOS-DIAMANTES, SEIS-CORAZONES, OCHO-PICAS, NUEVE-CORAZONES, K-CORAZONES]

BUILD SUCCESSFUL (total time: 0 seconds)

It is important to keep in mind that this code is to show how to manage or manage a single deck for several players, and does not pretend to be the super card game, that part is at the discretion of each user.

    
answered by 09.10.2017 в 07:37