因此,我正在製作一個連接4遊戲,可以在任何大小的板上連接任何數量的遊戲,並使用ai與您對戰。目前,它不能正確顯示電路板,我不相信電路板正在正確創建。另一個問題是當我真的嘗試放置第一枚硬幣時,我得到了一個突破錯誤。 (錯誤消息:「連接[x,n] .exe中0x00195832的第一次機會異常:0xC0000005:訪問衝突寫入位置0x5A5A5A57。」併發生在我嘗試設置陣列的「頂部」位置的電路板的添加函數中一個玩家/ AI硬幣。在C++中導致問題的動態二維數組
總體來說,我只是不使用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;
};
my 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;
}
使用'new'會引起麻煩,特別是當你沒有清理任何東西時。爲什麼'b'是一個指針呢?至於二維數組,你可以使用矢量矢量,但最好找到一個矩陣類來避免鋸齒。 – chris
This:'int ** thnk = new int * [m,n];'不是C++ ..更多C#。那麼..它將在C++中編譯,但它不會做你認爲它的工作。 – Brandon