1

Есть метод, который описывает движение фигуры Слон на шахматной доске. Последний Cell подсвечивается красным с ошибкой

Cell(int, int)' has private access in 'ru.chess.firuges.Cell. 

Я прекрасно понимаю, что конструктор в перечислении приватный. Вопрос: Как передать параметры правильно?

public Cell[] way(Cell source, Cell dest) {
    int distance = dest.x - source.x;
    Cell[] steps = new Cell[Math.abs(distance)];
    int deltaX = Integer.compare(source.x, dest.x);
    int deltaY = Integer.compare(source.y, dest.y);
    for (int i = 0; i < steps.length; i++) {
        steps[i] = new Cell(deltaX, deltaY);
    }
    return steps;
}

Вот само перечесление:

public enum Cell {
A1(0, 0), A2(0, 1), A3(0, 2), A4(0, 3), A5(0, 4), A6(0, 5), A7(0, 6), A8(0, 7),
B1(1, 0), B2(1, 1), B3(1, 2), B4(1, 3), B5(1, 4), B6(1, 5), B7(1, 6), B8(1, 7),
C1(2, 0), C2(2, 1), C3(2, 2), C4(2, 3), C5(2, 4), C6(2, 5), C7(2, 6), C8(2, 7),
D1(3, 0), D2(3, 1), D3(3, 2), D4(3, 3), D5(3, 4), D6(3, 5), D7(3, 6), D8(3, 7),
E1(4, 0), E2(4, 1), E3(4, 2), E4(4, 3), E5(4, 4), E6(4, 5), E7(4, 6), E8(4, 7),
F1(5, 0), F2(5, 1), F3(5, 2), F4(5, 3), F5(5, 4), F6(5, 5), F7(5, 6), F8(5, 7),
G1(6, 0), G2(6, 1), G3(6, 2), G4(6, 3), G5(6, 4), G6(6, 5), G7(6, 6), G8(6, 7),
H1(7, 0), H2(7, 1), H3(7, 2), H4(7, 3), H5(7, 4), H6(7, 5), H7(7, 6), H8(7, 7);

public final int x;
public final int y;

 Cell(int x, int y) {
    this.x = x;
    this.y = y;
}

Слон должен ходить по диагонали во все стороны, поэтому были определены дельты в методе

Например, логика пешки была написана таким образом:

public Cell[] way(Cell source, Cell dest) {
    Cell[] steps = new Cell[0];
    if (source.y == dest.y + 1 && source.x == dest.x) {
        steps = new Cell[] { dest };
    }
    return steps;
}

Она ходит только на один шаг в перед.

2 ответа 2

0

Довольно топорно, но как вариант:

public enum Cell {
   ...

    public static Cell getCell( int x, int y ) {
       switch ( x ) {
           case 0:
               switch ( y ) {
                   case 0: return A1;
                   case 1: return A2;
                   ...
               }
           ...
           case 7:
               ...
           default: throw new IllegalArgumentException();
       }
   }

    // или
    public static Cell getCell( int x, int y ) {
        return getColumn( x )[y];
    }

    public static Cell[] getColumn( int x ) {
        if ( x < 0 || x > 7 )
            throw new IllegalArgumentException();
        return Arrays.copyOfRange( values(), x*8, x*8 + 8 );
    //    Cell[] column = new Cell[8];
    //    Cell[] q = values();
    //    for ( int i = x*8, j = 0; i < x*8 + 8; i++, j++ ) {
    //        column[j] = q[i];
    //    }
    //    return column;
    }

}
public Cell[] way(Cell source, Cell dest) {
    ....
    for (int i = 0; i < steps.length; i++) {
        steps[i] = Cell.getCell(deltaX, deltaY);
    }
    return steps;
}
0

Вы не можете создать новое значение

new Cell(deltaX, deltaY);

если вам надо создать функцию, которая будет по двум значениям параметров возвращать имеющееся значение, то надо посмотреть Cell.values() и вы брать то что подходит.

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.