Fun Stuff > CLIKC

A programming thread!

<< < (25/37) > >>

ankhtahr:
You could find them here together with the original task PDF and the checkstyle xml.

Masterpiece:
ugh I hate the way they wrote the shiptype enum

I mean, it does what it has to, but so sloppily. You could write the same class like this:


--- Code: ---enum ShipType{
MINENSUCHER(1),
SCHLACHTSCHIFF(2),
GROSSKAMPSCHIFF(3),
FLUGZEUGTRAEGER(4);

public final int length;

private ShipType(int length){
this.length = length;
}

public int getLength(){
return this.length;
}
}
--- End code ---

which has a lot less redundant code.

Masterpiece:
There you go! I haven't checked if it's 100% correct, but if you're struck this should give you a good starting point. Have fun!


--- Code: ---package battleship;

public class Sea {

/**
* A representation of the ships in the coordinate field.
*/
private int[][] shipField;
/**
* A representation of the bomb-runs in the coordinate field.
*/
private String[][] bombField;
/**
* The width of the coordinate field.
*/
private int width;
/**
* The height of the coordinate field.
*/
private int height;

public Sea(int width, int height){

this.width = width;
this.height = height;

this.shipField = new int[width][height];
this.bombField = new String[width][height];

for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
this.shipField[x][y] = 0;
this.bombField[x][y] = null;
}
}
}

/**
* Adds a ship to the field. Only works if the ship fits in the field,
* the field is not occupied by another ship, and no bombs
* have been thrown yet.
* @param type The ship type.
* @param dir The direction the ship is facing.
* @param x The x - coordinate of the ship-rear
* @param y The y - coordinate of the ship-rear
* @return Returns true, if the ship could be placed.
*/
public boolean addShip(ShipType type, Direction dir, int x, int y){

// Checking if the ship rear is within coordinate field.
if (x < width && x >= 0 && y < height && y >= 0){

// Checking if no bombs have been thrown yet.
for(int n = 0; n < width; n++){
for(int m = 0; m < height; m++){
if(this.bombField[n][m] != null){
return false;
}
}
}

// Getting the ship length.
int shipLength = type.getLength();
// A helper variable used in the for-loops.
int iterator;

switch(dir){
case NORTH:
// Checking if the ship fits in the coordinate field.
if((y - shipLength) >= 0){
// Checking if no other ship occupies space.
for(iterator = 0; iterator < shipLength; iterator++){
if(this.shipField[x][y - iterator] != 0){
return false;
}
}
// Placing ship.
for(iterator = 0; iterator < shipLength; iterator++){
this.shipField[x][y - iterator] = type.getLength();
}
return true;
}else return false;
// Rinse and repeat for every direction.
case EAST:
if((x + shipLength) < this.width){
for(iterator = 0; iterator < shipLength; iterator ++){
if(this.shipField[x + iterator][y] != 0){
return false;
}
}
for(iterator = 0; iterator < shipLength; iterator ++){
this.shipField[x + iterator][y] = type.getLength();
}
return true;
}else return false;
case SOUTH:
if((y + shipLength) < this.height){
for(iterator = 0; iterator < shipLength; iterator++){
if(this.shipField[x][y + iterator] != 0){
return false;
}
}
for(iterator = 0; iterator < shipLength; iterator++){
this.shipField[x][y + iterator] = type.getLength();
}
return true;
}else return false;
case WEST:
if((x - shipLength) >= 0){
for(iterator = 0; iterator < shipLength; iterator ++){
if(this.shipField[x - iterator][y] != 0){
return false;
}
}
for(iterator = 0; iterator < shipLength; iterator ++){
this.shipField[x - iterator][y] = type.getLength();
}
return true;
}else return false;
default:
return false;
}
}else return false;

}

/**
* Places a bomb on the field. Sets {@link #bombField} to X or O,
* depending on whether a boat is hit.
* @param x X-Coordinate of the bomb hit.
* @param y Y-Coordinate of the bomb hit.
* @return Returns true if a ship has been hit.
*/
public boolean dropBomb(int x, int y){
// Checks if the field is occupied, and if it is, lays an X.
if(this.shipField[x][y] != 0){
this.bombField[x][y] = "X";
return true;
}else{
this.bombField[x][y] = "O";
return false;
}

}

/**
* A method that returns true if all the ships that were
* placed in the field were sunk.
* @return True if all ships are down.
*/
public boolean allShipsSunk(){

/* Helper Variables. Ship counter counts
* every cell with a ship and sunk counter
* counts every cell with a hit mark.
*/
int shipCounter = 0, sunkCounter = 0;

// Iterate over field
for(int x = 0; x < this.width; x ++){
for(int y = 0; y < this.height; y ++){
// If cell contains ship
if(this.shipField[x][y] != 0){
// Increment shipCounter
shipCounter ++;
// If cell contains hit marker
if(this.bombField[x][y].equals("X")){
// Increment sunkCounter
sunkCounter ++;
}
}
}
}

// If shipcounter and sunkcounter are the same,
// every ship has been sunk.
if(shipCounter == sunkCounter){
return true;
}else return false;
}

public String toStringWithShips(){
String stringWithShips = "";
String concat;

for(int y = 0; y < this.height; y ++){
for(int x = 0; x < this.width; x ++){
if(this.shipField[x][y] == 0){
concat = ".";
}else{
concat = String.valueOf(this.shipField[x][y]);
}
stringWithShips.concat(concat);
}
stringWithShips.concat("\n");
}
return stringWithShips;
}

public String toStringWithBombs(){
String stringWithBombs = "";
String concat;

for(int y = 0; y < this.height; y ++){
for(int x = 0; x < this.width; x ++){
if(this.bombField[x][y].equals(null)){
concat =  ".";
}else{
concat = this.bombField[x][y];
}
stringWithBombs.concat(concat);
}
stringWithBombs.concat("\n");
}
return stringWithBombs;
}

}
--- End code ---

ankhtahr:
Wow.

It's a very different approach to mine (and so incredibly much shorter) but it looks great! It'll be a great help if I run into trouble! Thanks a lot!

If we ever meet I owe you at least one beer! And I want us to meet one day!

Masterpiece:
This is short? I hate the way I wrote "addShip" but I can't think of a better way of doing it (but seriously, so much redundant code, SO MANY ITERATIONS OVER THE FIELD :'( )

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version