Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 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.     public void updateGrid() {
  61.         if (gideriVar()==true) {
  62.             if (araMesafe>1.5) {
  63.                 if(hunterX > preyX) hunterX -= 1;
  64.                 else if(hunterX < preyX) hunterX += 1;
  65.  
  66.                 if(hunterY > preyY) hunterY -= 1;
  67.                 else if(hunterY < preyY) hunterY +=1;
  68.             } else {
  69.                 if(hunterX>preyX && hunterY>preyY) {
  70.                     hunterX -= 1;
  71.                 } else if(hunterX > preyX) {
  72.                     hunterX -= 1;
  73.                     preyX -= 1;
  74.                 } else if (hunterX<preyX && hunterY<preyY) {
  75.                     hunterX += 1;
  76.                 } else if(hunterX < preyX) {
  77.                     hunterX += 1;
  78.                     preyX += 1;
  79.                 }
  80.  
  81.                 if(hunterY > preyY) {
  82.                     hunterY -= 1;
  83.                     preyY -= 1;
  84.                 } else if(hunterY < preyY) {
  85.                     hunterY +=1;
  86.                     preyY +=1;
  87.                 }
  88.             }
  89.         } else {
  90.             if (araMesafe>1.5) {
  91.                 if(hunterX > preyX) hunterX -= 1;
  92.                 else if(hunterX < preyX) hunterX += 1;
  93.  
  94.                 if(hunterY > preyY) hunterY -= 1;
  95.                 else if(hunterY < preyY) hunterY +=1;
  96.             } else {
  97.                 if(hunterX > preyX) {
  98.                     hunterX -= 1;
  99.                 } else if(hunterX < preyX) {
  100.                     hunterX += 1;
  101.                 }
  102.  
  103.                 if(hunterY > preyY) {
  104.                     hunterY -= 1;
  105.                 } else if(hunterY < preyY) {
  106.                     hunterY +=1;
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     public double araLen(int x1, int y1, int x2, int y2) {
  113.         araMesafe = Math.abs((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  114.         araMesafe = Math.pow(araMesafe, 1/2.0);
  115.         return araMesafe;
  116.  
  117.     }
  118.  
  119.     public boolean gideriVar() {
  120.         int mesafe;
  121.         if (hunterX > preyX) {
  122.             mesafe = (sutun-1) - (sutun-preyX);
  123.         } else if (hunterX < preyX) {
  124.             mesafe = (sutun-preyX);
  125.         } else if (hunterY > preyY) {
  126.             mesafe = (satir-1) - (satir-preyY);
  127.         } else if (hunterY<preyY) {
  128.             mesafe = (satir-preyY);
  129.         } else {
  130.             mesafe = 2;
  131.         }
  132.  
  133.         if (mesafe > 0) {
  134.             return true;
  135.         } else {
  136.             return false;
  137.         }
  138.  
  139.     }
  140.  
  141. }
  142.  
  143. public class odev2 {
  144.     public static void main(String[] args) {
  145.  
  146.         oyun obje = new oyun();
  147.  
  148.         Scanner input = new Scanner(System.in);
  149.  
  150.         System.out.printf("Grid bilgilerini giriniz (Sütun) (Satır): ");
  151.         String grid = input.nextLine();
  152.         String[] gridXY = grid.split(" ");
  153.         obje.sutun = Integer.parseInt(gridXY[0]);
  154.         obje.satir = Integer.parseInt(gridXY[1]);
  155.  
  156.         System.out.printf("Avın yerini giriniz (Sütun) (Satır): ");
  157.         String prey = input.nextLine();
  158.         String[] preyXY = prey.split(" ");
  159.         obje.preyX = Integer.parseInt(preyXY[0]);
  160.         obje.preyY = Integer.parseInt(preyXY[1]);
  161.  
  162.         System.out.printf("Avcının yerini giriniz (Sütun) (Satır): ");
  163.         String hunter = input.nextLine();
  164.         String[] hunterXY = hunter.split(" ");
  165.         obje.hunterX = Integer.parseInt(hunterXY[0]);
  166.         obje.hunterY = Integer.parseInt(hunterXY[1]);
  167.  
  168.         System.out.printf("Oyun modunu giriniz (i/s): ");
  169.         String oyunModu = input.next();
  170.  
  171.         String devamEt;
  172.         obje.count = 0;
  173.         if(oyunModu.equals("i")) {         
  174.             do {
  175.                 obje.gridOlustur();
  176.                 double ara = obje.araLen(obje.preyX, obje.preyY, obje.hunterX, obje.hunterY);
  177.                 if (ara == 0) break;
  178.                 System.out.printf("Aradaki mesafe: %.2f", ara);
  179.                 System.out.print("\nDevam? (e/h): ");
  180.                 devamEt = input.next();
  181.                 obje.updateGrid();
  182.                 obje.count=obje.count+1;
  183.             } while((devamEt.equals("e")));
  184.            
  185.             System.out.println("Avcı, avı " +obje.count+ " hamlede yakaladı.");
  186.  
  187.         } else if(oyunModu.equals("s")) {
  188.             obje.gridOlustur();
  189.             double ara = obje.araLen(obje.preyX, obje.preyY, obje.hunterX, obje.hunterY);
  190.             while(ara != 0) {
  191.                 obje.updateGrid();
  192.                 ara = obje.araLen(obje.preyX, obje.preyY, obje.hunterX, obje.hunterY);
  193.             }
  194.             if (ara==0) {
  195.                 obje.gridOlustur();
  196.             }
  197.  
  198.         } else {
  199.             System.out.println("öyle bir mod yok");
  200.         }
  201.  
  202.  
  203.     } //----------------------- main fonksiyonunun sonu -----------------------|
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement