我有一個問題,我一直試圖弄清楚,但我被卡住了。基本上我一直試圖在一款不是國際象棋的遊戲中實施白車的運動邏輯,但我堅持不懈。我給你的細節:在多維陣列中實現Rook邏輯
- 董事會是一個5x5的多維數組那裏只有每個球員
- 的目標是捕獲所有你的對手的棋子和捕捉他們所有的一個棋子和白嘴鴉將贏得比賽。
- 白嘴鴉可以在一個方向上儘可能地移動,直到他們碰到擋住他們路徑的東西。
我的車現在做正確的事情是,它可以去一個方向,但它可以在該行去任何地方。我需要幫助,試圖找出如何添加更多的邏輯,以確保只要路徑清晰,它只能走。下面是一個例子:
小「p」和「r」是玩家2的棋子,大「P」和「R」是玩家的棋子。現在右上方的R(白嘴鴉)只能向右移動,但是如果你這樣做了,它就會超越兵卒,然後可以儘可能地向下。
* R R R *
* P P P *
* * * * *
* p p p *
* r r r *
這裏是什麼我對車的代碼:
public boolean isLegalMove(Location from,Location to)
{
// Row is XPosition (Up/Down)
// Column is YPosition(Left/Right)
int fromRow = from.getXPosition();
int fromColumn = from.getYPosition();
int toRow = to.getXPosition();
int toColumn = to.getYPosition();
// higher row or column or both
if(((fromColumn >= toColumn) || (fromColumn <= toColumn)) && ((fromRow == toRow))) {
return true;
}
if(((fromRow >= toRow) || (fromRow <= toRow)) && ((fromColumn == toColumn))) {
return true;
}
return false;
}
我想我會作出另一種方法來檢查邏輯是否有路徑中的任何事情,並稱其isPathClear()
編輯:
下面的代碼的其餘部分:
public class Board
{
// The depth and width of the field.
public static final int ROW = 5;
public static final int COLUMN = 5;
public static final String EMPTYPIECE = " * ";
//Storage for the game pieces
private GamePiece [] [] gameBoard;
//Makes the balls and torches for player1
private Pawn1 p1Pawn1,p1Pawn2,p1Pawn3;
private Rook1 p1Rook1,p1Rook2,p1Rook3;
//Makes the ball and torchers for player2
private Pawn2 p2Pawn1,p2Pawn2,p2Pawn3;
private Rook2 p2Rook1,p2Rook2,p1Rook3;
/**
* Makes a 5x5 Gameboard
*/
public Board()
{
// initialise instance variables
gameBoard = new GamePiece [ROW][COLUMN];
//Makes pieces for player1
p1Pawn1 = new Pawn1();
p1Pawn2 = new Pawn1();
p1Pawn3 = new Pawn1();
p1Rook1 = new Rook1();
p1Rook2 = new Rook1();
p1Rook3 = new Rook1();
//Makes pieces for player2
p2Pawn1 = new Pawn2();
p2Pawn2 = new Pawn2();
p2Pawn3 = new Pawn2();
p2Rook1 = new Rook2();
p2Rook2 = new Rook2();
p2Rook3 = new Rook2();
}
/**
* Makes new games
*/
public void newGame()
{
// Assigns the piece of the board for player1
gameBoard[0][1] = p1Rook1;
gameBoard[0][2] = p1Rook2;
gameBoard[0][3] = p1Rook3;
gameBoard[1][1] = p1Pawn1;
gameBoard[1][2] = p1Pawn2;
gameBoard[1][3] = p1Pawn3;
// Assigns the pieces of the board for player2
gameBoard[4][1] = p2Rook1;
gameBoard[4][2] = p2Rook2;
gameBoard[4][3] = p2Rook3;
gameBoard[3][1] = p2Pawn1;
gameBoard[3][2] = p2Pawn2;
gameBoard[3][3] = p2Pawn3;
}
/**
* Displays the content of the board
*/
public void displayBoard()
{
System.out.println(" a b c d e");
int counter = 1;
for (int i = 0; i < gameBoard.length; i++){
System.out.print(counter);
for (int j = 0; j < gameBoard[i].length; j++) {
if (gameBoard[i][j] == null) {
System.out.print(EMPTYPIECE);
} else {
System.out.print(" " + gameBoard[i][j] + " ");
}
}
counter++;
System.out.println();
}
}
/**
* Moves the movepiece from one locatin to another
* @param from - where the location was from
* @param to - Where the location is going to
*/
public void movePiece(Location from,Location to) throws InvalidMoveException
{
int fromRow = from.getXPosition();
int fromColumn = from.getYPosition();
int toRow = to.getXPosition();
int toColumn = to.getYPosition();
if (gameBoard[fromRow][fromColumn] == null) {
throw new InvalidMoveException("Invalid input for source location.");
}
if (! checkBounds(from, to)) {
throw new InvalidMoveException("Invalid input for destination location.");
}
if (isSameLocation(from, to)){
throw new InvalidMoveException("Invalid move, source and destination cannot bethe same.");
}
if (! gameBoard[fromRow][fromColumn].isLegalMove(from, to)) {
throw new InvalidMoveException("Invalid move for this piece.");
}
gameBoard[toRow][toColumn] = gameBoard[fromRow][fromColumn];
gameBoard[fromRow][fromColumn] = null;
displayBoard();
}
/**
* Checks a proposed move to ensure it is within the bounds of the board.
* @param source location, destination location
* @return true if both source and destination are within bounds
*/
private boolean checkBounds(Location from, Location to)
{
int fromRow = from.getXPosition();
int fromColumn = from.getYPosition();
int toRow = to.getXPosition();
int toColumn = to.getYPosition();
boolean testFrom = (fromRow >= 0) && (fromColumn >= 0) && (fromRow < gameBoard.length) && (fromColumn < gameBoard[0].length);
boolean testTo = (toRow >= 0) && (toColumn >= 0) && (toRow < gameBoard.length) && (toColumn < gameBoard[0].length);
return testFrom && testTo;
}
/**
* Checks a proposed move to ensure source and destination are different.
* @param source location, destination location
* @return true if source and destination are the same
*/
private boolean isSameLocation(Location from, Location to)
{
int fromRow = from.getXPosition();
int fromColumn = from.getYPosition();
int toRow = to.getXPosition();
int toColumn = to.getYPosition();
return fromRow == toRow && fromColumn == toColumn;
}
所以,如果我檢查整個董事會,看看董事會是不是空的,那麼我可以安全地說,假設有一些事情的方式,並在這種情況下返回false? – user1879973
如果不知道你的主板的數據結構,我不能回答這個問題 – durron597
你不會檢查板子是否爲空。你必須檢查車的來源和目的地位置之間的單獨空間。 @ durron597隨意添加到您的答案(或告訴我,我錯過了什麼);我想加入我自己的答案,但認爲它最終會和你的答案太相似。 – Pops