Question

I'm currently working on a java version of minesweeper. When the user clicks on a mine or wins the game, i want to implement a yes, no option panel with would serve as "do you want to play again" sort of feature. Implementing no is simple but im having a problem in implementing the yes option where the program runs again but in my case it simply runs a new instance of the program and does not close the old instance. Suggestions? Thanks.

Here is the code where the user finds all the mines and wins:

 if (check == nomines)
           {
               endtime = System.nanoTime();
               Component temporaryLostComponent = null;
               JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations, you won! It took you " + (int)((endtime-starttime)/1000000000) + " seconds!");

               int p = JOptionPane.showConfirmDialog(this, "Play again?","Minefield",JOptionPane.YES_NO_OPTION);
               if (p == 1)
               {
                   System.exit(0); 
               }
               else 
               {
                   new Minefield(); 
               }

           }
       }
Was it helpful?

Solution

using a do while loop, it makes easy your work..

do{

  // your all operations...

 // at last prompt  yes or no ?
} while(choice.equalsIgnoreCase("yes"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top