繁体   English   中英

更新2D Java数组中的对象

[英]Update Object in 2D Java Array

我目前在下面有以下代码,我想用X更新2D阵列中的特定位置,然后重新打印电路板。 我目前被困在需要将数组中的对象更新为X的位置,但我不断得到:

Type mismatch: cannot convert from String to Ship

我究竟做错了什么? 我已经将所有对象放在板上。

public void updateBoard(int row, int column, Ocean ocean){
    //get ship array
    Ship[][] sea = ocean.getShipArray();
    //mark the x/y co-ordinate with X 
    sea[row][column] = "X"; // PROBLEM  
    // print the board
}

我已经包括了船级

public abstract class Ship {
// TODO add appropriate comments
@Getter
private int size;
private String type;
private String shortForm;

// TODO add appropriate comments
@Getter
@Setter(AccessLevel.PACKAGE)
private int bowRow;
@Getter
@Setter(AccessLevel.PACKAGE)
private int bowColumn;
@Getter
@Setter(AccessLevel.PACKAGE)
private boolean horizontal;

/**
 * An array of boolean which indicates whether that part of the ship has
 * been hit. This is initialised by the appropriate sub-class. Battleships
 * use all 4 locations; cruisers use the first 3; destroyers 2; submarines
 * 1; and "empty sea" 1.
 */
protected boolean[] hit;


/**
 * clears the hit array indicating whether that part of the "Ship" has been
 * hit
 */
protected Ship(int size, String type, String shortForm) {
    this.size = size;
    this.setType(type);
    this.shortForm = shortForm;
    hit = new boolean[size];
    for (int i = 0; i < hit.length; i++)
        hit[i] = false;
}

/**
 * Checks that ship of this size will not overlap another ship, or touch
 * another ship (vertically, horizontally, or diagonally) and that ship will
 * not "stick out" beyond the array.
 * 
 * @param row
 *            that will contain the bow
 * @param column
 *            that will contain the bow
 * @param horizontal = true if horizontal
 * @param ocean
 * @return true if it is okay to put a ship of this size with its bow in
 *         this location, with the given orientation.
 */
public boolean okToPlaceShipAt(int row, int column, boolean horizontal,
        Ocean ocean) {
    // Try to catch exception error if ship goes past board
    try{
        Ship ships[][] = ocean.getShipArray();

        for (int i = 0; i < getSize(); i++){
        if (!(ships[row][column] instanceof EmptySea)){ 
        //  System.out.println("Cant Print here + " + row + column + getSize());
                return false;
        }else{
            if (horizontal) {
                column++; 
            } else {
                row++;  
            }
        //  System.out.print(ships[row][column]);

            }
    //  System.out.println(" ");

        }
        return true;
    }
    catch(Exception err){
    //  System.out.println("OMG an error");
        return false;
    } 



}

/**
 * "places" the ship in the ocean, assigning values to the bowRow,
 * bowColumn, and horizontal. Places a reference to the ship in the ships
 * array in the Ocean object.
 * 
 * @param row
 *            to contain the bow
 * @param column
 *            to contain the bow
 * @param horizontal
 * @param ocean
 */
public void placeShipAt(int row, int column, boolean horizontal, Ocean ocean) {

    this.setBowRow(row);
    this.setBowColumn(column);
    this.setHorizontal(horizontal);

    Ship ships[][] = ocean.getShipArray();

    for (int i = 0; i < getSize(); i++) {
        // set position in array to contain the ship
        ships[row][column] = this;
        if (horizontal) {
            column++;
        } else {
            row++;
        }
    }
}

private int getSize() {
    // TODO Auto-generated method stub
    return size;
}

private void setHorizontal(boolean horizontal2) {
    // TODO Auto-generated method stub
    horizontal = horizontal2;
}

private void setBowColumn(int column) {
    // TODO Auto-generated method stub
    bowColumn = column;
}

private void setBowRow(int row) {
    // TODO Auto-generated method stub
    bowRow = row;
}

/**
 * If this ship has been hit, marks that part of the ship as "hit"
 * 
 * @param row
 *            User's supplied row shot
 * @param column
 *            User's supplied column shot
 * @return true if ship is hit, false otherwise
 */
public boolean shootAt(int row, int column) {
    if ((isHorizontal() && (row != getBowRow()))
            || (!isHorizontal() && (column != getBowColumn())))
        return false; // it's not a hit

    // it's a hit. Work out offset & set that position in hit array to true
    hit[(row - getBowRow() + column - getBowColumn())] = true;

            return true;
}

private int getBowRow() {
    // TODO Auto-generated method stub
    return bowRow;
}

private int getBowColumn() {
    // TODO Auto-generated method stub
    return bowColumn;
}

private boolean isHorizontal() {
    // TODO Auto-generated method stub
    return false;
}

/**
 * checks whether this ship is sunk - using the hit array
 * 
 * @return true if every part of the ship has been hit, false otherwise.
 */
public boolean isSunk() {

    for (boolean b : hit)
        if (!b)
            return false;

    return true;
}


/**
 * @return a single character String to use in Ocean's print method
 */
@Override
public String toString() {
    return shortForm;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}
public String setHit(String hit){
    return hit;
}

}

有点长 如果是扩展“潜艇”级潜艇的战舰,以下是其中的一个示例。 除大小外,所有其他均完全相同。

/*
 * A Battleship class which extends ship
 * Four Submarines in the game Length 1
 */
package Battleships;
public class Submarine extends Ship {
    private final static int SIZE = 1;
    /**
     * sets the length & clears the hit array
     */
    public Submarine() {
        super(SIZE, "Submarine", "S");
    }
}

seaShip[][]"X"String 您不能将String放置在保存Ship的变量中。

编辑:

由于您尝试射击一艘船,而不是放置一艘新船,我怀疑您真正需要的是使用船上的以下功能...

public boolean shootAt(int row, int column)

从Ship的代码中,我猜您实际上需要做的是...

sea[row][column].shootAt(row,column);

只需创建一个特殊飞船,并使其代表“ X”即可。 根据您更新的问题,您可以如下创建新船:

sea[row][column] = new Ship(1,"Cthulu","X");

现在,每当您打印Ship时,您都会看到一个'X',因为Ship类的toString()方法返回shortForm字符串值。

你也可以用自己的toString()方法,以延长Ship类有一个真正不同类型船舶的一个表示X.

暂无
暂无

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

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