2014-04-13 81 views
0

我無法讓我的遊戲正確翻轉棋子。我一直堅持這無數小時,似乎無法弄清楚這一點。我已經檢查過其他Reversi代碼示例,但很遺憾,仍然沒有骰子。我注意到,當我點擊gui中的一個已經佔用的BoardPiece時,程序並不感覺它被佔用。任何批評/幫助都會受到鼓勵!提前致謝。在Java的黑白棋翻轉棋子

類指數{

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

你有WO具有相同名稱不同的變量。

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 cprivate 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一層高。

for (int column = 0; column < 8; column++) { 
      index = new Index(row, column); 
      BoardSquares[row][column] = new BoardSquare(index, Color.white); 
      BoardSquares[row][column].addMouseListener(..); 
(你是在構造函數中,這是 bad滲出 this