Skip to main content
added 591 characters in body
Source Link
Roland Illig
  • 21.3k
  • 2
  • 33
  • 83

First, you should ask yourself whether the names you chose are good. For example, a typical game of connect four takes place on a single Board, yet you defined an array of boards and call this array a board. This doesn't make sense.

A completely different topic is spelling rules. In Java, field and variable names start with a lowercase letter. You have Board Board sometimes, which confuses every reader.

You did not say what you mean by optimize. But no matter if it's for speed or for clarity, you should separate the complicated game logic from the user interface logic. Doing this enables you to test the game logic using unit tests.

If you strive for speed, you should remember the last position where a piece was dropped, since you only need to check at this particular position whether there is a new row of 4.

Please use a program that formats your code whenever you save it. This will make it look more consistent.

Your use of the random number generator is unfair, since it favors lower indexes. A quick fix would be (int)(Math.random() * bestMoves.size()), but that still looks complicated. A better way is to define a random number generator and ask it for integer numbers:

class MiniMax {
    Random rnd = new Random();

    int randomMove() {
        return bestMoves.get(rnd.nextInt(bestMoves.size());
    }
}

Note that I changed the class name from Tree to MiniMax. By choosing clear names you make the comments of the form // This is … superfluous.

First, you should ask yourself whether the names you chose are good. For example, a typical game of connect four takes place on a single Board, yet you defined an array of boards and call this array a board. This doesn't make sense.

A completely different topic is spelling rules. In Java, field and variable names start with a lowercase letter. You have Board Board sometimes, which confuses every reader.

You did not say what you mean by optimize. But no matter if it's for speed or for clarity, you should separate the complicated game logic from the user interface logic. Doing this enables you to test the game logic using unit tests.

If you strive for speed, you should remember the last position where a piece was dropped, since you only need to check at this particular position whether there is a new row of 4.

Please use a program that formats your code whenever you save it. This will make it look more consistent.

First, you should ask yourself whether the names you chose are good. For example, a typical game of connect four takes place on a single Board, yet you defined an array of boards and call this array a board. This doesn't make sense.

A completely different topic is spelling rules. In Java, field and variable names start with a lowercase letter. You have Board Board sometimes, which confuses every reader.

You did not say what you mean by optimize. But no matter if it's for speed or for clarity, you should separate the complicated game logic from the user interface logic. Doing this enables you to test the game logic using unit tests.

If you strive for speed, you should remember the last position where a piece was dropped, since you only need to check at this particular position whether there is a new row of 4.

Please use a program that formats your code whenever you save it. This will make it look more consistent.

Your use of the random number generator is unfair, since it favors lower indexes. A quick fix would be (int)(Math.random() * bestMoves.size()), but that still looks complicated. A better way is to define a random number generator and ask it for integer numbers:

class MiniMax {
    Random rnd = new Random();

    int randomMove() {
        return bestMoves.get(rnd.nextInt(bestMoves.size());
    }
}

Note that I changed the class name from Tree to MiniMax. By choosing clear names you make the comments of the form // This is … superfluous.

Source Link
Roland Illig
  • 21.3k
  • 2
  • 33
  • 83

First, you should ask yourself whether the names you chose are good. For example, a typical game of connect four takes place on a single Board, yet you defined an array of boards and call this array a board. This doesn't make sense.

A completely different topic is spelling rules. In Java, field and variable names start with a lowercase letter. You have Board Board sometimes, which confuses every reader.

You did not say what you mean by optimize. But no matter if it's for speed or for clarity, you should separate the complicated game logic from the user interface logic. Doing this enables you to test the game logic using unit tests.

If you strive for speed, you should remember the last position where a piece was dropped, since you only need to check at this particular position whether there is a new row of 4.

Please use a program that formats your code whenever you save it. This will make it look more consistent.