import javax.swing.JOptionPane; import java.util.*; import java.io.*; import java.awt.*; /* * Writen By : Ahmad Jawed ZAFAR * Date : 10/02/2009 * */ public class Chess { public String GameBoard[][]; public char sign1, sign2; public int maxLineLength; public String pA[]; public String pB[]; public String horseA[]; public String elephantA[]; public String king1; public String Queen1; public String rockA[]; public String horseB[]; public String elephantB[]; public String king2; public String Queen2; public String rockB[]; public String currentPlayer; public boolean checkForMatt; public Chess() { checkForMatt = false; king1 = "K1"; king2 = "K2"; horseA = horseB = new String[2]; elephantA = elephantB = new String[2]; rockA = rockB = new String[2]; for (int j=0; j<2; j++) { horseA[j] = "H1"; horseB[j] = "H2"; elephantA[j] = "B1"; elephantB[j] = "B2"; rockA[j] = "R1"; rockB[j] = "R2"; } pA = new String[8]; pB = new String[8]; for (int i=0; i<8; i++) { pA[i] = "P" + 1; pB[i] = "P" + 2; } maxLineLength = 4; GameBoard = new String[8][8]; } public void initializeBoard() { checkForMatt = false; for (int i=0; i<8; i++) { for (int j=0; j<8; j++) { GameBoard[i][j] = " "; if (i == 1) { GameBoard[i][j] = pB[j]; } if (i == 6) { GameBoard[i][j] = pA[j]; } } } GameBoard[0][2] = GameBoard[0][5] = "B2"; GameBoard[7][2] = GameBoard[7][5] = "B1"; GameBoard[0][1] = GameBoard[0][6] = "H2"; GameBoard[7][1] = GameBoard[7][6] = "H1"; GameBoard[0][0] = GameBoard[0][7] = "R2"; GameBoard[7][0] = GameBoard[7][7] = "R1"; GameBoard[0][3] = "Q2"; GameBoard[0][4] = "K2"; GameBoard[7][3] = "Q1"; GameBoard[7][4] = "K1"; } public void printBoard() { System.out.println(" --1--+--2--+--3--+--4--+--5--+--6--+--7--+--8--+"); System.out.println(" +-----+-----+-----+-----+-----+-----+-----+-----+"); for (int i=0; i<8; i++) { System.out.print(i+1 + "-| "); for (int j=0; j<8; j++) { System.out.print(" " + GameBoard[i][j] + " | "); } System.out.println(); if (i==3) { System.out.println(" *************************************************"); } else { System.out.println(" +-----+-----+-----+-----+-----+-----+-----+-----+"); } } } public boolean checkWinner() { int checkForKing =0 ; for (int i=0; i<8; i++) { for (int j=0; j<8; j++) { if (GameBoard[i][j].equalsIgnoreCase("K1") || GameBoard[i][j].equalsIgnoreCase("K2")) { checkForKing++; } } } if (checkForKing < 2) { return true; } return false; } public boolean ifPawn(int xCur, int yCur, int xNex, int yNex) // if the item is a pion // { String tmp; if (GameBoard[yCur][xCur].substring(1).equalsIgnoreCase(GameBoard[yNex][xNex].substring(1))) { System.out.println("Friendly Fight Is Not Allowed"); return false; } if (xCur == xNex) // moving upward for A1 or downward for A2 (location must be empty) //////// { if (currentPlayer.equalsIgnoreCase("1")) // check for valid move for player 1 // { if ((yCur - yNex) == 1 && GameBoard[yNex][xNex].equalsIgnoreCase(" ")) { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else {return false;} } else if (currentPlayer.equalsIgnoreCase("2")) // check for valid move for player 2 // { if ((yCur - yNex) == -1 && GameBoard[yNex][xNex].equalsIgnoreCase(" ")) { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else {return false;} } } // end of movint to an empty cell /// else // moving for fight // { if (GameBoard[yNex][xNex].equalsIgnoreCase(" ")) // cell must not be empty // { return false; } else { if (currentPlayer.equalsIgnoreCase("1")) // if current player is player 1 // { if (((xCur - xNex) == -1 && (yCur - yNex) == 1) || ((xCur - xNex) == 1 && (yCur - yNex) == 1)) { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else {return false;} } else if (currentPlayer.equalsIgnoreCase("2")) // if current player is player // { if (((xCur - xNex) == -1 && (yCur - yNex) == -1) || ((xCur - xNex) == 1 && (yCur - yNex) == -1)) { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else {return false;} } } } return false; } ////////////// end if item is a pion //////////////////////// //********************************************************** // ///////////////// begin if the item is a Rock //////////////// public boolean ifRook(int xCur, int yCur, int xNex, int yNex) { String tmp; if (GameBoard[yCur][xCur].substring(1).equalsIgnoreCase(GameBoard[yNex][xNex].substring(1))) { System.out.println("Friendly Fight Is Not Allowed"); return false; } if (xCur == xNex && yCur != yNex) // moving vertically begin // { if (yCur > yNex) // moving vertically upward // { for (int i=yNex+1; i xNex) // moving horizontally left // { for (int i=xNex+1; i xNex && yCur > yNex && (xCur - xNex) == (yCur - yNex)) // moving to the north west // { int count=1; for (int i=yNex+1; i yNex)) && (xCur - xNex) == (yNex - yCur)) // moving to the north east // { int count=1; for (int i=yNex+1; i xNex) && (yCur < yNex)) && (xCur - xNex) == (yNex - yCur)) // moving to the south west // { int count=1; for (int i=yCur+1; i xNex && (xNex - xCur) == -1) // moving to the left // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else if (xCur == xNex && (yCur - yNex) == 1) // moving upward // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else if (xCur == xNex && (yCur - yNex) == -1) // moving downward // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else if ((xCur - xNex) == -1 && (yCur - yNex) == 1) // moving north east // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else if ((xCur - xNex) == 1 && (yCur - yNex) == 1) // moving north west // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else if ((xCur - xNex) == -1 && (yCur - yNex) == -1) // moving south east // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } else if ((xCur - xNex) == 1 && (yCur - yNex) == -1) // moving south west // { tmp = GameBoard[yCur][xCur]; GameBoard[yNex][xNex] = tmp; GameBoard[yCur][xCur] = " "; return true; } return false; } /////////////////////// end if item is The King //////////////////////// //********************************************************** // ///////////////////// begin if item is Queen ////////////// public boolean ifQueen(int xCur, int yCur, int xNex, int yNex) { String tmp; if (GameBoard[yCur][xCur].substring(1).equalsIgnoreCase(GameBoard[yNex][xNex].substring(1))) { System.out.println("Friendly Fight Is Not Allowed"); return false; } if (xCur == xNex && yCur != yNex) // moving vertically begin // { if (yCur > yNex) // moving vertically upward // { for (int i=yNex+1; i xNex) // moving horizontally left // { for (int i=xNex+1; i xNex && yCur > yNex && (xCur - xNex) == (yCur - yNex)) // moving to the north west // { int count=1; for (int i=yNex+1; i yNex)) && (xCur - xNex) == (yNex - yCur)) // moving to the north east // { int count=1; for (int i=yNex+1; i xNex) && (yCur < yNex)) && (xCur - xNex) == (yNex - yCur)) // moving to the south west // { int count=1; for (int i=yCur+1; i