Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement a class called Square that creates objects that represent s

ID: 3683188 • Letter: D

Question

Design and implement a class called Square that creates objects that represent squares on a chess board. The Square constructor should take three parameters: the row and column numbers of a square on a chess board, and a third integer that gives the size of a board that contains the square being constructed. (The illustration shows a knight on an 8 X 8 board but your construction should work with any NxN board with N >= 6.) An object of the Square class should have a row number and a column number that represent the square’s position on the board. An object of the square class should also have a method, called legalMoves(), that returns a list of the row and column numbers of those squares that can be reached from the given square by legal knight move. Include whatever additional attributes and methods as appropriate. Illustrate your class by providing a program that accepts as input a row number, a column number, and a board size, and displays the list of legal knight moves corresponding data. (Assume that the rows and columns are labeled with (0,0) in the upper left corner.) ****INCLUDE JAVADOC COMMENTS*******

Explanation / Answer

import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * This program will create a chessboard on the screen. This program will have methods to add and remove pieces.First we will Let a user click on a piece and a square, and if the move islegal , it will move the piece */ public class SquareChessBoard extends JFrame implements ActionListener { private JButton[][] squareshere; // This is the squares of the board private ChessPiece[][] chesspieces; //This will store the pieces private int boardsize; // These are the dimensions of the board /* * The constructor will builds a chessboard of the desired size here. */ public SquareChessBoard(int boardsize) { this.boardsize = boardsize; pieces = new ChessPiece[boardsize][boardsize]; squares = new JButton[boardsize][boardsize]; // This will create the squares on the chessboard JPanel panel = new JPanel(new GridLayout(boardsize, boardsize)); for (int i = 0; i the column of the piece */ public void remove(int row, int col) { chesspieces[row][col] = null; setDefaultSquare(row, col); } /** * This function will return true if there is a piece at a specific location of the board. row ->the row to examine col ->the column to examine returns true if there is a piece a this row and column and false if the square is empty */ public boolean hasPiece(int row, int col) { return (chesspieces[row][col] != null); } /** * This function will returns the chess piece at a specific location on the board. row -> the row for the piece col ->the column for the piece this returns the piece at the row and column or null if there is no piece there. */ public ChessPiece getPiece(int row, int col) { return chesspieces[row][col]; } private boolean firstPick = true; // if true, we are selecting a piece private int pieceRow; // to remember row of selected piece private int pieceCol; //to remember column of selected piece private Color pieceColor; //to remember color of selected piece private String pieceText; // to remember text on selected piece /** * now we will handle a button click. The method alternates between selecting a piece * and selecting any square. After both are selected, the piece's * legalMove is called, and if the move is legal, the piece is moved. * and hence e ->the event that triggered the method */ public void actionPerformed(ActionEvent e) { JButton p = (JButton)e.getSource(); int col = -1; int row = -1; // This will find first find which button (board square) was clicked. for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote