Java 如何检查2D阵列的相邻索引-Otherlo
为了清楚起见,在寻求帮助之前,我已经在Stack Overflow和其他网站上研究过类似的问题。我还包括了下面的所有代码,以防它可以帮助任何人理解这个问题 在游戏《奥赛罗》(Otherlo,也称为Reversi)中,有两名玩家使用颜色相反的瓷砖。玩家需要放置一块瓷砖,使其与相反颜色的瓷砖相邻,并且相反颜色的瓷砖必须在同一方向的两侧被瓷砖包围。例如,如果在黑色瓷砖的左侧有一个白色瓷砖,则需要在白色瓷砖的左侧放置另一个黑色瓷砖,以便将其包围。如果瓷砖被包围,它将翻转 (黑-白-黑)-->(黑-黑-黑) 环绕可以水平、对角或垂直发生 我遇到的问题是,我不知道如何检查所有的索引,看看它们是否同时起作用。我试着通过检查相邻的所有可能的值来检查与相反颜色的瓷砖相邻的值。这不能正常工作,因为它不能确保瓷砖的两面都被包围,也不能应用于一行中多个瓷砖的线条Java 如何检查2D阵列的相邻索引-Otherlo,java,multidimensional-array,othello,Java,Multidimensional Array,Othello,为了清楚起见,在寻求帮助之前,我已经在Stack Overflow和其他网站上研究过类似的问题。我还包括了下面的所有代码,以防它可以帮助任何人理解这个问题 在游戏《奥赛罗》(Otherlo,也称为Reversi)中,有两名玩家使用颜色相反的瓷砖。玩家需要放置一块瓷砖,使其与相反颜色的瓷砖相邻,并且相反颜色的瓷砖必须在同一方向的两侧被瓷砖包围。例如,如果在黑色瓷砖的左侧有一个白色瓷砖,则需要在白色瓷砖的左侧放置另一个黑色瓷砖,以便将其包围。如果瓷砖被包围,它将翻转 (黑-白-黑)-->(黑-黑-黑
/**
* Checks to see if a valid move can be made at the indicated OthelloCell,
* for the given player.
* @param xt The horizontal coordinate value in the board.
* @param yt The vertical coordinate value in the board.
* @param bTurn Indicates the current player, true for black, false for white
* @return Returns true if a valid move can be made for this player at
* this position, false otherwise.
*/
public boolean isValidMove(int xt, int yt, boolean bTurn)
{
int length = board[0].length;
// checks if the tile has already been picked, meaning it can no longer be selected
if(board[xt][yt].hasBeenPlayed())
{
return false;
}
else
{
/* For BLACK (Working) */
if(bTurn)
{
// checks tiles one row above
if(xt + 1 < length && board[xt + 1][yt].getBlackStatus() == false)
{
System.out.println("the one 1 row up and in the same column doesn't work");
return true;
}
if(xt + 1 < length && board[xt + 1][yt + 1].getBlackStatus() == false)
{
System.out.println("the one 1 row up and in the right column doesn't work");
return true;
}
if(xt + 1 < length && board[xt + 1][yt - 1].getBlackStatus() == false)
{
System.out.println("the one 1 row up and in the left column doesn't work");
return true;
}
// checks tiles left and right
if(yt + 1 < length && board[xt][yt + 1].getBlackStatus() == false)
{
System.out.println("the one in the same row and in the right column doesn't work");
return true;
}
if(yt > 1 && board[xt][yt - 1].getBlackStatus() == false)
{
System.out.println("the one in the same row and in the left column doesn't work");
return true;
}
// checks tiles one row below
if(xt > 1 && board[xt - 1][yt].getBlackStatus() == false)
{
System.out.println("The one 1 row down and in the same column doesn't work");
return true;
}
if(xt > 1 && board[xt - 1][yt + 1].getBlackStatus() == false)
{
System.out.println("The one 1 row down and in the right column doesn't work");
return true;
}
if(xt > 1 && board[xt - 1][yt - 1].getBlackStatus() == false)
{
System.out.println("The one 1 row down and in the left column doesn't work");
return true;
}
}
}
return false;
}
/**
* Checks to see if a valid move can be made at the indicated OthelloCell, in a
* particular direction (there are 8 possible directions). These are indicated by:
* (1,1) is up and right
* (1,0) is right
* (1,-1) is down and right
* (0,-1) is down
* (-1,-1) is down and left
* (-1,0) is left
* (-1,1) is left and up
* (0,1) is up
* @param xt The horizontal coordinate value in the board.
* @param yt The vertical coordinate value in the board.
* @param i -1 is left, 0 is neutral, 1 is right,
* @param j -1 is down, - is neutral, 1 is up.
* @param bTurn Indicates the current player, true for black, false for white.
* @return Returns true if this direction has pieces to be flipped, false otherwise.
*/
public boolean directionValid(int xt, int yt, int i, int j, boolean bTurn)
{
return true;
}
/**
*检查是否可以在指示的Otherlocell进行有效移动,
*对于给定的玩家。
*@param xt板中的水平坐标值。
*@param yt板中的垂直坐标值。
*@param bTurn表示当前玩家,黑色为真,白色为假
*@return返回true,如果该玩家可以在
*此位置,否则为假。
*/
公共布尔值isValidMove(int-xt,int-yt,布尔值bTurn)
{
int length=线路板[0]。长度;
//检查磁贴是否已拾取,这意味着无法再选择该磁贴
如果(板[xt][yt].hasBeenPlayed())
{
返回false;
}
其他的
{
/*适用于黑人(工作)*/
如果(b turn)
{
//检查上面一行的瓷砖
如果(xt+11&&board[xt][yt-1].getBlackStatus()==false)
{
System.out.println(“同一行和左列中的一个不起作用”);
返回true;
}
//检查下面的一行
如果(xt>1&&board[xt-1][yt].getBlackStatus()==false)
{
System.out.println(“下一行和同一列中的一行不起作用”);
返回true;
}
如果(xt>1&&board[xt-1][yt+1].getBlackStatus()==false)
{
System.out.println(“向下1行且在右侧列中的一行不起作用”);
返回true;
}
如果(xt>1&&board[xt-1][yt-1].getBlackStatus()==false)
{
System.out.println(“下一行和左列中的一行不起作用”);
返回true;
}
}
}
返回false;
}
/**
*检查以查看是否可以在指示的Otherlocell处进行有效移动
*特定方向(有8个可能的方向)。具体表现为:
*(1,1)是正确的
*(1,0)是对的
*(1,-1)是向下和向右的
*(0,-1)已关闭
*(-1,-1)向下并向左
*(-1,0)为左
*(-1,1)是向左和向上的
*(0,1)上升
*@param xt板中的水平坐标值。
*@param yt板中的垂直坐标值。
*@param i-1为左,0为中性,1为右,
*@param j-1处于下降状态,-处于空档,1处于上升状态。
*@param bTurn表示当前玩家,黑色为真,白色为假。
*@return如果此方向有要翻转的碎片,则返回true,否则返回false。
*/
公共布尔值方向有效(int-xt,int-yt,int-i,int-j,boolean-bTurn)
{
返回true;
}
以上是我遇到麻烦的两种方法
public class Othello
{
/** The board object. This board will be 8 x 8, and filled with OthelloCells.
* The cell may be empty, hold a white game piece, or a black game piece. */
private OthelloCell [][] board;
/** The coordinates of the active piece on the board. */
private int x, y;
/** Booleans indicating that the mouse is ready to be pressed, that it is
* black's turn to move (false if white's turn), and that the game is over. */
private boolean mousePressReady, blackTurn, gameOver;
/**
* Creates an Othello object, with a sized graphics canvas, and a 2D (8 x 8) array
* of OthelloCell, setting up initial values.
*/
/* COMPLETE */
public Othello ( )
{
StdDraw.setCanvasSize(500,650);
StdDraw.setXscale(0,1);
StdDraw.setYscale(0,1.3);
StdDraw.enableDoubleBuffering();
Font font = new Font("Arial", Font.BOLD, 30);
StdDraw.setFont(font);
startBoard();
}
/**
* Called by the constructor, or when the player hits the "RESET" button,
* initializing the game board (an 8 x 8 array of OthelloCell).
*/
/* COMPLETE */
public void startBoard ( )
{
mousePressReady = blackTurn = true;
gameOver = false;
board = new OthelloCell[8][8];
for ( int i = 0; i < board.length; i++ )
{
for ( int j = 0; j < board[i].length; j++ )
{
board[i][j] = new OthelloCell(i,j);
}
}
board[3][3].playIt();
board[3][3].setBlack(true);
board[4][4].playIt();
board[4][4].setBlack(true);
board[4][3].playIt();
board[4][3].setBlack(false);
board[3][4].playIt();
board[3][4].setBlack(false);
}
/**
* Sets up and runs the game of Othello.
*/
/* COMPLETE */
public static void main(String [] args)
{
Othello game = new Othello();
game.run();
}
/**
* Runs an endless loop to play the game. Even if the game is over, the
* loop is still ready for the user to press "RESET" to play again.
*/
/* COMPLETE */
public void run ( )
{
do
{
drawBoard();
countScoreAnddrawScoreBoard();
StdDraw.show();
StdDraw.pause(30);
makeChoice();
gameOver = checkTurnAndGameOver();
}
while(true);
}
/**
* Draws the board, in its current state, to the GUI.
*/
/* COMPLETE */
public void drawBoard ( )
{
StdDraw.setPenColor(new Color(150,150,150));
StdDraw.filledRectangle(0.5,0.75,0.5,0.75);
StdDraw.setPenColor(new Color(0,110,0));
StdDraw.filledSquare(0.5,0.5,0.45);
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.filledSquare(0.5,0.5,0.42);
for ( int i = 0; i < board.length; i++ )
{
for ( int j = 0; j < board[i].length; j++ )
{
board[i][j].drawCell();
}
}
}
/**
* Waits for the user to make a choice. The user can make a move
* placing a black piece or the white piece (depending on whose turn
* it is), or click on the "RESET" button to reset the game.
*/
/* COMPLETE */
public void makeChoice ( )
{
boolean moveChosen = false;
while(!moveChosen)
{
if(mousePressReady && StdDraw.isMousePressed())
{
mousePressReady = false;
double xval = StdDraw.mouseX();
double yval = StdDraw.mouseY();
if(xval > 0.655 && xval < 0.865 && yval > 1.15 && yval < 1.23) // This if checks for a reset.
{
startBoard();
return;
}
if(xval < 0.1 || xval > 0.9 || yval < 0.1 || yval > 0.9) // This if checks for a press off the board.
{
return;
}
int tempx = (int)(10 * (xval - 0.1));
int tempy = (int)(10 * (yval - 0.1));
if(isValidMove(tempx,tempy,blackTurn)) // This if checks to see if the move is valid.
{
x = tempx;
y = tempy;
playAndFlipTiles();
blackTurn = !blackTurn;
System.out.println(x + " " + y);
}
}
if(!StdDraw.isMousePressed() && !mousePressReady) // This if gives back control when the mouse is released.
{
mousePressReady = true;
return;
}
StdDraw.pause(20);
}
}
/**
* Checks to see if a valid move can be made at the indicated OthelloCell,
* for the given player.
* @param xt The horizontal coordinate value in the board.
* @param yt The vertical coordinate value in the board.
* @param bTurn Indicates the current player, true for black, false for white
* @return Returns true if a valid move can be made for this player at
* this position, false otherwise.
*/
public boolean isValidMove(int xt, int yt, boolean bTurn)
{
int length = board[0].length;
// checks if the tile has already been picked, meaning it can no longer be selected
if(board[xt][yt].hasBeenPlayed())
{
return false;
}
else
{
}
return false;
}
/**
* Checks to see if a valid move can be made at the indicated OthelloCell, in a
* particular direction (there are 8 possible directions). These are indicated by:
* (1,1) is up and right
* (1,0) is right
* (1,-1) is down and right
* (0,-1) is down
* (-1,-1) is down and left
* (-1,0) is left
* (-1,1) is left and up
* (0,1) is up
* @param xt The horizontal coordinate value in the board.
* @param yt The vertical coordinate value in the board.
* @param i -1 is left, 0 is neutral, 1 is right,
* @param j -1 is down, - is neutral, 1 is up.
* @param bTurn Indicates the current player, true for black, false for white.
* @return Returns true if this direction has pieces to be flipped, false otherwise.
*/
public boolean directionValid(int xt, int yt, int i, int j, boolean bTurn)
{
return true;
}
/**
* Places a game piece on the current cell for the current player. Also flips the
* appropriate neighboring game pieces, checking the 8 possible directions from the
* current cell.
*/
public void playAndFlipTiles ( )
{
board[x][y].setBlack(blackTurn);
board[x][y].playIt();
// To be completed by you.
}
/**
* A helper method for playAndFlipTiles. Flips pieces in a given direction. The
* directions are as follows:
* (1,1) is up and right
* (1,0) is right
* (1,-1) is down and right
* (0,-1) is down
* (-1,-1) is down and left
* (-1,0) is left
* (-1,1) is left and up
* (0,1) is up
* @param xt The horizontal coordinate value in the board.
* @param yt The vertical coordinate value in the board.
* @param i -1 is left, 0 is neutral, 1 is right,
* @param j -1 is down, - is neutral, 1 is up.
*/
public void flipAllInThatDirection(int xt, int yt, int i, int j)
{
}
/**
* Counts the white pieces on the board, and the black pieces on the board.
* Displays these numbers toward the top of the board, for the current state
* of the board. Also prints whether it is "BLACK'S TURN" or "WHITE'S TURN"
* or "GAME OVER".
*/
/* COMPLETE */
public void countScoreAnddrawScoreBoard ( )
{
int whiteCount = 0, blackCount = 0;
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(board[i][j].hasBeenPlayed())
{
if(board[i][j].getBlackStatus())
{
blackCount++;
}
else
{
whiteCount++;
}
}
}
}
drawScoresAndMessages(whiteCount,blackCount);
}
/**
* A helper method for countScoreAnddrawScoreBoard. Draws the scores
* and messages.
* @param whiteCount The current count of the white pieces on the board.
* @param blackCount The current count of the black pieces on the board.
*/
/* COMPLETE */
public void drawScoresAndMessages(int whiteCount, int blackCount)
{
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.filledRectangle(0.41,1.05,0.055,0.045);
StdDraw.filledRectangle(0.80,1.05,0.055,0.045);
StdDraw.filledRectangle(0.76,1.19,0.11,0.045);
StdDraw.setPenColor(new Color(255,255,255));
StdDraw.filledRectangle(0.41,1.05,0.05,0.04);
StdDraw.filledRectangle(0.80,1.05,0.05,0.04);
StdDraw.filledRectangle(0.76,1.19,0.105,0.04);
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.text(0.24,1.04,"BLACK");
StdDraw.text(0.41,1.04,"" + blackCount);
StdDraw.text(0.63,1.04,"WHITE");
StdDraw.text(0.80,1.04,"" + whiteCount);
StdDraw.text(0.76,1.18,"RESET");
if(gameOver)
{
StdDraw.text(0.34,1.18,"GAME OVER");
}
else if(blackTurn)
{
StdDraw.text(0.34,1.18,"BLACK'S TURN");
}
else
{
StdDraw.text(0.34,1.18,"WHITE'S TURN");
}
}
/**
* Checks to see if black can play. Checks to see if white can play.
* If neither can play, the game is over. If black can't go, then set
* blackTurn to false. If white can't go, set blackTurn to true.
* @return Returns true if the game is over, false otherwise.
*/
/* COMPLETE */
public boolean checkTurnAndGameOver ( )
{
boolean whiteCanGo = false, blackCanGo = false;
// To be completed by you.
return false;
}
}
/**
* Represents a single cell in the game of Othello. By default, a cell is black, and
* has not been played. When a game piece is "placed" on the board, the boolean played
* is set to true. If the game piece is black, then the boolean black is true, and if
* the game piece is white, then the boolean black is false. The ints x and y
* represent the coordinate values of the cell within the game board, with the lower
* left at (0,0) and the upper right at (7,7).
*/
class OthelloCell
{
/** The coordinates of the active piece on the board. */
private int x, y;
/** Booleans indicating if a piece has been played (or is empty), and indicating
* if the piece is black (or white) */
private boolean played, black;
/**
* Creates an OthelloCell object, at the given coordinate pair.
* @param i The horizontal coordinate value for the cell on the board.
* @param j The vertical coordinate value for the cell on the board.
*/
/* COMPLETE */
public OthelloCell(int i, int j)
{
played = false;
x = i;
y = j;
black = true;
}
/**
* Draws the cell on the board, in its current state.
*/
/* COMPLETE */
public void drawCell ( )
{
StdDraw.setPenColor(new Color(0,0,0));
StdDraw.filledSquare(0.15 + 0.1 * x, 0.15 + 0.1 * y, 0.05);
StdDraw.setPenColor(new Color(0,110,0));
StdDraw.filledSquare(0.15 + 0.1 * x, 0.15 + 0.1 * y, 0.048);
if(played)
{
for(int i = 0; i <= 20; i++)
{
if(black)
{
StdDraw.setPenColor(new Color(5+8*i,5+8*i,5+8*i));
}
else
{
StdDraw.setPenColor(new Color(255-8*i,255-8*i,255-8*i));
}
StdDraw.filledCircle(0.15 + 0.1 * x - i*0.001, 0.15 + 0.1 * y + i*0.001, 0.043-i*0.002);
}
}
}
/**
* Sets the piece to black (black true) or white (black false).
* @param bool The value to be assigned to the game piece.
*/
/* COMPLETE */
public void setBlack(boolean bool)
{
if(bool)
{
black = true;
}
else
{
black = false;
}
}
/**
* Return the status of black; true for a black piece, false for a white piece.
* @return Returns true for a black piece, false for a white piece.
*/
/* COMPLETE */
public boolean getBlackStatus ( )
{
return black;
}
/**
* Sets the value of played to true, to indicate that a piece has been placed on this cell.
*/
/* COMPLETE */
public void playIt ( )
{
played = true;
}
/**
* Return the status of played, indicating whether or not there is a game piece on this cell.
* @return Returns true if a game piece is on this cell, false otherwise.
*/
/* COMPLETE */
public boolean hasBeenPlayed ( )
{
return played;
}
}
公共级奥赛罗
{
/**董事会对象。该董事会将是8 x 8,并充满奥赛罗细胞。
*该单元可能是空的,可容纳一个白色游戏片或一个黑色游戏片*/
私人奥赛罗赛尔[]董事会;
/**板上活动件的坐标*/
私有整数x,y;
/**布尔值表示鼠标已准备好按下,即已按下
*轮到布莱克移动(如果轮到怀特则为假),游戏结束*/
私有布尔mousePressReady、blackTurn、gameOver;
/**
*创建一个Otherlo对象,该对象具有一个大小为8 x 8的图形画布和一个2D数组
*设置初始值。
*/
/*完整的*/
公共奥赛罗()
{
StdDraw.setCanvasSize(500650);
标准偏差设定标度(0,1);
标准偏差(0,1.3);
StdDraw.enabledDoubleBuffering();
Font Font=新字体(“Arial”,Font.BOLD,30);
StdDraw.setFont(字体);
起始板();
}
/**
*由构造函数调用,或
int rowNbr[] = new int[] {-1, -1, -1, 0, 0, 1, 1, 1};
int colNbr[] = new int[] {-1, 0, 1, -1, 1, -1, 0, 1};
for (int k = 0; k < 8; ++k) {
sop(matrix[row + rowNbr[k], col + colNbr[k]])
}