Results 1 to 3 of 3
  1. #1
    redblack is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default End battleship game

    PLEAASE HELP ME WITH MY ASSIGNMENT IN JAVA.. I NEED TO END THE GAME. IT NEEDS TO LOOP AND CHECK WHO WILL WIN THE GAME, IF ITS PLAYER 1 OR PLAYER 2. PLS HELP ME
    HERES THE CODE

    Java Code:
    public static void main(String[] args) {
        
        //get gridsize    
        int rowSize = Integer.parseInt(JOptionPane.showInputDialog("Welcome to Battleship. \nTo begin, enter the number of rows."));
        int colSize = Integer.parseInt(JOptionPane.showInputDialog("Thanks. \nNow enter the number of columns."));
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++)
                System.out.print("[ ]");
        System.out.println("");
        }
        System.out.println("\n");    
        JOptionPane.showMessageDialog(null, "Ok, now below in the monitor, your grid is printed.");
        
        
        
        //initialize boards - board is the array containing the ship data    
        int player1Board [][]= new int [rowSize][colSize];
        int player2Board [][]= new int [rowSize][colSize];
        
        
        //initialize field - field is the array that is being printed for battle
        char player1Field [][]= new char [rowSize][colSize];
        char player2Field [][]= new char [rowSize][colSize];
        for (int a=0; a<rowSize; a++)
            for (int b=0; b<colSize;b++){
                player1Field [a][b]=' ';
                player2Field [a][b]=' ';
            }
        
        //initialize board legends
        char startChar='a';
        char playerShipsLegend [][] = new char [rowSize][colSize];
        for (int a=0; a<rowSize;a++)
            for (int b=0; b<colSize; b++){
                 playerShipsLegend [a][b]= startChar;
                 startChar++;
        }
        
        //display board legends
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++)
                System.out.print("["+playerShipsLegend[a][b]+"]");
        System.out.println("");
        }
        System.out.println("\n");
        
        //process and print player 1 board
        String strPlayer1ShipLoc = JOptionPane.showInputDialog("Right, let player 2 look away for a moment while you position your ships. \nWith the reference grid shown in the monitor, \nenter the letters of where you want to put your ships \n(don't put a space between your letters)");
        char player1ShipLoc[] = strPlayer1ShipLoc.toCharArray();
        
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++){
              for (int c=0; c<player1ShipLoc.length; c++)
                  if(playerShipsLegend[a][b]==player1ShipLoc[c]) player1Board[a][b]=1;
              System.out.print("["+player1Board[a][b]+"]");  
            }
         System.out.println("");       
        }    
        System.out.println("\n");
        JOptionPane.showMessageDialog(null, "Review the Ships you have positioned in the monitor.");
        
        //display board legends again for player 2
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++)
                System.out.print("["+playerShipsLegend[a][b]+"]");
        System.out.println("");
        }
        System.out.println("\n");
        
        
        //process and display player 2 board
        String strPlayer2ShipLoc = JOptionPane.showInputDialog("Ok then, let's call player 2 and you turn around for a bit. \n\nPlayer 2, using the monitor, enter the desired position of your ships \nby typing in the letters in succession (no spaces please).");
        char player2ShipLoc[] = strPlayer2ShipLoc.toCharArray();   
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++){
              for (int c=0; c<player2ShipLoc.length; c++)
                  if(playerShipsLegend[a][b]==player2ShipLoc[c]) player2Board[a][b]=1;
              System.out.print("["+player2Board[a][b]+"]");  
            }
         System.out.println("");       
        }    
        System.out.println("\n");
        JOptionPane.showMessageDialog(null, "Review the Ships you have positioned in the monitor.");
        
        //display board legends again to start game
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++)
                System.out.print("["+playerShipsLegend[a][b]+"]");
        System.out.println("");
        }
        System.out.println("\n");
        
        
        
        //process player 1 move
        char player1move []= (JOptionPane.showInputDialog("Ok, let's start the game. Player 1, enter a letter to fire a missile.").toCharArray());
        int player1hit=0;
        for (int a=0; a<rowSize; a++)
            for (int b=0; b<colSize;b++){
                if (player1move[0]==playerShipsLegend[a][b]){
                     player2Field[a][b]='X';   
                        if(player2Board[a][b]==1){ 
                     player1hit=1;
                     player2Board[a][b]=0;
                    }
                }     
            }       
        //display player 2 field
        for (int a=0; a<rowSize; a++){
            for (int b=0; b<colSize;b++)
                System.out.print("["+player2Field[a][b]+"]");
        System.out.println("");
        }
        System.out.println("\n");
        
        if (player1hit==1)JOptionPane.showMessageDialog(null, "It's a hit!.");
            else JOptionPane.showMessageDialog(null, "You missed...");
    Last edited by JosAH; 07-18-2013 at 10:45 AM. Reason: added [code] ... [/code] tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: End battleship game

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: End battleship game

    Don't double post. The other thread you started has been closed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with battleship game, please!
    By goodvibes in forum New To Java
    Replies: 5
    Last Post: 07-25-2012, 11:04 AM
  2. Help with Battleship Game
    By Lucid15 in forum New To Java
    Replies: 59
    Last Post: 04-11-2012, 07:37 PM
  3. Battleship game - arrays
    By makig in forum New To Java
    Replies: 9
    Last Post: 11-17-2011, 07:21 PM
  4. Battleship Game - really need help
    By thrashsynergy in forum Java Applets
    Replies: 5
    Last Post: 04-25-2011, 07:21 PM
  5. Battleship game
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 09:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •