#Beginner. Need advice on how to structure and where to put functions

1 messages · Page 1 of 1 (latest)

indigo iron
#

SORRY FOR THE WALL OF TEXT, but i have no idea how ask in a shorter way.

Hi,
i'm pretty new to Java and want to program the game Battleships object orientated. At first for the Terminal, but i want to keep it in a way to be able to add a GUI (JavaFX) later to it.

Now i run in a general planning issue with my functions. How much should they do and should they call each other or should a controller handle it.

Here is an example for the game battleships.

I have a class Ship, a class Tile (which is one tile of the grid) and a Controller.

If a tile gets shoot, should:

  1. should i write custom functions that make sure, everything is handled the right way themselves?
  2. or the game controller handle everything with getter and setter methods ( and pretty much ignore relationships in the class diagram)

for example:
a tile gets shoot at and these things need to be done:

  1. Check if a ship is on the tile
  2. if there is no shipon the tilejust change the status to MISSED -> DONE, leave the function
  3. if there is a ship on the tile, change the status to HIT
  4. then check if the shipwas hit on all its parts and should sink now.
  5. If it should sink, change status of shipto SUNK

Again should a GameController handle all these steps or should a method in Tile maybe even the method from 1. call a method in Shipto check if it should sink, than sink it and than set itself to SUNK?

oh, i hope people can understand what i mean.

Ill write an example to make my question clear, just need some time.

blazing treeBOT
#

<@&987246399047479336> please have a look, thanks.

indigo iron
#

Sot this is my first example.
The Controller just calls
tile.getShoot

#

package model;

public class Tile {
    private TileStatus status = TileStatus.UNGUESSED;
    private Ship ship;
    
    public Tile() {
        this.status = TileStatus.UNGUESSED;
    }
    
    public void getShoot() {
        if (ship == null) {
            status = TileStatus.MISSED;
        } 
        else {
            status = TileStatus.HIT;
            if( ship.determineSinking()) {
                ship.sink();
            }
        }
    }    
    
    public void setStatus(TileStatus status) {
        this.status = status;
    }

    public TileStatus getStatus() {
        return status;
    }

    public boolean hasShip() {
        return ship != null;
    }
    
    public void setShip(Ship ship) {
        this.ship = ship;
    }
}
#
package model;

public class Ship {
    
    private ShipStatus status = ShipStatus.NOTSUNK;
    private Tile[] tiles;

    
    public Ship( Orientation orientation, Tile[] tiles) {        
        this.tiles = tiles;
    }
    
    public boolean determineSinking() {
        boolean sinking = true;
        
        for (Tile tile : tiles) {
            if (tile.getStatus() == TileStatus.UNGUESSED) {
                sinking = false;
                break;
            }
        }        
        return sinking;
    }
    
    public void sink() {
        
        for (Tile tile : tiles) {
            tile.setStatus(TileStatus.SUNK);
            status = ShipStatus.SUNK;
        }    
    }
    
    public ShipStatus getStatus() {
        return status;
    }
}```
#

SECOND EXAMPLE
Here i try to make it more Controller heavy

#

Call from the controller shoot(tile);

Function is in the Controller:

    public static void shoot(Tile tile) {
        if(tile.hasShip()) {
            tile.setStatus(TileStatus.HIT);
            
            if( tile.determineSinkingShip()) {
                tile.sinkShip();
            }
        }
        else {
            tile.setStatus(TileStatus.MISSED);
        }     
    }
#

So i would remove this part in Tile

    public void getShoot() {
        if (ship == null) {
            status = TileStatus.MISSED;
        } 
        else {
            status = TileStatus.HIT;
            if( ship.determineSinking()) {
                ship.sink();
            }
        }
    } 

and replace with a passthrough function (no idea if thats a thing)

    public boolean determineSinkingShip() {
        return ship.determineSinking();
    }

    public void sinkShip() {
        ship.sink();
    }

Full Tilein the next msg.

indigo iron
#
    package model;

    public class Tile {
    private TileStatus status = TileStatus.UNGUESSED;
    private Ship ship;
    
    public Tile() {
        this.status = TileStatus.UNGUESSED;
    }
    

    public boolean determineSinkingShip() {
        return ship.determineSinking();
    }
    
    public void sinkShip() {
        ship.sink();
    }
    
    public void setStatus(TileStatus status) {
        this.status = status;
    }

    public TileStatus getStatus() {
        return status;
    }

    public boolean hasShip() {
        return ship != null;
    }
    
    public void setShip(Ship ship) {
        this.ship = ship;
    }
}
indigo iron
#

Now the shipstill changes the status of its tilesif it's sunk. Should that part also be in the shoot fucntion of the Controller? Or should it be a seperate function? And if so should it be in the Controller of in the Tilesor even Shipclass?

Also how would i call it? Something like:

Tiles[] sinkingTiles = tiles.getTilesOfShip();
for (Tile tile : sinkingTiles){
  tile.setStatus(TileStatus.SUNK);
}```

maybe?