Мне очень жаль, что я раздражаюсь, но я немного паникую и подчеркиваю это. Пожалуйста, не ненавидите, так как это сообщение можно расценивать как сообщение «Пожалуйста, помогите отладке», но я в отчаянии. Я не понимаю, что я делаю неправильно, и я потратил так много времени, пытаясь понять это. Он работает, но просто продолжает сообщать о незаконном перемещении. Я был бы ДЕЙСТВИТЕЛЬНО благодарен за любую информацию по этому вопросу.
игра ...
public class Game {
private static String WHITEPLAYS_MSG = "White plays. Enter move:";
private static String BLACKPLAYS_MSG = "Black plays. Enter move:";
private static String ILLEGALMOVE_MSG = "Illegal move!";
private static String WHITEWINS_MSG = "White wins!";
private static String BLACKWINS_MSG = "Black wins!";
private Board gameBoard;
// Minimal constructor. Expand as needed (kt54)
public Game() {
gameBoard = new Board();
}
// Build on this method to implement game logic.
public void play() {
Player player1 = new Player("white"); //player one plays white
Player player2 = new Player("black"); //player two plays black
Piece piece1 = new Piece();
Piece piece2 = new Piece();
EasyIn2 reader = new EasyIn2();
gameBoard = new Board(); //initializes the board so dont need to do so in main
boolean done = false; //2 while loops within large while loop?
while (!done) { //keeps looping when no one has won yet
gameBoard.printBoard();
System.out.println(WHITEPLAYS_MSG);
String Player1Pos1 = reader.getString(); //gets user input ... move from... to.... temporary variables
int x1From = Player1Pos1.charAt(0) - 'a'; //to transform the letter ASCII values
int y1From = 8 - (Player1Pos1.charAt(1) - '1') - 1; // to transform the number
String Player1Pos2 = reader.getString();
int x1To = Player1Pos2.charAt(0) - 'a'; //to transform the letter
int y1To = 8 - (Player1Pos2.charAt(1) - '1') - 1; // to transform the number
//passing the entire gameBoard instance, not just the array
if (!gameBoard.canFindPiece(gameBoard.board, x1From, y1From)) { //to check if the starting position is allowed
System.out.println(ILLEGALMOVE_MSG);
if (gameBoard.canMovePiece(gameBoard.board, x1From, y1From, x1To, y1To, piece1, player1)) {
gameBoard.movePiece(gameBoard.board, x1From, y1From, x1To, y1To, player1, piece1);
} else {
System.out.println(ILLEGALMOVE_MSG);
}
}
if (player1.getNumberPiecesCaptured() == gameBoard.getStartingPiecesEach()) {
done = true;
System.out.println(WHITEWINS_MSG);
break;
} else {
плата ....
public class Board {
private static final int DEFAULT_SIZE = 8; //images for pieces to be displayed on board
private static final char FREE = '.';
private static final char WHITEROOK = '♖';
private static final char BLACKROOK = '♜';
private static final char WHITEBISHOP = '♗';
private static final char BLACKBISHOP = '♝';
public static int moveNumber=1;
private int startingPiecesEach= 4 ;
private int boardsize;
public char[][] board;
public Board() {
this.boardsize = DEFAULT_SIZE;
board = new char[boardsize][boardsize];
// Clear all playable fields
for (int x = 0; x < boardsize; x++)
for (int y = 0; y < boardsize; y++)
board[x][y] = FREE;
// Put a single bishop in the middle
// Obviously, you will need to replace this with your own initialisation code
board[0][7] = WHITEROOK;
board[2][7] = WHITEBISHOP;
board[5][7] = WHITEBISHOP;
board[7][7] = WHITEROOK;
board[0][0] = BLACKROOK;
board[2][0] = BLACKROOK;
board[5][0] = BLACKROOK;
board[7][0] = BLACKROOK;
}
public void printBoard() {
// Print the letters at the top
System.out.print(" ");
for (int x = 0; x < boardsize; x++)
System.out.print(" " + (char) (x + 'a'));
System.out.println();
for (int y = 0; y < boardsize; y++) {
// Print the numbers on the left side
System.out.print(8 - y);
// Print the actual board fields
for (int x = 0; x < boardsize; x++) {
System.out.print(" ");
char value = board[x][y];
if (value == FREE) {
System.out.print(".");
} else if (value >= WHITEKING && value <= BLACKPAWN) {
System.out.print(value);
} else {
System.out.print("X");
}
}
// Print the numbers on the right side
System.out.println(" " + (8 - y));
}
// Print the letters at the bottom
System.out.print(" ");
for (int x = 0; x < boardsize; x++)
System.out.print(" " + (char) (x + 'a'));
System.out.print("\n\n");
}
public int getStartingPiecesEach() {
return (startingPiecesEach);
}
public char getChar(int x, int y){
return(board[x][y]);
}
public boolean legalPieceMove(char[][] gameBoard,int fromX, int fromY, int toX, int toY, Piece piece) {
char pieceType = piece.getPieceType(gameBoard, fromX, fromY);
if (pieceType == WHITEROOK) {
Rook rook = new Rook("white");
return (rook.moveLegal(gameBoard, fromX, fromY, toX, toY));
} else {
if (pieceType == BLACKROOK) {
Rook rook = new Rook("black");
return (rook.moveLegal(gameBoard, fromX, fromY, toX, toY));
} else {
if (pieceType == WHITEBISHOP) {
Bishop bishop = new Bishop("white");
return (bishop.moveLegal(gameBoard, fromX, fromY, toX, toY));
} else {
if (pieceType == BLACKBISHOP) {
Bishop bishop = new Bishop("black");
return (bishop.moveLegal(gameBoard, fromX, fromY, toX, toY));
} else {
return false;
}
}
}
}
}
public boolean canMovePiece(char[][] gameBoard,int fromX, int fromY, int toX, int toY, Piece piece, Player player) {
if (legalPieceMove(gameBoard, fromX, fromY, toX, toY, piece) && piece.correctPlayer(gameBoard, fromX, fromY, toX, toY, player)) {
return true;
} else
{
return false;
}
}
public void movePiece(char[][] gameBoard,int fromX, int fromY, int toX, int toY, Player player, Piece piece) {
if (gameBoard[toX][toY] != FREE) {
removePiece(gameBoard, fromX, fromY, toX, toY, player, piece);
}
else { gameBoard[fromX][fromY]=FREE;
gameBoard[toX][toY]=piece.getPieceType(gameBoard,fromX,fromY);
moveNumber++;
}
}
//can possibly merge these to one for efficiency
public void removePiece(char[][] gameBoard,int fromX,int fromY, int toX, int toY, Player player, Piece piece) {
gameBoard[fromX][fromY] = FREE;
gameBoard[toX][toY] = piece.getPieceType(gameBoard, fromX, fromY);
player.addNumberPiecesCaptured();
moveNumber++;
}
public boolean canFindPiece(char[][] gameBoard, int fromX, int fromY) { //checks that the player has selected a piece
for (int i = 0; i < gameBoard.length; i++) {
for (int j = 0; j < gameBoard.length; j++) {
if (gameBoard[i][j] == gameBoard[fromX][fromY]) { //checks the user input co-ordinate is on the board
//break was here? //checks the piece is real, ie not a free space
if (gameBoard[fromX][fromY] != FREE) {
return true;
}
else {
return false;
}
}
}
}
return false;
}
}
шт ....
public class Piece {
private static final char WHITEROOK = '♖';
private static final char BLACKROOK = '♜';
private static final char WHITEBISHOP = '♗';
private static final char BLACKBISHOP = '♝';
public static final char FREE = '.';
public String colour;
public char getPieceType(char[][] gameBoard, int x, int y) { //method only used when space is not free
if (gameBoard[x][y] == WHITEROOK) {
return (WHITEROOK);
} else {
if (gameBoard[x][y] == BLACKROOK) {
return (BLACKROOK);
} else {
if (gameBoard[x][y] == WHITEBISHOP) {
return (WHITEBISHOP);
} else {
if (gameBoard[x][y] == BLACKBISHOP) {
return (BLACKBISHOP);
} else return (FREE);
}
}
}
}
public int getPieceColour(char[][] gameBoard, int x, int y) {//white indicated by 1s, black indicated by 2s, free spaces by 0
getPieceType(gameBoard, x, y);
if (gameBoard[x][y] == WHITEROOK) {
return (1);
} else {
if (gameBoard[x][y] == BLACKROOK) {
return (2);
} else {
if (gameBoard[x][y] == WHITEBISHOP) {
return (1);
} else {
if (gameBoard[x][y] == BLACKBISHOP) {
return (2);
} else {
return (0);
}
}
}
}
}
public boolean correctPlayer(char[][] gameBoard, int fromX, int fromY, int toX, int toY, Player player) {
if ((player.colour.equals("black") && getPieceColour(gameBoard, fromX, fromY) == 2) || (player.colour.equals("white") && getPieceColour(gameBoard, fromX, fromY) == 1)) {
return true;
} else {
return false;
}
}
}
пример ладьи ...
public class Rook extends Piece {
public Rook(String colour) {
this.colour= colour;
}
public ArrayList<int[]> possibleMoves = new ArrayList<int[]>();
public ArrayList<int[]> generatePossibleMoves(char[][] gameBoard, int xFrom, int yFrom) {
for (int i = 1; xFrom + i < gameBoard.length; i++) {
if (getPieceColour(gameBoard, xFrom + i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {//cannot go from free space to free
if (gameBoard[xFrom + i][yFrom] != FREE) {
int[] move = {xFrom + i, yFrom};
possibleMoves.add(move);
break; //stops iterating here since a rook is not allowed to jump over other pieces
} else
{
int[] move = {xFrom + i, yFrom};
possibleMoves.add(move);
}
}
}
for (int i = 1; xFrom - i >=0; i++) {
if (getPieceColour(gameBoard, xFrom - i, yFrom) != getPieceColour(gameBoard, xFrom, yFrom)) {
if (gameBoard[xFrom - i][yFrom] != FREE) {
int[] move = {xFrom - i, yFrom};
possibleMoves.add(move);
break;
}
else
{
int[] move = {xFrom - i, yFrom};
possibleMoves.add(move);
}
}
}
for (int i = 1; yFrom + i < gameBoard.length; i++) { //makes sure the place to be moved is on the board
if (getPieceColour(gameBoard, xFrom , yFrom+ i) != getPieceColour(gameBoard, xFrom, yFrom)) {
if (gameBoard[xFrom][yFrom+i] != FREE) {
int[] move = {xFrom, yFrom+i};
possibleMoves.add(move);
break;
}
else
{
int[] move = {xFrom, yFrom+i};
possibleMoves.add(move);
}
}
}
for (int i = 1; yFrom- i >=0; i++)
if (getPieceColour(gameBoard, xFrom, yFrom - i) != getPieceColour(gameBoard, xFrom, yFrom)) {
if (gameBoard[xFrom][yFrom - i] != FREE) {
int[] move = {xFrom, yFrom - i};
possibleMoves.add(move);
break;
} else {
int[] move = {xFrom, yFrom - i};
possibleMoves.add(move);
}
}
return possibleMoves;
}
public boolean moveLegal(char[][] gameBoard, int xFrom, int yFrom, int xTo, int yTo){
int wantedMove[] = {xTo, yTo}; //created wantedMove variable so that it could be checked if it is in the possibleMoves
possibleMoves= generatePossibleMoves(gameBoard, xFrom, yFrom); //problem
if (possibleMoves.contains(wantedMove)) {
return true;
}
else {
return false;
}
}
}