Results 1 to 2 of 2

Thread: battleship

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

    Default battleship

    its almost finished, but still need help. there's something wrong with the if else statement and while loop. it needs to stop if a player hits all the ships of the other player.





    boolean run = true;
    while(run == true)
    {
    //get gridsize
    int rowSize = Integer.parseInt(JOptionPane.showInputDialog("Welc ome to Battleship. \nTo begin, enter the number of rows."));
    int colSize = Integer.parseInt(JOptionPane.showInputDialog("Than ks. \nNow enter the number of columns."));
    int d,e;
    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");
    System.out.println("\n");
    System.out.println("\n");
    System.out.println("\n");
    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");
    System.out.println("\n");
    System.out.println("\n");
    System.out.println("\n");
    System.out.println("\n");
    JOptionPane.showMessageDialog(null, "Review the Ships you have positioned in the monitor.");



    //start of missile:


    JOptionPane.showMessageDialog(null, "Ok, let's start the game.");

    while(true){

    //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(" 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!.");
    d=0;
    while( d<= player2ShipLoc.length)
    {

    // int count = Integer.parseInt(new String(player1move)); for (int c=0; c<player2ShipLoc.length; c++)
    // if(playerShipsLegend[a][b]==player2ShipLoc[c])
    // player2Board[a][b]=1;

    if (d == player2ShipLoc.length )
    {

    JOptionPane.showMessageDialog(null, "PLAYER 1 WINS!");

    char letter[] = JOptionPane.showInputDialog(null, "Wanna try again? \n y or n").toCharArray();


    if(letter[0] == 'y')
    {
    System.out.println("wait for a while");
    run = true;
    break;
    }
    else
    {
    run = false;
    System.exit(0);
    }


    }

    d++;
    }
    }

    else
    {
    JOptionPane.showMessageDialog(null, "You missed...");
    }



    //player 2 turns::::



    //process player 2 move
    char player2move []= (JOptionPane.showInputDialog("Player 2, enter a letter to fire a missile.").toCharArray());
    int player2hit = 0;
    for (int a=0; a<rowSize; a++)
    for (int b=0; b<colSize;b++){
    if (player2move[0]==playerShipsLegend[a][b]){
    player1Field[a][b]='X';
    if(player1Board[a][b]==1){
    player2hit=1;
    player1Board[a][b]=0;

    }
    }
    }


    //display player 1 field
    for (int a=0; a<rowSize; a++){
    for (int b=0; b<colSize;b++)
    System.out.print("["+player1Field[a][b]+"]");
    System.out.println("");
    }
    System.out.println("\n");



    if (player2hit==1)
    {
    e=1;
    JOptionPane.showMessageDialog(null, "It's a hit!.");
    e++;

    if (e == 2 )
    {

    JOptionPane.showMessageDialog(null, "PLAYER 2 WINS!");

    char letter[] = JOptionPane.showInputDialog(null, "Wanna try again? \n yes or no?").toCharArray();


    if(letter[0] == 'y')
    {
    System.out.println("wait for a while");
    run = true;
    break;
    }
    else {
    run = false;
    System.exit(0);
    }



    }



    }

    else {
    JOptionPane.showMessageDialog(null, "You missed...");
    }



    }































    }

    }




    }

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

    Default Re: battleship

    You already have a thread for this: End battleship game

    Also, it is apparent that you didn't bother to go through the links I posted in that thread. Nor did you have the courtesy to reply there.

    I'm closing this as a duplicate. Please see the Forum Rules, paragraph 2.

    db

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

Similar Threads

  1. End battleship game
    By redblack in forum New To Java
    Replies: 2
    Last Post: 07-18-2013, 04:32 AM
  2. Battleship
    By Matthieu105 in forum New To Java
    Replies: 6
    Last Post: 11-28-2012, 03:24 PM
  3. Help with Battleship Game
    By Lucid15 in forum New To Java
    Replies: 59
    Last Post: 04-11-2012, 07:37 PM
  4. Battleship UML help
    By Lucid15 in forum New To Java
    Replies: 0
    Last Post: 03-12-2012, 04:15 AM
  5. battleship in java
    By matthewandalex5 in forum Advanced Java
    Replies: 2
    Last Post: 03-01-2012, 03:13 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
  •