极大极小算法的井字失败 [英] MiniMax Algorithm for Tic Tac Toe failure

查看:254
本文介绍了极大极小算法的井字失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 通过<一href="https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value">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)

这篇关于极大极小算法的井字失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