【发布时间】:2015-12-14 03:43:35
【问题描述】:
我正在尝试制作一个简单的 AI 来玩tictactoe,但它不起作用;当板子填满时,我得到一个 'std::out_of_range'(暴力破解直到第一个分支的第一个最终节点),我完全没有找到解决方案。
如果我在 0,0 处下 X,它会在最佳移动返回时崩溃
return jogadas.at(melhorMov); // return best move 第 10 次迭代后。
(Windows MingW 的完整代码 here 如果您愿意)
(上面显示的游戏是暴力破解步骤,而不是实际游戏)
人工智能算法:
-
移动/启动 AI
void IA::movimenta(){ //lauches the AI move action std::cout << "**Pensando**" << std::endl; //little message pensamentos = 0; //counter of iterations cmp = campo.getcampo(); //gets the pointer** to the tictactoe field AiMove jogada = MelhorJogada(AIjogador); //gets the best move for the AIplayer campo.joga(AIjogador,jogada.x,jogada.y); //puts the best move on the field } -
极小极大代码
#include "ia.h" #include <vector> #include <iostream> using namespace std; IA::IA() { } void IA::inicia(field &campo, field::jogador IAplayer){ this->campo = campo; this->AIjogador = IAplayer; if(IAplayer == field::x){ this->HUjogador = field::o; }else{ this->HUjogador = field::x; } } void printacamp(field::campo **campo){ cout << " 0 1 2" << endl; for (int var = 0; var < 3; ++var) { cout << var; cout << "|"; for (int var2 = 0; var2 < 3; ++var2) { switch (campo[var][var2]) { case field::NADA: cout << " "; break; case field::x_c: cout << "X"; break; case field::o_c: cout << "O"; break; default: break; } cout << "|"; } cout << endl; } } void IA::movimenta(){ //lauches the AI move action std::cout << "**Pensando**" << std::endl; //little message pensamentos = 0; //couter of iterations cmp = campo.getcampo(); //gets the pointer** to the tictactoe field AiMove jogada = MelhorJogada(AIjogador); //gets the best move for the AIplayer campo.joga(AIjogador,jogada.x,jogada.y); //puts the best move on the field } AiMove IA::MelhorJogada(field::jogador jogador){ //bruteforce through the possible moves and searches for the best one field::jogador vic = campo.checaJ(); // checks if someone wins the game (final node) if(vic == AIjogador){ //if AIplayer wins return AiMove(15); //add 15 points } else if(vic == jogador){ //if human wins return AiMove(-15); //add -15 points } else if(vic == field::NINGJ){ //if its a tie (full board no winners) return AiMove(0); //score 0 } //if none of those, the game is still in progress std::vector<AiMove> jogadas; //vector for moves, something tells me that it should be static but it gives alot of bugs and nosense moves (or no moves at all) AiMove jogada; //move for this iteration pensamentos++; //add counter cout << pensamentos << endl; //print counter for (int y = 0; y < 3; ++y) { //for each of the board for (int x = 0; x < 3; ++x) { if(cmp[x][y] == field::NADA){ // if its an empty space jogada.x = x; //gets the x,y of the empty space jogada.y = y; campo.jogaJ(jogador,jogada.x,jogada.y); //makes a move printacamp(cmp); //prints it on screen if(jogador == AIjogador){ //if its max(AI) next is human jogada.pontos = MelhorJogada(HUjogador).pontos; }else{ //if its min(Human) next is AI jogada.pontos = MelhorJogada(AIjogador).pontos; } jogadas.push_back(jogada); //store the score of the final node campo.jogaJ(field::NADAJ,jogada.x,jogada.y); // delete the move made for testings } } } int melhorMov = 0; // bestMove is 0 if(jogador == AIjogador){ // if AIturn(max) int melhorPon = -1000000; // best pontuation = -∞ for (int var = 0; var < jogadas.size(); ++var) { //best move/points of the subbranches if(jogadas.at(var).pontos > melhorPon){ melhorMov = var; melhorPon = jogadas.at(var).pontos; } } } else { //if human(min) int melhorPon = 1000000; //best pontution = +∞ for (int var = 0; var < jogadas.size(); ++var) { //best move/points of the subbranches if(jogadas.at(var).pontos < melhorPon){ melhorMov = var; melhorPon = jogadas.at(var).pontos; } } } return jogadas.at(melhorMov); // return best move } -
人工智能.h
#pragma once #ifndef IA_H #define IA_H #include "field.h" #include <vector> struct AiMove { AiMove() {}; AiMove(int Pontos) : pontos(Pontos) {} int x; int y; int pontos; }; class IA { public: IA(); void inicia(field &camp, field::jogador IAplayer); void movimenta(); private: field campo; AiMove MelhorJogada(field::jogador HUjogador); field::campo **cmp; field::jogador HUjogador; field::jogador AIjogador; int pensamentos = 0; }; #endif // IA_H
【问题讨论】:
-
SO 不是代码调试服务。
-
如果你建议,是的,我确实尝试调试代码,但我找不到修复它的方法,或者在逻辑中找到错误
-
请明确指出抛出异常的代码行。
-
如果我在 0,0 处玩 X,它会在返回最佳移动时崩溃
return jogadas.at(melhorMov); // return best move
标签: c++ artificial-intelligence tic-tac-toe minimax