Kopiuj
package cardgame;
class Card {
private Integer value;
private String name;
private String color;
public Card(Integer v, String n, String c) {
value = v;
name = n;
color = c;
}
public Card(Card pattern){
value=pattern.getValue();
name=pattern.getName();
color=pattern.getColor();
}
Integer getValue() {
return new Integer(value);
}
String getName() {
return new String(name);
}
String getColor() {
return new String(color);
}
}
package cardgame;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
class Deck implements Iterable {
private List<Card> deck = new LinkedList<Card>();
public void add(Card c) //dodawania karty do talii
{
deck.add(c);
}
Card popCard() //wyciąganie karty z talii
{
Random r = new Random();
int index = r.nextInt(deck.size());
Card tmp = new Card(deck.get(index));
deck.remove(index);
return tmp;
}
void createFull()
{
String[] kolory= new String[]{"kier","karo","pik","trefl"};
String[] figury=new String[]{"2","3","4","5","6","7","8","9","10","Walet","Dama","Krol","As"};
for (int i=0;i<4;i++)
{
for (int j=0;j<9;j++) //2-10
{
Card tmp = new Card(j+2,figury[j],kolory[i]); //generujemy kartę
deck.add(tmp); //dodajemy do talii
}
for (int j=9;j<12;j++) //walet, dama, krol
{
Card tmp=new Card(j-7,figury[j],kolory[i]); //generujemy kartę
deck.add(tmp); //dodajemy do talii
}
Card tmp=new Card(11,figury[12],kolory[i]); //as
deck.add(tmp); //dodajemy do talii
}
}
Integer getValue() //funkcja zwraca wartość wszystkich kart w talii
{
Integer sum = 0;
for (Card c : deck)
sum+=c.getValue();
return sum;
}
boolean isEmpty()
{
return deck.isEmpty();
}
public Iterator iterator() {
return deck.iterator();
}
@Override
public String toString(){
String retval = new String();
for(Iterator<Card> it = this.iterator();it.hasNext();){
Card karta = new Card(it.next());
retval+=karta.getValue()+" "+karta.getName()+" "+karta.getColor()+"\n";
}
retval+="W sumie "+this.getValue()+"\n";
return retval;
}
}
package cardgame;
public class Main {
public static void main(String[] args) {
Deck talia = new Deck();
Deck gracz1 = new Deck();
Deck gracz2 = new Deck();
talia.createFull();
while ((gracz1.getValue() <= 21) && (gracz2.getValue() <= 21)) {
gracz1.add(talia.popCard());
if (gracz1.getValue() <= 21) {
gracz2.add(talia.popCard());
}
}
if (gracz1.getValue() == 21) {
if (gracz2.getValue() == 21) {
System.out.println("Remis");
System.out.println("\nKarty 1:\n" + gracz1);
System.out.println("\nKarty 2:\n" + gracz2);
} else {
System.out.println("Wygrywa 1");
System.out.println("\nKarty 1:\n" + gracz1);
System.out.println("\nKarty 2:\n" + gracz2);
}
} else if (gracz2.getValue() == 21) {
System.out.println("Wygrywa 2");
System.out.println("\nKarty 1:\n" + gracz1);
System.out.println("\nKarty 2:\n" + gracz2);
} else if (gracz1.getValue() > 21) {
System.out.println("Wygrywa 2");
System.out.println("\nKarty 1:\n" + gracz1);
System.out.println("\nKarty 2:\n" + gracz2);
} else {
System.out.println("Wygrywa 1");
System.out.println("\nKarty 1:\n" + gracz1);
System.out.println("\nKarty 2:\n" + gracz2);
}
}
}