Ejemplo n.º 1
0
 @Override
 public boolean makeValidMove(int a, int b) {
   if (_game.get(a, b) != null && _game.get(a, b).color() == _color) {
     return false;
   } else if (a + b == _x + _y) {
     int dir = (a - _x) / Math.abs(a - _x);
     for (int i = _x + dir, j = _y - dir; i != a; i += dir, j -= dir) {
       if (_game.get(i, j) != null) {
         return false;
       }
     }
     Move move = new SingleMove(this, _x, _y, _game.get(a, b), a, b);
     return makeMoveCareful(move);
   } else if (a - b == _x - _y) {
     int dir = (a - _x) / Math.abs(a - _x);
     for (int i = _x + dir, j = _y + dir; i != a; i += dir, j += dir) {
       if (_game.get(i, j) != null) {
         return false;
       }
     }
     Move move = new SingleMove(this, _x, _y, _game.get(a, b), a, b);
     return makeMoveCareful(move);
   } else if (a == _x) {
     int dir = (b - _y) / Math.abs(b - _y);
     for (int i = _y + dir; i != b; i += dir) {
       if (_game.get(_x, i) != null) {
         return false;
       }
     }
     Move move = new SingleMove(this, _x, _y, _game.get(a, b), a, b);
     return makeMoveCareful(move);
   } else if (b == _y) {
     int dir = (a - _x) / Math.abs(a - _x);
     for (int i = _x + dir; i != a; i += dir) {
       if (_game.get(i, _y) != null) {
         return false;
       }
     }
     Move move = new SingleMove(this, _x, _y, _game.get(a, b), a, b);
     return makeMoveCareful(move);
   } else {
     return false;
   }
 }
Ejemplo n.º 2
0
 @Override
 public boolean canCapture(int a, int b) {
   if (a + b == _x + _y) {
     int dir = (a - _x) / Math.abs(a - _x);
     for (int i = _x + dir, j = _y - dir; i != a; i += dir, j -= dir) {
       if (_game.get(i, j) != null) {
         return false;
       }
     }
     return true;
   } else if (a - b == _x - _y) {
     int dir = (a - _x) / Math.abs(a - _x);
     for (int i = _x + dir, j = _y + dir; i != a; i += dir, j += dir) {
       if (_game.get(i, j) != null) {
         return false;
       }
     }
     return true;
   } else if (a == _x) {
     int dir = (b - _y) / Math.abs(b - _y);
     for (int i = _y + dir; i != b; i += dir) {
       if (_game.get(_x, i) != null) {
         return false;
       }
     }
     return true;
   } else if (b == _y) {
     int dir = (a - _x) / Math.abs(a - _x);
     for (int i = _x + dir; i != a; i += dir) {
       if (_game.get(i, _y) != null) {
         return false;
       }
     }
     return true;
   } else {
     return false;
   }
 }