HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Chess programming algorithm minimax to Alpha Beta

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
alphaminimaxprogrammingchessalgorithmbeta

Problem

I have written some code for alpha beta for Chess programming and not sure completely if that is the correct way to do it. Can someone please tell me if I implemented the alpha_beta properly?

```
public class Minimax{
private boolean side;
public static int depth;

public Minimax(boolean inputSide, int inputDepth) {
side = inputSide;
depth = inputDepth;
}

public int[] getMoves(Board board, boolean white, int inputDepth, boolean check) {
ArrayList values = new ArrayList();
int initScore = board.getScore();
boolean multipleMoves = false;
ArrayList multiple = new ArrayList();
if (white) {
values.add(new int[]{-2000, 0, 0, 0, 0});
}
else {
values.add(new int[]{2000, 0, 0, 0, 0});
}
int position = 0;
if (inputDepth == depth) {
int[] ret = {board.getScore(), 0, 0, 0, 0};
return ret;
}
for (Pieces[] subPiece : board.getArray()) {
for (Pieces piece : subPiece) {
if (piece != null && piece.white() == white) {
int[][] moves = piece.getMoves(board, false);
if (moves != null) {
for (int[] move : moves) {
Board newBoard = board.newBoard(move[0], move[1], move[2], move[3]);
if (newBoard != null) {
int score = getMoves(newBoard, !white, inputDepth + 1, newBoard.check(white))[0];
values.add(new int[]{score, move[0], move[1], move[2], move[3]});
if ((white && score > values.get(position)[0]) || (!white && score < values.get(position)[0])) {
position = values.size() - 1;
multipleMoves = false;
multiple.clear();
}

Solution

A couple of things on top of what @tim already covered.

Use Random.nextInt instead of Math.random

Instead of this:

position = (int)((multiple.size() - 1) * Math.random() + 1);


you can write much simpler like this:

position = 1 + random.nextInt(multiple.size() - 1);


where random is an instance of Random, created for example in the constructor. Simpler isn't it?

Use interface types instead of implementations

Instead of:

ArrayList values = new ArrayList();


This would be better:

List values = new ArrayList();


(but not quite good enough, see the next item)

Define Abstract Data Types for your problem domain

An Abstract Data Type (ADT) is essentially a collection of data and operations on that data.
In object oriented languages an ADT is usually modeled with a class,
where data is represented by member fields and the operations are represented by methods.

When I look at that ArrayList in the previous point,
I've no idea what it stands for,
what kind of logical object it represents,
and what the numbers in new int[]{-2000, 0, 0, 0, 0} and the other array initializations scattered in the code mean.

If you have defined an ADT for this,
the code would become all the more readable.
Instead of new ArrayList,
there might be new MinimaxValueSet().
Instead of values.add(new int[]{-2000, 0, 0, 0, 0}),
there might be valueSet.addNewSetWithScore(-2000),
where this helper method would fill in the repetitive zeros.

Don't print to the console

I suppose the println here was added for debugging:

public int getDepth() {
        System.out.println(depth);
        return depth;


It doesn't belong there and should be removed.
(I suggest to remove debugging code before pasting for code review,
it's just noise for reviewers.)

Code Snippets

position = (int)((multiple.size() - 1) * Math.random() + 1);
position = 1 + random.nextInt(multiple.size() - 1);
ArrayList<int[]> values = new ArrayList<int[]>();
List<int[]> values = new ArrayList<int[]>();
public int getDepth() {
        System.out.println(depth);
        return depth;

Context

StackExchange Code Review Q#88029, answer score: 5

Revisions (0)

No revisions yet.