Auf Thema antworten

Mein Kopf raucht schon. Ich habe bis jetzt folgendes. Alles noch in einer Klasse aber kann mal jemand drüberschauen und sagen ob das so ok ist?

[code=Java]public class Spielfeld {

    public static Status spielfeld [][]=new Status[10][10];

   

    public static void neuesSpielfeld(){

        for (int i = 0; i < spielfeld.length; i++) {

            for (int j = 0; j < spielfeld.length; j++) {

                spielfeld[i][j]=Status.WASSER_FREI;

            }

        }

    }

   


    public static boolean kannSetzen(int typ, int xkor, int ykor, int richtung){

        //Diesen Teil hab ich zwar schon ist aber 30Zeilen lang und funktioniert nicht Richtig

    }

   

   

    public static void setzeSchiff (int typ, int xkor, int ykor, int richtung){

        if(richtung==1){

            for (int i = xkor; i < xkor+typ; i++) {

                spielfeld[i][ykor]=Status.SCHIFF_FREI;

            }

        }

        else{

            for (int i = ykor; i < ykor+typ; i++) {

                spielfeld[xkor][i]=Status.SCHIFF_FREI;

            }

        }

       

    }

     public static void Spielfeldausgabe(){

            int zeile=1;

            System.out.println("    A  B  C  D  E  F  G  H  I  J");

            for (int i = 0; i < spielfeld.length; i++) {

                if(zeile==10){

                     System.out.print(""+zeile+" ");

                }

                else

                    System.out.print(""+zeile+"  ");

                zeile++;

                for (int j = 0; j < spielfeld.length; j++) {

                    System.out.print(spielfeld[i][j]);

                }

                System.out.println("");

            }

            }

     public static int xKor(String c){

         int x = -1;

            if(c.equals("A"))

                      x=0;

            else if(c.equals("B"))

                      x=1;

            else if(c.equals("C"))

                      x=2;

            else if(c.equals("D"))

                      x=3;

            else if(c.equals("E"))

                      x=4;

            else if(c.equals("F"))

                      x=5;

            else if(c.equals("G"))

                      x=6;

            else if(c.equals("H"))

                      x=7;

            else if(c.equals("I"))

                      x=8;

            else if(c.equals("J"))

                      x=9;

            else

                      System.out.println("Kein Passender Buchstabe eingegeben");

             return x;

       }


     public static void setzeSchiffSpielfeld(int a){

         String xkoordinate;

            int ykoordinate;

            boolean test=true;

            while(test){

            int x=-1;

            int y=-1;

            int richtung=-1;

            System.out.println("Setzen Sie ihr "+a+"er Schiff und geben Sie hierfür zuerst die Anfangskoodinate ein:");

            while(x==-1){

            System.out.println("Buchstabe:");

            xkoordinate=Input.in.readString();

            x = xKor(xkoordinate);

            }

            while(y==-1){

            System.out.println("Zahl:");

            ykoordinate= Input.in.readInteger();

            if(ykoordinate>=1&&ykoordinate<=10)

                y=ykoordinate-1;

            else

                System.out.println("Keine gültige Zahle eingegeben");

            }

            while(richtung!=0&&richtung!=1){   

            System.out.println("In welche Richtung wollen sie ihr Schiff bauen? Vertikal(=0) oder Horizontal(=1)");

            richtung =Input.in.readInteger();

            }

            if(kannSetzen(a,x,y,richtung)==true){

                setzeSchiff(a,x,y,richtung);

                test=false;

            }

            else

                System.err.println("Schiff kann nicht gesetzt werden!");

            }

     }

    

    public static void main(String[]args){

        neuesSpielfeld();

        setzeSchiffSpielfeld(5);

        Spielfeldausgabe();

        for (int i = 0; i < 2; i++) {

            setzeSchiffSpielfeld(4);

        }

        Spielfeldausgabe();

        for (int i = 0; i < 3; i++) {

            setzeSchiffSpielfeld(3);

        }

        Spielfeldausgabe();

        for (int i = 0; i < 4; i++) {

            setzeSchiffSpielfeld(2);

        }

        Spielfeldausgabe();

    }


}


[/code]



Oben