Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Need help with a java battleship program.

$
0
0
                                            
import java.util.*;

public class battleShip {

    private Scanner in;
    private boardPiece[][] eboard = {{new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()},
                                    {new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece(),new boardPiece()}};
    
    private char turn = 'X';
    private boolean win = false;
    private int count = 0;
    //private int mode = 2;
    private Random generator = new Random(System.currentTimeMillis());
    
    private ship epatBoat = new ship(1);
    private ship edestroyer = new ship(2);
    private ship esubmarine = new ship(3);
    private ship ebattleship = new ship(4);
    private ship eairCraftCar = new ship(5);
    private AIplay AI = new AIplay();
    
    public static void main(String [] args){
        String again;
        battleShip game = new battleShip();
        game.in = new Scanner(System.in);
   
        game.play();
        
        System.out.println("Would you like to play again?(Y/N): ");
        again = game.in.nextLine();
        while(again.charAt(0) == 'Y'){
            game.init();
            game.play();
            System.out.println("Would you like to play again?(Y/N): ");
            again = game.in.nextLine();
        }
        game.in.close();
        System.out.println("Good Bye!");
    }
    
    public void play(){
        printBoard();
 
        AIplace();
        System.out.println("Enter your moves in the form 'C4'");
        while(!win)
            move();
    }
    
    /** PRINT BOARD */
    
    public void printBoard(){
        System.out.println("                 ENEMY                   ");
        System.out.println("   1   2   3   4   5   6   7   8   9   10");
        for(int x=0; x<10; x++){
            System.out.print((char)(65+x) + " ");
            for(int y=0; y<10; y++){
                System.out.print(eboard[x][y].piece);
            }
            System.out.print("     ");

            
            System.out.println();
        }
    }

    
    public void AIplace(){

        boolean flag = false;
        for(int x=1;x<4;x++){
            flag = false;
            while(!flag){
                int a = generator.nextInt(2);
                int b = generator.nextInt(6);
                int c = generator.nextInt(6);
                if(a == 0)
                    a = 72;
                else
                    a = 86;
                b += 65;
                c += 49;
                
                if(a == 72 && (x + 1) > (7 - (c-48))){
                    //no good, try again
                }
                else if(a == 86 && (x + 1) > (7 - (b-64))){
                    //no good, try again
                }
                else{ //Valid Placement, except for overlap
                    String d = String.valueOf((char)B)/> + String.valueOf((char)c);
                    //System.out.println("AI LOCATION: " + d + " / " + b + " " + c + " / " + a);
                    if(x == 1){
                        flag = AIplaceBoard((char)a, epatBoat, d); 
                    }
                    else if(x == 2){
                        flag = AIplaceBoard((char)a, edestroyer, d);
                    }
                    else if(x == 3){
                        flag = AIplaceBoard((char)a, esubmarine, d);
                    }
                    else if(x == 4){
                        flag = AIplaceBoard((char)a, ebattleship, d);
                    }
                    else{
                        flag = AIplaceBoard((char)a, eairCraftCar, d);
                    }
                }
            }
            
        }
    }
    

public boolean AIplaceBoard(char layout, ship current, String locale){
        
        boolean overlap = false;
        
        if(layout == 'H'){
            for(int x=((int)locale.charAt(1)-49);x<((int)locale.charAt(1)-49)+(current.type + 1);x++){
                if(eboard[((int)locale.charAt(0)-65)][x].type != current.type &&
                   eboard[((int)locale.charAt(0)-65)][x].used == true)
                    overlap = true;
            }
        }
        else{  //layout == 'V'
            for(int x=((int)locale.charAt(0)-65);x<((int)locale.charAt(0)-65)+(current.type + 1);x++){
                if(eboard[x][((int)locale.charAt(1)-49)].type != current.type &&
                   eboard[x][((int)locale.charAt(1)-49)].used == true)
                    overlap = true;
            }
        }
        if(!overlap){ //If ships don't overlap
            if(layout == 'H'){
                for(int x=((int)locale.charAt(1)-49);x<((int)locale.charAt(1)-49)+(current.type + 1);x++){
                    //eboard[((int)locale.charAt(0)-65)][x].piece = "_@_|";
                    eboard[((int)locale.charAt(0)-65)][x].used = true;
                    eboard[((int)locale.charAt(0)-65)][x].type = current.type;
                }
            }
            else{  //layout == 'V'
                for(int x=((int)locale.charAt(0)-65);x<((int)locale.charAt(0)-65)+(current.type + 1);x++){
                    //eboard[x][((int)locale.charAt(1)-49)].piece = "_@_|";
                    eboard[x][((int)locale.charAt(1)-49)].used = true;
                    eboard[x][((int)locale.charAt(1)-49)].type = current.type;
                }
            }
            current.placed = true;
            current.location = locale;
            current.orientation = layout;
            return true;
        }
        else
            return false;
    }

    public void move(){
        String move = "";
        String valid = "";

        if(turn == 'X'){
            System.out.println("Enter move: "); 
            move = in.nextLine();
        }
        else
            move = moveAI();
        
        valid = checkMove(move);
        while(valid != "ok"){
            if(turn == 'X'){
                System.out.println("ERROR: "+ valid);
                move = in.nextLine();
            }
            else
                move = moveAI(); 
            valid = checkMove(move);
        }
        
        processMove(move);
        
        if(turn == 'O')
            printBoard();
        
        if(count >= 17)
            checkWin();
        
        if(turn == 'X')
            turn = 'O';
        else
            turn = 'X';
    }
    
