Skip to content

Commit

Permalink
first working version (but with limited interactivity)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Sep 6, 2015
1 parent 7d7b363 commit 03c51f8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.bean.SessionScoped;

import de.beyondjava.bootsfaces.chess.Exceptions.EndOfGameException;
import de.beyondjava.bootsfaces.chess.common.ChessConstants;
import de.beyondjava.bootsfaces.chess.common.Move;
import de.beyondjava.bootsfaces.chess.objectOrientedEngine.Chessboard;

@ManagedBean
@ViewScoped
@SessionScoped
public class Board implements Serializable {
private static final long serialVersionUID = 1L;

private Chessboard chessboard = new Chessboard();
private boolean isPieceSelected=false;

private boolean isPieceSelected = false;

private int selectedPieceRow;

private int selectedPieceColumn;

private List<Row> rows;

public Board() {

redraw();
}

Expand All @@ -44,22 +47,49 @@ public List<Row> getRows() {
public void setRows(List<Row> rows) {
this.rows = rows;
}

public String getOpacity(int row, int column) {
if (!isPieceSelected) return "1.0";
if (selectedPieceColumn==column && selectedPieceRow==row) return "1.0";
if (!isPieceSelected)
return "1.0";
if (selectedPieceColumn == column && selectedPieceRow == row)
return "1.0";
if (chessboard.isMovePossible(selectedPieceRow, selectedPieceColumn, row, column))
return "1.0";
return "0.7";
}

public void onclick(int row, int column) {
if (isPieceSelected) {
isPieceSelected=false;
}
else
{
isPieceSelected=true;
selectedPieceRow=row;
selectedPieceColumn=column;
isPieceSelected = false;
if (chessboard.isMovePossible(selectedPieceRow, selectedPieceColumn, row, column)) {
// chessboard = chessboard.move(selectedPieceRow,
// selectedPieceColumn, row, column,
// fields, board, guiState, images, checkmate, whiteMoves,
// blackMoves);

chessboard = chessboard.moveChessPiece(selectedPieceRow, selectedPieceColumn, row, column,
ChessConstants.W_QUEEN);
selectedPieceRow=0;
selectedPieceColumn=0;
try {
Move move = chessboard.findBestMove();
chessboard = chessboard.moveChessPiece(move.fromRow, move.fromColumn, move.toRow, move.toColumn,
ChessConstants.W_QUEEN);
} catch (EndOfGameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

} else {
int piece = chessboard.getChessPiece(row, column);
if (piece != ChessConstants.B_EMPTY && piece != ChessConstants.W_EMPTY) {
if (chessboard.isActivePlayersPiece(piece)) {
isPieceSelected = true;
selectedPieceRow = row;
selectedPieceColumn = column;
}
}
}
redraw();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class Row implements Serializable {
public Row(int row, int[] pieces) {
int background = 1-row%2;
for (int col=0; col<pieces.length; col++) {
fileNames.add("wikimediaimages/" + ImageNames.chessPiecesFileName[pieces[col]+background]);
int piece = pieces[col];
if (piece<0) piece=0; // treat en-passant-fields as empty fields
fileNames.add("wikimediaimages/" + ImageNames.chessPiecesFileName[piece+background]);
background=1-background;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected boolean isBlackPiece(int piece) {
return (((piece / 2) % 2) == 1);
}

protected boolean isActivePlayersPiece(int piece) {
public boolean isActivePlayersPiece(int piece) {
return activePlayerIsWhite == isWhitePiece(piece);
}

Expand Down

0 comments on commit 03c51f8

Please sign in to comment.