Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | None | 0 0
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    
  2. *   Muhammet Can                                                                                    *
  3. *   091101025                                                                                       *
  4. *   ödev2                                                                                          *
  5. *                                                                                                   *
  6. *   Girilen verilere göre avcı avı köşeye sıkıştırıp yakalar.                                         *
  7. *                                                                                                   *
  8. *   Ancak avcıyı daha zeki yapabilecek bir şekilde geliştiremedim. Matematiksel                     *
  9. *   olarak çözümü buldum ama koda dökemedim. Avcı bazı durumlarda, avı kenarlara daha               *
  10. *   çabuk sıkıştırabilecekken, malesef default ayarlara göre kovalamaya devam ediyor.                 *
  11. *                                                                                                   *
  12. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  13.  
  14. import java.util.*;
  15.  
  16. class oyun {
  17.     public int sutun, satir, preyX, preyY, hunterX, hunterY;
  18.     public double araMesafe;
  19.     public int count;
  20.    
  21.     public void gridOlustur() {
  22.         //----------------------- ilk 1234..n döngüsü -----------------------|
  23.         System.out.print("  ");
  24.  
  25.         for (int i=0; i<sutun; i++) {
  26.             if (i>8) {
  27.                 System.out.print(i+1+" ");
  28.             } else {
  29.                 System.out.print(" "+(i+1)+" ");
  30.             }
  31.  
  32.         }
  33.         //----------------------- ilk 1234..n döngüsü(bitimi) -----------------------|
  34.  
  35.         System.out.println("");
  36.  
  37.         //----------------------- ikinci kısım iç içe loop -----------------------|
  38.         for (int j=0; j<satir; j++) {
  39.             //----------------------- ilk olarak aşağı akan rakamlar -----------------------|
  40.             if (j<9) {
  41.                 System.out.print(j+1+" ");
  42.             } else {
  43.                 System.out.print(j+1);
  44.             }
  45.  
  46.             //----------------------- yan yana olan noktalar -----------------------|
  47.             for(int k=0; k<sutun; k++) {
  48.                 if ((((k+1)==preyX) && ((k+1)==hunterX)) && (((j+1)==preyY) && ((j+1)==hunterY))) {
  49.                     System.out.print(" # "); //vurdu
  50.                 }
  51.                 else if((k+1)==preyX && (j+1)==preyY) System.out.print(" P "); //prey yerleştir
  52.                 else if((k+1)==hunterX && (j+1)==hunterY) System.out.print(" H "); //hunter yerleştir
  53.                 else System.out.print(" . ");
  54.             }
  55.  
  56.             System.out.println("");
  57.         } //----------------------- grid çizimi ve H-P yerleşimi tamamen bitiyor -----------------------|
  58.     }//----------------------- gridOluştur metodu bitiyor -----------------------|
  59.  
  60.    
  61.     /*  updateGrid metodu avcının hareketini hesaplıyor.
  62.         normalde daha zeki yapabilmek için gerekli matematiksel şeyleri hesapladım,
  63.         ama bunları onlarca if halinde koda dökemedim bi türlü.    
  64.         o yüzden ilk yazdığım en troll, gerizekalı avcı mantığını yolluyorum. */
  65.     public void updateGrid() {
  66.         if (gideriVar()==true) {
  67.             if (araMesafe>1.5) {
  68.                 if(hunterX > preyX) hunterX -= 1;
  69.                 else if(hunterX < preyX) hunterX += 1;
  70.  
  71.                 if(hunterY > preyY) hunterY -= 1;
  72.                 else if(hunterY < preyY) hunterY +=1;
  73.             } else {
  74.                 if(hunterX>preyX && hunterY>preyY) {
  75.                     hunterX -= 1;
  76.                 } else if(hunterX > preyX) {
  77.                     hunterX -= 1;
  78.                     preyX -= 1;
  79.                 } else if (hunterX<preyX && hunterY<preyY) {
  80.                     hunterX += 1;
  81.                 } else if(hunterX < preyX) {
  82.                     hunterX += 1;
  83.                     preyX += 1;
  84.                 }
  85.  
  86.                 if(hunterY > preyY) {
  87.                     hunterY -= 1;
  88.                     preyY -= 1;
  89.                 } else if(hunterY < preyY) {
  90.                     hunterY +=1;
  91.                     preyY +=1;
  92.                 }
  93.             }
  94.         } else {
  95.             if (araMesafe>1.5) {
  96.                 if(hunterX > preyX) hunterX -= 1;
  97.                 else if(hunterX < preyX) hunterX += 1;
  98.  
  99.                 if(hunterY > preyY) hunterY -= 1;
  100.                 else if(hunterY < preyY) hunterY +=1;
  101.             } else {
  102.                 if(hunterX > preyX) {
  103.                     hunterX -= 1;
  104.                 } else if(hunterX < preyX) {
  105.                     hunterX += 1;
  106.                 }
  107.  
  108.                 if(hunterY > preyY) {
  109.                     hunterY -= 1;
  110.                 } else if(hunterY < preyY) {
  111.                     hunterY +=1;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     // araLen metodu avcı ve av arasındaki mesafeyi hesaplıyor.
  118.     public double araLen(int x1, int y1, int x2, int y2) {
  119.         araMesafe = Math.abs((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  120.         araMesafe = Math.pow(araMesafe, 1/2.0);
  121.         return araMesafe;
  122.  
  123.     }
  124.  
  125.     // gideriVar metodu avın daha fazla kaçıp kaçamayacağını hesaplıyor. av kaçabilecek durumdaysa, (kenarlara sıkışmadıysa)
  126.     // true dönüp kaçmasına izin veriyor.  
  127.     public boolean gideriVar() {
  128.         int mesafe;
  129.         if (hunterX > preyX) {
  130.             mesafe = (sutun-1) - (sutun-preyX);
  131.         } else if (hunterX < preyX) {
  132.             mesafe = (sutun-preyX);
  133.         } else if (hunterY > preyY) {
  134.             mesafe = (satir-1) - (satir-preyY);
  135.         } else if (hunterY<preyY) {
  136.             mesafe = (satir-preyY);
  137.         } else {
  138.             mesafe = 2;
  139.         }
  140.  
  141.         if (mesafe > 0) {
  142.             return true;
  143.         } else {
  144.             return false;
  145.         }
  146.  
  147.     }
  148.  
  149. }
  150.  
  151. public class odev2 {
  152.     public static void main(String[] args) {
  153.  
  154.         oyun obje = new oyun();
  155.  
  156.         Scanner input = new Scanner(System.in);
  157.  
  158.         System.out.printf("Grid bilgilerini giriniz (Sütun) (Satır): ");
  159.         String grid = input.nextLine();
  160.         String[] gridXY = grid.split(" ");
  161.         obje.sutun = Integer.parseInt(gridXY[0]);
  162.         obje.satir = Integer.parseInt(gridXY[1]);
  163.  
  164.         System.out.printf("Avın yerini giriniz (Sütun) (Satır): ");
  165.         String prey = input.nextLine();
  166.         String[] preyXY = prey.split(" ");
  167.         obje.preyX = Integer.parseInt(preyXY[0]);
  168.         obje.preyY = Integer.parseInt(preyXY[1]);
  169.  
  170.         System.out.printf("Avcının yerini giriniz (Sütun) (Satır): ");
  171.         String hunter = input.nextLine();
  172.         String[] hunterXY = hunter.split(" ");
  173.         obje.hunterX = Integer.parseInt(hunterXY[0]);
  174.         obje.hunterY = Integer.parseInt(hunterXY[1]);
  175.  
  176.         System.out.printf("Oyun modunu giriniz (i/s): ");
  177.         String oyunModu = input.next();
  178.  
  179.         String devamEt;
  180.         obje.count = 0;
  181.         if(oyunModu.equals("i")) {         
  182.             do {
  183.                 obje.gridOlustur();
  184.                 double ara = obje.araLen(obje.preyX, obje.preyY, obje.hunterX, obje.hunterY);
  185.                 if (ara == 0) break;
  186.                 System.out.printf("Aradaki mesafe: %.2f", ara);
  187.                 System.out.print("\nDevam? (e/h): ");
  188.                 devamEt = input.next();
  189.                 obje.updateGrid();
  190.                 obje.count=obje.count+1;
  191.             } while((devamEt.equals("e")));
  192.            
  193.             System.out.println("Avcı, avı " +obje.count+ " hamlede yakaladı.");
  194.  
  195.         } else if(oyunModu.equals("s")) {
  196.             obje.gridOlustur();
  197.             double ara = obje.araLen(obje.preyX, obje.preyY, obje.hunterX, obje.hunterY);
  198.             while(ara != 0) {
  199.                 obje.updateGrid();
  200.                 ara = obje.araLen(obje.preyX, obje.preyY, obje.hunterX, obje.hunterY);
  201.             }
  202.             if (ara==0) {
  203.                 obje.gridOlustur();
  204.             }
  205.  
  206.         } else {
  207.             System.out.println("öyle bir mod yok");
  208.         }
  209.  
  210.  
  211.     } //----------------------- main fonksiyonunun sonu -----------------------|
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement