繁体   English   中英

使用 OOP 在 4x4 网格中打印/更新对象

[英]Printing/Updating Objects within a 4x4 Grid Using OOP

请耐心等待,这门语言不是很好,而且我对编程非常陌生。

我打算使用 Eclipse 在 Java 中创建一个游戏,该游戏使用 OOP 创建一个 4x4 网格。

我将使用 Random Num Gen 和打印将对象随机放置在网格的坐标中,以便用户可以在每个回合中看到每个单元格中的内容。 网格将随着对象每回合分配新的随机位置而更新。 最终产品应如下所示: Printed Grid

class 网格

package grid;

import java.util.ArrayList;


public class Grid {

//variables & constructors//
private ArrayList <Row> theRows = new ArrayList <Row> ();
private final int NUMBER_OF_ROWS = 4;

public Grid() {
    for(int loop =1; loop <= this.NUMBER_OF_ROWS; loop++) {
        this.theRows.add(new Row(loop));
    }//end for loop
}//end Grid constructor



//use to find specific spot, check for enemies,etc
public void printSquare(int row, int square) {
    
    
}
        

//Find out if Ogre is on Square
public boolean isOgreOn (int row, int square) {
    
    for(Row tempRow : this.theRows) {
        if(tempRow.getNumber() == row) {
            //found correct row
            for(Square tempSquare : tempRow.getTheSquares()) {
                if(tempSquare.getNumber() == square){       
                    //found the correct square
                    if(tempSquare.getTheOgre() == null) {
                        //The ogre is not here
                        return false;
                    }
                }
            }
        }
    }
return true;
}// end isOgreOn method

//Find out how many enemies on Square
public int howManyOgreEnemiesOn(int row, int square) {
    for(Row tempRow : this.theRows) {
        if(tempRow.getNumber() == row) {
            //found correct row
            for(Square tempSquare : tempRow.getTheSquares()) {
                if(tempSquare.getNumber() == square) {
                    //found correct square
                    return tempSquare.getTheEnemies().size();
                }
            }
        }
    }
return 0;


}//end howManyOgreEnemiesOn

}//End Grid Class

class 行

   package grid;

    import java.util.ArrayList;

    public class Row {
    
    //variables getter & setters & constructor//

    private int number;
    private final int ROW_SIZE = 4;
    private ArrayList <Square> theSquares = new ArrayList <Square> ();
    
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }


    public ArrayList<Square> getTheSquares() {
        return theSquares;
    }

    public void setTheSquares(ArrayList<Square> theSquares) {
        this.theSquares = theSquares;
    }

    public Row(int number) {
        for(int loop = 1; loop <= this.ROW_SIZE; loop++) {
            this.theSquares.add(new Square(loop));
        }//end for loop
    }//end Row constructor
    
    }//end Row class

class 广场

import java.util.ArrayList;

public class Square {

    //variables getter & setters & constructor//
    int number;
    Ogre theOgre;
    ArrayList<OgreEnemy> theEnemies = new ArrayList<OgreEnemy>();


    public Square(int number) {
        setNumber(number);
    }

    public ArrayList<OgreEnemy> getTheEnemies() {
        return theEnemies;
    }

    public void setTheEnemies(ArrayList<OgreEnemy> theEnemies) {
        this.theEnemies = theEnemies;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Ogre getTheOgre() {
        return theOgre;
    }

    public void setTheOgre(Ogre theOgre) {
        this.theOgre = theOgre;
    }


}//end Square class

我将制作一个 GUI 初始化游戏,但其中没有游戏逻辑 go。

那么我的问题是 go 关于如上图所示打印坐标的最佳方法是什么?

  • 我应该制作一个包含所有坐标/点的新列表吗?
  • 并且每轮更新 map 以显示单元格的坐标,或者如果被占用,则 object 占用单元格?
  • 我觉得好像二维数组会是更好的选择,但我们被指示以这种方式开始网格构建。

显然,还有更多的代码/类,但我最关心的是在坐标内显示坐标/对象的选项。

您不需要二维数组

isOgreOn方法可以更改为此,但要注意出界异常的索引

  //Find out if Ogre is on Square
    public boolean isOgreOn(int row, int square) {
        return this.theRows.get(row).getTheSquares().get(square).theOgre != null;
    }// end isOgreOn method

howManyOgreEnemiesOn方法也是如此

  //Find out how many enemies on Square
    public int howManyOgreEnemiesOn(int row, int square) {
        return this.theRows.get(row).getTheSquares().get(square).getTheEnemies().size();

    }//end howManyOgreEnemiesOn

暂无
暂无

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

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