0

我正在用java创建一个简单的迷宫游戏。该代码读取数据文件并将其分配给String array. 玩家输入他或她想要进入的方向,每个位置包含一定数量的points (0, 1, or 2)对应于obstacleNumber (0, 1, or 2). 我想跟踪分数以及玩家移动的次数,但我不知道在哪里调用我的方法以确保计数正确。如果这是一个非常愚蠢的问题,我很抱歉,但我是 Java 新手!

数组已正确填充,但我无法计算移动和点数。

    import java.util.*; 
    import java.io.File;

    public class Program12
     {

 static public void main( String [ ] args ) throws Exception
 {

     if(args.length != 1) {
         System.out.println("Error -- usage is: java Lab11 roomData.txt"); 
         System.exit(0); 
     }

    File newFile = new File(args[0]); 
    Scanner inputFile = new Scanner(newFile);       

    int numberOfRows = inputFile.nextInt();
    int numberOfColumns = inputFile.nextInt();  

    Room[][] game; 

    game = new Room[numberOfRows][numberOfColumns]; 
    int rowNumber = 0; 
    int columnNumber = 0; 

    int moves = 0;
    int points = 0;


    for(int i = 0; i < numberOfRows; i++) 
    {
        for (int j = 0; j < numberOfColumns; j++) 
        {
            String obstacle = inputFile.nextLine(); 
            int obstacleNumber = inputFile.nextInt(); 

            Room room = new Room(obstacle, obstacleNumber); 

            game[i][j] = room;

        }
        System.out.println(); 
        countPoints(obstacleNumber, points);

    }


    while(true) 
    {
        printPlayerLocation(numberOfRows, numberOfColumns, rowNumber, columnNumber);

        System.out.println();
        System.out.print("Enter up, down, left, or right to move:  ");
        Scanner userInput = new Scanner(System.in);
        String in = userInput.nextLine();
        if(in.equals("left") || in.equals("right") || in.equals("up") || in.equals("down"))
        {
            if (in.equalsIgnoreCase("up"))  
            { 
                rowNumber = rowNumber - 1; 
            } 

            if (in.equalsIgnoreCase("down")) 
            {           
                rowNumber = rowNumber + 1; 
            } 

            if (in.equalsIgnoreCase("left"))  
            {   
                columnNumber = columnNumber - 1;  
            } 

            if (in.equalsIgnoreCase("right"))  
            {   
                columnNumber = columnNumber + 1; 
            }
        }
        else
        {
            System.out.println("Input invalid! Please enter up, down, left, or right.");
        }

        try 
        {
            System.out.println(game[columnNumber][rowNumber].toString());
        }

        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println("You cannot leave the boardwalk during the hunt! Please start over.");
            System.exit(0);     
        }

        countMoves(in, moves);
        //countPoints(obstacleNumber, points);
    }        
}

    public static void printPlayerLocation(int numberOfRows, int numberOfColumns, int rowNumber, int columnNumber)
    {
        System.out.println();
        System.out.print("***** PLAYER LOCATION *****");
        String[][] game = new String[numberOfRows][numberOfColumns];
        for (int i = 0; i < numberOfRows; i++)
        {
            for (int j = 0; j < numberOfColumns; j++)
            {
                game[i][j] = "*";
            }   
        }

        game[rowNumber][columnNumber] = "P";

        for (int i = 0; i < numberOfRows; i++)
        {
            System.out.println();
            for (int j = 0; j < numberOfColumns; j++)
            {
                System.out.printf("%-5s", game[i][j]);
            }   
        }
        System.out.println();
    }

    //doesn't correctly count the moves
    public static void countMoves(String in, int moves)
    {
        if(in.equals("left") || in.equals("right") || in.equals("up") || in.equals("down"))
        {
            moves++;
        }
        System.out.println("You have used " + moves + " moves.");
    }

    //doesn't correctly count the points
    public static void countPoints(int obstacleNumber, int points)
    {
        if(obstacleNumber == 0)
        {
            points = points;
        }
        if(obstacleNumber == 1)
        {
            points++;
        }
        if(obstacleNumber == 2)
        {
            points = points + 2;
        }
        System.out.println("You have obtained " + points + " points so far. Keep hunting!");
    }

数据文件有数组的大小(4 4)和一个障碍物编号和障碍物

    0 this obstacleNumber would award 0 points
    1 this obstacleNumber would award 1 point
    2 this obstacleNumber would award 2 points

16 次填充数组。

我希望示例输出打印玩家位置(它正确),要求用户输入一个方向(它正确),打印数据文件中的文本(再次已经完成),然后打印已经使用的动作以及玩家到目前为止获得的点数。

