Quick JAVA program revision
HW2/out.txt
Q y Invalid q y -
__MACOSX/HW2/._out.txt
HW2/.DS_Store
__MACOSX/HW2/._.DS_Store
HW2/test-input.txt
4 4: q 4 1 Q 4 5 q 8 6 Q 4 4 1 1: q 1 1 Q 1 2 q 4 5 q 1 2 2 3: q 2 4 q 2 3 Q 3 7 Q 8 8 Q 8 1 3 5: q 5 3 Q 4 3 q 1 3 q 5 5
__MACOSX/HW2/._test-input.txt
HW2/bin/Pawn.class
public synchronized class Pawn extends ChessPiece { public void Pawn(int, int, String); public boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._Pawn.class
HW2/bin/Knight.class
public synchronized class Knight extends ChessPiece { public void Knight(int, int, String); public boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._Knight.class
HW2/bin/ChessBoard.class
public synchronized class ChessBoard { public static int size; public static LinkedList chessPieces; public static ChessPiece one; public static ChessPiece two; public void ChessBoard(); public static void main(String[]) throws java.io.IOException; public static boolean checkDoubles(); public static void createList(String); }
__MACOSX/HW2/bin/._ChessBoard.class
HW2/bin/Rook.class
public synchronized class Rook extends ChessPiece { public void Rook(int, int, String); public boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._Rook.class
HW2/bin/Queen.class
public synchronized class Queen extends ChessPiece { public void Queen(int, int, String); public boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._Queen.class
HW2/bin/ChessPiece.class
public abstract synchronized class ChessPiece { public String cType; public boolean color; public int column; public int row; public void ChessPiece(int, int, String); public abstract boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._ChessPiece.class
HW2/bin/Bishop.class
public synchronized class Bishop extends ChessPiece { public void Bishop(int, int, String); public boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._Bishop.class
HW2/bin/King.class
public synchronized class King extends ChessPiece { public void King(int, int, String); public boolean isAttacking(ChessPiece, int); }
__MACOSX/HW2/bin/._King.class
HW2/bin/LinkedList.class
synchronized class LinkedList { public static int counter; public Node head; public void LinkedList(); public void add(ChessPiece); public ChessPiece get(int); public int size(); public void remove(); }
__MACOSX/HW2/bin/._LinkedList.class
HW2/bin/Node.class
synchronized class Node { public ChessPiece data; public Node next; public void Node(ChessPiece); public void setData(ChessPiece); public ChessPiece getData(); public void setNext(Node); public Node getNext(); }
__MACOSX/HW2/bin/._Node.class
__MACOSX/HW2/._bin
HW2/Makefile
JAVASRC = ChessBoard.java Node.java LinkedList.java ChessPiece.java King.java Queen.java Rook.java Bishop.java Knight.java Pawn.java SOURCES = README Makefile $(JAVASRC) MAINCLASS = ChessBoard CLASSES = ChessBoard.class Node.class LinkedList.class ChessPiece.class King.class Queen.class Rook.class Bishop.class Knight.class Pawn.class JARFILE = ChessBoard.jar all: $(JARFILE) $(JARFILE): $(CLASSES) echo Main-class: $(MAINCLASS) > Manifest jar cvfm $(JARFILE) Manifest $(CLASSES) rm Manifest $(CLASSES): $(JAVASRC) javac -Xlint $(JAVASRC) clean: rm $(CLASSES) $(JARFILE)
__MACOSX/HW2/._Makefile
HW2/ChessBoard.jar
META-INF/MANIFEST.MF
Manifest-Version: 1.0 Class-Path: . Main-Class: ChessBoard
Queen.class
public synchronized class Queen extends ChessPiece { public void Queen(int, int, String); public boolean isAttacking(ChessPiece, int); }
King.class
public synchronized class King extends ChessPiece { public void King(int, int, String); public boolean isAttacking(ChessPiece, int); }
Pawn.class
public synchronized class Pawn extends ChessPiece { public void Pawn(int, int, String); public boolean isAttacking(ChessPiece, int); }
Node.class
synchronized class Node { public ChessPiece data; public Node next; public void Node(ChessPiece); public void setData(ChessPiece); public ChessPiece getData(); public void setNext(Node); public Node getNext(); }
ChessPiece.class
public abstract synchronized class ChessPiece { public String cType; public boolean color; public int column; public int row; public void ChessPiece(int, int, String); public abstract boolean isAttacking(ChessPiece, int); }
Rook.class
public synchronized class Rook extends ChessPiece { public void Rook(int, int, String); public boolean isAttacking(ChessPiece, int); }
Bishop.class
public synchronized class Bishop extends ChessPiece { public void Bishop(int, int, String); public boolean isAttacking(ChessPiece, int); }
Knight.class
public synchronized class Knight extends ChessPiece { public void Knight(int, int, String); public boolean isAttacking(ChessPiece, int); }
ChessBoard.class
public synchronized class ChessBoard { public static int size; public static LinkedList chessPieces; public static ChessPiece one; public static ChessPiece two; public void ChessBoard(); public static void main(String[]) throws java.io.IOException; public static boolean checkDoubles(); public static void createList(String); }
LinkedList.class
synchronized class LinkedList { public static int counter; public Node head; public void LinkedList(); public void add(ChessPiece); public ChessPiece get(int); public int size(); public void remove(); }
__MACOSX/HW2/._ChessBoard.jar
HW2/test-output.txt
Q y Invalid q n -
__MACOSX/HW2/._test-output.txt
HW2/out(more).txt
r y - K n Invalid B n - K n q y
__MACOSX/HW2/._out(more).txt
HW2/src/LinkedList.java
HW2/src/LinkedList.java
class
LinkedList
{
public
static
int
counter
;
//count of list
public
Node
head
;
//head of LinkedList
//Default Constructor
public
LinkedList
()
{}
//Add chess piece in link list
public
void
add
(
ChessPiece
data
)
{
//if list is empty
if
(
head
==
null
)
{
head
=
new
Node
(
data
);
head
.
next
=
null
;
return
;
}
//if list is not empty
else
{
Node
pieceTemp
=
new
Node
(
data
);
Node
currentPiece
=
head
;
//traverse list untill last chess piece
while
(
currentPiece
.
getNext
()
!=
null
)
{
currentPiece
=
currentPiece
.
getNext
();
}
currentPiece
.
setNext
(
pieceTemp
);
}
counter
++
;
}
//get the position of chess piece
public
ChessPiece
get
(
int
index
)
{
Node
currentPiece
=
head
;
//Assign current pointer to head node
int
num
=
0
;
//Traverse list to find the chess piece
while
(
currentPiece
.
next
!=
null
&&
num
<
index
)
{
currentPiece
=
currentPiece
.
getNext
();
num
++
;
}
return
currentPiece
.
getData
();
}
//return size of linked list
public
int
size
()
{
return
counter
;
}
//remove linked list
public
void
remove
()
{
head
=
null
;
counter
=
0
;
}
}
__MACOSX/HW2/src/._LinkedList.java
HW2/src/Node.java
HW2/src/Node.java
class
Node
{
public
ChessPiece
data
;
//value of chess piece
public
Node
next
;
//next pointer
//Parameterized Constructor
public
Node
(
ChessPiece
dataValue
)
{
data
=
dataValue
;
next
=
null
;
}
//set data of node/ChessPiece
public
void
setData
(
ChessPiece
dataValue
)
{
data
=
dataValue
;
}
//get data of node/ChessPiece
public
ChessPiece
getData
()
{
return
data
;
}
//set pointer of node/ChessPiece to next
public
void
setNext
(
Node
nextValue
)
{
next
=
nextValue
;
}
//get pointer of node
public
Node
getNext
()
{
return
next
;
}
}
__MACOSX/HW2/src/._Node.java
HW2/src/.DS_Store
__MACOSX/HW2/src/._.DS_Store
HW2/src/Knight.java
HW2/src/Knight.java
public
class
Knight
extends
ChessPiece
{
//Parameterized Constructor
public
Knight
(
int
col
,
int
row
,
String
typ
)
{
super
(
col
,
row
,
typ
);
}
//Check if Knight is attacking or not
@
Override
public
boolean
isAttacking
(
ChessPiece
piece
,
int
board_size
)
{
int
c
=
piece
.
column
;
//chess pieces in column
int
r
=
piece
.
row
;
//chess pieces in row
//possible moves by Knight
int
[][]
moves
=
{{
this
.
row
+
2
,
this
.
column
+
1
},{
this
.
row
+
2
,
this
.
column
-
1
},
{
this
.
row
+
1
,
this
.
column
-
2
},{
this
.
row
+
1
,
this
.
column
+
2
},
{
this
.
row
-
1
,
this
.
column
-
2
},{
this
.
row
-
1
,
this
.
column
+
2
},
{
this
.
row
-
2
,
this
.
column
-
1
},{
this
.
row
-
2
,
this
.
column
+
1
}};
//check moves of knight and see knight is attacking or not
for
(
int
i
=
0
;
i
<
moves
.
length
;
i
++
)
{
if
(
moves
[
i
][
0
]
<=
ChessBoard
.
size
&&
moves
[
i
][
0
]
>
0
&&
moves
[
i
][
1
]
<=
ChessBoard
.
size
&&
moves
[
i
][
1
]
>
0
)
{
if
(
moves
[
i
][
0
]
==
r
&&
moves
[
i
][
1
]
==
c
)
return
true
;
}
}
return
false
;
}
}
__MACOSX/HW2/src/._Knight.java
HW2/src/Rook.java
HW2/src/Rook.java
public
class
Rook
extends
ChessPiece
{
//Parameterized Constructor
public
Rook
(
int
col
,
int
row
,
String
typ
)
{
super
(
col
,
row
,
typ
);
}
//Check if Rook is attacking or not
@
Override
public
boolean
isAttacking
(
ChessPiece
piece
,
int
board_size
)
{
int
c
=
piece
.
column
;
//number of pieces in column
int
r
=
piece
.
row
;
//number of pieces in row
if
(
this
.
row
==
r
||
this
.
column
==
c
)
return
true
;
return
false
;
}
}
__MACOSX/HW2/src/._Rook.java
HW2/src/Bishop.java
HW2/src/Bishop.java
public
class
Bishop
extends
ChessPiece
{
//Parameterized Constructor
public
Bishop
(
int
clm
,
int
row
,
String
type
)
{
super
(
clm
,
row
,
type
);
}
//Check if Bishop is attacking or not
@
Override
public
boolean
isAttacking
(
ChessPiece
piece
,
int
boardSize
)
{
int
r
=
piece
.
row
;
//chess pieces in row
int
c
=
piece
.
column
;
//chess pieces in column
for
(
int
i
=
this
.
row
+
1
,
j
=
this
.
column
+
1
;
i
<=
boardSize
&&
j
<=
boardSize
;
i
++
,
j
++
)
{
if
(
r
==
i
&&
c
==
j
)
{
return
true
;
}
}
for
(
int
i
=
this
.
row
+
1
,
j
=
this
.
column
-
1
;
i
<=
boardSize
&&
j
>
0
;
i
++
,
j
--
)
{
if
(
r
==
i
&&
c
==
j
)
{
return
true
;
}
}
for
(
int
i
=
this
.
row
-
1
,
j
=
this
.
column
-
1
;
i
>
0
&&
j
>
0
;
i
--
,
j
--
)
{
if
(
r
==
i
&&
c
==
j
)
{
return
true
;
}
}
for
(
int
i
=
this
.
row
-
1
,
j
=
this
.
column
+
1
;
i
>
0
&&
j
<=
boardSize
;
i
--
,
j
++
)
{
if
(
r
==
i
&&
c
==
j
)
{
return
true
;
}
}
return
false
;
}
}
__MACOSX/HW2/src/._Bishop.java
HW2/src/Queen.java
HW2/src/Queen.java
public
class
Queen
extends
ChessPiece
{
//Parameterized Constructor
public
Queen
(
int
col
,
int
row
,
String
typ
)
{
super
(
col
,
row
,
typ
);
}
//Check if Queen is attacking or not
@
Override
public
boolean
isAttacking
(
ChessPiece
piece
,
int
board_size
)
{
int
c
=
piece
.
column
;
//number of pieces in column
int
r
=
piece
.
row
;
//number of pieces in row
if
(
this
.
row
==
r
||
this
.
column
==
c
)
return
true
;
for
(
int
i
=
this
.
row
-
1
,
j
=
this
.
column
+
1
;
i
>
0
&&
j
<=
board_size
;
i
--
,
j
++
)
{
if
(
r
==
i
&&
c
==
j
)
return
true
;
}
for
(
int
i
=
this
.
row
+
1
,
j
=
this
.
column
-
1
;
i
<=
board_size
&&
j
>
0
;
i
++
,
j
--
)
{
if
(
r
==
i
&&
c
==
j
)
return
true
;
}
for
(
int
i
=
this
.
row
-
1
,
j
=
this
.
column
-
1
;
i
>
0
&&
j
>
0
;
i
--
,
j
--
)
{
if
(
r
==
i
&&
c
==
j
)
return
true
;
}
for
(
int
i
=
this
.
row
+
1
,
j
=
this
.
column
+
1
;
i
<=
board_size
&&
j
<=
board_size
;
i
++
,
j
++
)
{
if
(
r
==
i
&&
c
==
j
)
return
true
;
}
return
false
;
}
}
__MACOSX/HW2/src/._Queen.java
HW2/src/King.java
HW2/src/King.java
public
class
King
extends
ChessPiece
{
//Parameterized Constructor
public
King
(
int
col
,
int
row
,
String
typ
)
{
super
(
col
,
row
,
typ
);
}
//Check if King is attacking or not
@
Override
public
boolean
isAttacking
(
ChessPiece
piece
,
int
board_size
)
{
int
c
=
piece
.
column
;
//chess pieces in column
int
r
=
piece
.
row
;
//chess pieces in row
//possible moves by King
int
[][]
moves
=
{{
this
.
row
+
1
,
this
.
column
},{
this
.
row
-
1
,
this
.
column
},
{
this
.
row
,
this
.
column
-
1
},{
this
.
row
,
this
.
column
+
1
},{
this
.
row
+
1
,
this
.
column
+
1
},
{
this
.
row
+
1
,
this
.
column
-
1
},{
this
.
row
-
1
,
this
.
column
+
1
},{
this
.
row
-
1
,
this
.
column
-
1
}};
//check moves of king and see King is attacking or not
for
(
int
i
=
0
;
i
<
moves
.
length
;
i
++
)
{
if
(
moves
[
i
][
0
]
<=
board_size
&&
moves
[
i
][
0
]
>
0
&&
moves
[
i
][
1
]
<=
board_size
&&
moves
[
i
][
1
]
>
0
)
{
if
(
moves
[
i
][
0
]
==
r
&&
moves
[
i
][
1
]
==
c
)
return
true
;
}
}
return
false
;
}
}
__MACOSX/HW2/src/._King.java
HW2/src/ChessPiece.java
HW2/src/ChessPiece.java
public
abstract
class
ChessPiece
{
public
String
cType
;
//type of piece
public
boolean
color
;
//color of piece
public
int
column
,
row
;
//position of piece on board
//Parameterized Constructor
public
ChessPiece
(
int
col
,
int
row
,
String
typ
)
{
cType
=
typ
;
this
.
column
=
col
;
this
.
row
=
row
;
if
(
cType
.
equals
(
cType
.
toUpperCase
()))
this
.
color
=
true
;
else
this
.
color
=
false
;
}
//to check, if chess piece is attacking or not.
abstract
public
boolean
isAttacking
(
ChessPiece
piece
,
int
boardSize
);
}
__MACOSX/HW2/src/._ChessPiece.java
HW2/src/ChessBoard.java
HW2/src/ChessBoard.java
import
java
.
io
.
File
;
import
java
.
io
.
FileInputStream
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
io
.
PrintWriter
;
import
java
.
util
.
Scanner
;
public
class
ChessBoard
{
public
static
int
size
;
//size of board
public
static
LinkedList
chessPieces
;
//chess pieces
public
static
ChessPiece
one
;
//chess pieces of player 1
public
static
ChessPiece
two
;
//chess pieces of player 2
public
static
void
main
(
String
[]
args
)
throws
IOException
{
//Reading data from file
File
file
=
new
File
(
args
[
1
]);
Scanner
read
=
new
Scanner
(
new
FileInputStream
(
args
[
0
]));
PrintWriter
s
=
new
PrintWriter
(
new
FileWriter
(
file
));
while
(
read
.
hasNextLine
())
{
String
board
[]
=
read
.
nextLine
().
split
(
":"
);
//reding board line
chessPieces
=
new
LinkedList
();
//creating linkeList of chess Pieces
createList
(
board
[
1
].
trim
());
//create list of board
int
row
=
Integer
.
parseInt
(
board
[
0
].
split
(
" "
)[
0
]);
int
col
=
Integer
.
parseInt
(
board
[
0
].
split
(
" "
)[
1
]);
if
(
!
checkDoubles
())
{
boolean
flag
=
false
;
//check if a chess piece is attacking or not
for
(
int
i
=
0
;
i
<=
chessPieces
.
size
();
i
++
)
{
if
(
chessPieces
.
get
(
i
).
row
==
row
&&
chessPieces
.
get
(
i
).
column
==
col
)
{
String
c
=
chessPieces
.
get
(
i
).
cType
;
boolean
attacking
=
chessPieces
.
get
(
i
).
isAttacking
(
chessPieces
.
get
(
i
),
8
);
if
(
attacking
)
{
s
.
print
(
c
+
" y"
);
}
if
(
!
attacking
)
{
s
.
print
(
c
+
" n"
);
}
flag
=
true
;
break
;
}
}
if
(
!
flag
)
{
s
.
print
(
"-"
);
}
}
else
{
s
.
print
(
"Invalid"
);
}
s
.
println
();
chessPieces
.
remove
();
}
s
.
close
();
}
//check if two pieces are attacking
public
static
boolean
checkDoubles
()
{
for
(
int
i
=
chessPieces
.
size
();
i
>
0
;
i
--
)
{
for
(
int
j
=
1
;
j
<
i
;
j
++
)
{
if
(
chessPieces
.
get
(
i
).
row
==
chessPieces
.
get
(
j
).
row
&&
chessPieces
.
get
(
i
).
column
==
chessPieces
.
get
(
j
).
column
)
{
return
true
;
}
}
}
return
false
;
}
//create list of chess pieces and add it on relevant positions on board
public
static
void
createList
(
String
chess
)
{
String
board
[]
=
chess
.
split
(
" "
);
for
(
int
i
=
0
;
i
<
board
.
length
;
i
++
)
{
String
type
=
board
[
i
].
toLowerCase
();
String
original
=
board
[
i
];
int
row
=
Integer
.
parseInt
(
board
[
++
i
]);
int
col
=
Integer
.
parseInt
(
board
[
++
i
]);
switch
(
type
){
case
"q"
:
chessPieces
.
add
(
new
Queen
(
col
,
row
,
original
));
break
;
case
"n"
:
chessPieces
.
add
(
new
Knight
(
col
,
row
,
original
));
break
;
case
"b"
:
chessPieces
.
add
(
new
Bishop
(
col
,
row
,
original
));
break
;
case
"r"
:
chessPieces
.
add
(
new
Rook
(
col
,
row
,
original
));
break
;
case
"p"
:
chessPieces
.
add
(
new
Pawn
(
col
,
row
,
original
));
break
;
case
"k"
:
chessPieces
.
add
(
new
King
(
col
,
row
,
original
));
break
;
}
}
}
}
__MACOSX/HW2/src/._ChessBoard.java
HW2/src/Pawn.java
HW2/src/Pawn.java
public
class
Pawn
extends
ChessPiece
{
//Parameterized Constructor
public
Pawn
(
int
col
,
int
row
,
String
typ
)
{
super
(
col
,
row
,
typ
);
}
//Check if Pawn is attacking or not
@
Override
public
boolean
isAttacking
(
ChessPiece
piece
,
int
board_size
)
{
int
r
=
piece
.
row
;
//no of pieces in row
int
c
=
piece
.
column
;
//no of pieces in column
if
(
color
==
true
)
{
if
(
this
.
row
-
1
==
r
&&
this
.
column
-
1
==
c
)
{
return
true
;
}
if
(
this
.
row
-
1
==
r
&&
this
.
column
+
1
==
c
)
{
return
true
;
}
}
else
{
if
(
this
.
row
+
1
==
r
&&
this
.
column
-
1
==
c
)
{
return
true
;
}
if
(
this
.
row
+
1
==
r
&&
this
.
column
+
1
==
c
)
{
return
true
;
}
}
return
false
;
}
}