嘿伙计们我用C ++编写了一个连接四个程序,但是我很难用我的算法找出问题,找出对角线的获胜动作。这是我写的代码:我的水平和垂直函数可以工作
//checks for a horizontal win
//returns 1 if there's a win
for(int i=0;i<numrows;i++)
for(int j=0;j<rowsize-3;j++)
if(board[i][j]!=" " && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3])
return 1;
//checks for a vertical win
//returns one if theres a win
for(int i=0;i<numrows-3;i++)
for(int j=0;j<rowsize;j++)
if(board[i][j] !=" " && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j])
return 1;
int numrows=6;
int rowsize=7;
//checks the right side of a diagonal for a win
for(int i=0;i<numrows;i++)
for(int j=0;j<rowsize;j++)
if(board[i][j] !=" " && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3])
return 1;
//checks left diagonal win for a win
for(int i=0;i<numrows;i++)
for(int j=0;j<rowsize;j++)
if(board[i][j] !=" " && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3])
return 1;
//creates a typical 7 by 6 connect four board
for (int i=0;i<numrows; i++){
vector<string> row;
for (int j=0;j<rowsize; j++)
row.push_back(" ");
board.push_back(row);
}
答案 0 :(得分:-1)
我认为这可能是一个错误,因为评估所有电路板的电路板[i + x] [j + -y]将超出数组范围。
例如,当你有i = 4,j = 4时,第一个if语句将寻找电路板[4] [4],电路板[5] [5],电路板[6] [6],电路板[7 ] [7]