Java minimax game on dots and boxes

I am trying to implement an AI that Minimax uses to play with dots and boxes ( http://en.wikipedia.org/wiki/Dots_and_Boxes )

Here is what I still have:

    public Line makeMove(GameState gs) {
    if (gs.getRemainingLines().size() == 1) {
        return gs.getRemainingLines().get(0);
    }

    if (gs.getPlayer() == 1) {
        int minscore = -1;
        GameState g = gs.clone();
        Line lnew = null;
        List<Line> l = gs.getRemainingLines();
        for (Line l2 : l) {
            g.addLine(l2);
            if (evaluate(g) > minscore) {
                minscore = (evaluate(g));
                lnew = l2;
            }
        }
        return lnew;
    } else {
        int maxscore = 999;
        GameState g = gs.clone();
        Line lnew = null;
        List<Line> l = gs.getRemainingLines();
        for (Line l2 : l) {
            g.addLine(l2);
            if (evaluate(g) < maxscore) {
                maxscore = (evaluate(g));
                lnew = l2;
            }
        }
        return lnew;
    }

}

However, he continues to return null, and I do not think that I observe minimax correctly. Can anyone give me some pointers.

getRemainingLines() returns a list of moves that are still possible.

evaluate() returns int for evaluation.

+4
source share
2 answers

. ( ) , . , gs.getRemainingLines ? ( ?)

, , , .

- :

float minimax_max(GameState g)
{
    if (g is terminal or max depth reached)
        return eval(g);

    float bestVal = -inf;
    bestMove = null;

    moves = g->getLegalMoves();
    for (m : moves)
    {
        ApplyMove(m);
        if (g->nextPlayer == maxPlayer)
            nextVal = minimax_max(g);
        else
            nextVal = minimax_min(g);
        if (nextVal > bestVal)
        {
            bestVal = nextVal;
            bestMove = m;
        }
        UndoMove(m);
    }

    return bestVal;
}

, / , . minimax_min, if.

, , . , .

, , GetMoves(), ApplyMove(), UndoMove() eval(), . ( , .)

, , , :

  • .

  • , , , . ( , , .)

  • , , . ( , .)

  • , , . (, , 1 )

  • . , , , .

  • , . .

(, , null, , ), .

+2

, , , gs.getRemainingLines() .

, GameState g . , ,

int minscore = -1;
Line lnew = null;
List<Line> l = gs.getRemainingLines();
for (Line l2 : l) {
    GameState g = gs.clone();
    g.addLine(l2);
    if (evaluate(g) > minscore) {
        minscore = (evaluate(g));
        lnew = l2;
    }
}

int minscore = -1;
GameState g = gs.clone();
Line lnew = null;
List<Line> l = gs.getRemainingLines();
for (Line l2 : l) {
    g.addLine(l2);
    if (evaluate(g) > minscore) {
        minscore = (evaluate(g));
        lnew = l2;
    }
    g.removeLine(l2);
}

, minimax (http://en.wikipedia.org/wiki/Minimax), , makeMove ( , min-max, ).

public GameState makeMove(GameState gs) {
   if (gs.getRemainingLines().size() == 1) {
       GameState g = gs.clone();
       g.addLine(gs.getRemainingLines().get(0));
       return g;
   }

   if (gs.getPlayer() == 1) {
       GameState g = gs.clone();
       g.setPlayer(2);
       int bestValue = -1;
       Line lbest = null;
       List<Line> lines = gs.getRemainingLines();
       for (Line line : lines) {
           g.addLine(line);
           GameState val = makeMove(g);
           g.removeLine(line);
           if (evaluate(val) > bestValue) {
               bestValue = evaluate(g);
               lbest = line;
           }
       }
       g.addLine(lbest);
       return g;
   } else {
       GameState g = gs.clone();
       g.setPlayer(1);
       int bestValue = 999;
       Line lbest = null;
       List<Line> lines = gs.getRemainingLines();
       for (Line line : lines) {
           g.addLine(line);
           GameState val = makeMove(g);
           g.removeLine(line);
           if (evaluate(val) < bestValue) {
               bestValue = evaluate(g);
               lbest = line;
           }
       }
       g.addLine(lbest);
       return g;
   }

}
+1

Source: https://habr.com/ru/post/1534083/


All Articles