Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Dialog Bug in java

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Dialog Bug in java

    Hello All.

    I am building few games in java and i am currently facing a problem in the game of "Snakes and ladders", the game works fine, however, when the winner riches 100 I have a dialogue panel pop up which asks the user if he wants to play again with "yes" "no" and "cancel" option.

    I have a few questions and a bug to solve.

    The bug:
    When I click yes on the dialogue panel the panel keeps reopening again and again. doesn't allow the user to play or do anything at all other than close the frame.

    Question:

    As you will see in my code I have a frame, button, player etc. because I am building more than 1 game I keep re-creating a new frame, button etc for each game which is not ideal for an OOP.
    I have a game logic class which works fine but whenever I create classes like player, board, buttons etc. I seem to fail whenever I call them, so I am kinda stuck.

    Any suggestions on how to make my code more object-oriented would be helpful.

    Thanks in advance.



    The bug is here if I am not wrong :

     
       @Override
        protected void win(int square)
        {
            for(int k=0;k<10;k++)
                for(int l=0;l<10;l++)
                {
                    gameOver=1;
                    if (JOptionPane.showConfirmDialog(new JFrame("Winner"),"You are the Winner!!, Would you like to play again?") == JOptionPane.YES_OPTION) {
                        restart();
                    } else {
                        System.exit(0);
                    }
                }
        }
     
        @Override
        protected void restart(){
     
            p1value=0;
            p2value=0;
            rePrint();
     
        }
     
     
        private void rePrint()
        {
            try{
     
                int i;
                for(i =0; i <10; i++)
                {
                    int j;
                    for(j =0; j <10; j++)
                    {
                        // grid[i][j].setIcon(null);
                        grid[i][j].setIcon(icon[i][j]);
                    }
                }
     
            }catch(Exception e)
            {
                //System.out.println("error in repaint"+e);
            }
     
        }



    The entire code below :

    Snake class


     
    package uk.ac.glos.games.ct5025.s1705553.Games;
     
     
    import uk.ac.glos.games.ct5025.s1705553.Model.Game;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    public class Snakes extends Game implements ActionListener
    {
     
        private final JTabbedPane tabPane;
        private JPanel gamePanel;
     
        private Icon p1;
        private Icon p2;
        private final Icon[][] icon= new Icon[10][10];
        private Icon diceIcon;
     
        private final JButton[][] grid=new JButton[10][10];
        private JButton start;
        private final JButton bPlayer1;
        private final JButton bPlayer2;
     
        private final JLabel dice;
     
        private final Random randomNo;
     
        private int imageFlag;
        private int prevIp1;
        private int prevJp1;
     
        private int path;
        private int p1value;
        private int p2value;
     
        private int gameOver;
        // --Commented out by Inspection (10/27/2018 1:43 AM):private int cnoFlag=0;
     
     
        private String str;
     
        private final int[][] board={
                {100,99,98,97,96,95,94,93,92,91},
                {81,82,83,84,85,86,87,88,89,90},
                {80,79,78,77,76,75,74,73,72,71},
                {61,62,63,64,65,66,67,68,69,70},
                {60,59,58,57,56,55,54,53,52,51},
                {41,42,43,44,45,46,47,48,49,50},
                {40,39,38,37,36,35,34,33,32,31},
                {21,22,23,24,25,26,27,28,29,30},
                {20,19,18,17,16,15,14,13,12,11},
                { 1, 2, 3, 4, 5, 6, 7, 8, 9,10},
        };
     
     
        public Snakes()
        {
     
            JFrame f = new JFrame("Snakes and Ladders");
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
            // JPanel mainPanel = new JPanel();
            tabPane =new JTabbedPane(JTabbedPane.LEFT);
     
            f.setLayout(new BorderLayout());
     
            randomNo= new Random();
     
            bPlayer1= new JButton("player1");
            bPlayer2= new JButton("player2");
     
            bPlayer1.setEnabled(false);
            bPlayer2.setEnabled(false);
     
     
            bPlayer1.addActionListener(this);
            bPlayer2.addActionListener(this);
     
            dice = new JLabel();
     
            Game();
     
            f.add(gamePanel,BorderLayout.CENTER);
            f.setResizable(false);
            f.setSize(900,900);
            //f.pack();
            f.setVisible(true);
     
     
        }
     
     
        private void Game()
        {
            JLabel l1= new JLabel();
            JLabel l2= new JLabel();
     
            start= new JButton("Start");
            start.addActionListener(this);
     
            gamePanel= new JPanel();
     
     
            JPanel gameCenter = new JPanel();
            JPanel gameWest = new JPanel();
            new JPanel();
            JPanel gameNorth;
            new JPanel();
            JPanel gameSouth;
     
            gameCenter.setLayout(new GridLayout(10,10));
            // JPanel gameEast = new JPanel(new GridLayout(2, 1));
     
            gameWest.setLayout(new FlowLayout());
            gameNorth =new JPanel(new FlowLayout());
            gameSouth = new JPanel(new FlowLayout());
     
            gamePanel.setLayout(new BorderLayout());
     
            p1= new ImageIcon("images/snakesAndLadders/player1.gif");
            p2= new ImageIcon("images/snakesAndLadders/player2.gif");
     
            l1.setIcon(p1);
            l2.setIcon(p2);
     
            for(int i=0;i<10;i++)
                for(int j=0;j<10;j++)
                {
                    grid[i][j]= new JButton();
                    //b[i][j].addActionListener(this);
                    path=board[i][j];
                    str=Integer.toString(path);
                    icon[i][j]=new ImageIcon("images/snakesAndLadders/"+str+".jpg");
                    grid[i][j].setIcon(icon[i][j]);
     
                }
     
     
            str=Integer.toString(1);
            diceIcon= new ImageIcon("images/snakesAndLadders/GameDice/"+str+".jpg");
            dice.setIcon(diceIcon);
     
     
            gameSouth.add(l1);
            gameSouth.add(bPlayer1);
            gameSouth.add(dice);
            gameSouth.add(bPlayer2);
            gameSouth.add(l2);
            gameNorth.add(start);
     
            for(int i=0;i<10;i++)
            {
                for(int j=0;j<10;j++)
                {
                    gameCenter.add(grid[i][j]);
                    //System.out.print("["+i+"]["+j+"],");
                }
                //System.out.println();
            }
     
     
     
            gamePanel.add(gameCenter,BorderLayout.CENTER);
            gamePanel.add(gameWest,BorderLayout.WEST);
            gamePanel.add(gameNorth,BorderLayout.NORTH);
            gamePanel.add(gameSouth,BorderLayout.SOUTH);
     
            tabPane.addTab("Game",gamePanel);
     
        }
     
        @Override
        protected void checkWin()
        {
            int number=randomNo.nextInt(3);
     
            //System.out.println("Chance is :"+n) ;
            if(number==0)
                checkWin();
     
     
            if(number==1)
            {
                bPlayer2.setEnabled(false);
                bPlayer1.setEnabled(true);
     
            }
            else if(number==2)
            {
                bPlayer2.setEnabled(true);
                bPlayer1.setEnabled(false);
     
            }
     
        }
     
        public void actionPerformed(ActionEvent e)
        {
     
            int number;
            try{
                if(e.getSource()==bPlayer1)
                {
     
                    //System.out.println("JB Player");
                    do{
                        number = rollDice();
                    }while(number==0);
     
                    //System.out.println("UR playerPass value is :"+n);
                    p1value=p1value+number;
     
                    if(p1value>=100)
                    {
                        p1value=100;
                    }
     
                    player1ImageTravel(p1value);
     
                    bPlayer2.setEnabled(true);
                    bPlayer1.setEnabled(false);
                    setBothImage();
                }
                else if(e.getSource()==bPlayer2)
                {
                    //System.out.println("JB Computer");
                    do{
                        number = rollDice();
                    }while(number==0);
     
                    //System.out.println("Computer Pass Value is "+n);
                    p2value=p2value+number;
                    if(p2value>=100)
                    {
                        p2value=100;
                    }
                    player2ImageTravel(p2value);
                    bPlayer2.setEnabled(false);
                    bPlayer1.setEnabled(true);
     
                    setBothImage();
                }
                else if(e.getSource()==start)
                {
                    gameOver=0;
                    p1value=0;
                    p2value=0;
                    //int imageFlag2 = 0;
                    rePrint();
                    start.setEnabled(false);
                    checkWin();
                }
                else
                {
                    imageChange(e);
                }
            }catch(Exception ee)
            {
                //System.out.println("Error in BUTTON:"+ee);
            }
        }
     
     
     
        private int rollDice()
        {
            int i =	randomNo.nextInt(7);
     
            str=Integer.toString(i);
            diceIcon= new ImageIcon("images/snakesAndLadders/GameDice/"+str+".jpg");
            dice.setIcon(diceIcon);
     
            return i;
     
        }
     
        @Override
        protected void win(int square)
        {
            for(int k=0;k<10;k++)
                for(int l=0;l<10;l++)
                {
                    gameOver=1;
                    if (JOptionPane.showConfirmDialog(new JFrame("Winner"),"You are the Winner!!, Would you like to play again?") == JOptionPane.YES_OPTION) {
                        restart();
                    } else {
                        System.exit(0);
                    }
                }
        }
     
        @Override
        protected void restart(){
     
            p1value=0;
            p2value=0;
            rePrint();
     
        }
     
     
        private void rePrint()
        {
            try{
     
                int i;
                for(i =0; i <10; i++)
                {
                    int j;
                    for(j =0; j <10; j++)
                    {
                        // grid[i][j].setIcon(null);
                        grid[i][j].setIcon(icon[i][j]);
                    }
                }
     
            }catch(Exception e)
            {
                //System.out.println("error in repaint"+e);
            }
     
        }
     
        private void player1ImageTravel(int playerImage)
        {
            int i,j = 0;
            int noFlag=0;
     
            if(gameOver==0)
            {
                try{
                    rePrint();
                    for(i=0;i<10;i++)
                    {
                        for(j=0;j<10;j++)
                        {
                            if(playerImage==board[i][j])
                            {
                                noFlag=1;
                                break;
                            }
                        }
                        if(noFlag==1)
                            break;
                    }
     
                    if(noFlag==1)
                    {
                        Thread.sleep(200);
                        grid[i][j].setIcon(p1);
                        prevIp1=i;
                        prevJp1=j;
     
                        if(playerImage==4)
                        {
                            playerImage=25;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==13)
                        {
                            playerImage=46;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==33)
                        {
                            playerImage=49;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==42)
                        {
                            playerImage=63;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==50)
                        {
                            playerImage=69;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==62)
                        {
                            playerImage=81;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==74)
                        {
                            playerImage=92;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
     
     
                        if(playerImage==99)
                        {
                            playerImage=41;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==89)
                        {
                            playerImage=53;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==76)
                        {
                            playerImage=58;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==66)
                        {
                            playerImage=45;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==54)
                        {
                            playerImage=31;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==43)
                        {
                            playerImage=18;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==40)
                        {
                            playerImage=3;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
                        else if(playerImage==27)
                        {
                            playerImage=5;
                            player1ImageTravel(playerImage);
                            p1value=playerImage;
                        }
     
                    }
     
                    if(playerImage>=100)
                    {
     
                        win(0);
     
                    }
                }
                catch(Exception ignored)
                {
     
                }
            }
     
        }
     
        private void setBothImage()
        {
            int i;
            int j=0;
            int noFlag=0;
     
            if(gameOver==0)
            {
                for(i=0;i<10;i++)
                {
                    for(j=0;j<10;j++)
                    {
                        if(p1value==board[i][j])
                        {
                            noFlag=1;
                            break;
                        }
                    }
                    if(noFlag==1)
                        break;
                }
     
                if(noFlag==1)
                {
                    grid[i][j].setIcon(p1);
                }
     
                noFlag=0;
     
                for(i=0;i<10;i++)
                {
                    for(j=0;j<10;j++)
                    {
                        if(p2value==board[i][j])
                        {
                            noFlag=1;
                            break;
                        }
                    }
                    if(noFlag==1)
                        break;
                }
     
                if(noFlag==1)
                {
                    grid[i][j].setIcon(p2);
                }
            }
        }
     
     
        private void player2ImageTravel(int playerImage)
        {
     
            int i,j = 0;
            int noFlag=0;
     
            try{
                rePrint();
                for(i=0;i<10;i++)
                {
                    for(j=0;j<10;j++)
                    {
                        if(playerImage==board[i][j])
                        {
                            noFlag=1;
                            break;
                        }
                    }
                    if(noFlag==1)
                        break;
                }
     
                if(noFlag==1)
                {
                    path=board[prevIp1][prevJp1];
                    str=Integer.toString(path);
                    Thread.sleep(200);
                    icon[prevIp1][prevJp1]=new ImageIcon("images/snakesAndLadders/"+str+".jpg");
                    Thread.sleep(200);
                    grid[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
                    grid[i][j].setIcon(p1);
                    prevIp1=i;
                    prevJp1=j;
     
     
                    if(playerImage==4)
                    {
                        playerImage=25;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==13)
                    {
                        playerImage=46;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==33)
                    {
                        playerImage=49;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==42)
                    {
                        playerImage=63;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==50)
                    {
                        playerImage=69;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==62)
                    {
                        playerImage=81;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==74)
                    {
                        playerImage=92;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
     
     
                    if(playerImage==99)
                    {
                        playerImage=41;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==89)
                    {
                        playerImage=53;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==76)
                    {
                        playerImage=58;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==66)
                    {
                        playerImage=45;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==54)
                    {
                        playerImage=31;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==43)
                    {
                        playerImage=18;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==40)
                    {
                        playerImage=3;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
                    else if(playerImage==27)
                    {
                        playerImage=5;
                        player2ImageTravel(playerImage);
                        p2value=playerImage;
                    }
     
                }
     
                if(playerImage>=100)
                {
                    //System.out.println("u r win");
                    gameOver=1;
                    win(0);
                }
     
            }
            catch(Exception ignored)
            {
     
            }
     
        }
     
        private void imageChange(ActionEvent e)
        {
            int i,j = 0;
            int flag=0;
     
            try{
                for( i=0;i<10;i++)
                {
                    for( j=0;j<10;j++)
                    {
                        if(e.getSource()==grid[i][j])
                        {
                            //System.out.println("Button Found");
                            //System.out.println("b["+i+"]["+j+"]");
                            flag=1;
                            break;
                        }
                        //System.out.println(j);
                    }
     
                    if(flag==1)
                        break;
                }
     
     
                if(imageFlag==0)
                {
                    Thread.sleep(200);
                    grid[i][j].setIcon(p1);
                    imageFlag=1;
                    prevIp1=i;
                    prevJp1=j;
                }
                else
                {
                    path=board[prevIp1][prevJp1];
                    str=Integer.toString(path);
                    Thread.sleep(200);
                    icon[prevIp1][prevJp1]=new ImageIcon("images/snakesAndLadders/"+str+".jpg");
                    Thread.sleep(200);
                    grid[prevIp1][prevJp1].setIcon(icon[prevIp1][prevJp1]);
                    grid[i][j].setIcon(p1);
                    prevIp1=i;
                    prevJp1=j;
                }
            }
            catch(Exception ee)
            {
                ee.printStackTrace();
            }
        }
     
    }


    Game class:


     
    package uk.ac.glos.games.ct5025.s1705553.Model;
     
    import javax.swing.*;
     
    public class Game {
     
     
        protected final JButton[] grid = new JButton[9];
        protected final int[] board = new int[9];
        private boolean won = false;
        protected int currentPlayer = 1;
     
     
        protected void checkWin() {
     
     
            if ((board[0] == board[1]) && (board[1] == board[2]) && board[2] != 0) {
                win(0);
            }
            if ((board[3] == board[4]) && (board[4] == board[5]) && board[5] != 0) {
                win(5);
            }
            if ((board[6] == board[7]) && (board[7] == board[8]) && board[8] != 0) {
                win(8);
            }
     
            //----------------------------------------------------------------------
     
            if ((board[0] == board[3]) && (board[3] == board[6]) && board[6] != 0) {
                win(6);
            }
            if ((board[1] == board[4]) && (board[4] == board[7]) && board[7] != 0) {
                win(7);
            }
            if ((board[2] == board[5]) && (board[5] == board[8]) && board[8] != 0) {
                win(8);
            }
     
            //---------------------------------------------------------------------
     
            if ((board[0] == board[4]) && (board[4] == board[8]) && board[8] != 0) {
                win(8);
            }
            if ((board[6] == board[4]) && (board[4] == board[2]) && board[2] != 0) {
                win(2);
            }
     
            for (int aBoard : board) {
                if (aBoard == 0) return;
            }
     
            if (!won) draw();
     
        }
     
        protected void win(int square) {
            won = true;
     
            String winner = board[square] == 1 ? "X" : "O";
     
            if (JOptionPane.showConfirmDialog(new JFrame("Winner! Congratulations " + winner), "Player " + winner + " has won! Would you like to play again?") == JOptionPane.YES_OPTION) {
                restart();
            } else {
                System.exit(0);
            }
        }
     
        protected void restart() {
            won = false;
            currentPlayer = 1;
     
            for (int i = 0; i < 9; i++) {
                board[i] = 0;
                grid[i].setIcon(null);
            }
        }
     
        private void draw() {
            if (JOptionPane.showConfirmDialog(new JFrame("Its a draw!"), "Its a draw! Would you like to play again? ") == JOptionPane.YES_OPTION) {
                restart();
            } else {
                System.exit(0);
            }
        }
    }

    Main class:


     
    package uk.ac.glos.games.ct5025.s1705553.Display;
     
    import uk.ac.glos.games.ct5025.s1705553.Games.DiceGame;
    import uk.ac.glos.games.ct5025.s1705553.Games.Noughts;
    import uk.ac.glos.games.ct5025.s1705553.Games.Snakes;
     
     
    import javax.swing.*;
    import java.awt.*;
     
    class Main {
     
        private static void createAndShowUI() {
            JFrame frame = new JFrame("CT5025 Games");
            frame.getContentPane().add(new GUI().getMainPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setResizable(false);
        }
     
        public void openGameNoughtsAndCrosses() {
            Noughts noughts = new Noughts();
            noughts.init_components();
        }
     
        public void openGameSnakesAndLadders() {
            @SuppressWarnings("unused") Snakes snakes = new Snakes();
     
        }
     
        public void openDiceGame() {
            @SuppressWarnings("unused") DiceGame diceGame = new DiceGame();
        }
     
        public static void main(String[] args){
            EventQueue.invokeLater(Main::createAndShowUI);
        }
    }

    GUI Class:

     
    package uk.ac.glos.games.ct5025.s1705553.Display;
     
    import javax.swing.*;
    import java.awt.*;
     
     
    class GUI {
        private static final Dimension MAIN_PANEL_SIZE = new Dimension(450,300);
        private final JPanel mainPanel = new JPanel();
     
     
        public GUI(){
            ImageIcon noughtsAndCrosses = new ImageIcon("images/mainWindow/NaC.jpg");
            ImageIcon snakesAndLadders = new ImageIcon("images/mainWindow/SaL.jpg");
            ImageIcon diceGame = new ImageIcon("images/mainWindow/DiceGame.jpg");
            JButton openGameNoughtsAndCrosses =  new JButton(" TicTacToe",noughtsAndCrosses);
            openGameNoughtsAndCrosses.setBounds(150,120,130,35);
            openGameNoughtsAndCrosses.addActionListener(e -> {
                Main gui = new Main();
                gui.openGameNoughtsAndCrosses();
            });
            JButton openGameSnakesAndLadders =  new JButton(" Snakes And Ladders",snakesAndLadders);
            openGameSnakesAndLadders.setBounds(115,170,200,35);
            openGameSnakesAndLadders.addActionListener(e -> {
                Main gui = new Main();
                gui.openGameSnakesAndLadders();
            });
            JButton openDiceGame =  new JButton(" Dice Game",diceGame);
            openDiceGame.setBounds(145,220,140,35);
            openDiceGame.addActionListener(e -> {
                Main gui = new Main();
                gui.openDiceGame();
            });
            mainPanel.setPreferredSize(MAIN_PANEL_SIZE);
            mainPanel.add(openGameNoughtsAndCrosses);
            mainPanel.add(openGameSnakesAndLadders);
            mainPanel.add(openDiceGame);
            mainPanel.setLayout(null);
        }
     
        public JPanel getMainPanel() {
            return mainPanel;
        }
    }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Dialog Bug in java

    It may be throwing an exception in the rePrint() method. But since you commented out the print statement, nothing is shown so it may quit quietly. Also, you don't need to create a frame for the option call. You can just use null there.

    Regards,
    Jim
    Last edited by jim829; October 27th, 2018 at 11:23 AM.

  3. The Following User Says Thank You to jim829 For This Useful Post:

    Toni (October 27th, 2018)

  4. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dialog Bug in java

    I changed to null and uncommented to see if I am getting an exception, but nothing still the same thing. I click and it keeps popping up again asking me to choose an option.

    Seems like there is a problem with the restart(), but I just don't get it, maybe I am missing something.


     
       @Override
        protected void win(int square)
        {
            for(int k=0;k<10;k++)
                for(int l=0;l<10;l++)
                {
                    gameOver=1;
                    if (JOptionPane.showConfirmDialog(null,"You are the Winner!!, Would you like to play again?") == JOptionPane.YES_OPTION) {
                        restart();
                    } else {
                        System.exit(0);
                    }
                }
        }
     
        @Override
        protected void restart(){
     
            p1value=0;
            p2value=0;
            rePrint();
     
        }
     
     
        private void rePrint()
        {
            try{
     
                int i;
                for(i =0; i <10; i++)
                {
                    int j;
                    for(j =0; j <10; j++)
                    {
                        // grid[i][j].setIcon(null);
                        grid[i][j].setIcon(icon[i][j]);
                    }
                }
     
            }catch(Exception e)
            {
                System.out.println("error in repaint"+e);
            }
     
        }

  5. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Dialog Bug in java

    Why is your Snakes class extending the Game class? I thought Game was tic-tac-toe. Normally, Game would only contain code that is common to ALL games. Then you would have a TicTacToe class, Snake class, or any other game to take advantage of those features by extending Game. The way you have it doesn't make much sense. The tic-tac-toe grid in Game doesn't have anything to do with Snakes (as far as I know).

    Regards,
    Jim

  6. The Following User Says Thank You to jim829 For This Useful Post:

    Toni (October 27th, 2018)

  7. #5
    Junior Member
    Join Date
    Sep 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Dialog Bug in java

    Yes you totally right, that is where I am failing. I cannot make a game logic common to all 3 games, because TicTacToe has a draw, snakes and ladder there is only one winner. so I thought I make a game logic for tic tac toe and then override that logic for the other games.

    I don't know if that makes any sense. I am new in java, not very good with the whole OOP.

    Thanks for helping.

  8. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Dialog Bug in java

    If there is no commonality between the games then there is no need to force it. Just write the code required for each game. You can write some shared methods in a class and make then static. For example, you could do something like this:

    public class JOptionDemo {
       public static void main(String[] args) {
    	  if (Utility.playAgain()) {
    		 System.out.println("Great, let's play again.");
    	  } else {
    		 System.out.println("Ok, bye for now.");
    	  }
       }
    }
     
    class Utility {
       public static boolean playAgain() {
    	  return JOptionPane.showConfirmDialog(null,
    	        "Do you want to play again?") == JOptionPane.YES_OPTION;
       }
    }

    You can put similar methods in the Utility class or a GameSupport class. Then you don't have to keep
    re-coding the same thing each time. If you had several card games, you could write a shared shuffle method which would return
    a shuffled deck. The deck itself could be a class that is shared between card games. It's all up to you.

    Regards,
    Jim

Similar Threads

  1. [SOLVED] Crazy Java error/bug?
    By Cronus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2013, 06:42 PM
  2. Java Dialog Box HELP
    By Green Light in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2013, 02:33 AM
  3. bug on lightwave java game library
    By anon176 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 4th, 2012, 10:46 AM
  4. Begginer Java - Bug in my code?
    By rk2010 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 7th, 2012, 08:12 AM
  5. pop3 in java, need a bug clearance
    By ttsdinesh in forum Java Networking
    Replies: 5
    Last Post: September 24th, 2009, 08:25 PM