Skip to content

Instantly share code, notes, and snippets.

@MartelliEnrico
Created December 17, 2012 22:34
Show Gist options
  • Save MartelliEnrico/4323042 to your computer and use it in GitHub Desktop.
Save MartelliEnrico/4323042 to your computer and use it in GitHub Desktop.
package solitari;
public class Carta {
int numero;
int seme;
public Carta(int numero, int seme) {
this.numero = numero;
this.seme = seme;
}
public int getNumero() {
return numero;
}
public int getSeme() {
return seme;
}
}
package solitari;
public class CarteBriscola extends Mazzo {
public CarteBriscola() {
super(40, new String[] {
"Denara", "Coppe", "Spade", "Bastoni"
}, new String[] {
"1", "2", "3", "4", "5", "6", "7", "Fante", "Cavallo", "Re"
});
}
}
package solitari;
public class CartePoker extends Mazzo {
public CartePoker() {
super(52, new String[] {
"Cuori", "Quadri", "Fiori", "Picche"
}, new String[] {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Donna", "K"
});
}
}
package solitari;
import java.util.Random;
public class Mazzo {
int numeroCarte, numeroSemi;
Carta[] carte;
String[] nomi;
String[] semi;
protected Random numeroRandom;
public Mazzo(int numeroCarte, String[] semi, String[] nomi) {
this.numeroCarte = numeroCarte;
this.numeroSemi = semi.length;
this.nomi = nomi;
this.semi = semi;
genera();
mescola();
}
protected void genera() {
Carta[] tmp = new Carta[numeroCarte];
int cartePerSeme = numeroCarte / numeroSemi;
for(int i = 0; i < numeroSemi; i++) {
for(int j = 1; j <= cartePerSeme; j++) {
tmp[i * cartePerSeme + j - 1] = new Carta(j, i);
}
}
this.carte = tmp;
}
public void mescola() {
for(int i = 0; i < numeroCarte; i++) {
/*
* TODO: Trovare perché non va questo maledetto indiceRandom
* int indiceRandom = numeroRandom.nextInt(numeroCarte);
*/
Carta tmp = carte[i];
carte[i] = carte[numeroCarte-i-1];
carte[numeroCarte-i-1] = tmp;
}
}
public int getNumeroCarte() {
return numeroCarte;
}
public int getNumeroSemi() {
return numeroSemi;
}
}
package solitari;
public class Solitari {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CarteBriscola cb = new CarteBriscola();
CartePoker cp = new CartePoker();
for(int i = 0; i < cb.getNumeroCarte(); i++) {
System.out.println(cb.nomi[cb.carte[i].getNumero()-1] + " di " + cb.semi[cb.carte[i].getSeme()]);
}
for(int i = 0; i < cp.getNumeroCarte(); i++) {
System.out.println(cp.nomi[cp.carte[i].getNumero()-1] + " di " + cp.semi[cp.carte[i].getSeme()]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment