Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class GameTactic {
- char[][] matrix;
- int size;
- public GameTactic(char[][] matrix, int size) {
- this.matrix = matrix;
- this.size = size;
- }
- public int GenerateCost(Node n){
- if(this.matrix[n.getRow()][n.getCol()]=='R'){
- return 1;
- }
- if(this.matrix[n.getRow()][n.getCol()]=='D'){
- return 3;
- }
- if(this.matrix[n.getRow()][n.getCol()]=='H'){
- return 10;
- }
- return -1;
- }
- public List<Node> GetPossibleNeigh(Node n,int timeStamp){
- int row=n.getRow();
- int col=n.getCol();
- List<Node> possibleNeigh=new ArrayList<>();
- //R
- if(col<size-1 && matrix[row][col+1]!='W') {
- Node R = new Node(row,col+1);
- String path=n.getPath()+"-"+"R";
- R.setPath(path);
- R.setPriority("R");
- R.setCost(GenerateCost(R));
- R.setTimeStamp(timeStamp);
- possibleNeigh.add(R);
- }
- //RD
- if(col<size-1 && row<size-1 && matrix[row][col+1]!='W' && matrix[row+1][col]!='W' && matrix[row+1][col+1]!='W') {
- Node RD = new Node(row+1,col+1);
- String path=n.getPath()+"-"+"RD";
- RD.setPath(path);
- RD.setPriority("RD");
- RD.setCost(GenerateCost(RD));
- RD.setTimeStamp(timeStamp);
- possibleNeigh.add(RD);
- }
- //D
- if(row<size-1 && matrix[row+1][col]!='W') {
- Node D = new Node(row+1,col);
- String path=n.getPath()+"-"+"D";
- D.setPath(path);
- D.setPriority("D");
- D.setCost(GenerateCost(D));
- D.setTimeStamp(timeStamp);
- possibleNeigh.add(D);
- }
- //DL
- if(col>0 && row<size-1 && matrix[row+1][col-1]!='W' && matrix[row+1][col]!='W' && matrix[row][col-1]!='W') {
- Node DL = new Node(row+1,col-1);
- String path=n.getPath()+"-"+"DL";
- DL.setPath(path);
- DL.setPriority("DL");
- DL.setCost(GenerateCost(DL));
- DL.setTimeStamp(timeStamp);
- possibleNeigh.add(DL);
- }
- //L
- if(col>0 && matrix[row][col-1]!='W') {
- Node L = new Node(row,col-1);
- String path=n.getPath()+"-"+"L";
- L.setPath(path);
- L.setPriority("L");
- L.setCost(GenerateCost(L));
- L.setTimeStamp(timeStamp);
- possibleNeigh.add(L);
- }
- //LU
- if(col>0 && row>0 && matrix[row-1][col-1]!='W' && matrix[row-1][col]!='W' && matrix[row][col-1]!='W') {
- Node LU = new Node(row-1,col-1);
- String path=n.getPath()+"-"+"LU";
- LU.setPath(path);
- LU.setPriority("LU");
- LU.setCost(GenerateCost(LU));
- LU.setTimeStamp(timeStamp);
- possibleNeigh.add(LU);
- }
- //U
- if(row>0 && matrix[row-1][col]!='W') {
- Node U = new Node(row-1,col);
- String path=n.getPath()+"-"+"U";
- U.setPath(path);
- U.setPriority("U");
- U.setCost(GenerateCost(U));
- U.setTimeStamp(timeStamp);
- possibleNeigh.add(U);
- }
- //RU
- if(col<size-1 && row>0 && matrix[row-1][col+1]!='W' && matrix[row-1][col]!='W' && matrix[row][col+1]!='W') {
- Node RU = new Node(row-1,col+1);
- String path=n.getPath()+"-"+"RU";
- RU.setPath(path);
- RU.setPriority("RU");
- RU.setCost(GenerateCost(RU));
- RU.setTimeStamp(timeStamp);
- possibleNeigh.add(RU);
- }
- return possibleNeigh;
- }
- public boolean IsGoal(Node n){
- if(n.getRow()==size-1 && n.getCol()==size-1){
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement