- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.ComponentOrientation;
- import java.awt.Cursor;
- import java.awt.GridLayout;
- import java.awt.event.InputEvent;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Map;
- import java.util.TreeMap;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JToolBar;
- import java.awt.Dimension;
- private static final long serialVersionUID = 1L;
- private int heldX, heldY, heldI = -1;
- // Map the full names of the pieces to their codenames (wRook, wQueen, etc.)
- // Stores the current player's move - we can easily match it against
- // the first character of the pieces array
- private char currPlayer = ' ';
- private int[][] moves = new int[10][6];
- private int currMove = 0;
- /**
- * This is the default constructor
- */
- public ChessGame() {
- super();
- initialize();
- buildBoard();
- }
- /**
- * This method initializes btnUndo
- *
- * @return javax.swing.JButton
- */
- if (btnUndo == null) {
- btnUndo.setText("Undo");
- btnUndo.setEnabled(false);
- undoMove();
- }
- });
- }
- return btnUndo;
- }
- new ChessGame().setVisible(true);
- }
- /**
- * This method initializes this
- *
- * @return void
- */
- private void initialize() {
- this.setSize(671, 555);
- this.setContentPane(getJContentPane());
- this.setTitle("Basic Chess");
- }
- /**
- * This method initializes jContentPane
- *
- * @return javax.swing.JPanel
- */
- if (jContentPane == null) {
- }
- return jContentPane;
- }
- /**
- * This method initializes jPanel
- *
- * @return javax.swing.JPanel
- */
- if (jPanel == null) {
- gridLayout.setRows(8);
- gridLayout.setHgap(5);
- gridLayout.setVgap(5);
- gridLayout.setColumns(8);
- jPanel.setLayout(gridLayout);
- //buildBoard();
- }
- return jPanel;
- }
- private void newGame()
- {
- resetBoard();
- resetPieces();
- }
- private void resetPieces()
- {
- jPieces[0][0] = "bRook";
- jPieces[0][1] = "bKnight";
- jPieces[0][2] = "bBishop";
- jPieces[0][3] = "bKing";
- jPieces[0][4] = "bQueen";
- jPieces[0][5] = "bBishop";
- jPieces[0][6] = "bKnight";
- jPieces[0][7] = "bRook";
- jPieces[1][0] = "bPawn";
- jPieces[1][1] = "bPawn";
- jPieces[1][2] = "bPawn";
- jPieces[1][3] = "bPawn";
- jPieces[1][4] = "bPawn";
- jPieces[1][5] = "bPawn";
- jPieces[1][6] = "bPawn";
- jPieces[1][7] = "bPawn";
- jPieces[6][0] = "wPawn";
- jPieces[6][1] = "wPawn";
- jPieces[6][2] = "wPawn";
- jPieces[6][3] = "wPawn";
- jPieces[6][4] = "wPawn";
- jPieces[6][5] = "wPawn";
- jPieces[6][6] = "wPawn";
- jPieces[6][7] = "wPawn";
- jPieces[7][0] = "wRook";
- jPieces[7][1] = "wKnight";
- jPieces[7][2] = "wBishop";
- jPieces[7][3] = "wKing";
- jPieces[7][4] = "wQueen";
- jPieces[7][5] = "wBishop";
- jPieces[7][6] = "wKnight";
- jPieces[7][7] = "wRook";
- RepaintPieces();
- }
- {
- try
- {
- if(pieceName != null && pieceName != "")
- {
- InputStream inIcon = ClassLoader.getSystemResourceAsStream("pociu/games/chess/" + pieceName + ".png");
- //System.out.println("Painted " + pieceName + " at " + i);
- }
- else
- {
- lblCells[i].setIcon(null);
- //System.out.println("Cleared cell at " + i);
- }
- }
- {
- e.printStackTrace();
- }
- }
- private void RepaintPieces()
- {
- int i = 0;
- for(int x = 0; x < 8; x++)
- {
- for(int y = 0; y < 8; y++)
- {
- if(jPieces[x][y] != null && !jPieces[x][y].equals(""))
- {
- PaintPiece(jPieces[x][y], i);
- }
- else
- {
- PaintPiece("", i);
- }
- i++;
- }
- }
- }
- private void ClearHlight(int i, int rowNum)
- {
- if((i + rowNum) % 2 == 0)
- {
- }
- else
- {
- }
- }
- private void undoMove()
- {
- if(btnUndo.isEnabled() && currMove > 0)
- {
- currMove--;
- movePiece(moves[currMove][3], moves[currMove][4], moves[currMove][5], moves[currMove][0], moves[currMove][1], moves[currMove][2], true);
- }
- }
- private void resetBoard()
- {
- currMove = 0;
- pieceName.clear();
- pieceName.put("bRook", "Black Rook");
- pieceName.put("bQueen", "Black Queen");
- pieceName.put("bPawn", "Black Pawn");
- pieceName.put("bKnight", "Black Knight");
- pieceName.put("bBishop", "Black Bishop");
- pieceName.put("bKing", "Black King");
- pieceName.put("wRook", "White Rook");
- pieceName.put("wQueen", "White Queen");
- pieceName.put("wPawn", "White Pawn");
- pieceName.put("wKnight", "White Knight");
- pieceName.put("wBishop", "White Bishop");
- pieceName.put("wKing", "White King");
- pieceName.put("wRook", "White Rook");
- // If we're holding a piece, clear the hover of the cell
- if(heldI >= 0 && heldX >= 0)
- {
- ClearHlight(heldI, heldX);
- }
- switchPlayer();
- heldX = heldY = heldI = -1;
- }
- private void buildBoard()
- {
- // First reset the variables, maps, etc.
- resetBoard();
- int rowColor = 0;
- int i = 0;
- for(int x = 0; x <= 7; x++)
- {
- rowColor++;
- for(int y = 0; y <= 7; y++)
- {
- lblCells[i].setOpaque(true);
- if(rowColor % 2 == 0)
- {
- }
- else
- {
- }
- final int passX = x;
- final int passY = y;
- final int passI = i;
- // If we're holding a piece show that along with the cell we're hovering
- if(heldI > 0)
- {
- lblStatus.setText("Picked up " + pieceName.get(jPieces[heldX][heldY]) + " at " + showBoardRelative(heldX, heldY) + " | Hovering: " + showBoardRelative(passX, passY));
- }
- else // Just show what we're hovering
- {
- lblStatus.setText("Hovering: " + showBoardRelative(passX, passY));
- }
- // Unless we hover the one we're holding...
- if(passI != heldI)
- {
- }
- }
- });
- lblStatus.setText("");
- // Unless we hover the one we're holding...
- if(passI != heldI)
- {
- // Clear the hover effect
- ClearHlight(passI, passX);
- }
- }
- });
- {
- showCellInfo(passX, passY, passI);
- }
- {
- clickCell(e, passX, passY, passI);
- }
- }
- });
- jPanel.add(lblCells[i]);
- rowColor++;
- i++;
- }
- }
- resetPieces();
- }
- // Translates grid relative coordinates to chess board relative
- // For ex.: 0x0 to 8A
- {
- String chessCoord = "";
- chessCoord = (x - 8) * -1 + "" + (char)(y + 65);
- return chessCoord;
- }
- private void showCellInfo(int x, int y, int i)
- {
- if(jPieces[x][y] != null && !jPieces[x][y].equals(""))
- {
- JOptionPane.showMessageDialog( null, pieceName.get(jPieces[x][y]) + " located at " + showBoardRelative(x, y), "Cell Information", JOptionPane.INFORMATION_MESSAGE );
- }
- else
- {
- JOptionPane.showMessageDialog( null, "No piece located at " + showBoardRelative(x, y), "Cell Information", JOptionPane.INFORMATION_MESSAGE );
- }
- }
- private boolean isValidMove(int fromX, int fromY)
- {
- if(jPieces[fromX][fromY].length() > 0 && jPieces[fromX][fromY].charAt(0) == currPlayer)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private void movePiece(int fromX, int fromY, int fromI, int toX, int toY, int toI, boolean isUndo)
- {
- if(fromX == toX && fromY == toY)
- {
- ClearHlight(fromI, fromX);
- lblStatus.setText("Move canceled.");
- }
- else if(isValidMove(fromX, fromY) || isUndo == true)
- {
- PaintPiece(jPieces[fromX][fromY], toI);
- PaintPiece("", fromI);
- ClearHlight(fromI, fromX);
- lblStatus.setText("Moved " + pieceName.get(jPieces[fromX][fromY]) + " from " + showBoardRelative(fromX, fromY) + " to " + showBoardRelative(toX, toY));
- jPieces[toX][toY] = jPieces[fromX][fromY];
- jPieces[fromX][fromY] = "";
- if(currMove > 9)
- {
- pushbackUndos();
- }
- moves[currMove] = new int[6];
- moves[currMove][0] = fromX;
- moves[currMove][1] = fromY;
- moves[currMove][2] = fromI;
- moves[currMove][3] = toX;
- moves[currMove][4] = toY;
- moves[currMove][5] = toI;
- movedPieces[currMove] = jPieces[fromX][fromY];
- btnUndo.setEnabled(true);
- if(isUndo == false)
- {
- currMove++;
- }
- switchPlayer();
- }
- else
- {
- ClearHlight(fromI, fromX);
- JOptionPane.showMessageDialog( null, "It's the " + lblCurrPlayer.getText(), "Illegal Move", JOptionPane.INFORMATION_MESSAGE );
- }
- }
- private void pushbackUndos()
- {
- for(int i = 0; i < 9; i++)
- {
- moves[i] = moves[i + 1];
- movedPieces[i] = movedPieces[i + 1];
- }
- currMove--;
- }
- private void switchPlayer()
- {
- //System.out.write(currPlayer);
- if(currPlayer == 'w')
- {
- currPlayer = 'b';
- lblCurrPlayer.setText("Black Player's Turn.");
- }
- else if(currPlayer == 'b' || currPlayer == ' ')
- {
- currPlayer = 'w';
- lblCurrPlayer.setText("White Player's Turn.");
- }
- }
- {
- if(heldI != -1) // We're dropping a piece
- {
- movePiece(heldX, heldY, heldI, x, y, i, false);
- heldX = heldY = heldI = -1;
- }
- else // We're picking up a piece
- {
- if(jPieces[x][y] == null || jPieces[x][y].equals(""))
- {
- lblStatus.setText("No piece to pick up.");
- }
- else
- {
- lblStatus.setText("Picked up " + pieceName.get(jPieces[x][y]) + " from " + showBoardRelative(x, y));
- heldX = x;
- heldY = y;
- heldI = i;
- }
- }
- }
- /**
- * This method initializes tlbMain
- *
- * @return javax.swing.JToolBar
- */
- if (tlbMain == null) {
- lblCurrPlayer.setText("");
- lblCurrPlayer.setOpaque(true);
- lblStatus.setText("");
- tlbMain.setFloatable(false);
- tlbMain.add(getBtnNewGame());
- tlbMain.add(getBtnUndo());
- tlbMain.add(lblCurrPlayer);
- tlbMain.add(lblStatus);
- }
- return tlbMain;
- }
- /**
- * This method initializes btnNewGame
- *
- * @return javax.swing.JButton
- */
- if (btnNewGame == null) {
- btnNewGame.setText("New Game");
- if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "Are you sure you wish to end this game?"))
- {
- newGame();
- }
- }
- });
- }
- return btnNewGame;
- }
- } // @jve:decl-index=0:visual-constraint="10,10"
Next
« Prev Post
« Prev Post
Previous
Next Post »
Next Post »
Post a Comment
Thanks for your comment
Subscribe to:
Post Comments (Atom)