我正在我的 C++ 类中处理一个项目。我上交了之前的作业,我在其中实现了 Hex 游戏的命令行版本,使用最少的 AI 来与玩家对战。对于我目前的任务,我正在用一些 Monte Carlo 的东西来扩展 AI。为此,我试图将 AI 决策移到它自己的单独类中(它最初只是作为包含 main 的文件中的函数调用)。G++ 给我带来了我不理解的可怕的编译器错误,所以希望有人能指出我的语法错误在哪里,因为我是 C++ 新手并且无法追踪它们,而且 g++ 错误并不是特别有用时间。HexAI.cpp中的函数内容应该不是问题,因为它们是从原始位置复制粘贴的,所以我还移动了它们引用的全局变量。我唯一添加的是类定义、构造函数/析构函数和 HexAI::getMove() 函数。向量板声明是一个整数的二维向量,如果该点被占用,则用于保存 0,如果玩家 1 拥有该位置,则为 1,如果玩家 2 拥有该位置,则用于保存 2。提前感谢所有帮助,由于我是新手,请随时指出我遗漏的任何内容,并尽可能详细地指出您的需要。我只想了解发生了什么并继续前进,因为我整个下午都被困在这上面。如果玩家 1 拥有它,则为 1,如果玩家 2 拥有它,则为 2。提前感谢所有帮助,由于我是新手,请随时指出我遗漏的任何内容,并尽可能详细地指出您的需要。我只想了解发生了什么并继续前进,因为我整个下午都被困在这上面。如果玩家 1 拥有它,则为 1,如果玩家 2 拥有它,则为 2。提前感谢所有帮助,由于我是新手,请随时指出我遗漏的任何内容,并尽可能详细地指出您的需要。我只想了解发生了什么并继续前进,因为我整个下午都被困在这上面。
HexAI.h
#include <vector>
#ifndef HEXAI_H
#define HEXAI_H
typedef vector<int> intvec;
class HexAI{
public:
HexAI(int s);
~HexAI();
int* getMove(vector<intvec> board);
private:
void AIRandomMove(vector<intvec> board);
void AI (vector<intvec> board);
int compMadeMove; //decides if it is the first turn or not
int compMoveI; //row
int compMoveJ; //column
int size; //size of the board
};
#endif
HexAI.cpp
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "HexAI.h"
using namespace std;
typedef enum playerColor{
neutral, p1, p2
}playerColor;
typedef vector<int> intvec;
HexAI::HexAI(int s){
compMoveI = 0;
compMoveJ = 0;
compMadeMove = false;
size = s;
srand(time(NULL));
}
~HexAI::HexAI(){
//nothing needs to be deleted
}
//when it throws its hands in the air
void HexAI::AIRandomMove(vector<intvec> board){
bool acceptableMove = false;
while( not acceptableMove){
int a = rand() % (size - 1);//it has trouble when its randomly on the bottom
int b = rand() % size;
if(board[a][b] == neutral){
acceptableMove = true;
compMoveI = a;
compMoveJ = b;
}
}
}
//makes a decision based on the board, sets 2 variables
//that are read in as the move later on
//tries to go straight top to bottom, move left or right if that spot is taken.
//goes for a random move if it can't decide how to move
void HexAI::AI (vector<intvec> board){
//catch seg faults that show up 1 turn after random indexes put it on the bottom row
if(compMadeMove and compMoveI == (size - 1) ){ //if it randomly ends up on the bottom, move it to the top and start over, here to fix seg faults
compMadeMove = false;
}
//determine starting position
if( not compMadeMove ){//first move, pick a spot on the top
bool goodguess = false;
int startPos = rand() % size;
while(!goodguess){
if(board[0][startPos] != 0){
startPos = rand() % size;
}else{
goodguess = true;
}
}
compMoveI = 0;
compMoveJ = startPos;
compMadeMove = true;
}else{//later moves
int potentialMove = board[compMoveI + 1][compMoveJ];
if(potentialMove == neutral){//try to just move down one
compMoveI += 1;
}else{//try downleft or just left or right
potentialMove = board[compMoveI + 1][compMoveJ - 1];
if(potentialMove == neutral){//downleft
compMoveI += 1;
compMoveJ -= 1;
}else{//downLeft was taken, go left or right
potentialMove = board[compMoveI][compMoveJ + 1];
if(potentialMove == neutral){//go right
compMoveJ += 1;
}else{//hopefully left works?
potentialMove = board[compMoveI][compMoveJ - 1];
if(potentialMove == neutral){
compMoveJ -= 1;
}else{//ARG I give up, random
AIRandomMove(board);
}
}
}
}
}
if(compMoveI < 0 or compMoveI >= size or compMoveJ < 0 or compMoveJ >= size){
AIRandomMove(board);
}
cout << endl << "My move is " << compMoveI << " " << compMoveJ << endl << endl;
}
int* HexAI::getMove(vector<intvec> board){
AI(board);
int* ij = new int[2];
ij[0] = compMoveI;
ij[1] = compMoveJ;
return ij;
}
g++ 错误
g++ -c -Wall HexAI.cpp
In file included from HexAI.cpp:5:
HexAI.h:6: error: expected initializer before â<â token
HexAI.h:13: error: expected â;â before â(â token
HexAI.h:17: error: âvectorâ has not been declared
HexAI.h:17: error: expected â,â or â...â before â<â token
HexAI.h:18: error: âvectorâ has not been declared
HexAI.h:18: error: expected â,â or â...â before â<â token
HexAI.cpp:23: error: expected constructor, destructor, or type conversion before â::â token
HexAI.cpp:29: error: prototype for âvoid HexAI::AIRandomMove(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)â does not match any in class âHexAIâ
HexAI.h:17: error: candidate is: void HexAI::AIRandomMove(int)
HexAI.cpp:47: error: prototype for âvoid HexAI::AI(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)â does not match any in class âHexAIâ
HexAI.h:18: error: candidate is: void HexAI::AI(int)
HexAI.cpp:96: error: no âint* HexAI::getMove(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)â member function declared in class âHexAIâ
make: *** [HexAI.o] Error 1