Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我会得到一个“;矢量下标超出范围“;我的简单游戏中的错误? 我刚从2周内学会C++,决定用一个小ai做一个TIC TAC TOC游戏。_C++_Vector_Syntax_Syntax Error - Fatal编程技术网

为什么我会得到一个“;矢量下标超出范围“;我的简单游戏中的错误? 我刚从2周内学会C++,决定用一个小ai做一个TIC TAC TOC游戏。

为什么我会得到一个“;矢量下标超出范围“;我的简单游戏中的错误? 我刚从2周内学会C++,决定用一个小ai做一个TIC TAC TOC游戏。,c++,vector,syntax,syntax-error,C++,Vector,Syntax,Syntax Error,代码如下: #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <vector> #include <algorithm> using std::cout; using std::cin; using std::string; using std::vector; using std::time; using

代码如下:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>

using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::time;
using std::endl;

//global functions
void boardmaker(vector<vector<char>>& board);
void instructions(vector<vector<char>>& board);
void display(const vector<vector<char>>& board);
void error();
int randomizer(int limit);
bool move(int userchoice, vector<vector<char>>& rboard, int random);
bool wincon(const vector<vector<char>>& board);
void robot(vector<vector<char>>& board,  int xORy);
void useless(vector<vector<char>>& rboard, int choice);
//global elements
const char X= 'x';
const char O= 'O';
int blank;

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    //elements
    vector<vector<char>> board(3);
    int startposition = 5;
    bool success = false;
    int xORy = randomizer(2);
    int xORyV2 = xORy + 1;
    int x = 1;
    //intro of the program
    while (startposition != 0 && startposition != 1) {
    boardmaker(board);
    instructions(board);
        cin >> startposition;
        cout << "\n";
        if (startposition != 0 && startposition != 1) {
            cout << "?";
            cout << "\n";
            error();
        }
    }
    blank = 0;


    //game loop
    while (wincon(board) == false && blank < 9) {
        int choice;
        //human player
        if (startposition % 2 == 0) {
            cout << "choose a piece to move :";
            cin >> choice;
            if (move(choice, board, xORy) == false) {
                error();
            }
            else
            {
                blank++;
            }
            error();
            display(board);
            if (wincon(board)) {
                x = 1;
            }
        }
         
            //machine
        else {
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            cin.get();
            robot(board, xORyV2);
            error();
            display(board);
            if (wincon(board) == true) {
                x = 0;
            }
            blank++;
        }
        wincon(board);
        startposition++;
    }
    error();
    if (wincon(board) == true && x == 1) {
        cout << "U WON";
    }
    else if(wincon(board) == true && x == 0) {
        cout << "BILLY WON";
    }
    else if(blank > 8){
        cout << "TIE";
    }


}


void boardmaker(vector<vector<char>>& rboard)
{
    int x = 0;
    int j = 3;

    for (vector<char>& looper : rboard) {
        while (x < j) {
            //casting integer to char
            char c = '0' + x;
            looper.push_back(c);
            x++;

        }
        j += 3;
    }
}

void instructions(vector<vector<char>>& rboard)
{
    cout << "\t\twelcome to tic tac toc\n\t\t-------------------------------\n" << endl;
    cout << "u know how to play b" << endl;
    display(rboard);
    cout << "to start first (0) computer (1):";
}
void display(const vector<vector<char>>& board)
{
    for (vector<char> loop : board) {
        for (char x : loop) {
            cout << x;
            cout << "  |  ";
        }
        cout << "\n\n";
    }
}
void error()
{
    for (int x = 0; x < 50; x++) {
        cout << "\n\n";

    }
}

int randomizer(int limit)
{
    
    int random = rand() % limit;
    return random;
}
bool move(int userchoice, vector<vector<char>>& rboard, int random)
{
    char userchoicev2 = userchoice + '0';
    for (vector<char>& looper : rboard) {
        for (char& x : looper) {
            if (userchoicev2 == x){
                if (random == 1) {
                    x = X;
                }
                else {
                    x = O;
                }
                return true;
            }
        }
    }
    return false;
}

bool wincon(const vector<vector<char>>& board)
{
    bool check = false;
    vector<char>::iterator bot1;
    int lap = 0;
    int col = 0;
    //horizntal
    if(blank != 8){
        for (vector<char> loop : board) {
            bot1 = loop.begin();
            if (*bot1++ == *(bot1) && *bot1++ == *bot1) {
                check = true;
                return true;
            }
        }

        //vertical
        for (int j = 0; j < 3; j++) {
            if (board[lap++][j] == board[lap][j] && board[(lap++)][j] == board[(lap)][j]) {
                check = true;
                return check;
            }
            lap = 0;
        }
        //diagonal check
        if (board[lap++][col++] == board[lap][col] && board[lap++][col++] == board[lap][col]) {
            check = true;
            return check;
        }
        lap = 0;
        col = 2;
        //diagonal check 2
        if (board[lap++][col--] == board[lap][col] && board[lap++][col--] == board[lap][col]) {
            check = true;
            return check;
        }
        

        return false;

    }
    else
    {
        bool check = false;
    }
    return check;
}
void robot(vector<vector<char>>& board,  int xORy)
{
    int n = 0;
    bool check = false;

    //wincheck
    while (n < 9) {
        check = move(n, board, xORy);
        if (check == true) {
            if (wincon(board) == true) {
                return;
            }
            useless(board, n);
        }
        n++;
    }
    n = 0;
    //dont make him win check
    while (n < 9) {
        check = move(n, board, (xORy - 1));
        if (check == true) {
            if (wincon(board) == true) {
                useless(board, n);
                move(n, board, xORy);
                return;
            }
            useless(board, n);
        }
        n++;
    }


    
    //random
    for (int i = 0; i < 1000; i++)
    {

        int random = randomizer(9);
        check = move(random, board, xORy);
        if (check == true) {
            return;
        }
    }
}
void useless(vector<vector<char>>& rboard, int choice){
    



    int lap = 0;
    char a = choice + '0';
    for(int x = 0; x < 3; x++) {
        for (int j = 0; j < 3; j++){
            if (lap == choice) {
                rboard[x][j] = a;
            }
            lap++;
        }
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::string;
使用std::vector;
使用std::时间;
使用std::endl;
//全局函数
void boardmaker(向量和板);
无效指令(矢量和电路板);
无效显示(常数向量和板);
无效错误();
int随机化器(int限制);
布尔移动(int userchoice、vector&rboard、int random);
布尔温康(恒向量和板);
void机器人(向量和板,int-xORy);
无效无效(矢量和rboard,整数选择);
//全局元素
常量字符X='X';
const char O='O';
int空白;
int main()
{
静态施法(时间(0));
//元素
矢量板(3);
int起始位置=5;
布尔成功=假;
int xORy=随机化器(2);
int xORyV2=xORy+1;
int x=1;
//节目简介
while(startposition!=0&&startposition!=1){
董事会;
指示(委员会);
cin>>起始位置;

cout 250行代码需要筛选。请创建一个,这是一项很好的技能,因为将代码减少到最低限度以重现问题通常是解决问题的最佳方法。@Tas所以我删除了这段sht代码并创建了一段新代码,我会这样做。首先,这段代码不会编译..我得到的是
numeric\limits
不是
std
的成员,对于
max
也是一个类似的问题。需要
#包括
,才能获得这些。这让人对你发布的代码实际上就是你正在运行的代码缺乏信心。但除此之外,还有大约7个序列点警告,它们都在
board
就是我的地方ndexed。我猜你将
lap
col
增加一次太多,或者用
++
操作符调用UB。@yano是的,我认为问题在于lap和col,我删除了它们并输入了数字,它没有给出任何错误,但这没有意义,为什么它会这样做并出错。这将是学习h的好时机使用调试器。当它崩溃时,你可以检查变量、内存、查看调用堆栈等。我敢打赌,当C++执行时,<代码> LAP 和/或<代码> COLL/COD>将是2。C++没有编译错误并不意味着程序没有任何问题。在数组或向量的范围之外唱歌。