java - Java中的计数器循环

标签 java loops methods counter

我创建了一个游戏,玩家可以在游戏板上收集 pod 。我需要进行规避操作,以便在 5 个回合后,当玩家距 pods 1 个空格时, pods 将传送至距玩家 10 个空格的位置。我相信我的计数器不起作用,这就是规避机动不起作用的原因。

public class Pod {


    int podPositionX;
    int podPositionY;
    String podDirection;
    int layoutWidth; 
    int layoutHeight;
    boolean podVisable = true; 
    int playerX; 
    int playerY;
    int count = 0;

    public Pod(int x, int y, String direction, int width, int height){

        podPositionX = x;
        podPositionY = y;
        podDirection = direction;
        layoutWidth = width; 
        layoutHeight = height;
        count=count+1;

    }   


    //Inspector Methods?
    // Will get the the pods positon and will return it
    public int getX()
    {
             return podPositionX;
    }

    //This method returns the current Y coordinate of the pod.
    public int getY() 
    {
        return podPositionY;
    }


    //This method returns true if the pod is visible, false otherwise. Once the 
    //pod has been caught, it should always return false.
    public boolean isVisible(){

        if (playerX == podPositionX && playerY == podPositionY && podVisable == true){
            podVisable = false;}
        return podVisable; 

    }


    // to move pod diagonally across screen//

    public void move(){
        //Calling invasive methods!!
        while  (count>5 && count < 100){
        podPositionX=transportX();
        podPositionY=transportY();
        }

        /****************** To make pod move **************/
        if (podDirection.equals("NW")|| podDirection.equals("NE")){
            podPositionY = podPositionY + 1;}

        if (podDirection.equals("SW")|| podDirection.equals("SE")){ 
            podPositionY = podPositionY-1;}

        if (podDirection.equals("NE")|| podDirection.equals("SE")){
            podPositionX = podPositionX+1;}

        if(podDirection.equals("NW")|| podDirection.equals("SW")){
           podPositionX = podPositionX-1;}

        /****************To make Pod bounce off wall******************/

        //make pod bounce off left wall
        if (podPositionX <= 1){
            if (podDirection.equals("SW")){
                podDirection = "SE";}

            if (podDirection.equals("NW")){
                podDirection = "NE";}
        }
        //make pod bounce off top
        if (podPositionY >= layoutHeight-1){
            if (podDirection.equals("NW")){
                podDirection = "SW";}

            if (podDirection.equals("NE")){
                podDirection = "SE";}
        }

        //make pod bounce off right wall
        if (podPositionX >= layoutWidth-1){
            if (podDirection.equals("NE")){
                podDirection = "NW";}

            if (podDirection.equals("SE" )){
                podDirection = "SW";}
        }

        //make pod bounce off bottom wall
        if (podPositionY <= 1){

            if (podDirection.equals("SW")){
                podDirection = "NW";}

            if (podDirection.equals("SE")){
                podDirection = "NE";}
            }

        }



    // to get player x and y positions
    public void playerAt(int x, int y){

        playerX = x; 
        playerY = y;




}
    //envasive maneuver so that after 5 turns the pod can be transported away from the player if it is 1 spot away from the player.
    //then the count is set to 100 so it will exit the loop and not be able to transport the pod again. 
    public int transportX(){


            if (podPositionX == playerX -1 || podPositionX == playerX +1){
                podPositionX= playerX +10;
                count = 100;}

            return podPositionX;
    }
    public int transportY(){
        if  (podPositionY == playerY -1 || podPositionY == playerY +1){
                podPositionY= playerY +10;
                count=100;}
        return podPositionY;
    }

}

