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.