    public String checkMove(String move){
        
        if(((int)move.charAt(0)-100) >= 10)
            return "Invalid Row. Can only be A-J. Enter another move: ";    
        if(((int)move.charAt(0)-100) >= 10)
            return "Invalid Column. Can only be 1-10. Enter another move: ";
        if(turn == 'X'){
            if(eboard[((int)move.charAt(0)-65)][((int)move.charAt(1)-49)].selected)  
                return "Already chosen. Enter another move: ";
        }
        else{

        }
        
        return "ok";
    }
    
    public void processMove(String move){
        
        count++;
        char himi = ' ';
        String message = "";
        
            if(eboard[((int)move.charAt(0)-65)][((int)move.charAt(1)-49)].used){   
                himi = 'X';
                message = "Hit!";
            }
            else{
                himi = 'o';
                message = "Miss!";
            }
            eboard[((int)move.charAt(0)-65)][((int)move.charAt(1)-49)].piece = "_"+himi+"_|";
            eboard[((int)move.charAt(0)-65)][((int)move.charAt(1)-49)].selected = true;
            System.out.println(message);
            
            int counter = 0;
            int stype = eboard[((int)move.charAt(0)-65)][((int)move.charAt(1)-49)].type;
            if(counter == (stype + 1)){
                    String xship = "";
                    if(stype == 1){
                        xship = "Patrol Boat!";
                        epatBoat.destroyed = true;
                    }
                    else if(stype == 2){
                        xship = "Destroyer!";
                        edestroyer.destroyed = true;
                    }
                    else if(stype == 3){
                        xship = "Submarine!";
                        esubmarine.destroyed = true;
                    }
                    else if(stype == 4){
                        xship = "Battleship!";
                        ebattleship.destroyed = true;
                    }
                    else{
                        xship = "Air Craft Carrier!";
                        eairCraftCar.destroyed = true;
                    }
                    message += " You destroyed the " + xship;
                }
            }



    
    public void checkWin(){  
       if(epatBoat.destroyed && edestroyer.destroyed && esubmarine.destroyed && ebattleship.destroyed && eairCraftCar.destroyed){
           win = true;
           System.out.println("Congratulations! You've won!");
       }
    }
    
    public void init(){
          for(int x=0;x<6;x++){
              for(int y=0;y<6;y++){
                  eboard[x][y] = new boardPiece();
              }          
          }
          turn = 'X';
          win = false;
          count = 0;
          
    
          
          epatBoat = new ship(1);
          edestroyer = new ship(2);
          esubmarine = new ship(3);
          ebattleship = new ship(4);
          eairCraftCar = new ship(5);
          
    }
    
    public String moveAI(){
        int x = 0;
        int y = 0;
        
        if(AI.count<1 || (AI.himi == 'M' && AI.himi2 == 'M') || AI.error){
            x = generator.nextInt(6);
            y = generator.nextInt(6);
            x += 65;
            y += 49;
            AI.error = false;
        }
        else if(AI.himi == 'H'){
            x = AI.lasta;
            if(AI.lastb<54)
                y = AI.lastb + 1;
            else
                y = AI.lastb - 1;
        }
        else if(AI.himi2 == 'H' && AI.himi == 'M'){
            if(AI.lasta2<70)
                x = AI.lasta2 + 1;
            else
                x = AI.lasta2 - 1;
            y = AI.lastb2;
        }

        String z = String.valueOf((char)x) + String.valueOf((char)y);
        
        AI.lasta2 = AI.lasta;
        AI.lastb2 = AI.lastb;
        AI.lasta = x;
        AI.lastb = y;
        
        return z;
    }
    
    class boardPiece{
        public String piece;
        public char player;
        public boolean used;
        public boolean selected;
        public int type;
        
        boardPiece(){
            piece = "___|";
            used = false;
            selected = false;
            type = 0;
        }
    }
    
    class ship{
        public int type;
        public boolean placed;
        public boolean destroyed;
        public String location;
        public char orientation;
        
        ship(int x){
            type = x;
            placed = false;
            destroyed = false;
            location = "";
            orientation = ' ';
        }
    }
    
    class AIplay{
        public int lasta;
        public int lastb;
        public int lasta2;
        public int lastb2;
        public char himi;
        public char himi2;
        public int count;
        public boolean error;
        
        AIplay(){
            lasta = 0;
            lastb = 0;
            lasta2 = 0;
            lastb2 = 0;
            himi = 'M';
            himi2 = 'M';
            count = 0;
            error = false;
        }
    }
}




I am working on a java battleship program. I need to have it randomly place ships on the board, but I also need to have it import ship placements. I really am getting frustrated with this. I am literally getting sick because of how much stress this is putting on me. I really could use some help please.

Sorry I didn't say what I was having trouble with.

At the current moment when you place a coordinate, it sometimes places in 2 spots. And when I try do do anything in the 10th column, it only reads the '1' in '10'.

I have no idea how to even set up an import scanner. I have the format but I really am lost.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>