Advertisement
nguyenvanquan7826

Class Algorithm in pokemon game

Mar 24th, 2014
2,578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.16 KB | None | 0 0
  1. // http://nguyenvanquan7826.wordpress.com/2014/03/25/thuat-toan-game-pokemon-pikachu/
  2. package nguyenvanquan7826;
  3.  
  4. import java.awt.Point;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.util.Scanner;
  8.  
  9. public class Algorithm {
  10.     private int size = 5;
  11.     private int barrier = 2;
  12.     private int[][] matrix;
  13.  
  14.     public Algorithm() {
  15.         readFile();
  16.         showMatrix();
  17.         System.out.println(checkTwoPoint(new Point(2, 2), new Point(4, 4))
  18.                 .toString());
  19.     }
  20.  
  21.     // read matrix from file input
  22.     private void readFile() {
  23.         File fileInput = new File(getClass().getResource(
  24.                 "/nguyenvanquan7826/input").getFile());
  25.         try {
  26.             Scanner scan = new Scanner(fileInput);
  27.             size = scan.nextInt();
  28.             size += 2;
  29.             matrix = new int[size][size];
  30.             for (int i = 0; i < size; i++) {
  31.                 for (int j = 0; j < size; j++) {
  32.                     matrix[i][j] = scan.nextInt();
  33.                 }
  34.             }
  35.         } catch (FileNotFoundException e) {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39.  
  40.     // show matrix
  41.     private void showMatrix() {
  42.         for (int i = 1; i < size - 1; i++) {
  43.             for (int j = 1; j < size - 1; j++) {
  44.                 System.out.print(matrix[i][j] + " ");
  45.             }
  46.             System.out.println();
  47.         }
  48.     }
  49.  
  50.     // check with line x, from column y1 to y2
  51.     private boolean checkLineX(int y1, int y2, int x) {
  52.         // find point have column max and min
  53.         int min = Math.min(y1, y2);
  54.         int max = Math.max(y1, y2);
  55.         // run column
  56.         for (int y = min; y <= max; y++) {
  57.             if (matrix[x][y] == barrier) { // if see barrier then die
  58.                 System.out.println("die: " + x + "" + y);
  59.                 return false;
  60.             }
  61.             System.out.println("ok: " + x + "" + y);
  62.         }
  63.         // not die -> success
  64.         return true;
  65.     }
  66.  
  67.     private boolean checkLineY(int x1, int x2, int y) {
  68.         int min = Math.min(x1, x2);
  69.         int max = Math.max(x1, x2);
  70.         for (int x = min; x <= max; x++) {
  71.             if (matrix[x][y] == barrier) {
  72.                 System.out.println("die: " + x + "" + y);
  73.                 return false;
  74.             }
  75.             System.out.println("ok: " + x + "" + y);
  76.         }
  77.         return true;
  78.     }
  79.  
  80.     // check in rectangle
  81.     private int checkRectX(Point p1, Point p2) {
  82.         // find point have y min and max
  83.         Point pMinY = p1, pMaxY = p2;
  84.         if (p1.y > p2.y) {
  85.             pMinY = p2;
  86.             pMaxY = p1;
  87.         }
  88.         for (int y = pMinY.y + 1; y < pMaxY.y; y++) {
  89.             // check three line
  90.             if (checkLineX(pMinY.y, y, pMinY.x)
  91.                     && checkLineY(pMinY.x, pMaxY.x, y)
  92.                     && checkLineX(y, pMaxY.y, pMaxY.x)) {
  93.  
  94.                 System.out.println("Rect x");
  95.                 System.out.println("(" + pMinY.x + "," + pMinY.y + ") -> ("
  96.                         + pMinY.x + "," + y + ") -> (" + pMaxY.x + "," + y
  97.                         + ") -> (" + pMaxY.x + "," + pMaxY.y + ")");
  98.                 // if three line is true return column y
  99.                 return y;
  100.             }
  101.         }
  102.         // have a line in three line not true then return -1
  103.         return -1;
  104.     }
  105.  
  106.     private int checkRectY(Point p1, Point p2) {
  107.         // find point have y min
  108.         Point pMinX = p1, pMaxX = p2;
  109.         if (p1.x > p2.x) {
  110.             pMinX = p2;
  111.             pMaxX = p1;
  112.         }
  113.         // find line and y begin
  114.         for (int x = pMinX.x + 1; x < pMaxX.x; x++) {
  115.             if (checkLineY(pMinX.x, x, pMinX.y)
  116.                     && checkLineX(pMinX.y, pMaxX.y, x)
  117.                     && checkLineY(x, pMaxX.x, pMaxX.y)) {
  118.  
  119.                 System.out.println("Rect y");
  120.                 System.out.println("(" + pMinX.x + "," + pMinX.y + ") -> (" + x
  121.                         + "," + pMinX.y + ") -> (" + x + "," + pMaxX.y
  122.                         + ") -> (" + pMaxX.x + "," + pMaxX.y + ")");
  123.                 return x;
  124.             }
  125.         }
  126.         return -1;
  127.     }
  128.  
  129.     /**
  130.      * p1 and p2 are Points want check
  131.      *
  132.      * @param type
  133.      *            : true is check with increase, false is decrease return column
  134.      *            can connect p1 and p2
  135.      */
  136.     private int checkMoreLineX(Point p1, Point p2, int type) {
  137.         // find point have y min
  138.         Point pMinY = p1, pMaxY = p2;
  139.         if (p1.y > p2.y) {
  140.             pMinY = p2;
  141.             pMaxY = p1;
  142.         }
  143.         // find line and y begin
  144.         int y = pMaxY.y;
  145.         int row = pMinY.x;
  146.         if (type == -1) {
  147.             y = pMinY.y;
  148.             row = pMaxY.x;
  149.         }
  150.         // check more
  151.         if (checkLineX(pMinY.y, pMaxY.y, row)) {
  152.             while (matrix[pMinY.x][y] != barrier
  153.                     && matrix[pMaxY.x][y] != barrier) {
  154.                 if (checkLineY(pMinY.x, pMaxY.x, y)) {
  155.  
  156.                     System.out.println("TH X " + type);
  157.                     System.out.println("(" + pMinY.x + "," + pMinY.y + ") -> ("
  158.                             + pMinY.x + "," + y + ") -> (" + pMaxY.x + "," + y
  159.                             + ") -> (" + pMaxY.x + "," + pMaxY.y + ")");
  160.                     return y;
  161.                 }
  162.                 y += type;
  163.             }
  164.         }
  165.         return -1;
  166.     }
  167.  
  168.     private int checkMoreLineY(Point p1, Point p2, int type) {
  169.         Point pMinX = p1, pMaxX = p2;
  170.         if (p1.x > p2.x) {
  171.             pMinX = p2;
  172.             pMaxX = p1;
  173.         }
  174.         int x = pMaxX.x;
  175.         int col = pMinX.y;
  176.         if (type == -1) {
  177.             x = pMinX.x;
  178.             col = pMaxX.y;
  179.         }
  180.         if (checkLineY(pMinX.x, pMaxX.x, col)) {
  181.             while (matrix[x][pMinX.y] != barrier
  182.                     && matrix[x][pMaxX.x] != barrier) {
  183.                 if (checkLineX(pMinX.y, pMaxX.y, x)) {
  184.                     System.out.println("TH Y " + type);
  185.                     System.out.println("(" + pMinX.x + "," + pMinX.y + ") -> ("
  186.                             + x + "," + pMinX.y + ") -> (" + x + "," + pMaxX.y
  187.                             + ") -> (" + pMaxX.x + "," + pMaxX.y + ")");
  188.                     return x;
  189.                 }
  190.                 x += type;
  191.             }
  192.         }
  193.         return -1;
  194.     }
  195.  
  196.     private MyLine checkTwoPoint(Point p1, Point p2) {
  197.         // check line with x
  198.         if (p1.x == p2.x) {
  199.             if (checkLineX(p1.y, p2.y, p1.x)) {
  200.                 return new MyLine(p1, p2);
  201.             }
  202.         }
  203.         // check line with y
  204.         if (p1.y == p2.y) {
  205.             if (checkLineY(p1.x, p2.x, p1.y)) {
  206.                 return new MyLine(p1, p2);
  207.             }
  208.         }
  209.  
  210.         int t = -1; // t is column find
  211.  
  212.         // check in rectangle with x
  213.         if ((t = checkRectX(p1, p2)) != -1) {
  214.             return new MyLine(new Point(p1.x, t), new Point(p2.x, t));
  215.         }
  216.  
  217.         // check in rectangle with y
  218.         if ((t = checkRectY(p1, p2)) != -1) {
  219.             return new MyLine(new Point(t, p1.y), new Point(t, p2.y));
  220.         }
  221.         // check more right
  222.         if ((t = checkMoreLineX(p1, p2, 1)) != -1) {
  223.             return new MyLine(new Point(p1.x, t), new Point(p2.x, t));
  224.         }
  225.         // check more left
  226.         if ((t = checkMoreLineX(p1, p2, -1)) != -1) {
  227.             return new MyLine(new Point(p1.x, t), new Point(p2.x, t));
  228.         }
  229.         // check more down
  230.         if ((t = checkMoreLineY(p1, p2, 1)) != -1) {
  231.             return new MyLine(new Point(t, p1.y), new Point(t, p2.y));
  232.         }
  233.         // check more up
  234.         if ((t = checkMoreLineY(p1, p2, -1)) != -1) {
  235.             return new MyLine(new Point(t, p1.y), new Point(t, p2.y));
  236.         }
  237.         return null;
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement