Ejemplo n.º 1
0
 public static void main(String[] args) {
   /**
    * Computing ALL computations for 8x8 takes 26,360,768 computations g1, or (8,7) takes the
    * longest; 2,503,040 a3, or (6,1) takes the shortest: 512 Average: 411,887
    */
   try {
     if (args[0].charAt(0) == '0') mutatedChessBoard();
     else if (args[0].length() == 1) { // these lines read coordings from (1,1) to (8,8)
       m = Integer.parseInt(args[0]) - 1; // converts the string to a 0-7 int scale for row
       n = Integer.parseInt(args[1]) - 1; // converts the string to a 0-7 int scale for column
       EightQueens(m, n);
     } else {
       // these lines are for reading from a chess board; ie, a1-h8
       n = (int) args[0].charAt(0) - 97; // converts the character to the column coordinate
       m =
           ((int) args[0].charAt(1) - 49 - (nByn - 1))
               * (-1); // converts the character to the row coordinate
       // m=((int)args[0].charAt(1)-49-7)*(-1);
       letterEightQueens(m, n);
     }
   } catch (Exception ex) {
     System.out.println(ex.getMessage());
     board.display();
     System.out.println("That is not a spot on the chessboard.");
     System.out.println("Chessboard locations: ");
     board.letterBoard();
     System.out.println("\nLayman's terms: ");
     board.coordinateBoard();
   }
 }
Ejemplo n.º 2
0
  /**
   * This code takes a board input where the upper left is (1,1) and the lower right is (8,8). It
   * then displays the board and gives the queens using those coordinates. The calculations are done
   * as if the upper left is (0,0) and the lower right is (7,7)
   */
  public static void EightQueens(int a, int b) {
    ChessBoard temp = new ChessBoard();
    temp.copy(board);

    if (board.getSpace(a, b) == " ") {
      if (board.queenAmount() != 8) {
        board.placeQueen(a, b); // place queen
        for (int i = 0; i < 8; i++)
          for (int j = 0; j < 8; j++) {
            // count++; //for checking total calculations
            if (board.getSpace(i, j) != " ") {
            } // System.out.println( "cannot place queen at ("+i+","+j+")");
            else EightQueens(i, j);
          }
        if (board.queenAmount() != 8) board.copy(temp); // retrace steps to the most previous bord
      }
    }

    /**
     * The next two sections of code change the solutions from a (0,0-7,7) scale to the "Layman's
     * scale" of (1,1-8,8)
     */
    if (board.queenAmount() == 8)
      System.out.println("Queen location: (" + (a + 1) + "," + (b + 1) + ")");

    if (m == a && b == n) {
      System.out.println("Initial placement (" + (a + 1) + "," + (b + 1) + ")");
      // the next line shows the amount of computations the program took to calculate the solution
      // total+=count;
      board.display();
    }
  }
Ejemplo n.º 3
0
  /**
   * This methods does calculations using a chessboard as if the upper left corner was (0,0) and the
   * lower right corner was (7,7). It then displays the board and the queen locations using standard
   * chessboard identifications.
   */
  public static void letterEightQueens(int a, int b) {
    ChessBoard temp = new ChessBoard(nByn);
    temp.copy(board);

    if (board.getSpace(a, b) == " ") {
      if (board.queenAmount() != nByn) {
        board.placeQueen(a, b); // place queen
        for (int i = 0; i < nByn; i++)
          for (int j = 0; j < nByn; j++) {
            // count++; //for checking total calculations
            if (board.getSpace(i, j) != " ") {
            } // System.out.println( "cannot place queen at ("+i+","+j+")");
            else letterEightQueens(i, j);
          }
        if (board.queenAmount() != nByn)
          board.copy(temp); // retrace steps to the most previous bord
      }
    }

    /**
     * The next two sections of code change the integer solutions to the character solution, namely,
     * converts from a (0,0-7,7) scale to an (a1-h8) scale
     */
    if (board.queenAmount() == nByn) // if there are 8 queens on the board
    System.out.println("Queen location: " + (char) (b + 97) + (nByn - a));

    if (m == a && b == n) {
      System.out.println("Initial placement " + (char) (b + 97) + (nByn - a));
      // the next line shows the amount of computations the program took to calculate the solution
      // total+=count;
      board.display();
    }
  }
Ejemplo n.º 4
0
  static boolean solve(int numQueens, int whichCol) {
    // Bottom-out condition 1:
    if (numQueens == 0) {
      // None to assign - done.
      return true;
    }

    // Bottom-out condition 2:
    if (whichCol >= board.size()) {
      // No columns left to try: done.
      return false;
    }

    // Try every un-forbidden spot in each row.
    for (int row = 0; row < board.size(); row++) {
      if (!board.isForbidden(row, whichCol)) {

        // Try this location.
        board.addQueen(row, whichCol);

        boolean solutionExists = solve(numQueens - 1, whichCol + 1);

        if (solutionExists) {
          return true;
        }

        // Else, un-do
        board.removeQueen(row, whichCol);
      }
    }

    // Couldn't find a solution.
    return false;
  }
Ejemplo n.º 5
0
 private int examineMove(AbstractMove theMove, Player player, int depth, String position) {
   Player player2 = player.getOpponent();
   player2.findAllMoves(theMove.isCheck());
   int endPositionScore = getScoreForEndPosition(player2);
   if (endPositionScore != -1) {
     itsTable.add(position, endPositionScore);
     return endPositionScore;
   } else if (depth == 1) {
     int score = itsScorer.getScore(itsBoard, itsPlayer);
     itsTable.add(position, score);
     return score;
   } else {
     int bestScore = 1234;
     Collection moves = getAllMoves(player2);
     for (Iterator i = moves.iterator(); i.hasNext(); ) {
       AbstractMove move = (AbstractMove) i.next();
       move.getPiece().findMoves_safe();
       move = player2.movePiece(move);
       String newPosition = itsBoard.getPositionCode() + player2.getColor();
       int thisScore = -321;
       boolean hasIt = itsTable.has(newPosition);
       if (hasIt) {
         thisScore = itsTable.getScore();
       } else {
         thisScore = examineMove(move, player2, depth - 1, newPosition);
         itsTable.add(newPosition, thisScore);
       }
       bestScore = compareScores(player2, bestScore, thisScore);
       player2.reverseMove(move);
     }
     return bestScore;
   }
 }
Ejemplo n.º 6
0
 private int getScoreForEndPosition(Player player) {
   int returnValue = -1;
   if (player.isInStaleMate() || itsBoard.isThirdOccuranceOfPosition()) returnValue = 500;
   else if (player.isInCheckMate()) {
     if (player == itsPlayer) returnValue = 0;
     else returnValue = 1000;
   }
   return returnValue;
 }