您如何正确计算用户输入“ up, down, right, or left”的次数以及用户到目前为止获得了多少分?提前感谢您的帮助,我真诚地感谢您的宝贵时间。

4

3 回答 3

2

移动计数不正确,因为 countMoves 不会增加变量从主函数移动。如果你这样做:

System.out.println(moves);
countMoves(in, moves);
System.out.println(moves);

您会看到该值没有改变。因此,您可以向 countMoves 添加一个返回值并使用它进行 assingn 移动:

moves = countMoves(in,moves);

或者你可以在这里增加动作:

if(in.equals("left") || in.equals("right") || in.equals("up") || in.equals("down"))
    {
        moves++;
        if (in.equalsIgnoreCase("up"))  
        {

            rowNumber = rowNumber - 1; 
        } 

        if (in.equalsIgnoreCase("down")) 
        {           
            rowNumber = rowNumber + 1; 
        } 

        if (in.equalsIgnoreCase("left"))  
        {   columnNumber = columnNumber - 1;  
        } 

        if (in.equalsIgnoreCase("right"))  
        {   columnNumber = columnNumber + 1; 
        }
    }

我认为与积分相同。线

points = points;

只有当您有一个可以获取点值但分配一个具有自己值的变量的类变量才有意义时才有意义。

所以也许添加一个返回到 countPoints 并用它分配点:

points = countPoints(obstacleNumber, points);
于 2013-04-16T07:52:53.150 回答
1

在 Java 中,方法的参数总是 按值传递。但是,它可能会与对象混淆。但是对于像这样的原始类型,int它非常简单。对于函数countMovescountPoints您仅分别给出movesand的值points,而不是它们的引用。也就是说,这些方法实际上与另一个变量一起工作。该变量被初始化为您提供的值,您可以根据需要更改它,但对该变量所做的更改仅对方法可见。因此,为了使更改对外部变量可见,您必须重置它们的值。例如,您可以这样做:

public static int countMoves(String in, int moves) {
       //... change the value of moves as you want
       return moves; //return the new value
}

然后使用如下方法:

moves = countMoves(in, moves);

其中 set 变量是您在 main.xml 中定义的变量。类似地对于countPoints. 另一种可能性是在类中定义movesand并使方法方法直接修改这些变量而不传递它们,例如:pointsProgram12count

public static void countMoves(String in) {
    moves = ...
}

在这种情况下,moves定义的移动对Program12是可见的countMoves,因此您可以直接更改所需的变量;无需重置它。

--

不过大不过。您拥有的代码相当意大利面条。您应该考虑如何更好地将代码结构化和划分为密切相关的逻辑单元。在面向对象编程中,您使用类来完成。例如,您可以定义一个名为的类GameState来保存变量movespoints您需要的任何其他内容,并在其中定义count方法或其他方法来修改游戏的统计信息。不要让main方法定义程序的逻辑。它应该仅用于读取输入以初始化某种类Game并写入以输出Game.

于 2013-04-16T07:56:49.140 回答
1

我可能在这里错了(一大早......)但我猜你总是得到相同的动作积分值?这是因为您没有增加实际movepoints的值。当您将 Int 作为参数发送给方法时,您不会发送指向 Int 的指针,而是发送它的副本,该副本将由该方法使用,然后在离开它时将其删除。您需要在增加值后返回移动点数或将它们作为静态属性。尝试这样做:

...
moves = countMoves(String in, int moves);
...

public static int countMoves(String in, int moves) {
    if(in.equals("left") || in.equals("right") || in.equals("up") || in.equals("down")) {
        moves++;
    }
    System.out.println("You have used " + moves + " moves.");
    return moves;
}

或者您可以在识别移动方向时增加它们(这更有效,因为您不必重新检查移动是否有效):

if (in.equalsIgnoreCase("up")) { 
    rowNumber = rowNumber - 1; 
    moves++;
}
...

编辑
点问题:
由于您没有发布 Room 是如何实现的,我只是即兴创作,但我认为它应该看起来像这样:

...  
points = countPoints(game[rowNumber][columnNumber].getObstacleNumber(), points);
...

并将 countPoints() 更改为:

public static int countPoints(int obstacleNumber, int points) {
        if(obstacleNumber == 0) points = points;
        if(obstacleNumber == 1) points++;
        if(obstacleNumber == 2) points += 2;

        System.out.println("You have obtained " + points + " points so far. Keep hunting!");
        return points;
    }

或者只是(只要您知道输入是正确的):

public static int countPoints(int obstacleNumber, int points) {
        points += obstableNumber;
        System.out.println("You have obtained " + points + " points so far. Keep hunting!");
        return points;
    }
于 2013-04-16T07:48:22.403 回答