通过一个二维数组移动,而不使用for循环,怎么做呢?

huangapple go评论127阅读模式
英文:

How do you move through a 2D array without for loops?

问题

我正在尝试使用我得到的测试文件来构建一个寻宝游戏。这些文本文件包含字符S、W、E、N和T,它们都对应着方向,除了T,它代表宝藏。一切都运行得很顺利,直到它移动了行/列的长度。我怀疑这与for循环有关,但我不确定。是否有一种方法可以在不使用for循环的情况下完成这个任务,或者是否有人有任何建议可以帮助我解决这个问题?

这是我的更新后的代码:

import java.util.*;

public class NewGridGame {
    public static final int FALL_OFF = -1;
    public static final int GOING_IN_CIRCLES = -2;
    private int row;
    private int col;
    private char[][] gameBoard;

    NewGridGame(int conRow, int conCol, char[][] conGameBoard) {
        row = conRow;
        col = conCol;
        gameBoard = new char[row][col];
        for (int i = 0; i < gameBoard.length; i++) {
            for (int j = 0; j < gameBoard[i].length; j++) {
                gameBoard[i][j] = conGameBoard[i][j];
            }
        }
        System.out.println(Arrays.deepToString(gameBoard));
    }

    public int playGame() {
        boolean[][] beenHereBefore = new boolean[row][col];
        int turns = 0;
        int i = 0;
        int j = 0;
        while (true) {
            if (beenHereBefore[i][j] == true) {
                return GOING_IN_CIRCLES;
            } else {
                beenHereBefore[i][j] = true;
            }
            if (gameBoard[i][j] == 'N') {
                if (i - 1 >= 0) {
                    i--;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == 'S') {
                if (i + 1 < row) {
                    i++;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == 'E') {
                if (j + 1 < col) {
                    j++;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == 'W') {
                if (j - 1 >= 0) {
                    j--;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == 'T') {
                return turns;
            }
        }
    }
}

这是一个测试文件的示例:

ES
TW

这个测试文件应该返回3,也就是移动的次数(在我的代码中是回合数),但实际上它在移动到W(需要2个回合)后返回-2,这只有在多次到达相同位置时才会返回。另外,并非所有的数组都是方形的,有些是1x200或4x5的例子。

英文:

I am trying to build a treasure hunt game using test files I have been given. These texts files are of chars S,W,E,N and T which all correspond to directions except for T, which is the treasure. Everything works fine until it moves the length of the rows/columns. I suspect it has something to do with the for loops but I'm not certain. Is there a way to do this without for loops or does anyone have any advice to get this back on track?

Here is my Updated code so far:

import java.util.*;

public class NewGridGame {
    public static final int FALL_OFF = -1;
    public static final int GOING_IN_CIRCLES = -2;
    private int row;
    private int col;
    private char[][] gameBoard;

    NewGridGame(int conRow, int conCol, char[][] conGameBoard) {
        row = conRow;
        col = conCol;
        gameBoard = new char[row][col];
        for (int i = 0; i &lt; gameBoard.length; i++) {
            for (int j = 0; j &lt; gameBoard[i].length; j++) {
                gameBoard[i][j] = conGameBoard[i][j];
            }
        }
        System.out.println(Arrays.deepToString(gameBoard));
    }

    public int playGame() {
        boolean[][] beenHereBefore = new boolean[row][col];
        int turns = 0;
        int i = 0;
        int j = 0;
        while (true) {
            if (beenHereBefore[i][j] == true) {
                return GOING_IN_CIRCLES;
            } else {
                beenHereBefore[i][j] = true;
            }
            if (gameBoard[i][j] == &#39;N&#39;) {
                if (i - 1 &gt;= 0) {
                    i--;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == &#39;S&#39;) {
                if (i + 1 &lt; row) {
                    i++;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == &#39;E&#39;) {
                if (j + 1 &lt; col) {
                    j++;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == &#39;W&#39;) {
                if (j - 1 &gt;= 0) {
                    j--;
                    turns++;
                    System.out.println(turns);
                    System.out.println(gameBoard[i][j]);
                } else {
                    return FALL_OFF;
                }
            } else if (gameBoard[i][j] == &#39;T&#39;) {
                return turns;
            }
        }
    }
}

Here is an example of a test file as well.

ES
TW

this one should return 3, which is the number of moves (turns, in my code) but instead it gets to W (which takes 2 moves) and returns -2, which is only for when it's gone to a position more than once. Additionally, not all of the Arrays are squares, some are 1x200 or 4x5 for examples.

答案1

得分: 0

如果您知道棋盘的情况是这样的0, 0开始您不会陷入循环也不会移出棋盘您可以使用类似这样的代码

    public int playGame()
    {
      int numMoves = 0; 
      int currentRow = 0;
      int currentCol = 0;
    
      while(gameBoard[currentRow][currentCol] != 'T')
      {
        switch (gameBoard[currentRow][currentCol])
        {
          case 'E': currentCol++; break;
          case 'W': currentCol--; break;
          case 'S': currentRow--; break;
          case 'N': currentRow++; break;
          default:
             throw new IllegalStateException("Unrecognized Move");
        }
        numMoves++;          
      }
      
      return numMoves;
    }

您可能想要使用`if-else`而不是`switch`。

如果您需要检查循环或移出棋盘的情况您可以添加这些检查
英文:

If you know that the board is such that, starting at (0, 0), you won't get stuck in a loop or move off the board you can just used something like this:

public int playGame()
{
int numMoves = 0; 
int currentRow = 0;
int currentCol = 0;
while(gameBoard[currentRow][currentCol] != &#39;T&#39;)
{
switch (gameBoard[currentRow][currentCol])
{
case &#39;E&#39;: currentCol++; break;
case &#39;W&#39;: currentCol--; break;
case &#39;S&#39;: currentRow--; break;
case &#39;N&#39;: currentRow++; break;
default:
throw new IllegalStateException(&quot;Unrecognized Move&quot;);
}
numMoves++;      	  	
}
return numMoves;
}

You may want to use if-then-else instead of a switch.

If you need to check for loops or off-board moves you should be able to add these checks in.

huangapple
  • 本文由 发表于 2020年4月6日 07:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61050725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定