Main objective: Store the chess pieces on an 8 x 8 chessboard as a linked list.
ID: 3598629 • Letter: M
Question
Main objective: Store the chess pieces on an 8 x 8 chessboard as a linked list.
Implement the following procedures for a given chessboard:
• Determine Validity: Verify that two pieces do not occupy the same
square, and there is exactly one king of each color (white and black).
• Find Piece on square: Given a square, determine the chess piece at that
square, if any. As in HW1, squares are specified as (column, row).
• Discover an Attack (by a particular piece): Find out if the piece (found by
Fine piece) attacks another piece. Note that the pieces must be of different
colors. Do not worry about blocking--assume that pieces can “pass through”
other pieces on the board. This is not true for proper chess, but it makes coding
much easier for this assignment. Thus a piece x on a square can attack another
piece y on a different square if x would be able travel to y’s square (according to
the rules of chess) if there were no other pieces on the board. (You’ll just have to
find one attack, not all of them.)
• Do not work about non-standard moves, such as castling.
You will not get any credit if you do not implement using a linked list for the
set of pieces on the board. Furthermore, you have to write your own linked list
from scratch. You cannot use any built-in libraries for linked lists.
Suggestions for coding: You do not have to follow the following
instructions, but they might make life easier for you (and your code nicer). Create
a (super)class Chess Piece with child subclasses for each different type of piece.
Each of these subclasses can implement its own attacking function. Think about
how you want to represent the position and color of each piece.
Create a class ChessBoard that has a Linked List of ChessPiece objects. (You
must do this via a Linked List.) This class can have methods for determining
validity, finding, etc.
2
Format: You should provide a Makefile. Running make should create
“Chessboard.jar”. Your program should take two command line arguments, an
input file and an output file.
The format of the input file is as follows. Each line starts with two integers,
referring to a specific chessboard position; the integers specify a column and a
row. This is followed by a colon. Then the line contains a list of pieces
consisting of “piece column row”, where piece is a character that is one of k
(king), q (queen), r (rook), b (bishop). (There are no pawns or knights in HW2.)
If the character is capitalized, then the piece is black; if it is not capitalized, then
the piece is white. For example, a line could be:
8 2: q 4 3 k 4 4 r 8 2 R 8 8 b 1 1 K 4 8 B 7 7
The portion after the colon describes the pieces on an 8 x 8 chessboard, and
the portion before the colon is a query asking “what is at position (8,2)”. The
answer here is a white rook, denoted r. This pattern of pairs of lines continues
throughout the input file.
Do not worry about error handling on the input; you can assume that inputs
will always have this format. No piece will be placed outside the chessboard. If
you want to, you can use the Java “split” method to split strings; there’s a good
example of its use with white space (such as blanks) here.
Output: On running (say) :
java -jar Chessboard.jar in.txt out.txt
the file in.txt is read in and the output file out.txt should be produced.
The i’th line of the output file out.txt corresponds to the i’th chessboard in
in.txt. The i’th line of the output file should contain:
• If the i’th chessboard is not valid, then the i’th line should be “Invalid” (without the
quotes).
• If the i’th chessboard is valid, then first output the chess piece (if any)
that’s at the query square. If there is none, then just print “-” (without the
quotes). If there is a piece at the query square, print a space, and then print
“y” (the piece attacks some other piece) or “n” (it does not attack another
piece). For the example given above, the output would be:
r y
because the white rook at (8.2) attacks a black rook at (8,8)
input.txt: 4 4: q 4 1 Q 4 5 k 3 1 q 8 6 Q 4 4 K 7 3
1 1: K 6 8 q 1 1 Q 1 2 q 4 5 b 1 2 k 3 7
2 3: r 2 4 q 2 3 Q 3 7 Q 8 8 Q 8 1 K 5 7 k 1 2
3 5: q 5 3 K 8 7 Q 4 3 q 1 3 b 5 5 k 8 1
2 2: k 7 5 r 2 2 Q 4 3 K 7 5 R 7 8 k 8 1
corresponding output: Q y
Invalid
q n
-
Invalid
Explanation / Answer
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class ListExample extends JFrame
{
BufferedReader br = null;
FileReader fr = null;
List<String> ListA=new ArrayList<String>();
List<String> ListE=new ArrayList<String>();
List<String> ListI=new ArrayList<String>();
List<String> ListO=new ArrayList<String>();
List<String> ListU=new ArrayList<String>();
DefaultListModel<String> l1 = new DefaultListModel<>();
DefaultListModel<String> l2 = new DefaultListModel<>();
DefaultListModel<String> l3 = new DefaultListModel<>();
DefaultListModel<String> l4 = new DefaultListModel<>();
DefaultListModel<String> l5 = new DefaultListModel<>();
JFrame f= new JFrame();
private JButton jButton1 = new JButton("Add");
ListExample(){
try{
fr = new FileReader("D:\test.txt.txt");
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.charAt(0)=='A' || sCurrentLine.charAt(0)=='a'){
System.out.println("A : " + sCurrentLine);
ListA.add(sCurrentLine);
}
else if(sCurrentLine.charAt(0)=='E' || sCurrentLine.charAt(0)=='e'){
System.out.println("E : " + sCurrentLine);
ListE.add(sCurrentLine);
}
else if(sCurrentLine.charAt(0)=='I' || sCurrentLine.charAt(0)=='i'){
System.out.println("I : " + sCurrentLine.charAt(0));
ListI.add(sCurrentLine);
}
else if(sCurrentLine.charAt(0)=='O' || sCurrentLine.charAt(0)=='o'){
System.out.println("O : " + sCurrentLine.charAt(0));
ListO.add(sCurrentLine);
}
else if(sCurrentLine.charAt(0)=='U' || sCurrentLine.charAt(0)=='u'){
System.out.println("U : " + sCurrentLine.charAt(0));
ListU.add(sCurrentLine);
}
}
JList<String> list1 = new JList<>(l1);
JList<String> list2 = new JList<>(l2);
JList<String> list3 = new JList<>(l3);
JList<String> list4 = new JList<>(l4);
JList<String> list5 = new JList<>(l5);
list1.setBounds(30, 35, 115, 200);
f.add(list1);
list2.setBounds(180, 35, 110, 195);
f.add(list2);
list3.setBounds(330, 35, 100, 195);
f.add(list3);
list4.setBounds(460, 30, 95, 200);
f.add(list4);
list5.setBounds(590, 35, 110, 190);
f.add(list5);
jButton1.setSize(70,20);
jButton1.setVisible(true);
jButton1.setText("Add");
f.add(jButton1);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
f.setSize(816, 537);
f.setLayout(null);
f.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void jButton1_actionPerformed(ActionEvent e) {
System.out.println("Click Me");
for(String str:ListA){
l1.addElement(str);
}
for(String str:ListE){
l2.addElement(str);
}
for(String str:ListI){
l3.addElement(str);
}
for(String str:ListO){
l4.addElement(str);
}
for(String str:ListU){
l5.addElement(str);
}
JList<String> list1 = new JList<>(l1);
JList<String> list2 = new JList<>(l2);
JList<String> list3 = new JList<>(l3);
JList<String> list4 = new JList<>(l4);
JList<String> list5 = new JList<>(l5);
list1.setBounds(30, 35, 115, 200);
f.add(list1);
list2.setBounds(180, 35, 110, 195);
f.add(list2);
list3.setBounds(330, 35, 100, 195);
f.add(list3);
list4.setBounds(460, 30, 95, 200);
f.add(list4);
list5.setBounds(590, 35, 110, 190);
f.add(list5);
}
public static void main(String args[])
{
new ListExample();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.