2016-05-16 8 views
-3

Sie müssen zwei separate Programme erstellen, ein Hauptprogramm und die Battleship-Klasse. Jemand anderes bekommt das Hauptprogramm, um den Benutzer zu bitten, eine Koordinate zu erraten, Sie können einfach die gesetzten Variablen eingeben, um so zu tun, als ob der Benutzer rät.Wie suche ich ein 2D-Array nach einem Element?

public class MainMethod {

public static void main(String[] args) { 

    Battleship_BraydenH_R1 var = new Battleship_BraydenH_R1(); 

    //initialize variables 
    int[][] LAYOUT = new int[7][7]; 

    int ROW = 1, COLUMN = 2; 
    Battleship_BraydenH_R1.hitDetect(ROW, COLUMN); 

} 

}

public class Battleship_BraydenH_R1 {

/** 
*Program Header 
*Program Name: Battleship 
*Program Description: This class tells the main method whether or not a ship has been hit or whether or not a ship has been sunk. 
*Program Creator: Brayden H 
*Revision 1 
*Date: May 9, 2016 
*/ 

public static void batClass() 
{ 
    // initialize variables 
    int[][] LAYOUT = { 
     {0,0,0,1,0,0,0}, 
     {0,0,1,0,0,0,0}, 
     {0,1,0,1,0,0,1}, 
     {1,0,0,1,0,1,0}, 
     {0,0,0,1,1,0,0}, 
     {0,0,0,1,0,0,0}, 
     {0,0,1,0,0,0,0}, 
    }; // layout array 

} // batClass 

public static boolean[] hitDetect (int X, int Y) 
{ 
    int[] counter = new int[3]; // counter array 
    //counter[0] = boat[0]; 
    boolean HIT = false, SUNK = false; // sets 'HIT' and 'SUNK' booleans to false 
    boolean[] STATUS = new boolean [2]; // array with 2 elements to be able to return 'HIT' and 'SUNK' 
    STATUS[0] = false; // MISS 
    STATUS[1] = true; // HIT 

    // if the user guessed the correct coordinates for boat 1 
    if ((X == 3 && Y == 4)||(X == 4 && Y == 4)||(X == 5 && Y == 4)) 
    { 
     STATUS[1] = true; // the boat was hit 
     X = 4; // turns 1 into 4 so the program knows when a ship is sunk 
     Y = 4; // turns 1 into 4 so the program knows when a ship is sunk 
    } // boat 1 

    // if the user guessed the correct coordinates for boat 2 
    else if ((X == 1 && Y == 4)||(X == 2 && Y == 3)||(X == 3 && Y == 2)||(X == 4 && Y == 1)) 
    { 
     STATUS[1] = true; // the boat was hit 
     X = 4; // turns 1 into 4 so the program knows when a ship is sunk 
     Y = 4; // turns 1 into 4 so the program knows when a ship is sunk 
    } // boat 2 

    // if the user guessed the correct coordinates for boat 3 
    else if ((X == 3 && Y == 7)||(X == 4 && Y == 6)||(X == 5 && Y == 5)||(X == 6 && Y == 4)||(X == 7 && Y == 3)) 
    { 
     STATUS[1] = true; // the boat was hit 
     X = 4; // turns 1 into 4 so the program knows when a ship is sunk 
     Y = 4; // turns 1 into 4 so the program knows when a ship is sunk 
    } // boat 3 

    // if a boat was not hit 
    else 
    { 
     STATUS[1] = false; // the boat was not hit 
    } 

    (THIS IS WHERE I WANT THE PROGRAM TO SEARCH THE LAYOUT) 

    // searches layout 
    double a[][]=new double[7][7]; 
    for(int row = 0 ; row < 3 ; row++) 
    { 
     for(int col = 0 ; col < 7 ; col++) 
     { 

     } 
    } 


    return STATUS; // returns 'STATUS' to main method 
} // hitDetect method 

} // Battleship Klasse

+1

Bitte einen Text zu Ihrer Frage hinzufügen. Erklären Sie Ihr Problem auf eine leicht verständliche Art und Weise und nicht in Code-Kommentaren, um bessere Antworten zu erhalten. –

+0

Fertig, hoffe die Erklärungen helfen! –

Antwort

1

First off ich brauchen würde, wo die Schiffe wissen sind in deiner Matrix. Im Anschluss an die Benutzereingabe müssten Sie nur Ihre Matrix durchsuchen, um herauszufinden, ob die Eingabe mit einem Schiff übereinstimmt.

Sagen wir, Sie haben ein Schiff auf Position a [3] [3]. Der beste Weg dies zu überprüfen wäre, den Inhalt dieser Position auf 1 (wenn es ein Schiff gibt) und 0 zu setzen, wenn es keine gibt.

Also, wenn Sie einen Benutzer raten wie: x = 1; y = 4;

Alles, was Sie würde die empfangene Position müssen prüfen, um herauszufinden, ob es ein 1 (hat ein Schiff) oder 0 (kein Schiff dort):

if(a[x][y] == 1) { 
    //user guesses correctly 
} else { 
    //user found no ship 
} 
Verwandte Themen