C++ c+中的动态二维数组+;引起问题

C++ c+中的动态二维数组+;引起问题,c++,arrays,pointers,dynamic,2d,C++,Arrays,Pointers,Dynamic,2d,所以我正在制作一个连接4游戏,可以在任何大小的棋盘上连接任意数量的棋盘,并使用ai与你比赛。目前,它没有正确显示电路板,我不相信电路板的创建是正确的。另一个问题是,当我实际尝试放置第一枚硬币时,会出现中断错误。(错误消息:“Connect[x,n].exe:0xC0000005:访问冲突写入位置0x5A57中0x00195832处的首次机会异常。”并在我尝试将阵列的“顶部”位置设置为玩家/ai硬币的板的添加函数中发生 总的来说,我不认为我在C++中使用了一个2D动态数组,希望有人能看到我的错误

所以我正在制作一个连接4游戏,可以在任何大小的棋盘上连接任意数量的棋盘,并使用ai与你比赛。目前,它没有正确显示电路板,我不相信电路板的创建是正确的。另一个问题是,当我实际尝试放置第一枚硬币时,会出现中断错误。(错误消息:“Connect[x,n].exe:0xC0000005:访问冲突写入位置0x5A57中0x00195832处的首次机会异常。”并在我尝试将阵列的“顶部”位置设置为玩家/ai硬币的板的添加函数中发生

总的来说,我不认为我在C++中使用了一个2D动态数组,希望有人能看到我的错误

我的“主要”cpp

// Connect[x,n].cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "time.h"
#include <string>
#include <iostream>
#include "windows.h"
#include "winioctl.h"
#include <cstdlib>
#include "AIConnectBoard.h"


using namespace std;





int main(int argc, char* argv[])
{
    char again;
    int tm, tn, conNum;
    char aiturn;

do{

    srand((unsigned)time(NULL)); //random seed based on time
    cout << "Board Width:";
    cin >> tn;
    cout << "Board Height:";
    cin >> tm;
    cout << "Connect?";
    cin >> conNum;
    cout << "AI moves first?(y/n)";
    cin >> aiturn;


    AIConnectBoard *b = new AIConnectBoard();
    b->setBoard(tm, tn);
    b->setFirst(aiturn);


    while (b->checkwin()==0) //keep asking for moves until someone wins
    {
        b->dispBoard();
        if (b->endt == 0)
        {
            int coincol;
            if (b->turn == b->plyr)
            {
                do
                {
                cout << "What column?" << "(1"<< "-" << tn << ")" << '\n';
                cin >> coincol;

                if ((coincol > tn) || (coincol < 1))
                {
                    cout << "That is not an accepted value, please enter a number between 1 and " << tn <<'\n';
                }

                }while ((coincol > tn) || (coincol < 1));
                coincol--;
            }
            if (b->add(coincol, b->plyr) == 0)
            {
                if (b->endt == 0)
                {
                    if (b->add(coincol, b->plyr) == 0)
                    {
                        b->lin = 6 - b->top[coincol]; //place players choice
                        b->checkturn();
                        b->turn = b->ai;

                        if (b->endt == 0)
                        {
                            int t;
                            b->col = b->Think();
                            t = b->add(b->col, b->ai);
                            if (t == 0)
                            {
                                b->lin = 6 - b->top[b->col]; //place ai's choice
                                b->turn = b->plyr;
                            }
                        }
                    }
                }
            }
        }
    }

    cout << "Play Again? (y/n)";
    cin >> again;
} while (again != 'n');
return 0;
};
//Connect[x,n].cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“time.h”
#包括
#包括
#包括“windows.h”
#包括“winioctl.h”
#包括
#包括“AIConnectBoard.h”
使用名称空间std;
int main(int argc,char*argv[])
{
再次烧焦;
int tm、tn、conNum;
char-aiturn;
做{
srand((无符号)time(NULL));//基于时间的随机种子
cout>tn;
cout>tm;
cout>conNum;
cout>aiturn;
A连接板*b=新的A连接板();
b->立根板(tm、tn);
b->setFirst(aiturn);
while(b->checkwin()==0)//继续请求移动,直到有人赢为止
{
b->dispBoard();
如果(b->endt==0)
{
int coincol;
如果(b->turn==b->plyr)
{
做
{
cout lin=6-b->top[coincol];//放置玩家选择
b->checkturn();
b->turn=b->ai;
如果(b->endt==0)
{
int t;
b->col=b->Think();
t=b->add(b->col,b->ai);
如果(t==0)
{
b->lin=6-b->top[b->col];//放置ai的选择
b->turn=b->plyr;
}
}
}
}
}
}
}
又不能了;
}而(又是!=“n”);
返回0;
};
我的aiconnectboard.cpp

#include "stdafx.h"
#include "AIConnectBoard.h"
#include "stdafx.h"
#include "time.h"
#include <string>
#include <iostream>
#include "windows.h"
#include "winioctl.h"
#include <cstdlib>

using namespace std;

//constructor
AIConnectBoard::AIConnectBoard(){
endt = 0;
turn = 1;
}

//destructor
AIConnectBoard::~AIConnectBoard()
{

}

//create board to m x n specifications
void AIConnectBoard::setBoard(const int tn, const int tm)
{
n = tn;
m = tm;
noPlayer = 0;
plyr = 1;
ai = 2;
int** arry = 0;
arry = new int*[n];
for (int i = 0; i < n; i++) //make board 2d && make empty
{
    arry[i] = new int[m];
    arry[i][m] = noPlayer;
}

int** thnk = new int*[m, n];
int* top = new int[n];
for (int i = 0; i < m; i++) //set top to bottom of board
{
    top[i] = 0;
}
return;
}

//sets first plyr/turn. Red moves first, then Yellow
void AIConnectBoard::setFirst(char a)
{
if (a == 'y') //ai goes first
    {
        turn=ai;
        aiColor= 'R';
        plyrColor= 'Y';
    }
else //plyr goes first
{
    turn=plyr;
    aiColor= 'Y';
    plyrColor = 'R';
}
}

//output board

*******GETS OUPUT AS 2 ROWS OF 0s AND DOESNT FILL EVERYTHING

void AIConnectBoard::dispBoard()
{
for (int x=1;x<=m;x++) //rows on top of board
{
    cout << x << " ";
}
cout << '\n';
for (int i = 0; i < m; i++) //display current state of board
{
    for (int j = 0; j < n; j++)
    {
        if (arry[i,j] == (int*)1)
        {
            cout << plyrColor << " ";
        }
        if (arry[i,j] == (int*)2)
        {
            cout << aiColor << " ";
        }
        else if (arry[i,j]==(int*)0)
        {
            cout << "O  ";
        }
    }
    cout << '\n';
}
}

//AI starts thinking--set difficulty here
int AIConnectBoard::Think() //levels of recursion
{
int i = (m*n)/7; //will give 6 levels of recursion on 7x6
i = recur;
return check(i);
}

//check for an end to game (win/lose/draw)
void AIConnectBoard::checkturn()
{
int temp;
temp = checkwin();
if (temp == plyr)
{
    cout << "You win!" << '\n';
    endt=1;
}
else if (temp == ai)
{
    cout << "You lose!" << '\n';
    endt=1;
}
else if (temp == 0)
{
    for (t = 0; t < n; t++)
    {
        if (n - top[t] < 0)
        {
            temp = 1;
        }
    }
    if (temp == 0)
    {
        endt = 1;
        return;
    }
}
}

//1st AI check score of move function
int AIConnectBoard::check(int i)
{
int co, score, t, g, j = 0, p;
i--;
if (i == -1)
{
    score = position();
    return score;
}

if (i % 2 == 0)
{
    int max = 0, k;
    int j = 0;
    int co = 0;
    for (t = 0; t < n; t++)
    {
        g = add(t, ai);
        if (g == 0)
        {
            if (checkwin() == ai)
            {
                sub(t);

                if (i == recur - 1)
                {
                    return t;
                }
                else
                {
                    return 9000;
                }
            }
            k = check(i);

            if (co == 0)
            {
                max = k;
                co = 1;
                j = t;
            }

            if (k == max)
            {
                p = rand() % m + 1;
                if (p > 4)
                {
                    j = t;
                }
            }

            if (k > max)
            {
                max = k;
                j = t;
            }
            sub(t);
        }
        score = max;
    }
}
else
{
    int min = 0, k = 0;
    co = 0;
    for (t = 0; t < m; t++)
    {
        g = add(t, plyr);
        if (g == 0)
        {
            if (checkwin() == plyr)
            {
                sub(t);
                return -10000;
            }
            k = check(i);
            if (co == 0)
            {
                min = k; co = 1; j = t;
                if (k < min)
                {
                    min = k;
                    j = t;
                }
            }
            sub(t);
        }
        score = min;
    }
}
if (i == recur - 1)
{
    return j;
}
return score;
}


//add a coin to the specified c column (g for ai vs plyr)
int AIConnectBoard::add(int c, int g)
{
int* coin;
int tcoin = 0;
coin = &tcoin; //initialize coin to something

//set coin to be ai or player
if (g == plyr)
{
    coin = &plyr;
}
if (g == ai)
{
    coin = &ai;
}
//if not at top of board, drop coin and increase top array
if (top[c] < n)
{
    if (g == plyr)
    {
        arry[c][top[c]] = plyr;   *******WHERE I GET THE EXCEPTION ERROR (ASSUMING IM USING A NULL POINTER OR SOMETHING, BUT CANT TELL WHERE)
        top[c]++;
    }
    else if (g == ai)
    {
        arry[c][top[c]] = ai;
        top[c]++;
    }
    return 1;
}
}



//remove a coin from the specified c column
int AIConnectBoard::sub(int c)
{
top[c]--;
arry[c, top[c] = 0];
return 0;
}

//keeps track of where AI is currently contemplating a move
int AIConnectBoard::position()
{
int u, z, x, y, j, score;
int gh = 0, hg = 0;
score = 0;

//Empty Think
for (x = 0; x < m; x++)
{
    for (y = 0; y < n; y++)
    {
        thnk[x, y] = 0;
    }
}
for (y = 0; y < n; y++)
{
    for (x = 0; x < m; x++)
    {
        if (arry[x, y] == 0)
        {
            score += checkhole(x, y);
        }
        if (y>0)
        {
            if (thnk[x, y] == (int*)ai && arry[x, y - 1] != 0)
            {
                gh++;
            }
            if (thnk[x, y] == (int*)plyr && arry[x, y - 1] != 0)
            {
                hg++;
                score = score - 4000;
            }
        }
    }
}

if (gh > 1)
{
    score += (gh - 1) * 500;
}
if (gh == 1)
{
    score = score - 100;
}
if (hg > 1)
{
    score -= (hg - 1) * 500;
}

for (x = 0; x < m; x++)
{
    gh = 0;
    for (y = 1; y < 6; y++)
    {
        if (thnk[x, y] == (int*)ai && arry[x, y - 1] == (int*)ai)
        {
            u = 0; j = 0;
            for (int z = y - 1; 0>-1; z--)
            {
                if (thnk[x, z] == (int*)plyr)
                {
                    u = 1;
                }
                if (arry[x, z] == 0)
                {
                    j++;
                }
            }
            if (u == 0)
            {
                score -= 1500 + j*m; //m or 7?
            }
            if (u == 1)
            {
                score -= 300;
            }
        }
        if (thnk[x, y] == (int*)plyr && thnk[x, y - 1] == (int*)plyr)
        {
            u = 0; j = 0;
            for (int z = y - 1; z > -1; z--)
            {
                if (thnk[x, z] == (int*)ai)
                {
                    u = 1;
                }
                if (arry[x, z] == 0)
                {
                    j++;
                }
            }
            if (u == 0)
            {
                score -= 1500 + j*m; //m or 7?
            }
            if (u == 1)
            {
                score -= 300;
            }
        }
        if (thnk[x, y] == (int*)plyr)
        {
            u = 0;
            for (z = y - 1; y > -1; y--)
            {
                if (thnk[x, z] == (int*)ai)
                {
                    u = 1;
                }
            }
            if (u == 1)
            {
                score += 30;
            }
            if (thnk[x, y] == (int*)ai)
            {
                u = 0;
                for (z = y - 1; z > -1; z--)
                {
                    if (thnk[x, z] == (int*)plyr)
                    {
                        u = 1;
                    }
                }
                if (u == 1)
                {
                    score -= 30;
                }
            }
        }
    }
}
return score;
}

//check score of current hole AI is contemplating
int AIConnectBoard::checkhole(int x, int y)
{
int score = 0;
int max, min;
int d0 = 0, d1 = 0, d2 = 0, d3 = 0;

//d1  ai check rows
if ((x + 1)<m && (y - 1)>-1)
{
    if (arry[x + 1, y - 1] == (int*)ai)
    {
        d1++;
        if ((x + 2)<m && (y - 2)>-1)
        {
            if (arry[x + 2, y - 2] == (int*)ai)
            {
                d1++;
                if ((x + 3)<m && (y - 3)>-1)
                {
                    if (arry[x + 3, y - 3] == (int*)ai)
                    {
                        d1++;
                    }
                }
            }
        }
    }
}

//d1 ai check columns
if ((x - 1) > -1 && (y + 1) < n)
{
    if (arry[x - 1, y + 1] == (int*)ai)
    {
        d1++;
        if ((x - 2) >-1 && (y + 2) < n)
        {
            if (arry[x - 2, y + 2] == (int*)ai)
            {
                d1++;
                if ((x - 3) >-1 && (y + 3) < n)
                {
                    if (arry[x - 3, y + 3] == (int*)ai)
                    {
                        d1++;
                    }
                }
            }
        }
    }
}
//d2 ai check rows
if ((x - 1)<m && (y - 1)>-1)
{
    if (arry[x - 1, y - 1] == (int*)ai)
    {
        d2++;
        if ((x - 2)<m && (y - 2)>-1)
        {
            if (arry[x - 2, y - 2] == (int*)ai)
            {
                d2++;
                if ((x - 3)<m && (y - 3)>-1)
                {
                    if (arry[x - 3, y - 3] == (int*)ai)
                    {
                        d2++;
                    }
                }
            }
        }
    }
}
//d2 ai check columns
if ((x + 1) > -1 && (y + 1) < n)
{
    if (arry[x + 1, y + 1] == (int*)ai)
    {
        d2++;
        if ((x + 2) >-1 && (y + 2) < n)
        {
            if (arry[x + 2, y + 2] == (int*)ai)
            {
                d2++;
                if ((x + 3) >-1 && (y + 3) < n)
                {
                    if (arry[x + 3, y + 3] == (int*)ai)
                    {
                        d2++;
                    }
                }
            }
        }
    }
}
//d0 ai check
if ((y - 1) > -1)
{
    if (arry[x, y - 1] == (int*)ai)
    {
        d0++;
        if ((y - 2) > -1)
        {
            if (arry[x, y - 2] == (int*)ai)
            {
                d0++;
                if ((y - 3) > -1)
                {
                    if (arry[x, y - 3] == (int*)ai)
                    {
                        d0++;
                    }
                }
            }
        }
    }
}
//d3 ai check 
if ((x - 1) > -1)
{
    if (arry[x - 1, y] == (int*)ai)
    {
        d3++;
        if ((x - 2) > -1)
        {
            if (arry[x - 2, y] == (int*)ai)
            {
                d3++;
                if ((x - 3) > -1)
                {
                    if (arry[x - 3, y] == (int*)ai)
                    {
                        d3++;
                    }
                }
            }
        }
    }
}
//d3 ai check rows
if (x + 1 < m)
{
    if (arry[x + 1, y] == (int*)ai)
    {
        d3++;
        if (x + 2 < m)
        {
            if (arry[x + 2, y] == (int*)ai)
            {
                d3++;
                if (x + 3 < m)
                {
                    if (arry[x + 3, y] == (int*)ai)
                    {
                        d3++;
                    }
                }
            }
        }
    }
}
max = d0;
//score manipulations
if (d1 > max){ max = d1; }
if (d2 > max){ max = d2; }
if (d3 > max){ max = d3; }
if (max == 2)
{
    score += 5;
}
if (max > 2)
{
    score += 71;
    thnk[x, y] = (int*)ai;
    if ((d1 < 3) && (d2 < 3) && (d3 < 3))
    {
        score -= 10;
    }
}
//d1 plyr check rows
if ((x + 1)<m && (y - 1)>-1)
{
    if (arry[x + 1, y - 1] == (int*)plyr)
    {
        d1++;
        if ((x + 2)<m && (y - 2)>-1)
        {
            if (arry[x + 2, y - 2] == (int*)plyr)
            {
                d1++;
                if ((x + 3)<m && (y - 3)>-1)
                {
                    if (arry[x + 3, y - 3] == (int*)plyr)
                    {
                        d1++;
                    }
                }
            }
        }
      }
}
//d1 plyr check columns
if ((x - 1) > -1 && (y + 1) < n)
{
    if (arry[x - 1, y + 1] == (int*)plyr)
    {
        d1++;
        if ((x - 2) >-1 && (y + 2) < n)
        {
            if (arry[x - 2, y + 2] == (int*)plyr)
            {
                d1++;
                if ((x - 3) >-1 && (y + 3) < n)
                {
                    if (arry[x - 3, y + 3] == (int*)plyr)
                    {
                        d1++;
                    }
                }
            }
        }
    }
}
//d2 plyr check rows
if ((x - 1)<m && (y - 1)>-1)
{
    if (arry[x - 1, y - 1] == (int*)plyr)
    {
        d2++;
        if ((x - 2)<m && (y - 2)>-1)
        {
            if (arry[x - 2, y - 2] == (int*)plyr)
            {
                d2++;
                if ((x - 3)<m && (y - 3)>-1)
                {
                    if (arry[x - 3, y - 3] == (int*)plyr)
                    {
                        d2++;
                    }
                }
            }
        }
    }
}
//d2 plyr check columns
if ((x + 1) > -1 && (y + 1) < n)
{
    if (arry[x + 1, y + 1] == (int*)plyr)
    {
        d2++;
        if ((x + 2) >-1 && (y + 2) < n)
        {
            if (arry[x + 2, y + 2] == (int*)plyr)
            {
                d2++;
                if ((x + 3) >-1 && (y + 3) < n)
                {
                    if (arry[x + 3, y + 3] == (int*)plyr)
                    {
                        d2++;
                    }
                }
            }
        }
    }
}
//d0 plyr check
if ((y - 1) > -1)
{
    if (arry[x, y - 1] == (int*)plyr)
    {
        d0++;
        if ((y - 2) > -1)
        {
            if (arry[x, y - 2] == (int*)plyr)
            {
                d0++;
                if ((y - 3) > -1)
                {
                    if (arry[x, y - 3] == (int*)plyr)
                    {
                        d0++;
                    }
                }
            }
        }
    }
}
//d3 plyr check 
if ((x - 1) > -1)
{
    if (arry[x - 1, y] == (int*)plyr)
    {
        d3++;
        if ((x - 2) > -1)
        {
            if (arry[x - 2, y] == (int*)plyr)
            {
                d3++;
                if ((x - 3) > -1)
                {
                    if (arry[x - 3, y] == (int*)plyr)
                    {
                        d3++;
                    }
                }
            }
        }
    }
}
//d3 ai check rows
if (x + 1 < m)
{
    if (arry[x + 1, y] == (int*)plyr)
    {
        d3++;
        if (x + 2 < m)
        {
            if (arry[x + 2, y] == (int*)plyr)
            {
                d3++;
                if (x + 3 < m)
                {
                    if (arry[x + 3, y] == (int*)plyr)
                    {
                        d3++;
                    }
                }
            }
        }
    }
}
min = d0;
//score manipulations
if (d1 > min){ min = d1; }
if (d2 > min){ min = d2; }
if (d3 > min){ min = d3; }
if (min == 2)
{
    score -= 4;
}
if (min > 2)
{
    score -= 70;
    thnk[x, y] = (int*)plyr;
    if ((d1 < 3) && (d2 < 3) && (d3 < 3))
    {
        score += 10;
    }
}
return score;

}

//check for win condition (i.e. "conNum" in a row)
int AIConnectBoard::checkwin()
{
int r = 0;
int x, y;
for (y = 2; y > -1; y--)
{
    for (x = 0; x < m; x++)
    {
        checku(x, y, r);
    }
}
for (y = 0; y < n; y++)
{
    for (x = 0; x < conNum; x++)
    {
        check2r(x, y, r);
    }
}
for (y = 2; y>-1; y--)
{
    for (x = 0; x < conNum; x++)
    {
        checkr(x, y, r);
    }
}
for (y = 2; y>-1; y--)
{
    for (x = 3; x < m; x++)
    {
        checkl(x, y, r);
    }
}
return r;
}


//check win functions
    void AIConnectBoard::checku(int x, int y, int& r)
{
if ((arry[x, y] == (int*)2) && (arry[x, y + 1] == (int*)2) && (arry[x, y + 2] ==  (int*)2) && (arry[x, y + 3] == (int*)2))r = 2;
if ((arry[x, y] == (int*)1) && (arry[x, y + 1] == (int*)1) && (arry[x, y + 2] == (int*)1) && (arry[x, y + 3] == (int*)1))r = 1;
}

void AIConnectBoard::check2r(int x, int y, int& r)
{
if ((arry[x, y] == (int*)2) && (arry[x + 1, y] == (int*)2) && (arry[x + 2, y] == (int*)2) && (arry[x + 3, y] == (int*)2))r = 2;
if ((arry[x, y] == (int*)1) && (arry[x + 1, y] == (int*)1) && (arry[x + 2, y] == (int*)1) && (arry[x + 3, y] == (int*)1))r = 1;
}

void AIConnectBoard::checkr(int x, int y, int& r)
{
if ((arry[x, y] == (int*)2) && (arry[x + 1, y + 1] == (int*)2) && (arry[x + 2, y + 2] == (int*)2) && (arry[x + 3, y + 3] == (int*)2))r = 2;
if ((arry[x, y] == (int*)1) && (arry[x + 1, y + 1] == (int*)1) && (arry[x + 2, y + 2] == (int*)1) && (arry[x + 3, y + 3] == (int*)1))r = 1;
}

void AIConnectBoard::checkl(int x, int y, int& r)
{
if ((arry[x, y] == (int*)2) && (arry[x - 1, y + 1] == (int*)2) && (arry[x - 2, y + 2] == (int*)2) && (arry[x - 3, y + 3] == (int*)2))r = 2;
if ((arry[x, y] == (int*)1) && (arry[x - 1, y + 1] == (int*)1) && (arry[x - 2, y + 2] == (int*)1) && (arry[x - 3, y + 3] == (int*)1))r = 1;
}
#包括“stdafx.h”
#包括“AIConnectBoard.h”
#包括“stdafx.h”
#包括“time.h”
#包括
#包括
#包括“windows.h”
#包括“winioctl.h”
#包括
使用名称空间std;
//建造师
A连接板::A连接板(){
endt=0;
圈数=1;
}
//析构函数
A连接板::~A连接板()
{
}
//创建符合m x n规格的电路板
无效A连接板::立杆板(常数int tn,常数int tm)
{
n=总氮;
m=tm;
noPlayer=0;
plyr=1;
ai=2;
int**arry=0;
arry=新整数*[n];
对于(int i=0;i-1&(y+3)-1)
{
if(arry[x,y-1]==(int*)ai)
{
d0++;
如果((y-2)>-1)
{
if(arry[x,y-2]==(int*)ai)
{
d0++;
如果((y-3)>-1)
{
if(arry[x,y-3]==(int*)ai)
{
d0++;
}
}
}
}
}
}
//d3人工智能检查
如果((x-1)>-1)
{
if(arry[x-1,y]==(int*)ai)
{
d3++;
如果((x-2)>-1)
{
if(arry[x-2,y]==(int*)ai)
{
d3++;
如果((x-3)>-1)
{
if(arry[x-3,y]==(int*)ai)
{
d3++;
}
}
}
}
}
}
//d3 ai检查行
如果(x+1max){max=d1;}
如果(d2>max){max=d2;}
如果(d3>max){max=d3;}
如果(最大==2)
{
分数+=5分;
}
如果(最大值>2)
{
得分+=71分;
thnk[x,y]=(int*)ai;
如果((d1<3)和&(d2<3)和&(d3<3))
{
得分-=10分;
}
}
//d1 plyr检查行
如果((x+1)-1)
{
如果(arry[x+1,y-1]==(int*)plyr)
{
d1++;
如果((x+2)-1)
{
如果(arry[x+2,y-2]==(int*)plyr)
{
d1++;
如果((x+3)-1)
{
如果(arry[x+3,y-3]==(int*)plyr)
{
d1++;
}
}
}
}
}
}
//d1 plyr检查列
如果((x-1)>-1&(y+1)int* top = new int[n];