-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessDriver.java
158 lines (126 loc) · 4.48 KB
/
ChessDriver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package Chess;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/*
CURRENTLY:
Move functionality:
- All functional pieces seem to work correctly
Capture functionality:
- All functional pieces seem to work correctly
TODO:
- Fix driver class so it's not such a mess
- Start looking at testing and static analysis
*/
public class ChessDriver {
public static void main(String[] args)
{
startChessValidation();
}
public static void startChessValidation()
{
Board board = new Board();
board.createBoard();
//Getting user input for board positions
String[] white = getBoardInput("WHITE");
String[] black = getBoardInput("BLACK");
//ColorPieces objects
ColorPieces whitePieces = new ColorPieces(white);
ColorPieces blackPieces = new ColorPieces(black);
//Getting user input for PTM
String pieceToMove = getPieceToMove();
//Determine if PTM is white or black
char color = getPTMColor(pieceToMove, white, black);
List blocked;
List otherColorSpaces;
if(color == 'w')
{
blocked = Arrays.asList(whitePieces.getPiecePosition());
otherColorSpaces = Arrays.asList(blackPieces.getPiecePosition());
}
else
{
blocked = Arrays.asList(blackPieces.getPiecePosition());
otherColorSpaces = Arrays.asList(whitePieces.getPiecePosition());
}
//Create object of PTM based on its piece type
ChessPiece piece = selectPieceType(pieceToMove, color, blocked, otherColorSpaces);
//Show allowable moves of PTM
piece.printPossibleMoves(pieceToMove, piece.getPossibleMoves());
}
//Gets input for white or black pieces and turns input into white or black array
public static String[] getBoardInput(String color)
{
Scanner scan = new Scanner(System.in);
System.out.print(color + ": ");
String input = scan.nextLine();
//Putting current pieces into arrays
String[] pieceArray = input.split(", ");
//System.out.println(Arrays.toString(pieceArray));
return pieceArray;
}
//Puts white and black arrays together and makes it an ArrayList
//Returns the array list
public static List<String> mergeArrays(String[] white, String[] black)
{
int length = white.length + black.length;
int j = 0;
String[] occupiedSpaces = new String[length];
for (int i = 0; i < length; i++)
{
if(i < white.length)
{
occupiedSpaces[i] = white[i];
}
else
{
occupiedSpaces[i] = black[j];
j++;
}
}
//System.out.println(Arrays.toString(occupiedSpaces));
List blocked = Arrays.asList(occupiedSpaces);
return blocked;
}
//Gets input for the piece that the user wants to move
public static String getPieceToMove()
{
Scanner scan = new Scanner(System.in);
System.out.print("PIECE TO MOVE: ");
return scan.nextLine();
}
public static char getPTMColor(String ptm, String[] white, String[] black)
{
List whiteList = Arrays.asList(white);
if(whiteList.contains(ptm))
{
return 'w';
}
else
{
return 'b';
}
}
public static ChessPiece selectPieceType(String ptm, char color, List<String> blocked, List<String> otherColorSpaces)
{
char pieceID = ptm.charAt(0);
String position = ptm.substring(1);
switch(pieceID)
{
case 'K':
return new King(position, color, blocked, otherColorSpaces);
case 'Q':
return new Queen(position, color, blocked, otherColorSpaces);
case 'R':
return new Rook(position, color, blocked, otherColorSpaces);
case 'B':
return new Bishop(position, color, blocked, otherColorSpaces);
case 'N':
return new Knight(position, color, blocked, otherColorSpaces);
case 'P':
return new Pawn(position, color, blocked, otherColorSpaces);
default:
throw new IllegalArgumentException();
}
}
}