chessai

college code for ai playing chess in java

git clone https://9o.is/git/chessai.git

ChessCoordinate.java

(6164B)


      1 package chess;
      2 
      3 import chess.utils.ChessColor;
      4 import chess.utils.ChessDirection;
      5 
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 /**
     10  * Check
     11  */
     12 public class ChessCoordinate {
     13 
     14     public static final int MAX = 8;
     15 
     16     private char file;
     17     private int rank;
     18 
     19 
     20     public ChessCoordinate(char file, int rank) {
     21       this.file = file;
     22       this.rank = rank;
     23     }
     24 
     25     public ChessCoordinate(int file, int rank) {
     26         this((char) (file-1+97), rank); // 97 = 'a'
     27     }
     28 
     29     public char getFile() {
     30         return file;
     31     }
     32 
     33     public int getRank() {
     34         return rank;
     35     }
     36 
     37     public int getFileNumber() {
     38         return file - 96; // 97 = 'a'
     39     }
     40 
     41     //CHECK
     42     public ChessCoordinate getCoordinate(
     43             ChessState state, ChessMove move) {
     44 
     45         int file = getFileNumber();
     46         int rank = getRank();
     47 
     48         int steps = move.getSteps();
     49         ChessDirection dir = move.getDirection();
     50 
     51         final ChessPiece srcPiece =
     52                 ChessPiece.find(state.get(move.getCoordinate()));
     53 
     54         if(srcPiece.getColor() == ChessColor.WHITE) {
     55             if(dir == ChessDirection.FORWARD)     rank += steps;
     56             else if(dir == ChessDirection.BACK)   rank -= steps;
     57             else if(dir == ChessDirection.LEFT)   file -= steps;
     58             else if(dir == ChessDirection.RIGHT)  file += steps;
     59 
     60             else if(dir == ChessDirection.FORWARD_LEFT) {
     61                 file -= steps;
     62                 rank += steps;
     63             } else if(dir == ChessDirection.FORWARD_RIGHT) {
     64                 file += steps;
     65                 rank += steps;
     66             } else if(dir == ChessDirection.BACK_LEFT) {
     67                 file -= steps;
     68                 rank -= steps;
     69             } else if(dir == ChessDirection.BACK_RIGHT) {
     70                 file += steps;
     71                 rank -= steps;
     72             } else if(dir == ChessDirection.KNIGHT_FORWARD_LEFT) {
     73                 file -= steps;
     74                 rank += steps+1;
     75             } else if(dir == ChessDirection.KNIGHT_FORWARD_RIGHT) {
     76                 file += steps;
     77                 rank += steps+1;
     78             } else if(dir == ChessDirection.KNIGHT_BACK_LEFT) {
     79                 file -= steps;
     80                 rank -= steps+1;
     81             } else if(dir == ChessDirection.KNIGHT_BACK_RIGHT) {
     82                 file += steps;
     83                 rank -= steps+1;
     84             } else if(dir == ChessDirection.KNIGHT_LEFT_FORWARD) {
     85                 file -= steps+1;
     86                 rank += steps;
     87             } else if(dir == ChessDirection.KNIGHT_LEFT_BACK) {
     88                 file -= steps+1;
     89                 rank -= steps;
     90             } else if(dir == ChessDirection.KNIGHT_RIGHT_FORWARD) {
     91                 file += steps+1;
     92                 rank += steps;
     93             } else if(dir == ChessDirection.KNIGHT_RIGHT_BACK) {
     94                 file += steps+1;
     95                 rank -= steps;
     96             }
     97         } else {
     98             if(dir == ChessDirection.FORWARD)     rank -= steps;
     99             else if(dir == ChessDirection.BACK)   rank += steps;
    100             else if(dir == ChessDirection.LEFT)   file += steps;
    101             else if(dir == ChessDirection.RIGHT)  file -= steps;
    102 
    103             else if(dir == ChessDirection.FORWARD_LEFT) {
    104                 file += steps;
    105                 rank -= steps;
    106             } else if(dir == ChessDirection.FORWARD_RIGHT) {
    107                 file -= steps;
    108                 rank -= steps;
    109             } else if(dir == ChessDirection.BACK_LEFT) {
    110                 file += steps;
    111                 rank += steps;
    112             } else if(dir == ChessDirection.BACK_RIGHT) {
    113                 file -= steps;
    114                 rank += steps;
    115             } else if(dir == ChessDirection.KNIGHT_FORWARD_LEFT) {
    116                 file += steps;
    117                 rank -= steps+1;
    118             } else if(dir == ChessDirection.KNIGHT_FORWARD_RIGHT) {
    119                 file -= steps;
    120                 rank -= steps+1;
    121             } else if(dir == ChessDirection.KNIGHT_BACK_LEFT) {
    122                 file += steps;
    123                 rank += steps+1;
    124             } else if(dir == ChessDirection.KNIGHT_BACK_RIGHT) {
    125                 file -= steps;
    126                 rank += steps+1;
    127             } else if(dir == ChessDirection.KNIGHT_LEFT_FORWARD) {
    128                 file += steps+1;
    129                 rank -= steps;
    130             } else if(dir == ChessDirection.KNIGHT_LEFT_BACK) {
    131                 file += steps+1;
    132                 rank += steps;
    133             } else if(dir == ChessDirection.KNIGHT_RIGHT_FORWARD) {
    134                 file -= steps+1;
    135                 rank -= steps;
    136             } else if(dir == ChessDirection.KNIGHT_RIGHT_BACK) {
    137                 file -= steps+1;
    138                 rank += steps;
    139             }
    140         }
    141 
    142         return new ChessCoordinate(file,rank);
    143     }
    144 
    145     public List<ChessCoordinate> surroundingCoordinates(int d) {
    146         List<ChessCoordinate> coors = new ArrayList<ChessCoordinate>();
    147 
    148         coors.add(new ChessCoordinate(file+d,rank+d));
    149         coors.add(new ChessCoordinate(file+d,rank));
    150         coors.add(new ChessCoordinate(file+d,rank-d));
    151         coors.add(new ChessCoordinate(file,rank-d));
    152         coors.add(new ChessCoordinate(file-d,rank-d));
    153         coors.add(new ChessCoordinate(file-d,rank));
    154         coors.add(new ChessCoordinate(file-d,rank+d));
    155         coors.add(new ChessCoordinate(file,rank+d));
    156 
    157         for(int i=0; i<coors.size(); i++) {
    158             if(coors.get(i).invalid())
    159                 coors.remove(i);
    160         }
    161         return coors;
    162     }
    163 
    164     public List<ChessCoordinate> surroundingCoordinates(int d, int d1) {
    165         List<ChessCoordinate> coors = new ArrayList<ChessCoordinate>();
    166 
    167         for(int i=d; i<=d1; i++) {
    168             coors.addAll(surroundingCoordinates(i));
    169         }
    170         return coors;
    171     }
    172 
    173     public boolean invalid() {
    174         return file > 'h' || file < 'a' ||
    175                 rank > ChessCoordinate.MAX || rank < 1;
    176     }
    177 
    178     public String toString() {
    179         return ""+file+rank;
    180     }
    181 
    182     public boolean equals(Object o) {
    183         ChessCoordinate coor = (ChessCoordinate) o;
    184         return coor.getRank() == this.getRank() &&
    185                 coor.getFile() == this.getFile();
    186     }
    187 }