learningjava/14july2025/Game.java

111 lines
4.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.util.*;
import java.util.function.Function;
public class Game {
private Wallet kullanici;
public Game(Wallet kullanici) {
this.setKullanici(kullanici);
}
public void baslat() {
System.out.println(this.kullanici.getIsim());
while (true) {
if (this.getKullanici().getPara() <= 0) {
System.out.println("İflas!");
break;
}
System.out.println("Mevcut bakiye:" + this.getKullanici().getPara());
this.bj();
}
}
public void bj() {
System.out.println("Kullanmak istediğiniz miktarı giriniz:");
Scanner scanner = new Scanner(System.in);
Double miktar = scanner.nextDouble();
scanner.nextLine();
if (this.kullanici.paraGuncelleme(miktar * -1)) {
List<String> masa = new ArrayList<>();
String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for (int i = 0; i < 4; i++) {
for (String rank : ranks) {
masa.add(rank);
}
}
Collections.shuffle(masa);
List<String> kullaniciEli = new ArrayList<>();
List<String> kurpiyerEli = new ArrayList<>();
kullaniciEli.add(masa.remove(0));
kurpiyerEli.add(masa.remove(0));
kullaniciEli.add(masa.remove(0));
kurpiyerEli.add(masa.remove(0));
Function<List<String>, Integer> degerHesapla = hand -> {
int total = 0;
int aces = 0;
for (String card : hand) {
switch (card) {
case "A": total += 11; aces++; break;
case "K": case "Q": case "J": case "10": total += 10; break;
default: total += Integer.parseInt(card);
}
}
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
};
System.out.println("Kullanıcı Eli: " + kullaniciEli + " (" + degerHesapla.apply(kullaniciEli) + ")");
System.out.println("Kurpiyer Eli: [" + kurpiyerEli.get(0) + ", ?]");
int kullaniciDeger = degerHesapla.apply(kullaniciEli);
boolean kullaniciPatladi = false;
while (kullaniciDeger < 21) {
System.out.println("Tamam mı devam mı?? (t/d)");
String tamamdevam = scanner.nextLine().trim().toLowerCase();
if (!tamamdevam.equals("d")) break;
kullaniciEli.add(masa.remove(0));
kullaniciDeger = degerHesapla.apply(kullaniciEli);
System.out.println("Kullanıcı eli: " + kullaniciEli + " (" + kullaniciDeger + ")");
if (kullaniciDeger > 21) {
System.out.println("Patladın!");
kullaniciPatladi = true;
break;
}
}
if (!kullaniciPatladi) {
int kurpiyerDeger = degerHesapla.apply(kurpiyerEli);
System.out.println("Kurpiyer Eli: " + kurpiyerEli + " (" + kurpiyerDeger + ")");
while (kurpiyerDeger < 17) {
kurpiyerEli.add(masa.remove(0));
kurpiyerDeger = degerHesapla.apply(kurpiyerEli);
System.out.println("Kurpiyer çekti: " + kurpiyerEli + " (" + kurpiyerDeger + ")");
}
if (kurpiyerDeger > 21 || kullaniciDeger > kurpiyerDeger) {
System.out.println("Kazandın!");
kullanici.paraGuncelleme(2 * miktar);
} else if (kullaniciDeger < kurpiyerDeger) {
System.out.println("Adam kazandı...");
} else {
System.out.println("Dostluk kazandı, para iade edildi.");
kullanici.paraGuncelleme(miktar);
}
}
}
}
public Wallet getKullanici() {
return this.kullanici;
}
public void setKullanici(Wallet kullanici) {
this.kullanici = kullanici;
}
}