🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Reversi game help

Started by
2 comments, last by Alberth 4 years, 3 months ago

I'm trying to write an Othello game.

I wrote some code to check if a square is a legal move:

    // checks if black can move somewhere
    static boolean isLegalMove(Board board, int squareNumber) {
        int startX = squareNumber % 8, startY = squareNumber / 8;
        if (board.boardColors[startX][startY] != 'e') return false;

        char[] string = new char[80];

        for (int dy = -1; dy <= 1; dy++)
            for (int dx = -1; dx <= 1; dx++) {
                // keep going if both velocities are zero
                if (dy == 0 && dx == 0) continue;
                string[0] = 0;

                //////////////////////
                for (int ctr = 1; ctr < 8; ctr++) {
                    int x = startX + ctr * dx;
                    int y = startY + ctr * dy;
                    if (x >= 0 && y >= 0 && x < 8 && y < 8) string[ctr - 1] = board.boardColors[x][y];
                    else string[ctr - 1] = 0;
                }
                ///////////////////////

                if (canMove(string)) return true;
            }
        return false;
    }

Anyone know how I can make this code better?

Thank you.

Advertisement

Depend on the style
Read the post “the box” it explain a lot

But for what i see, you are probably doing to much in one place, not commentd code. Even if it is in a object could go “spaggeti” code quickly, which is unmantainable code.

In my code i'm very methodical, every thing is made width a purpose.

You have very stuff in there: bad naming of variables. Variables called string. Which you can read in just a couple of files, in large projects can be very hard to read, etc…

  • ‘e’ in your board test is magic, it is better to make a constant named EMPTY or such with that value, and use the constant everywhere.
  • You are minimizing the number of curly brackets to group code. I used to do that too. For single line statements (your ‘continue’ line or the ‘canMove’ line) that is mostly fine (although some people write curly brackets even there), for multi-line statements (ie the entire ‘for dy loop’, and the if/else on the x/y bounds test), I would recommend to simply always write curly brackets. It reduces the amount of careful thinking you have to do whether the compiler understands correctly what you wrote.
  • I don't understand why you need to make a copy of the board into a string to decide “canMove”. Can't you simply pass (startx, starty, dx, dy) into “canMove”?
  • That also eliminates allocating 20-40 times a string of length 80 (assuming you call this function for every square).
  • Why does the ‘ctr’ loop keep going when you hit the edge of the board? Is there a purpose to continue writing zeroes in the string?
  • Why do you allocate 80 characters if you only use 8?

This topic is closed to new replies.

Advertisement