Last active
January 15, 2019 08:49
-
-
Save ahmetaa/ae41b9fb3fc2d1925c7828f34adfcbb5 to your computer and use it in GitHub Desktop.
robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.Set; | |
public class OyunAlani { | |
public static final int MAX_DENEME_SAYISI = 10000; | |
Hucre[][] alan; | |
int en, boy; | |
Hucre robotBaslangic; | |
Hucre dogruCevap; | |
List<Hucre> yanlisCevaplar = new ArrayList<>(); | |
List<Hucre> kayalar = new ArrayList<>(); | |
private Random rnd = new Random(); | |
public OyunAlani( | |
int en, | |
int boy, | |
int yanlisCevapSayisi, | |
int kayaSayisi) { | |
this.en = en; | |
this.boy = boy; | |
this.alan = new Hucre[en][boy]; | |
// tüm alanı boş hücrelerle doldur. | |
for (int i = 0; i < en; i++) { | |
alan[i] = new Hucre[boy]; | |
for (int j = 0; j < boy; j++) { | |
alan[i][j] = new Hucre(i, j, Tur.BOS); | |
} | |
} | |
// robot başlangıcını seç | |
robotBaslangic = bosAlanBul(); | |
// doğru cevabı seç. | |
dogruCevapHucresiBul(); | |
kayalariYerlestir(kayaSayisi + yanlisCevapSayisi); | |
// yanlis cevapların üzerinden geçmek yasak olduğu için kayaların bir kısmını yanlı cevap olarak | |
// değiştir. | |
for (int i = 0; i < yanlisCevapSayisi; i++) { | |
int k = rnd.nextInt(kayalar.size()); | |
Hucre h = kayalar.remove(k); | |
h.tur = Tur.YANLIS; | |
yanlisCevaplar.add(h); | |
} | |
} | |
private void dogruCevapHucresiBul() { | |
while (true) { | |
Hucre h = bosAlanBul(); | |
// robota çok yakın olmasın. | |
if (Math.abs(h.x - robotBaslangic.x) < 2 || Math.abs(h.y - robotBaslangic.y) < 2) { | |
continue; | |
} | |
this.dogruCevap = h; | |
this.dogruCevap.tur = Tur.DOGRU; | |
return; | |
} | |
} | |
public Hucre getRobotBaslangic() { | |
return robotBaslangic; | |
} | |
enum Tur { | |
BOS, DOGRU, YANLIS, KAYA | |
} | |
Hucre getHucre(int x, int y) { | |
return alan[x][y]; | |
} | |
/// Matris icinde 0=BOS 1=DOGRU 2=YANLIS 3=KAYA | |
/// Robotun başlangıç koordinatı için getRobotBaslangic() kullan. | |
public int[][] sayiMatrisiOlarak() { | |
int[][] sonuc = new int[en][boy]; | |
for (int i = 0; i < en; i++) { | |
Hucre[] satir = alan[i]; | |
sonuc[i] = new int[satir.length]; | |
for (int j = 0; j < satir.length; j++) { | |
Tur tur = satir[j].tur; | |
int deger = 0; // boş | |
if (tur == Tur.KAYA) { | |
deger = 3; | |
} else if (tur == Tur.DOGRU) { | |
deger = 1; | |
} else if (tur == Tur.YANLIS) { | |
deger = 2; | |
} | |
sonuc[i][j] = deger; | |
} | |
} | |
return sonuc; | |
} | |
static class Hucre { | |
int x, y; | |
Tur tur; | |
public Hucre(int x, int y, Tur tur) { | |
this.x = x; | |
this.y = y; | |
this.tur = tur; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
Hucre hucre = (Hucre) o; | |
if (x != hucre.x) { | |
return false; | |
} | |
return y == hucre.y; | |
} | |
@Override | |
public int hashCode() { | |
int result = x; | |
result = 31 * result + y; | |
return result; | |
} | |
@Override | |
public String toString() { | |
return tur.name(); | |
} | |
} | |
Hucre bosAlanBul() { | |
int denemeSayisi = 0; | |
while (denemeSayisi < MAX_DENEME_SAYISI) { | |
int i = rnd.nextInt(en); | |
int j = rnd.nextInt(boy); | |
Hucre hucre = alan[i][j]; | |
if (robotBaslangic != null && robotBaslangic.equals(hucre)) { | |
continue; | |
} | |
if (hucre.tur == Tur.BOS) { | |
return hucre; | |
} | |
denemeSayisi++; | |
} | |
throw new IllegalStateException("Boş alan bulunamadı."); | |
} | |
void kayalariYerlestir(int kayaSayisi) { | |
int denemeSayisi = 0; | |
while (denemeSayisi < MAX_DENEME_SAYISI) { | |
for (int i = 0; i < kayaSayisi; i++) { | |
Hucre hucre = bosAlanBul(); | |
hucre.tur = Tur.KAYA; | |
kayalar.add(hucre); | |
} | |
denemeSayisi++; | |
if (yolAcikMi()) { | |
return; | |
} else { | |
// yol bulunamadığı için yeniden kayaların yer aldığı hücreleri. | |
for (Hucre hucre : kayalar) { | |
hucre.tur = Tur.BOS; | |
} | |
kayalar = new ArrayList<>(); | |
} | |
} | |
throw new IllegalStateException("Kayalara geldik"); | |
} | |
boolean yolAcikMi() { | |
List<Hucre> kontrolEdilecek = new ArrayList<>(); | |
Set<Hucre> kontrolEdildi = new HashSet<>(); | |
kontrolEdilecek.add(robotBaslangic); | |
while (!kontrolEdilecek.isEmpty()) { | |
// Kontrol edilecek hücrelerin komşularında yer alan ve henüz kontrol edilmemiş | |
// yeni boş hücreler bu listeye eklenir. | |
List<Hucre> yeniListe = new ArrayList<>(); | |
for (Hucre hucre : kontrolEdilecek) { | |
int x = hucre.x; | |
int y = hucre.y; | |
// sol hücre | |
if (x > 0) { | |
Hucre sol = alan[x - 1][y]; | |
if (sol.tur != Tur.KAYA && !kontrolEdildi.contains(sol)) { | |
yeniListe.add(sol); | |
} | |
} | |
// sag hücre | |
if (x < en - 1) { | |
Hucre sag = alan[x + 1][y]; | |
if (sag.tur != Tur.KAYA && !kontrolEdildi.contains(sag)) { | |
yeniListe.add(sag); | |
} | |
} | |
// üst hücre | |
if (y > 0) { | |
Hucre yukari = alan[x][y - 1]; | |
if (yukari.tur != Tur.KAYA && !kontrolEdildi.contains(yukari)) { | |
yeniListe.add(yukari); | |
} | |
} | |
// alt hücre | |
if (y < boy - 1) { | |
Hucre asagi = alan[x][y + 1]; | |
if (asagi.tur != Tur.KAYA && !kontrolEdildi.contains(asagi)) { | |
yeniListe.add(asagi); | |
} | |
} | |
kontrolEdildi.add(hucre); | |
} | |
// eğer yeni hücrelerden biri doğru cevap ile aynı ise yol bulundu demektir, dön. | |
for (Hucre hucre : yeniListe) { | |
if (hucre.tur == Tur.DOGRU) { | |
return true; | |
} | |
} | |
// yeni boş hücrelerin komşularına bakılması gerektiği için | |
// listeyi kontrolEdilecek listesine ata. yeni liste dönügünün başında sıfırdan tekrar | |
// üretilecek. | |
kontrolEdilecek = yeniListe; | |
} | |
// Buraya geldiysek doğru cevaba gidememişiz demektir. | |
return false; | |
} | |
private void ciz() { | |
for (int i = 0; i < en; i++) { | |
for (int j = 0; j < boy; j++) { | |
Hucre h = alan[i][j]; | |
if (h.equals(robotBaslangic)) { | |
System.out.print("R"); | |
continue; | |
} | |
if (h.tur == Tur.BOS) { | |
System.out.print("·"); | |
} else if (h.tur == Tur.DOGRU) { | |
System.out.print("D"); | |
} else if (h.tur == Tur.YANLIS) { | |
System.out.print("Y"); | |
} else if (h.tur == Tur.KAYA) { | |
System.out.print("*"); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
private void cizSayiMatrisi(int[][] matris) { | |
for (int i = 0; i < en; i++) { | |
for (int j = 0; j < boy; j++) { | |
int h = matris[i][j]; | |
if (i == robotBaslangic.x && j == robotBaslangic.y) { | |
System.out.print("R"); | |
continue; | |
} | |
if (h == 0) { | |
System.out.print("·"); | |
} else if (h == 1) { | |
System.out.print("D"); | |
} else if (h == 2) { | |
System.out.print("Y"); | |
} else if (h == 3) { | |
System.out.print("*"); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
for (int i = 0; i < 10; i++) { | |
OyunAlani alan = new OyunAlani(4, 7, 3, 10); | |
alan.ciz(); | |
System.out.println(); | |
int[][] m = alan.sayiMatrisiOlarak(); | |
alan.cizSayiMatrisi(m); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment