Skip to content

Commit 9e20b82

Browse files
committed
added pj2
0 parents  commit 9e20b82

26 files changed

+724
-0
lines changed

.gitignore

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff
5+
.idea/**/workspace.xml
6+
.idea/**/tasks.xml
7+
.idea/**/usage.statistics.xml
8+
.idea/**/dictionaries
9+
.idea/**/shelf
10+
*.idea
11+
12+
# Generated files
13+
.idea/**/contentModel.xml
14+
15+
# Sensitive or high-churn files
16+
.idea/**/dataSources/
17+
.idea/**/dataSources.ids
18+
.idea/**/dataSources.local.xml
19+
.idea/**/sqlDataSources.xml
20+
.idea/**/dynamic.xml
21+
.idea/**/uiDesigner.xml
22+
.idea/**/dbnavigator.xml
23+
24+
# Gradle
25+
.idea/**/gradle.xml
26+
.idea/**/libraries
27+
28+
# Gradle and Maven with auto-import
29+
# When using Gradle or Maven with auto-import, you should exclude module files,
30+
# since they will be recreated, and may cause churn. Uncomment if using
31+
# auto-import.
32+
# .idea/artifacts
33+
# .idea/compiler.xml
34+
# .idea/jarRepositories.xml
35+
# .idea/modules.xml
36+
# .idea/*.iml
37+
# .idea/modules
38+
# *.iml
39+
# *.ipr
40+
41+
# CMake
42+
cmake-build-*/
43+
44+
# Mongo Explorer plugin
45+
.idea/**/mongoSettings.xml
46+
47+
# File-based project format
48+
*.iws
49+
50+
# IntelliJ
51+
out/
52+
53+
# mpeltonen/sbt-idea plugin
54+
.idea_modules/
55+
56+
# JIRA plugin
57+
atlassian-ide-plugin.xml
58+
59+
# Cursive Clojure plugin
60+
.idea/replstate.xml
61+
62+
# Crashlytics plugin (for Android Studio and IntelliJ)
63+
com_crashlytics_export_strings.xml
64+
crashlytics.properties
65+
crashlytics-build.properties
66+
fabric.properties
67+
68+
# Editor-based Rest Client
69+
.idea/httpRequests
70+
71+
# Android studio 3.1+ serialized cache file
72+
.idea/caches/build_file_checksums.ser
73+
74+
#Appl
75+
.DS_Store

Bishop.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
3+
public class Bishop extends Piece {
4+
public Bishop(Color c) {
5+
super(c);
6+
}
7+
8+
public String toString() {
9+
return this.color()+"b";
10+
}
11+
12+
public List<String> moves(Board b, String loc) {
13+
char c = loc.charAt(0);
14+
int r = Character.getNumericValue(loc.charAt(1));
15+
List<String> to = new LinkedList<>();
16+
to.addAll(moveByDirection(b, c, r, "upRight"));
17+
to.addAll(moveByDirection(b, c, r, "downRight"));
18+
to.addAll(moveByDirection(b, c, r, "upLeft"));
19+
to.addAll(moveByDirection(b, c, r, "downLeft"));
20+
return to;
21+
}
22+
23+
}

BishopFactory.java

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class BishopFactory implements PieceFactory {
2+
public char symbol() { return 'b'; }
3+
public Piece create(Color c) { return new Bishop(c); }
4+
}

Board.java

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import java.util.LinkedList;
2+
import java.util.List;
3+
import java.util.NoSuchElementException;
4+
5+
public class Board {
6+
private static Board instance = new Board();
7+
8+
//col:a-h == 0-7 row:1-8 == 0-7
9+
private Piece[][] pieces = new Piece[8][8];
10+
private List<BoardListener> listeners;
11+
12+
private Board() { }
13+
14+
public static Board theBoard() {
15+
return instance;
16+
}
17+
18+
// Returns piece at given loc or null if no such piece
19+
// exists
20+
public Piece getPiece(String loc) {
21+
int col = getCol(loc);
22+
int row = getRow(loc);
23+
return pieces[col][row];
24+
}
25+
26+
public void addPiece(Piece p, String loc) {
27+
int col = getCol(loc);
28+
int row = getRow(loc);
29+
if(pieces[col][row] == null){
30+
pieces[col][row] = p;
31+
}else throw new NoSuchElementException("Board addPiece exception: position is already occupied!");
32+
}
33+
34+
public void movePiece(String from, String to) {
35+
int fcol = getCol(from);
36+
int frow = getRow(from);
37+
int tcol = getCol(to);
38+
int trow = getRow(to);
39+
//check if there is a piece in from position
40+
if(pieces[fcol][frow] != null){
41+
//check if move valid
42+
List<String> toList = pieces[fcol][frow].moves(instance, from);
43+
if(toList.contains(to)){
44+
for(BoardListener l : listeners){
45+
//broadcast onMove
46+
l.onMove(from, to, pieces[fcol][frow]);
47+
}
48+
//to position already has a piece, capture
49+
if(pieces[tcol][trow] != null){
50+
//broadcast onCapture
51+
for(BoardListener l : listeners){
52+
l.onCapture(pieces[fcol][frow], pieces[tcol][trow]);
53+
}
54+
}
55+
//from capture to
56+
pieces[tcol][trow] = pieces[fcol][frow];
57+
//from is vacant
58+
pieces[fcol][frow] = null;
59+
}else{
60+
throw new NoSuchElementException("board movePiece exception: invalid move: " + from + " to " + to );
61+
}
62+
}else {
63+
throw new NoSuchElementException("board movePiece exception: There is no piece at: " + from);
64+
}
65+
}
66+
67+
public void clear() {
68+
//reassign, garbage collection
69+
pieces = new Piece[8][8];
70+
}
71+
72+
public void registerListener(BoardListener bl) {
73+
listeners.add(bl);
74+
}
75+
76+
public void removeListener(BoardListener bl) {
77+
listeners.remove(bl);
78+
}
79+
80+
public void removeAllListeners() {
81+
listeners = new LinkedList<BoardListener>();
82+
}
83+
84+
public void iterate(BoardExternalIterator bi) {
85+
for(int i = 0; i < 8; i++){
86+
for(int j = 0; j < 8; j++){
87+
bi.visit(toStringLoc(i, j), pieces[i][j]);
88+
}
89+
}
90+
}
91+
92+
private int getRow (String loc){
93+
int r = Character.getNumericValue(loc.charAt(1)) - 1;
94+
if((r >= 0) && (r <= 7)) {
95+
return Character.getNumericValue(loc.charAt(1)) - 1;
96+
}else {
97+
throw new NoSuchElementException("Board exception: row wrong" + loc);
98+
}
99+
}
100+
101+
private int getCol (String loc) {
102+
char c = loc.charAt(0);
103+
if ((c >= 'a') && (c <= 'h')) {
104+
return c - 'a';
105+
} else {
106+
throw new NoSuchElementException("Board exception: loc col wrong" + loc);
107+
}
108+
}
109+
private String toStringLoc(int col, int row){
110+
char c = (char) (col + 'a');
111+
StringBuffer sb = new StringBuffer();
112+
sb.append(c);
113+
sb.append(row + 1);
114+
return sb.toString();
115+
}
116+
}

BoardExternalIterator.java

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface BoardExternalIterator {
2+
void visit(String loc, Piece p);
3+
}

BoardListener.java

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface BoardListener {
2+
void onMove(String from, String to, Piece p);
3+
void onCapture(Piece attacker, Piece captured);
4+
}

BoardPrinter.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class BoardPrinter implements BoardExternalIterator {
2+
public void visit(String loc, Piece p) {
3+
if (p != null) {
4+
System.out.println(loc + "=" + p.toString());
5+
}
6+
}
7+
}

Chess.java

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.io.*;
2+
import java.nio.file.FileVisitResult;
3+
4+
public class Chess {
5+
//read layout file, add pieces
6+
public static void layOut(String l) throws FileNotFoundException, IOException {
7+
try {
8+
File file = new File(l);
9+
BufferedReader r = new BufferedReader(new FileReader(file));
10+
String rs;
11+
while((rs = r.readLine()) != null){
12+
if(rs.isEmpty()||rs.charAt(0)=='#'){
13+
continue;
14+
}else{
15+
String[] sa = rs.trim().split("=");
16+
String loc = sa[0];
17+
//may not place two pieces in the same location
18+
Piece p = Piece.createPiece(sa[1]);
19+
Board.theBoard().addPiece(p, loc);
20+
}
21+
}
22+
} catch (FileNotFoundException e) {
23+
e.printStackTrace();
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
} catch (Exception e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
//read moves file, move pieces
31+
public static void playMoves(String m) throws FileNotFoundException, IOException{
32+
try {
33+
File file = new File(m);
34+
BufferedReader r = new BufferedReader(new FileReader(file));
35+
String rs;
36+
while((rs = r.readLine()) != null){
37+
if(rs.isEmpty()||rs.charAt(0)=='#'){
38+
continue;
39+
}else{
40+
String[] sa = rs.trim().split("-");
41+
String from = sa[0];
42+
String to = sa[1];
43+
Board.theBoard().movePiece(from, to);
44+
}
45+
}
46+
} catch (FileNotFoundException e) {
47+
e.printStackTrace();
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
public static void main(String[] args) throws IOException {
53+
if (args.length != 2) {
54+
System.out.println("Usage: java Chess layout moves");
55+
}
56+
Piece.registerPiece(new KingFactory());
57+
Piece.registerPiece(new QueenFactory());
58+
Piece.registerPiece(new KnightFactory());
59+
Piece.registerPiece(new BishopFactory());
60+
Piece.registerPiece(new RookFactory());
61+
Piece.registerPiece(new PawnFactory());
62+
Board.theBoard().registerListener(new Logger());
63+
// args[0] is the layout file name
64+
// args[1] is the moves file name
65+
// Put your code to read the layout file and moves files
66+
// here.
67+
layOut(args[0]);
68+
playMoves(args[1]);
69+
70+
// Leave the following code at the end of the simulation:
71+
System.out.println("Final board:");
72+
Board.theBoard().iterate(new BoardPrinter());
73+
}
74+
}

Color.java

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public enum Color { BLACK, WHITE };

King.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
3+
public class King extends Piece {
4+
public King(Color c) {
5+
super(c);
6+
}
7+
8+
public String toString() {
9+
return this.color()+"k";
10+
}
11+
12+
//board's rows 1-8 and the columns a-h
13+
//eg. a3
14+
public List<String> moves(Board b, String loc) {
15+
char c = loc.charAt(0);
16+
int r = Character.getNumericValue(loc.charAt(1));
17+
//check loc is valid
18+
if(checkLocValid(c,r)){
19+
List<String> toLoc = new LinkedList<>();
20+
for(int i = -1; i <= 1; i++){
21+
for(int j = -1; j <= 1; j++ ){
22+
// exclude now loc position
23+
if(!(i == 0) && (j == 0)){
24+
StringBuffer sb = new StringBuffer();
25+
// move to legally position on Board
26+
if(checkLocValid((char) (c + i), r + j)) {
27+
sb.append((char) (c + i));
28+
sb.append(r + j);
29+
toLoc.add(sb.toString());
30+
}
31+
}
32+
}
33+
}
34+
return toLoc;
35+
}else{
36+
throw new NoSuchElementException("King move exception: wrong start loc: " + loc);
37+
}
38+
}
39+
}

KingFactory.java

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class KingFactory implements PieceFactory {
2+
public char symbol() { return 'k'; }
3+
public Piece create(Color c) { return new King(c); }
4+
}

0 commit comments

Comments
 (0)