极大极小算法的井字失败极小、算法

由网友(大叔的仙女棒)分享简介:我想实现一个极大极小算法的井字与α-β剪枝。现在我有程序的运行,但它似乎并不奏效。每当我运行它似乎在所有的方块输入垃圾。所以,我的极小函数接受一个板状态,并修改了状态,这样,当它完成,董事会状态包含了一个最好的举动,我实现它。于是,我把这等于修改后的板上。这里是我的函数的极大极小的算法:I'm trying to i...

我想实现一个极大极小算法的井字与α-β剪枝。现在我有程序的运行,但它似乎并不奏效。每当我运行它似乎在所有的方块输入垃圾。所以,我的极小函数接受一个板状态,并修改了状态,这样,当它完成,董事会状态包含了一个最好的举动,我实现它。于是,我把这等于修改后的板上。这里是我的函数的极大极小的算法:

I'm trying to implement a minimax algorithm for tic tac toe with alpha-beta pruning. Right now I have the program running, but it does not seem to be working. Whenever I run it it seems to input garbage in all the squares. I've implemented it so that my minimax function takes in a board state and modifies that state so that when it is finished, the board state contains the next best move. Then, I set 'this' to equal the modified board. Here are my functions for the minimax algorithm:

void board::getBestMove() {
  board returnBoard;
  miniMax(INT_MIN + 1, INT_MAX -1, returnBoard);

  *this = returnBoard;
}

int board::miniMax(int alpha, int beta, board childWithMaximum) {
  if (checkDone())
    return boardScore();

  vector<board> children = getChildren();
  for (int i = 0; i < 9; ++i) {
    if(children.empty()) break;

    board curr = children.back();
    if (curr.firstMoveMade) { // not an empty board
      board dummyBoard;
      int score = curr.miniMax(alpha, beta, dummyBoard);

      if (computerTurn && (beta > score)) {
        beta = score;
        childWithMaximum = *this;
        if (alpha >= beta) break;
      } else if (alpha < score) {
        alpha = score;
        childWithMaximum = *this;
        if (alpha >= beta) break;
      }
    }
  }
  return computerTurn? alpha : beta;
}

vector<board> board::getChildren() {
  vector<board> children;

  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (getPosition(i, j) == '*') { //move not made here
        board moveMade(*this);
        moveMade.setPosition(i, j);
        children.push_back(moveMade);
      }
    }
  }

  return children;
}

这是我的全部文件,如果有人想尝试运行它:

And here are my full files if someone wants to try running it:

的.cpp: http://pastebin.com/ydG7RFRX .H: http://pastebin.com/94mDdy7x

.cpp : http://pastebin.com/ydG7RFRX .h : http://pastebin.com/94mDdy7x

推荐答案

可能有很多问题,你的code ...你确定贴了不少吧。因为你问你的问题有责任在你尝试在自己的第一个一切你可以再降低问题的必要code的最小量,以澄清到底是怎么回事。正因为如此,我不认为你已经把太多精力在问这个问题。

There may be many issues with your code... you sure posted a lot of it. Because you are asking your question it is incumbent on you to try everything you can on your own first and then reduce your question to the smallest amount of code necessary to clarify what is going on. As it is, I don't feel that you've put much effort into asking this question.

但也许我还可以提供一些帮助:

But maybe I can still provide some help:

void board::getBestMove() {
  board returnBoard;
  miniMax(INT_MIN + 1, INT_MAX -1, returnBoard);

  *this = returnBoard;
}

请参阅如何说的话 *此= returnBoard

这一定意味着你想获得一个董事从后极大极小

That must mean that you want to get a board back from miniMax.

但怎么看待极大极小定义!

int board::miniMax(int alpha, int beta, board childWithMaximum)

它接受 childWithMaximum 通过pass按值所以它的不能的这种方式返回船上。

It accepts childWithMaximum via pass by value so it cannot return a board in this way.

您想要说什么可能是:

int board::miniMax(int alpha, int beta, board & childWithMaximum)
阅读全文

相关推荐

最新文章