0

I am creating a tic tac toe game for my AP Computer Science class and so far it ran without error, or that the error was resolved with a simple fix. However, when I added an if statement to the program it continuously displayes an error stating "unreachable code". I am still unable to determine the cause or how to fix this issue.

I set up the game using the "Magic Square", to where each row and diagonal is set to a variable and adds up to 15, which is how a winner is determined by the program.

The error appears at this if statement:

if ( topx == 15 || middlex == 15 || bottomx == 15 || leftx == 15 || centerx == 15 || rightx == 15 || diag1x == 15 || diag2x == 15 ) { String XWIN = ("X wins!"); g.drawString(XWIN, 60, 50); }

The above if statement pertains to the x player, but the if statement for the o player also has the same error.

The entire code is shown below (I apologize for the poor notes)

    public void paint(Graphics g){

             this.setSize(450, 430); //sets the game screen size.

             //initial directions
            System.out.println("Player 1 (x) goes first. there are 9 boxes available.");
            System.out.println("The numbers correspond to the boxes respectively.");
            System.out.println("1 being top left, 2 being top center, 3 being top right, and so on.");      
            System.out.print("PLAYER 1 enter location of x: ");


                  setBackground( Color.black ); //background color

     //listed variables assigned to each box, x and o, respectively    
         int topx = 0;
         int middlex = 0;
         int bottomx = 0;
         int leftx = 0;
         int centerx = 0;
         int rightx = 0;
         int diag1x = 0;
         int diag2x = 0;


         int topo = 0;
         int middleo = 0;
         int bottomo = 0;
         int lefto = 0;
         int centero = 0;
         int righto = 0;
         int diag1o = 0;
         int diag2o = 0;

        //board                       
        g.setColor( Color.WHITE );  
        g.drawRect(50,50,225,225);
        g.drawLine(125, 50, 125, 275);
        g.drawLine(200, 50, 200, 275);
        g.drawLine(50, 125, 275, 125);
        g.drawLine(50, 200, 275, 200);



        //for loop and implemented scanner
        for (int  x = 1;  x <= 5; x++)   {

            g.setColor( Color.CYAN);
        Scanner keyboard1 = new Scanner(System.in);
        int p1 = keyboard1.nextInt();
        System.out.print("PLAYER 2 enter location of o: ");
        switch (p1){ 

        case 1:
            g.drawLine(75, 75, 100, 100);
            g.drawLine(100, 75, 75, 100);
        topx += 8;
        leftx += 8;
        diag1x +=8;

        break;

        case 2:
            g.drawLine(150, 75, 175, 100);
            g.drawLine(175, 75, 150, 100);
            centerx += 1;
            topx += 1;

        break;

        case 3:
            g.drawLine(225, 75, 250, 100);
            g.drawLine(250, 75, 225, 100);
            rightx += 6;
            topx += 6;
            diag2x += 6;

        break;

        case 4:
            g.drawLine(75, 150, 100, 175);
            g.drawLine(75, 175, 100, 150);
            middlex += 3;
            leftx += 3;

        break;

        case 5:
            g.drawLine(150, 150, 175, 175);
            g.drawLine(150, 175, 175, 150);
            diag1x += 5;
            diag2x += 5;
            middlex += 5;
            centerx += 5;

        break;

        case 6:
            g.drawLine(225, 150, 250, 175);
            g.drawLine(225, 175, 250, 150);
            middlex += 7;
            rightx += 7;

        break;

        case 7:
            g.drawLine(75, 225, 100, 250);
            g.drawLine(75, 250, 100, 225);
            diag2x += 4;
            leftx += 4;
            bottomx += 4;

        break;

        case 8:
            g.drawLine(150, 225, 175, 250);
            g.drawLine(150, 250, 175, 225);
            bottomx += 9;
            centerx += 9;

        break;

        case 9:
            g.drawLine(225, 225, 250, 250);
            g.drawLine(225, 250, 250, 225);
            bottomx += 2;
            rightx += 2;
            diag1x += 2;

        break;

        if ( topx == 15 || middlex == 15 || bottomx == 15 || leftx == 15 || centerx == 15 || rightx == 15 || diag1x == 15 || diag2x == 15 ) {
             String XWIN = ("X wins!");
             g.drawString(XWIN, 60, 50);
            }

        g.setColor( Color.GREEN);
        Scanner keyboard2 = new Scanner(System.in); 
        int p2  = keyboard2.nextInt();
        System.out.print("PLAYER 1 enter location of x: ");
        switch (p2){

        case 1:
            g.drawOval(75, 75, 25, 25);
            topo += 8;
            lefto += 8;
            diag1o +=8;
        break;

        case 2:
            g.drawOval(150, 75, 25, 25);
            centero += 1;
            topo += 1;
        break;

        case 3:
            g.drawOval(225, 75, 25, 25);
            righto += 6;
            topo += 6;
            diag2o += 6;
        break;

        case 4:
            g.drawOval(75, 150, 25, 25);
            middleo += 3;
            lefto += 3;
        break;

        case 5:
            g.drawOval(150, 150, 25, 25);
            diag1o += 5;
            diag2o += 5;
            middleo += 5;
            centero += 5;
        break;

        case 6:
            g.drawOval(225, 150, 25, 25);
            middleo += 7;
            righto += 7;
        break;

        case 7:
            g.drawOval(75, 225, 25, 25);
            diag2o += 4;
            lefto += 4;
            bottomo += 4;
        break;

        case 8:
            g.drawOval(150, 225, 25, 25);
            bottomo += 9;
            centero += 9;
        break;

        case 9:
            g.drawOval(225, 225, 25, 25);
            bottomo += 2;
            righto += 2;
            diag1o += 2;
        break;          

        if ( topo == 15 || middleo == 15 || bottomo == 15 || lefto == 15 || centero == 15 || righto == 15 || diag1o == 15 || diag2o == 15 ) {
             String OWIN = ("O wins!");
             g.drawString(OWIN, 60, 50);
            }
        default : System.out.println("This is not a valid input. Please enter a number 1 through 9.");

                }

            }

        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Your break is before the conditional, hence the code cannot be reached. – Dave Newton Jan 13 '14 at 18:37
  • It appears you have the `if` defined after the last `break;` of your `switch`. That would define it as unreachable code. – CodeChimp Jan 13 '14 at 18:41
  • Also, correcting your tabbing would help show that things are not lined up right. – CodeChimp Jan 13 '14 at 18:41
  • *"The entire code is shown.."* Wow! 213 LOC in one method.. This problem could be cut down to a single [MCVE](http://stackoverflow.com/help/mcve) of no more than 50 LOC. Please do that in future. Also not that any method should typically be no more than a 'screen-full'. 213 LOC is ***far too long!*** – Andrew Thompson Jan 13 '14 at 18:45
  • Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). Also, does it use AWT or Swing? – Andrew Thompson Jan 13 '14 at 18:47
  • Thank you all greatly for your responses. I apologize for poorly formatting this question and displaying the entire code. I am very new to programming in java, or programming in general for that matter. I appreciate your help and feedback making this a learning process. @AndrewThompson, AWT is used in this code. – ohnogodzilla Jan 13 '14 at 18:53
  • 1
    OK.. Is it specified by the school? Also: Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. -- There are two questions, **and I expect to see two answers from you.** – Andrew Thompson Jan 13 '14 at 18:57
  • 1. Yes, according to our cirriculum we implement AWT and I dont believe my teacher has ever specified using Swing, however I now see this to be a better practice. 2. The reason AWT rather than Swing was used is because such was instructed to be used by the entire class. – ohnogodzilla Jan 13 '14 at 19:03
  • @JVMATL No it wouldn't. The [tag:homework] tag was retired some time ago. For very good reasons. It is felt that either a question is worth answering, or not, based purely on its merits. Where it originates from is not relevant. – Andrew Thompson Jan 13 '14 at 21:37
  • @ohnogodzilla Tip: Add an `@` before a person's name to *notify* them of a new comment. As to the teacher. Tell them that the person who wrote that blog article (me) thinks they need sacking. They are a disgrace to the profession of teaching. And my deepest condolences to you for having such an antiquated teacher. :-( – Andrew Thompson Jan 13 '14 at 21:40

2 Answers2

5

It's right after a break for heaven's sake. The break forces the code flow to end and to leave the current block, an so any code after it in the block is never reached. And so the compiler is complaining correctly. Don't do this.

There's much other problematic code in your post including using a Scanner in a paint method.

You should scrap this code, and read the Swing tutorials, and the general Java tutorials. For one thing you should almost never override a paint method, but instead the paintComponent method of a JComponent or JPanel. For another these methods should be for painting and painting only and should never be blocked for instance by a Scanner. This will just freeze your GUI making it useless.

I would re-structure this program greatly.

  • I wouldn't use a Scanner at all in a GUI program. GUI programs by their nature are event-driven, while Scanner type code is linear console code which is completely incompatible.
  • Instead use listeners such as a MouseListener on JLabels held in a JPanel that uses GridLayout.
  • If I painted anything, again it would be in a paintComponent override in a JPanel.
  • But I'd try avoiding painting and instead would swap ImageIcons in my JLabel that held "X", "O", or blank images, depending on their state.

Edit: Since you have to use AWT, some changes to the recs:

  • Still, do not use Scanner for the very same reasons.
  • Still use event-driven programming such as a MouseListener attached to Labels.
  • If you need to do custom graphics, then override the paint method.
  • Again, no program logic and no blocking code should go in this method. It should be for drawing and drawing only.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
2

The error has nothing to do with the contents of your if statement; you put the if statement right after a break statement, and before a subsequent case -- there's no way for program execution to reach that statement. (That's what 'unreachable code' means -- in general, when you see that error, you need to step back and look at the context, and try to figure out what set of inputs should reach that line - usually, while you are trying to figure that out, you will see why it can't happen.)

JVMATL
  • 2,064
  • 15
  • 25