What1slove / RefactoringLab

Вторая лабораторная по рефакторингу
0 stars 0 forks source link

Длинный метод + прочие недостатки (Board) #4

Open What1slove opened 4 years ago

What1slove commented 4 years ago

Метод paint большого размера (примерно 120 строчек), состоит из кусков связанных кусков кода разделённых комментариями Почему плохо: 1 Тяжело читается и понимается 2 Тяжело модифицируется Так же в методе есть две взаимоисключающие проверки на одном уровне: if (gameover) и if (!gameover) Код: `public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g;

    if (pause)
    {
        g.setColor(Color.white);
        g.setFont(bigfnt);
        drawCenteredText(g2d, "PAUSED", getHeight() / 2);
        return;
    }

    super.paint(g); // will clear screen

    if (!gameover) {
        // draw level board
        drawBoard(g2d, levelinfo.getBoardFrontCoords(), crawler.getColumn());

        // draw crawler
        if (crawler.isVisible()){
            Color c = Color.YELLOW;
            if (dptLeft > 0)
                c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
            drawObject(g2d, c, crawler.getCoords(), crawlerzoffset);
        }

        if (boardpov < -Crawler.CHEIGHT) {
            // pov shows game level board in the distance; add stars for fun
            for (List<int[]> s : stars) {
                Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
                drawObject(g2d, c, s);
            }
        }

        // draw crawler's missiles
        Color missileColors[] = {Color.BLUE, Color.RED, Color.green};
        for (Missile m : crawler.getMissiles()) {
            if (m.isVisible()) {
                drawObject(g2d, Color.YELLOW, m.getCoords(levelinfo));
                drawObject(g2d, missileColors[r.nextInt(missileColors.length)], m.getLayerCoords(levelinfo));
            }
        }

        // draw exes
        for (Ex ex : exes) {
            if (ex.isVisible())
                if (ex.isPod())
                    drawObject(g2d, Color.MAGENTA, ex.getCoords(levelinfo), crawlerzoffset);
                else
                    drawObject(g2d, Color.RED, ex.getCoords(levelinfo), crawlerzoffset);
            else {
                // not visible but still in list means just killed
                drawObject(g2d, Color.WHITE, ex.getDeathCoords(levelinfo)); 
            }
        }

        // draw enemy missiles
        for (Missile exm : enemymissiles) {
            if (exm.isVisible()) {
                drawObject(g2d, Color.GRAY, exm.getCoords(levelinfo));
                drawObject(g2d, Color.RED, exm.getLayerCoords(levelinfo));
            }
        }

        // draw spikes and spinnythings
        for (Spike s : spikes) {
            if (s.isVisible()) {
                List<int[]> spikeCoords = s.getCoords(levelinfo);
                drawObject(g2d, Color.GREEN, spikeCoords);
                spikeCoords.set(0, spikeCoords.get(1)); // add white dot at end
                drawObject(g2d, Color.WHITE, spikeCoords);
                if (s.isSpinnerVisible()) {
                    List<int[]> spinCoords = s.getSpinnerCoords(levelinfo);
                    drawObject(g2d, Color.GREEN, spinCoords);
                }
            }
        }

        // other crudethings?  vims, for extra lives?
    }

    g2d.setColor(Color.GREEN);
    g2d.setFont(bigfnt);

// g2d.drawString("SCORE:", 5, 15); g2d.drawString(Integer.toString(score), 100, 50); if (score > hiscore) hiscore = score; g2d.setFont(stdfnt); drawCenteredText(g2d, "HIGH: " + hiscore, 30); drawCenteredText(g2d, "LEVEL: "+levelnum, 55); g2d.drawString("LIVES:", 680, 30); g2d.drawString(Integer.toString(lives), 745, 30);

    if (levelprep){
        g2d.setColor(Color.YELLOW);
        drawCenteredText(g2d, "SUPERZAPPER RECHARGE", B_HEIGHT *2/3);
    }

    if (gameover) {
        g.setColor(Color.GREEN);
        drawCenteredText(g2d, "GAME OVER", getHeight() / 2, bigfnt);
        drawCenteredText(g2d, "PRESS SPACE TO RESTART", getHeight() * 3/4);

        FileWriter f=null;
        try {
            f = new FileWriter("wbt.hi");
            f.write(Integer.toString(hiscore));
            f.close();
        }
        catch (Exception e)
        { // if we can't write the hi score file...oh well. 
        }

    }

    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}`
LidiaIvanova commented 4 years ago

Так же в методе есть две взаимоисключающие проверки на одном уровне: if (gameover) и if (!gameover) - это можно засчитать как условную сложность