我的老师给我们提供的代码。无法触摸,因此我无法在此文件中放置计数器。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Project1 extends JFrame implements ActionListener {

    // Member variables for visual objects.
    private JLabel[][] board; // 2D array of labels. Displays either # for player,
                              // * for pod, or empty space
    private JButton northButton, // player presses to move up
                    southButton, // player presses to move down
                    eastButton,  // player presses to move right
                    westButton;  // player presses to move left

    // Current width and height of board (will make static later).
    private int width = 15;
    private int height = 9;

    // Current location of player
    private int playerX = 7;
    private int playerY = 4;

    // Pod object stored in array for efficiency
    private Pod[] pods;
    int podCount = 4;

    public Project1() {

        // Construct a panel to put the board on and another for the buttons
        JPanel boardPanel = new JPanel(new GridLayout(height, width));
        JPanel buttonPanel = new JPanel(new GridLayout(1, 4));

        // Use a loop to construct the array of labels, adding each to the
        // board panel as it is constructed. Note that we create this in
        // "row major" fashion by making the y-coordinate the major 
        // coordinate. We also make sure that increasing y means going "up"
        // by building the rows in revers order.
        board = new JLabel[height][width];
        for (int y = height-1; y >= 0; y--) {
            for (int x = 0; x < width; x++) {

                // Construct a label to represent the tile at (x, y)
                board[y][x] = new JLabel(" ", JLabel.CENTER);

                // Add it to the 2D array of labels representing the visible board
                boardPanel.add(board[y][x]);
            }
        }

        // Construct the buttons, register to listen for their events,
        // and add them to the button panel
        northButton = new JButton("N");
        southButton = new JButton("S");
        eastButton = new JButton("E");
        westButton = new JButton("W");

        // Listen for events on each button
        northButton.addActionListener(this);
        southButton.addActionListener(this);
        eastButton.addActionListener(this);
        westButton.addActionListener(this);

        // Add each to the panel of buttons
        buttonPanel.add(northButton); 
        buttonPanel.add(southButton); 
        buttonPanel.add(eastButton); 
        buttonPanel.add(westButton);

        // Add everything to a main panel attached to the content pane
        JPanel mainPanel = new JPanel(new BorderLayout());
        getContentPane().add(mainPanel);
        mainPanel.add(boardPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // Size the app and make it visible
        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Auxiliary method to create game setup
        createGame();
    }

    // Auxiliary method used to create board. Sets player, treasure, and walls.
    public void createGame() {

        // Construct array of Pod objects
        pods = new Pod[podCount];

        // Construct each Pod in the array, passing it its initial location,
        // direction of movement, and the width and heigh of board. This will
        // later be modified to be done at random.
        pods[0] = new Pod(1, 5, "NE", width, height);
        pods[1] = new Pod(2, 1, "SW", width, height);
        pods[2] = new Pod(12, 2, "NW", width, height);
        pods[3] = new Pod(13, 6, "SE", width, height);

        // Call method to draw board
        drawBoard();

    }

    // Auxiliary method to display player and pods in labels.
    public void drawBoard() {

        // "Erase" previous board by writing " " in each label
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                board[y][x].setText(" ");
            }
        }

        // Get location of each pod and write * into that label. We only
        // do this for pods not yet caught.
        for (int p = 0; p < podCount; p++) {
            if (pods[p].isVisible()) {
                board[pods[p].getY()][pods[p].getX()].setText("*");
            }
        }

        // Write the player onto the board.
        board[playerY][playerX].setText("#");
    }


    public void actionPerformed(ActionEvent e) {

        // Determine which button was pressed, and move player in that
        // direction (making sure they don't leave the board).
        if (e.getSource() == southButton && playerY > 0) {
            playerY--;
        }
        if (e.getSource() == northButton && playerY < height-1) {
            playerY++;
        }
        if (e.getSource() == eastButton && playerX < width-1) {
            playerX++;
        }
        if (e.getSource() == westButton && playerX > 0) {
            playerX--;
        }

        // Move the pods and notify the pods about player location.        
        for (int p = 0; p < podCount; p++) {
            pods[p].move();
            pods[p].playerAt(playerX, playerY);
        }

        // Redraw the board
        drawBoard();

    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Project1 a = new Project1();
    }
}

最佳答案

我现在正在快速查看这个。你说问题出在 count 变量上。我看到你在构造函数中增加它,即

public Pod(int, int, String, int, int){
count = count + 1;
}

为什么不把它放在move方法中呢。这样,每次使用 move 方法时,count 变量都会增加 1。

public void move(){
this.count++;
}

此外,很难准确地了解您认为代码在做什么。我尝试运行它,但什么也没发生。有时,在控制台上打印一些东西是很好的。

关于java - Java中的计数器循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32999287/

相关文章:

java - 二元决策图

java - Given, when, then约定和异常处理。使用 Mockito 和 JUnit

java.rmi.server.codebase 在 linux 上不工作

java - 我在 ojdbc8 中找不到 OracleCallablestatement。是不是被替换了?如果是的话,用什么?

c++ - 从出现在该值之前的 vector<pair> 添加第二个值

java - 对实例变量、创建新对象和方法感到困惑吗?

c - 最多20个30位数字的乘法

java - 遍历不同长度的并行数组

c# - 试图让 View 记住 ClientID

python - 在调用类实现的实例中调用方法 - 但在其他方面不相关