I have a program that should simulate a card game. The Deck class is responsible for creating the deck and shuffling it (Naipe object). When trying to create the object (only one will be created) I get the error of the title.
This is my main and the Deck class
import java.util.*;
public class Naipes {
public static void main() {
//instancia de la clase principal para poder crear el objeto deck
Naipes instancia = new Naipes();
int cartas;
int manos;
Deck deck = new Deck(); //creacion del deck
ArrayList<Mano> mesa = new ArrayList<Mano>(); // mesa contendra los objetos mano
Scanner sc = new Scanner(System.in);
System.out.print("Cantidad de jugadores (manos) que hay en la mesa: ");
manos = sc.nextInt();
sc.nextLine();
System.out.print("\nCantidad de cartas que tendra cada mano: ");
cartas = sc.nextInt();
if ((manos * cartas) > 52) {
System.out.print("NO HAY SUFICIENTES CARTAS");
System.exit(1);
}
for (int i = 1; i <= manos; i++) {
mesa.add(new Mano(i, cartas)); // el contador "i" representara el ID de la mano creada
// loop que asigna los naipes a las manos
for (int n = 0; n < manos; n++) {
((Mano) mesa.get(i-1)).naipes.add(Deck.baraja.remove(0));
}
}
}
private class Deck {
ArrayList<Naipe> baraja = new ArrayList<Naipe>();
public Deck() {
// lista que almacena los objetos naipe
crearBaraja(this.baraja);
barajearDeck(this.baraja);
}
private ArrayList<Naipe> crearBaraja(ArrayList baraja) {
int suit = 0; //el palo del Naipe
char signo;
for(int i = 0; i < 5; i++) {
//loop crea naipes del 0 al 10
for( int val = 2; val < 11; val++) {
signo = (char)(val + '0');
baraja.add(new Naipe(suit, val, signo));
}
// se crean naipes J, Q, R, A
baraja.add(new Naipe(suit, 10, 'J'));
baraja.add(new Naipe(suit, 10, 'Q'));
baraja.add(new Naipe(suit, 10, 'R'));
baraja.add(new Naipe(suit, 11, 'A'));
suit++;
}
}
private void barajearDeck(ArrayList baraja) {
Collections.shuffle(baraja);
}
}
If you need to see the classes Hand and Playing Cards are here
private class Mano {
int ID;
ArrayList<Naipe> naipes = new ArrayList<Naipe>();
int numNaipes;
public Mano (int identidad, int cantidad_naipes) {
this.ID = identidad;
this.numNaipes = cantidad_naipes;
}
public int suma() {
int suma = 0;
for (int i = 0; i < this.numNaipes; i++) {
suma += this.naipes.get(i).darValor();
}
return suma;
}
public void impMano() {
for (int i = 0; i < this.numNaipes; i++) {
this.naipes.get(i).mostrar();
}
}
}
private class Naipe {
int valor;
char simbolo;
String mazo;
private Naipe (int palo, int val, char signo) {
this.valor = val;
this.simbolo = signo;
this.mazo = determinarMazo(palo);
}
private String determinarMazo(int palo) {
String suit;
if (palo == 0)
suit = "Picas";
else if (palo == 1)
suit = "Diamantes";
else if (palo == 2)
suit = "Treboles";
else
suit = "Corazones";
return suit;
}
public int darValor() {
return this.valor;
}
public void mostrar() {
System.out.println(this.simbolo + " de " + this.mazo);
}
}