我一直在使用C ++足够长的时间来尝试构建一个简单的tic-tac-toe游戏。玩家通过提供所需的元素轮流将1和2添加到动态大小的游戏板中。
提供的功能决定玩家的回合和游戏板更新工作o.k.,但下面的代码检查由0表示的董事会的获胜条件,如:
然而不起作用。经过测试,电路板每回合都会更新一次。我怀疑的问题是数组算术。如何实现算法以成功检测条件?提前谢谢。
bool chkCondition(int rows, int columns, int**board){
int counter=0;
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
if(board[i][j]==1 && board[i][j]+1==1 && board[i][j]+2==1){//checks horizontally for 1
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==1 && board[i][j]+columns==1 && board[i][j]+2*columns==1){//checks vertically for 1
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==1 && board[i][j]+columns+1==1 && board[i][j]+2*columns+2==1){//checks diagonally for 1
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j]+1==2 && board[i][j]+2==2){//checks horizontally for 2
cout << "Player 2 wins!";
return true;
}else if(board[i][j]==2 && board[i][j]+columns==2 && board[i][j]+2*columns==2){//checks vertically for 2
cout << "Player 2 wins!";
return true;
}else if(board[i][j]==2 && board[i][j]+columns+1==2 && board[i][j]+2*columns+2==2){//checks diagonally for 2
cout << "Player 2 wins!";
return true;
}else if(board[i][j]==0){//attempts to detect if the game is a draw
if(counter==(rows+columns)-3){
cout << "It's a draw!";
return true;
}
}
}
}
return false;
counter++;
}
修改 算术被更改为正确的格式以查找元素而不是添加它们的值。新代码在这里:
bool chkCondition(int rows, int columns, int**board){
int counter=0;
for(int i=0; i<rows; i++){
counter++;
for(int j=0; j<columns; j++){
if(board[i][j]==1 && board[i][j+1]==1 && board[i][j+2]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j+1]==2 && board[i][j+2]==2){
cout << "Player 2 wins!";
return true;
}
if(counter==rows | counter>rows-1){
//cout << "L--------\n";
//cout << "checking vertical\n";
if(board[i][j]==1 && board[i][j-columns]==1 && board[i][j-columns-columns]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j-columns]==2 && board[i][j-columns-columns]==2){
cout << "Player 2 wins!";
return true;
//cout << "checking diagonal\n";
}else if(board[i][j]==1 && board[i][j-columns-1]==1 && board[i][j-columns-columns-2]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j-columns-1]==2 && board[i][j-columns-columns-2]==2){
cout << "Player 2 wins!";
return true;
}
}
if(counter==1 && counter<rows-1){
//cout << "H--------\n";
//cout << "checking vertical\n";
if(board[i][j]==1 && board[i][j+columns]==1 && board[i][j+columns+columns]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j+columns]==2 && board[i][j+columns+columns]==2){
cout << "Player 2 wins!";
return true;
//cout << "checking diagonal\n";
}else if(board[i][j]==1 && board[i][j+columns+1]==1 && board[i][j+columns+columns+2]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j+columns+1]==2 && board[i][j+columns+columns+2]==2){
cout << "Player 2 wins!";
return true;
}
}
if(counter>2 && counter<rows-1){
//cout << "M--------\n";
//cout << "checking vertical\n";
if(board[i][j]==1 && board[i][j+columns]==1 && board[i][j-columns]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j+columns]==2 && board[i][j-columns]==2){
cout << "Player 2 wins!";
return true;
//cout << "checking diagonal\n";
}else if(board[i][j]==1 && board[i][j+columns+1]==1 && board[i][j-columns-1]==1){
cout << "Player 1 wins!";
return true;
}else if(board[i][j]==2 && board[i][j+columns+1]==2 && board[i][j-columns-1]==2){
cout << "Player 2 wins!";
return true;
}
}
}
}
return false;
}
外部if语句用于确定计数器在迭代中计算当前行是否位于表的开头,中间或末尾以避免分段错误。这是有效的,程序不会崩溃。但是,尽管算法是合乎逻辑的,但所有垂直和对角条件语句根本不起作用。任何人都能说出发生了什么吗?
答案 0 :(得分:0)
正如评论所说,问题是你如何访问表格。另一个问题是你如何使用循环。如果您首先水平检查,那么您的循环将转到每一行,但不应该转到每一列,否则您将获得一个很好的分段错误。 例如第一部分(假设你实际上把+ 1 / + 2放在好地方
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
if(board[i][j]==1 && board[i][j+1]==1 && board[i][j+2]==1){//checks horizontally for 1
cout << "Player 1 wins!";
return true;
当j为column-2或columns时,它会给你一个分段错误。要解决这个问题,你需要在第二个之前停下来。对于水平检查,它与线条相同。只做一个这样的循环比较复杂,或者你需要添加更多ifs以避免这个错误。