简体   繁体   English

瓷砖调换位置时遇到麻烦-珠宝

[英]Having trouble making tiles swap positions - bejeweled

I'm fairly new to Java and I'm having a problem with arrays in a game I am creating, which is similar to Bejeweled. 我对Java还是很陌生,并且在创建的游戏中遇到了数组问题,类似于Bejeweled。

In this game, I have one class called Tile, which represents one colored tile on the board, along with its column and row (integers)on the board. 在这个游戏中,我有一类叫做Tile的类,它代表棋盘上的一个彩色图块,以及棋盘上的行和列(整数)。 I have another class for the board, called Board, that manages these tiles in a 2D array of integers, organized by column and row. 我还有另一种用于开发板的类,称为“开发板”,可以按列和行组织2D整数数组来管理这些图块。

My problem occurs I swap two tiles on the screen. 我的问题发生了,我在屏幕上交换了两个图块。 When this happens, their columns and rows are swapped, but the array that they are saved in does not recognize this change. 发生这种情况时,将交换其列和行,但是保存在其中的数组无法识别此更改。 On the screen, everything looks fine. 在屏幕上,一切看起来都很好。 The two tiles will switch positions. 这两个图块将切换位置。

For instance, if I have two adjacent tiles that I want to switch, at (column0, row0) and (column1, row0) . 例如,如果我要切换两个相邻的图块,分别位于(column0, row0)(column1, row0) In my array of tiles, these are tiles array[0][0] and array[1][0] . 在我的图块数组中,这些是图块array[0][0]array[1][0] When I switch them, the first tile is now at the second tile's old position, and the second tile is now at the first tile's old position. 当我切换它们时,第一个图块现在处于第二个图块的旧位置,第二个图块现在处于第一个图块的旧位置。 However, the first tile is still recognized as array[0][0] , even though it should now be array[1][0] . 但是,尽管第一个图块现在应该是array[1][0] ,但它仍被识别为array[0][0] array[1][0]

The significance of this is that my program will not recognize three consecutive tiles with the same color, and therefore it will not clear them from the board. 这样做的意义在于,我的程序将无法识别出具有相同颜色的三个连续图块,因此不会从板上清除它们。 Is there anything I can do to fix this? 有什么我可以解决的吗?

If you have any suggestions at all, that would be great. 如果您有任何建议,那就太好了。 Thank you. 谢谢。

Not sure what exactly is wrong from your description. 不确定您的描述到底出了什么问题。 The code should look similar to this: 该代码应类似于以下内容:

class Tile {
  int col;
  int row;
  void setPos(int newCol, int newRow) {
    col = newCol;
    row = newRow;
}

class Board {
  Tile[][] array;

  void swap(int col0, int row0, int col1, int row1) {
    // Get hold of the tile objects to swap
    Tile tile0 = array[col0][row0];
    Tile tile1 = array[col1][row1];

    // Swap the positions stored in the tile objects
    tile0.setPos(col1, row1);
    tile1.setPos(col0, row0); 

    // Swap the tile objects in the array
    array[col0][row0] = tile1;
    array[col1][row1] = tile0;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM