import java.util.Scanner;
public class Exercise7 {
static int x_WhiteKing, y_WhiteKing, x_BlackKing, y_BlackKing = 0; //흑백의 왕위치
static final int x_Min = 0;
static final int y_Min = 0;
static final int x_Max = 7;
static final int y_Max = 7;
static char[][] fld; //chess 판 규격
static boolean inChk = false;
static char key;
static int chessNo = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int lineNo = 0;
try {
while(sc.hasNext()){
x_WhiteKing = 0;
y_WhiteKing = 0;
x_BlackKing = 0;
y_BlackKing = 0; //흑백의 왕위치 초기화
lineNo = 0; //chess 행라인 초기화
fld = new char[x_Max+1][y_Max+1]; //chess판의 필드 초기화
int validIsFinal = 0;
String line = ""; //입력 라인
//fld값 및 k(K)의 위치 저장.
while(lineNo<=x_Max && !(line = sc.nextLine()).equals("")){
setFldAndKingsLocate(line.toCharArray(), lineNo);
lineNo++;
if(line.equals("........")) validIsFinal = validIsFinal + 0;
else validIsFinal = validIsFinal + 1;
}
//빈칸을 건너뛰고, 체스가 모두 '.'으로 이뤄진 경우 전체 while문을 종료시킨다.
if(line.equals(""))continue;
else if(validIsFinal == 0) break;
// 1. r(Rook)
// 2. b(Bishop)
// 3. q(Queen)
// 4. p(Pawn)
// 5. n(Knight)
chessNo++;
//White in check
if(doKnight(x_WhiteKing, y_WhiteKing, 'n')){
printResult(chessNo, "White");
continue;
}
if(doRook(x_WhiteKing, y_WhiteKing, 'r')){
printResult(chessNo, "White");
continue;
}
if(doBishop(x_WhiteKing, y_WhiteKing, 'b')){
printResult(chessNo, "White");
continue;
}
if(doQueen(x_WhiteKing, y_WhiteKing, 'q')){
printResult(chessNo, "White");
continue;
}
if(doUpperPawn(x_WhiteKing, y_WhiteKing, 'p')){
printResult(chessNo, "White");
continue;
}
//Black in check
if(doRook(x_BlackKing, y_BlackKing, 'R')){
printResult(chessNo, "Black");
continue;
}
if(doQueen(x_BlackKing, y_BlackKing, 'Q')){
printResult(chessNo, "Black");
continue;
}
if(doBishop(x_BlackKing, y_BlackKing, 'B')){
printResult(chessNo, "Black");
continue;
}
if(doKnight(x_BlackKing, y_BlackKing, 'N')){
printResult(chessNo, "Black");
continue;
}
if(doBottomPawn(x_BlackKing, y_BlackKing, 'P')){
printResult(chessNo, "Black");
continue;
}
printResult(chessNo, "No");
}
} catch (IllegalStateException ise) {
ise.printStackTrace();
} catch (NoSuchElementException nsee) {
nsee.printStackTrace();
} catch (ArrayIndexOutOfBoundsException ae){
ae.printStackTrace();
}
}
private static void printResult(int chessNo, String blackOrWhite){
StringBuffer sb = new StringBuffer();
sb.append("Game #");
sb.append(chessNo);
sb.append(": ");
sb.append(blackOrWhite);
sb.append(" king is in check.");
System.out.println(sb.toString());
}
private static boolean doRook(int x, int y, char keyName){
key = keyName;
if(goLeftStraight(x, y)) return true;
else if(goRightStraight(x, y)) return true;
else if(goUpperStraight(x, y)) return true;
else if(goBottomStraight(x, y)) return true;
else return false;
}
private static boolean doBishop(int x, int y, char keyName){
key = keyName;
if(goLeftUpperStraight(x, y)) return true;
else if(goLeftBottomStraight(x, y)) return true;
else if(goRightUpperStraight(x, y)) return true;
else if(goRightBottomStraight(x, y)) return true;
else return false;
}
private static boolean doQueen(int x, int y, char keyName){
key = keyName;
if(goLeftStraight(x, y)) return true;
else if(goRightStraight(x, y)) return true;
else if(goUpperStraight(x, y)) return true;
else if(goBottomStraight(x, y)) return true;
else if(goLeftUpperStraight(x, y)) return true;
else if(goLeftBottomStraight(x, y)) return true;
else if(goRightUpperStraight(x, y)) return true;
else if(goRightBottomStraight(x, y)) return true;
else return false;
}
private static boolean doKnight(int x, int y, char keyName){
key = keyName;
if(((x-1)>=x_Min)&&((y-2)>=y_Min)&&isBreakForKnight(x-1,y-2)) return inChk;
if(((x-2)>=x_Min)&&((y-1)>=y_Min)&&isBreakForKnight(x-2,y-1)) return inChk;
if(((x+1)<=x_Max)&&((y+2)<=y_Max)&&isBreakForKnight(x+1,y+2)) return inChk;
if(((x+2)<=x_Max)&&((y+1)<=y_Max)&&isBreakForKnight(x+2,y+1)) return inChk;
if(((x-1)>=x_Min)&&((y+2)<=y_Max)&&isBreakForKnight(x-1,y+2)) return inChk;
if(((x-2)>=x_Min)&&((y+1)<=y_Max)&&isBreakForKnight(x-2,y+1)) return inChk;
if(((x+1)<=x_Max)&&((y-2)>=y_Min)&&isBreakForKnight(x+1,y-2)) return inChk;
if(((x+2)<=x_Max)&&((y-1)>=y_Min)&&isBreakForKnight(x+2,y-1)) return inChk;
return false;
}
private static boolean doUpperPawn(int x, int y, char keyName){
key = keyName;
if(goLeftUpperOnce(x, y)) return true;
else if(goRightUpperOnce(x, y)) return true;
else return false;
}
private static boolean doBottomPawn(int x, int y, char keyName){
key = keyName;
if(goLeftBottomOnce(x, y)) return true;
else if(goRightBottomOnce(x, y)) return true;
else return false;
}
private static boolean goLeftStraight(int x, int y){
while((--x>=x_Min))
isBreak(x,y);
return inChk;
}
private static boolean goRightStraight(int x, int y){
while((++x<=x_Max))
isBreak(x,y);
return inChk;
}
private static boolean goUpperStraight(int x, int y){
while((--y>=y_Min))
isBreak(x,y);
return inChk;
}
private static boolean goBottomStraight(int x, int y){
while((++y<=y_Max))
isBreak(x,y);
return inChk;
}
private static boolean goLeftUpperStraight(int x, int y){
while((--x>=x_Min)&&(--y>=y_Min))
isBreak(x,y);
return inChk;
}
private static boolean goLeftBottomStraight(int x, int y){
while((--x>=x_Min)&&(++y<=y_Max))
isBreak(x,y);
return inChk;
}
private static boolean goRightUpperStraight(int x, int y){
while((++x<=x_Max)&&(--y>=y_Min))
isBreak(x,y);
return inChk;
}
private static boolean goRightBottomStraight(int x, int y){
while((++x<=x_Max)&&(++y<=y_Max))
isBreak(x,y);
return inChk;
}
private static boolean goRightUpperOnce(int x, int y){
if((++x<=x_Max)&&(--y>=y_Min))
isBreak(x,y);
return inChk;
}
private static boolean goLeftUpperOnce(int x, int y){
if((--x>=x_Min)&&(--y>=y_Min))
isBreak(x,y);
return inChk;
}
private static boolean goRightBottomOnce(int x, int y){
if((++x<=x_Max)&&(++y<=y_Max))
isBreak(x,y);
return inChk;
}
private static boolean goLeftBottomOnce(int x, int y){
if((--x>=x_Min)&&(++y<=y_Max))
isBreak(x,y);
return inChk;
}
private static boolean isBreak(int x, int y) throws ArrayIndexOutOfBoundsException{
try{
if(fld[x][y] == key){
inChk = true;
return true;
}else if(fld[x][y] == '.'){
return false;
}else{
return true;
}
}catch(ArrayIndexOutOfBoundsException ae){
ae.printStackTrace();
throw ae;
}
}
private static boolean isBreakForKnight(int x, int y){
if(fld[x][y] == key){
inChk = true;
return true;
}else{
return false;
}
}
private static void setFldAndKingsLocate(char[] val, int lineNo){
for(int y=0; y<val.length; y++){
fld[lineNo][y] = val[y];
if(val[y] == 'k'){
x_BlackKing = lineNo;
y_BlackKing = y;
}else if(val[y] == 'K'){
x_WhiteKing = lineNo;
y_WhiteKing = y;
}
}
}
}
댓글 없음 :
댓글 쓰기