2014-04-13 1 views
0

게임을 올바르게 타일을 뒤집을 수 없습니다. 나는 이것에 무수한 시간 동안 붙어 있었고 이것을 알아낼 수 없었다. 나는 다른 Reversi 코드 예제를 확인했지만 불행히도 주사위는 아직 없습니다. 점령 된 BoardPiece를 gui에서 클릭 할 때마다 프로그램은 점유 된 것을 감지하지 못합니다. 비판/도움이 권장됩니다! 미리 감사드립니다.자바의 Reversi에서 게임 조각을 뒤집기

클래스 인덱스 {

int row; 
int column; 

public Index(int row, int column) { 
    this.row = row; 
    this.column = column; 
} 

public int getRow() { 
    return row; 
} 

public int getColumn() { 
    return column; 
} 
public boolean equals(Index other){ 
    if(this.row == other.row && this.column == other.column){ 
     return true; 
    } 
    return false; 
} 

}

수준의 게임 판은 JPanel의 {

static ArrayList<BoardSquare> possibleCaptures = new ArrayList<>(); 
public BoardSquare BoardSquares[][] = new BoardSquare[8][8]; 
BoardSquare bs; 
Index index; 

GameBoard() { 
    setLayout(new GridLayout(8, 8)); 
    for (int row = 0; row < 8; row++) { 
     for (int column = 0; column < 8; column++) { 
      index = new Index(row, column); 
      BoardSquares[row][column] = new BoardSquare(index, Color.white); 
      add(BoardSquares[row][column]); 

     } 
    } 
} 

public BoardSquare getBoardSquare(Index index) { 
    return BoardSquares[index.row][index.column]; 
} 

public void StartingPieces(HumanPlayer p1, aiPlayer p2) { 

    BoardSquares[3][3].setColor(p1); 

    BoardSquares[3][4].setColor(p2); 

    BoardSquares[4][4].setColor(p1); 

    BoardSquares[4][3].setColor(p2); 

} 

class BoardSquare extends JLabel { 

    private Index index; 
    private Color c = Color.white; 
    private Border b = BorderFactory.createLineBorder(Color.BLACK, 2); 

    BoardSquare(Index index, final Color c) { 
     addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent e) { 
       if (e.getButton() == 1) { 
        if (c == Color.white) { 
         checkCaptures(); 
         System.out.println("Checking"); 
        } else { 
         System.out.println("This square is already occupied."); 
        } 
       } 

      } 
     }); 
     this.index = index; 
     this.c = c; 
     setBorder(b); 

    } 
    public Index getIndex(){ 
     return index; 
    } 

    public void setColor(Player p) { 

     if (p instanceof HumanPlayer) { 
      c = Color.blue; 
     } 
     if (p instanceof aiPlayer) { 
      c = Color.red; 
     } 

    } 

    public Color getColor() { 
     return c; 
    } 

    private void checkCaptures() { 
     Direction[] directions = Direction.values(); 

     for (Direction direction : directions) { 
      // get next piece's index along the direction 
      Index nextIndex = direction.next(this.index); 

      if (isValid(nextIndex)) { // if the index is not valid (i.e. edge of the board) ignore it 

       // get next piece in the same direction 
       BoardSquare bs = getBoardSquare(nextIndex); 

       // find all pieces that should be captured in this direction 
       ArrayList<BoardSquare> squaresToCapture = new ArrayList<>(); 
       bs.findCaptures(squaresToCapture, this.c, direction); 

       for (BoardSquare candidate : squaresToCapture) { 
        // flip the color (WHITE to BLACK and vice-versa) 
        candidate.capture(); 

       } 
      } 
     } 
    } 

    public void findCaptures(ArrayList<BoardSquare> possibleCaptures, Color col, Direction d) { 
     Index next = d.next(this.index); 
     if (this.c == col) { 
      // This piece has the same color with the first one. 
      // No need to search further for this direction. All pieces collected in the list 
      // between the first one and this one, have opposite color and should be captured. 

     } else if (this.c == Color.white) { 
      // found a blank piece. Stop the search and clear any captured pieces found so far 
      possibleCaptures.clear(); 
     } else { 
      // this piece has the opposite color of the first 
      if (isValid(next)) { 
       // this is not the last piece in this direction. 
       // Since it has a flipped color it is a candidate for capturing 
       possibleCaptures.add(this); 

       // ask the next piece recursively to also check itself 
       BoardSquare bs = getBoardSquare(next); 
       bs.findCaptures(possibleCaptures, col, d); 
      } else { 
       // next index is not valid i.e. we have reached board edge. 
       // Stop the search and clear any captured pieces found so far 
       possibleCaptures.clear(); 
      } 
     } 

    } 

    public void capture() { 
     if (c == Color.red) { 
      c = Color.blue; 
     } 
     if (c == Color.blue) { 
      c = Color.red; 
     } 
    } 

    public String colorStatus() { 
     if (c == Color.white) { 
      return "white"; 
     } 
     if (c == Color.red) { 
      return "red"; 
     } else { 
      return "blue"; 
     } 

    } 

    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     setText(colorStatus()); 
    } 

    public boolean isValid(Index index) { 
     if (index.getRow() <= 7 && index.getRow() >= 0) { 
      if (index.getColumn() <= 7 && index.getColumn() >= 0) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 

}

답변

0

이름이 같은 다른 변수 하시다가 확장됩니다.

class BoardSquare extends JLabel { 

    private Index index; 
    private Color c = Color.white; 
    private Border b = BorderFactory.createLineBorder(Color.BLACK, 2); 

    BoardSquare(Index index, final Color c) { 
     addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent e) { 
       if (e.getButton() == 1) { 
        if (c == Color.white) { 

어느 쪽이 c 이니? final Color c 또는 private Color c? 제 생각에는 private Color c이지만, final Color c을 사용합니다.

내가 제안 :

class BoardSquare extends JLabel { 

    private Index index; 
    private Color myColor = Color.white; 
    private Border b = BorderFactory.createLineBorder(Color.BLACK, 2); 

    BoardSquare(Index index, final Color initialColor) { 
     addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent e) { 
       if (e.getButton() == 1) { 
        if (myColor.equals(Color.white)) { 

(테스트되지 않음)

더 나은 아직 MouseListener를 한 층 더 추가하는 것입니다. (생성자에 this이 누출되었습니다 (bad)

for (int column = 0; column < 8; column++) { 
      index = new Index(row, column); 
      BoardSquares[row][column] = new BoardSquare(index, Color.white); 
      BoardSquares[row][column].addMouseListener(..);