Ejemplo n.º 7
0
 public void CheckRight(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (c + 1 < 8 && cell[r][c + 1].state != ' ' && cell[r][c + 1].state != turn) {
     for (int i = c + 2; i < 8; i++) {
       if (cell[r][i].state == turn) {
         can = true;
         for (int j = c; j < i; j++) {
           cell[r][j].state = cel.state = turn;
           cell[r][j].repaint();
         }
         return;
       } else {
         if (cell[r][i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 8
0
 public void CheckLeft(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (c - 1 >= 0 && cell[r][c - 1].state != ' ' && cell[r][c - 1].state != turn) {
     for (int i = c - 2; i >= 0; i--) {
       if (cell[r][i].state == turn) {
         can = true;
         for (int j = i + 1; j <= c; j++) {
           cell[r][j].state = cel.state = turn;
           cell[r][j].repaint();
         }
         return;
       } else {
         if (cell[r][i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 9
0
 public void CheckDown(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r + 1 < 8 && cell[r + 1][c].state != ' ' && cell[r + 1][c].state != turn) {
     for (int i = r + 2; i < 8; i++) {
       if (cell[i][c].state == turn) {
         can = true;
         for (int j = r; j < i; j++) {
           cell[j][c].state = cel.state = turn;
           cell[j][c].repaint();
         }
         return;
       } else {
         if (cell[i][c].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 10
0
 public void CheckUp(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r - 1 >= 0 && cell[r - 1][c].state != ' ' && cell[r - 1][c].state != turn) {
     for (int i = r - 2; i >= 0; i--) {
       if (cell[i][c].state == turn) {
         can = true;
         for (int j = r; j > i; j--) {
           cell[j][c].state = cel.state = turn;
           cell[j][c].repaint();
         }
         return;
       } else {
         if (cell[i][c].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 11
0
 public void search(ChessBoard board, Player player) {
   itsTable = new PositionHashTable();
   itsMoves.clear();
   itsPlayer = player;
   itsBoard = board;
   Collection moves = getAllMoves(player);
   for (Iterator i = moves.iterator(); i.hasNext(); ) {
     AbstractMove move = (AbstractMove) i.next();
     move.getPiece().findMoves_safe();
     move = player.movePiece(move);
     itsBoard.recordPosition();
     String positionCode = itsBoard.getPositionCode() + player.getColor();
     int score = examineMove(move, player, itsSearchQuality, positionCode);
     move.setScore(score);
     itsMoves.add(move);
     itsBoard.removeLastPosition();
     player.reverseMove(move);
   }
 }
Ejemplo n.º 12
0
 /**
  * The ComputeAll method runs all possible initial board placements. This method is not called
  * anywhere, so it must be manually implemented
  */
 public static void ComputeAll() {
   for (int i = 0; i < 8; i++) {
     for (int j = 0; j < 8; j++) {
       // count=0;
       m = i;
       n = j;
       EightQueens(m, n);
       count = 0;
       board.reset();
     }
   }
 }
Ejemplo n.º 13
0
 /**
  * constructor
  *
  * @param b
  * @param color
  */
 public Knight(ChessBoard b, int color) {
   this.color = color;
   if (color == 0) // white knights
   {
     if (b.getPiece(7, 1) == null) {
       b.setPiece(this, 7, 1);
       curx = 7;
       cury = 1;
     } else {
       b.setPiece(this, 7, 6);
       curx = 7;
       cury = 6;
     }
   } else {
     if (b.getPiece(0, 1) == null) {
       b.setPiece(this, 0, 1);
       curx = 0;
       cury = 1;
     } else {
       b.setPiece(this, 0, 6);
       curx = 0;
       cury = 6;
     }
   }
 }
Ejemplo n.º 14
0
 public void CheckLeftDown(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r + 1 < 8
       && c - 1 >= 0
       && cell[r + 1][c - 1].state != ' '
       && cell[r + 1][c - 1].state != turn) {
     for (int i = 2; (r + i) < 8 && (c - i) >= 0; i++) {
       if (r + i < 8 && c - i >= 0 && cell[r + i][c - i].state == turn) {
         can = true;
         for (int j = 0; j < i; j++) {
           cell[r + j][c - j].state = cel.state = turn;
           cell[r + j][c - j].repaint();
         }
         return;
       } else {
         if (cell[r + i][c - i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 15
0
 public void CheckRightUp(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r - 1 >= 0
       && c + 1 < 8
       && cell[r - 1][c + 1].state != ' '
       && cell[r - 1][c + 1].state != turn) {
     for (int i = 2; (r - 2) >= 0 && (c + i) < 8; i++) {
       if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == turn) {
         can = true;
         for (int j = 0; j < i; j++) {
           cell[r - j][c + j].state = cel.state = turn;
           cell[r - j][c + j].repaint();
         }
         return;
       } else {
         if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 16
0
 public void CheckRightDown(ChessBoard cel) {
   int r = cel.getRow();
   int c = cel.getColumn();
   if (r + 1 < 8
       && c + 1 < 8
       && cell[r + 1][c + 1].state != ' '
       && cell[r + 1][c + 1].state != turn) {
     for (int i = 2; (i + r) < 8 && (i + c) < 8; i++) {
       if (r + i < 8 && c + i < 8 && cell[i + r][c + i].state == turn) {
         can = true;
         for (int j = 0; j < i; j++) {
           cell[r + j][c + j].state = cel.state = turn;
           cell[r + j][c + j].repaint();
         }
         return;
       } else {
         if (cell[r + i][c + i].state == ' ') break;
         else continue;
       }
     }
   }
 }
Ejemplo n.º 17
0
  static void solveQueens(int numQueens, int size) {
    board = new ChessBoard(size);

    boolean solutionExists = solve(numQueens, 0);

    if (!solutionExists) {
      System.out.println(
          "No solution for " + numQueens + " on a " + size + " x " + size + " board");
      return;
    }

    System.out.println(
        "Solution found for " + numQueens + " on a " + size + " x " + size + " board");

    System.out.println(board);
    board.display();
  }
Ejemplo n.º 18
0
  public void CheckPlace(ChessBoard cel) {
    int r = cel.getRow();
    int c = cel.getColumn();
    if (r - 1 >= 0 && cell[r - 1][c].state != ' ' && cell[r - 1][c].state != turn) {
      for (int i = r - 2; i >= 0; i--) {
        if (cell[i][c].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[i][c].state == ' ') break;
          else continue;
        }
      }
    }

    if (r + 1 < 8 && cell[r + 1][c].state != ' ' && cell[r + 1][c].state != turn) {
      for (int i = r + 2; i < 8; i++) {
        if (cell[i][c].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[i][c].state == ' ') break;
          else continue;
        }
      }
    }

    if (c - 1 >= 0 && cell[r][c - 1].state != ' ' && cell[r][c - 1].state != turn) {
      for (int i = c - 2; i >= 0; i--) {
        if (cell[r][i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r][i].state == ' ') break;
          else continue;
        }
      }
    }

    if (c + 1 < 8 && cell[r][c + 1].state != ' ' && cell[r][c + 1].state != turn) {
      for (int i = c + 2; i < 8; i++) {
        if (cell[r][i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r][i].state == ' ') break;
          else continue;
        }
      }
    }

    if (c - 1 >= 0
        && r - 1 >= 0
        && cell[r - 1][c - 1].state != ' '
        && cell[r - 1][c - 1].state != turn) {
      for (int i = 2; (r - i) >= 0 && (c - i) >= 0; i++) {
        if (cell[r - i][c - i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r - i][c - i].state == ' ') break;
          else continue;
        }
      }
    }

    if (r + 1 < 8
        && c - 1 >= 0
        && cell[r + 1][c - 1].state != ' '
        && cell[r + 1][c - 1].state != turn) {
      for (int i = 2; (r + i) < 8 && (c - i) >= 0; i++) {
        if (r + i < 8 && c - i >= 0 && cell[r + i][c - i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r + i][c - i].state == ' ') break;
          else continue;
        }
      }
    }

    if (r - 1 >= 0
        && c + 1 < 8
        && cell[r - 1][c + 1].state != ' '
        && cell[r - 1][c + 1].state != turn) {
      for (int i = 2; (r - 2) >= 0 && (c + i) < 8; i++) {
        if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == turn) {
          canPut = true;
          return;
        } else {
          if (r - i >= 0 && c + i < 8 && cell[r - i][c + i].state == ' ') break;
          else continue;
        }
      }
    }

    if (r + 1 < 8
        && c + 1 < 8
        && cell[r + 1][c + 1].state != ' '
        && cell[r + 1][c + 1].state != turn) {
      for (int i = 2; (i + r) < 8 && (i + c) < 8; i++) {
        if (r + i < 8 && c + i < 8 && cell[i + r][c + i].state == turn) {
          canPut = true;
          return;
        } else {
          if (cell[r + i][c + i].state == ' ') break;
          else continue;
        }
      }
    }
  }
Ejemplo n.º 19
0
  public int Clicked(ChessBoard cel) {
    int judge = 0;
    int r = cel.getRow();
    int c = cel.getColumn();
    System.out.println("落子前:" + cell[3][5].taken);

    if (gameStart) {
      if (cel.taken == false) {
        Check(cel);
      }
      System.out.println(cell[3][5].taken);
      System.out.println(can);
      if (can && cel.taken == false) {
        /*for (int i = 0; i < 8; i++)
        for (int j = 0; j < 8; j++)
        if (cell[i][j].changed == true)
        {
        	cell[i][j].ChangeBack();
        }*/
        RememberState();
        ShowChessNumber();
        list.add(cel);
        JudgeWhoIsWinner();
        turn = TakeTurn();
        cel.taken = true;
        System.out.println("落子后:" + cell[3][5].taken);
        can = false;
        judge = 1;
        for (int i = 0; i < 8; i++)
          for (int j = 0; j < 8; j++)
            if (cell[i][j].changed) {
              cell[i][j].ChangeBack();
            }

        boolean flag = CheckAll();
        if (!flag && white + black < 64) CheckAtTheEnd();
        /*else
        {
        	for (int i = 0; i < 8; i++)
        	for (int j = 0; j < 8; j++)
        	if (cell[i][j].taken == false)
        	{
        		CheckPlace(cell[i][j]);
        		if (canPut)
        		{
        			cell[i][j].ChangeBackground();
        			canPut = false;
        		}
        	}
        }*/
      } else {
        JOptionPane.showMessageDialog(null, "无法在该位置落子");
        judge = 0;
        System.out.println(cell[3][5].taken);
      }
      return judge;
    } else {
      JOptionPane.showMessageDialog(null, "游戏还未开始或已结束");
      return 0;
    }
  }