1

C++ クラスのプロジェクトに取り組んでいます。プレイヤーと対戦する最小限の AI を使用して、ゲーム Hex のコマンド ライン バージョンを実装する以前の課題を提出しました。私の現在の任務では、モンテカルロ関連のものを使って AI を拡張しています。それに備えて、AI の決定を独自の別のクラスに移動しようとしています (元々は、main を含むファイルの関数として呼び出されていました)。G++ は、私が理解できない恐ろしいコンパイラ エラーを私に与えているので、誰かが私の構文エラーがどこにあるかを指摘できることを願っています。時間。HexAI.cpp の関数の内容は問題ないはずです。それらは元の場所からコピーアンドペーストされたため、それらが参照したグローバル変数も移動しました。私が追加したのは、クラス定義、コンストラクター/デストラクター、および HexAI::getMove() 関数だけです。ベクトル ボードの宣言は整数の 2 次元ベクトルであり、スポットが取られている場合は 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
4

3 に答える 3

3

std::ヘッダー内のすべての宣言の前に追加するvectorと、ほとんどのエラーが修正されます。たとえば:

typedef vector<int> intvec;

次のようにする必要があります。

typedef std::vector<int> intvec;

~デストラクタの実装で混同しました。これは次のとおりです。

~HexAI::HexAI(){

次のようにする必要があります。

 HexAI::~HexAI(){

isの使用はお勧めしませんが、using namespace std;入力を少し節約できますが、後で問題が発生する可能性があります。

于 2013-06-03T22:17:27.880 に答える
0

変化する

typedef vector<int> intvec;

typedef std::vector<int> intvec;
于 2013-06-03T22:17:50.187 に答える
0
  • HexAI.h にはnousing namespace stdがないため、「ベクター」が何であるかはわかりません。std::vector代わりに使用する必要があります。
  • ~HexAI::HexAIする必要がありますHexAI::~HexAI

それ以外の場合はコンパイルする必要があります。私が助けてくれることを願っています。

于 2013-06-03T22:17:52.170 に答える