package battleship;

public class BattleShip {


    public static void main(String[] args) 
    {
        Addons a = new Addons();
        Board b = new Board();
        Computer c = new Computer();
        Human h = new Human();
        LinkedList list = new LinkedList();



        while(a.intro())
        {
            a.sleep(1);
            a.countDown(5);
            b.prepareBoard();
        do
        {
            h.play(b.getBoard());
            a.checkWinner(b.getBoard());
            System.out.println("How the Board stands.");
            a.printBoard(b.getBoard());
            a.sleep(3);

            c.play(b.getBoard());
            a.checkWinner(b.getBoard());
            System.out.println("How the Board stands.");
            a.printBoard(b.getBoard());
        }while(b.nextRound() && a.checkWinner(b.getBoard()));

        list.printLinkedList();
        }
    }
}




public boolean intro()
    {
        Sound s = new Sound();



        String input= " ";
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
                          +"                                             \n"
                          +"                 Welcome                     \n"
                          +"                   to                        \n"
                          +"               BattleShip                    \n"
                          +"                                             \n"
                          +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        System.out.println("This game is a turn based strategy game that\n"
                          +"the basic need is to sink the enemy ships before\n"
                          +"the enemy sinks yours, you have to input x and\n"
                          +"y coordinates within the range. On the enemy side\n"
                          +"~ means misses and * means hits. Happy Hunting!\n\n");
        s.playIntro();
        do
        {

        input = JOptionPane.showInputDialog("Do you want to play? Y or N?").toUpperCase();

        switch(input)
        {
            case "Y":
                return true;

            case "N": 
                return false;
            default:
                break;

        }
        }while(input != "N" || input != "Y");
        return false;      
    }



public void playIntro()
    {
        AudioStream bGM;

        try{
        bGM = new AudioStream(new FileInputStream("intro.wav"));


        AudioPlayer.player.start(bGM);


        } catch (IOException error)
        {
            System.out.println("File not Found: " + error);
        }

    }

Ok I put sound in my program, everything is working fine, but if I exit the program by placing N in the dialog box in intro(); the music continues playing and the program does not exit. Any suggestions?

Your program does not quit when you put "N" into the dialog box, because the condition for quitting the while loop as you have put it is that the input should be unequal to "N" OR unequal to "Y". When the input is equal to "N" it is still unequal to "Y", and hence, the condition for looping is met. Your program does not quit. :\

Corrected code:

package battleship;

public class BattleShip {


    public static void main(String[] args) 
    {
        Addons a = new Addons();
        Board b = new Board();
        Computer c = new Computer();
        Human h = new Human();
        LinkedList list = new LinkedList();



        while(a.intro())
        {
            a.sleep(1);
            a.countDown(5);
            b.prepareBoard();
        do
        {
            h.play(b.getBoard());
            a.checkWinner(b.getBoard());
            System.out.println("How the Board stands.");
            a.printBoard(b.getBoard());
            a.sleep(3);

            c.play(b.getBoard());
            a.checkWinner(b.getBoard());
            System.out.println("How the Board stands.");
            a.printBoard(b.getBoard());
        }while(b.nextRound() && a.checkWinner(b.getBoard()));

        list.printLinkedList();
        }
    }
}




public boolean intro()
    {
        Sound s = new Sound();



        String input= " ";
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
                          +"                                             \n"
                          +"                 Welcome                     \n"
                          +"                   to                        \n"
                          +"               BattleShip                    \n"
                          +"                                             \n"
                          +"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        System.out.println("This game is a turn based strategy game that\n"
                          +"the basic need is to sink the enemy ships before\n"
                          +"the enemy sinks yours, you have to input x and\n"
                          +"y coordinates within the range. On the enemy side\n"
                          +"~ means misses and * means hits. Happy Hunting!\n\n");
        s.playIntro();
        do
        {

        input = JOptionPane.showInputDialog("Do you want to play? Y or N?").toUpperCase();

        switch(input)
        {
            case "Y":
                return true;

            case "N": 
                return false;
            default:
                break;

        }
        }while(input != "N");
        return false;      
    }



public void playIntro()
    {
        AudioStream bGM;

        try{
        bGM = new AudioStream(new FileInputStream("intro.wav"));


        AudioPlayer.player.start(bGM);


        } catch (IOException error)
        {
            System.out.println("File not Found: " + error);
        }

    }

EDIT: That do loop is an infinite loop, because the input can't be equal to "N" and "Y" simultaneously.

That is an OR statement thus the ||, ofcourse it can't be to both. It doesn't go forever since I don't get infinite jOptionPane. It does kick me out, just that the game doesnt close. Either way I tried your code and it does the same thing mine does.

}while(input != "N");

This is the only line I changed.

It doesn't go forever since I don't get infinite jOptionPane.

You aren't calling on to infinite JOptionPane instances, anyway because the same variable is being re-initialized in every iteration.

It does kick me out, just that the game doesnt close

Where are you running your game? As in from the cmd line, netbeans, or what?

EDIT: Have you considered using the stop() function to stop playing the music once you are done? Might not help, but do try.

That line you changed causes the same effect as mine. And Netbeans.I don't think it has to do anything with the loop, I think the Audio is pending and doesn't let my program end, I need to find a way to stop this music.

thegaulking: if you want to provide 'corrected code', at least make sure you provide 'correct code', which ... you don't.

while(input != "N");

very, very bad indeed.

this should actually be:

while ( !input.equals("N"));

or

while ( !input.equalsIgnoreCase("N"));

commented: Sorry, did not notice. I just removed the "Y" part and re-posted. :\ +0

We are not here to look at syntax specially when none of either of your suggestions contributes to the issue at hand.

Gauk: I am trying the stop(); just haven't found out how to implement it.

Did you have a look at the link? It shows how, I believe. :|

I did take a look, I implemented it the way it shows and there is no sound at all now.

EDITED: now we are back where we started again... Stop doesnt do anything.

public void playIntro(String temp)
    {
        FileInputStream bGM;
        if (temp.equals("start")) {           

            try 
            {
                bGM = new FileInputStream("intro.wav");
                AudioStream as = new AudioStream(bGM);
                AudioPlayer.player.start(as);
            } 
            catch (Throwable e) 
            {
                e.printStackTrace();
            }
        }
        if (temp.equals("stop")) {

                try {
                    bGM = new FileInputStream("intro.wav");
                    AudioStream as = new AudioStream(bGM);
                    AudioPlayer.player.stop(as);
                } catch (Throwable e) {
                    e.printStackTrace();
                }
        }
    }

Nandomo: this has nothing to do with Syntax.

== and != are operators that CAN be used to compare two Objects BUT, and here's the stinger: it 'll compare the reference of the two objects, NOT the value. even though the values of both objects might be equal, the references don't have to be.

so: you are asking us why your start and stop don't run when you want it .... well, part of that is: it is possible that, even though you think that your if is executed, (or while ended) because the boolean expression looks to return true, this doesn't mean it does true.

so: in some cases, it is possible since you don't compare your variables correctly.

in your last post, you use "start" and "stop", while in your previous, the Strings which you compare are "Y" and "N".

it's easier to see any possible mistakes if you provide your entire up to date code.

You are speaking to me like if I started programing 5 minutes ago, go help someone else, me and gauk were doing just fine before you got here.

Nandomo: considering from what I've seen of your code, and the way you respond to help ... might very well have been just five minutes.
but I won't force myself into the conversation, nor will I bore you with any "bad" help, so good luck with the program, won't bother you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.