程序启动正常,但当我输入坐标时,它会崩溃并弹出一个:
运行时检查失败#2 - 变量'x'周围的堆栈已损坏。
我不明白为什么当我现在遵循教程时崩溃。
我完全被困......任何建议或提示?
#include <stdlib.h>
#include <stdio.h>
void print_board(int board[9][8])
{
int x,y;
char z = 1;
system("cls");
for(x=1;x<9;x++){
printf("%d",z);
z++;
for(y=1;y<9;y++){
printf("|");
switch(board[x][y]){
case 0:
printf(" ");
break;
case 1:
printf("x");
break;
case 2:
printf("o");
break;
case 3:
printf("X");
break;
case 4:
printf("O");
break;
}
}
printf("\n");
}
printf(" 1 2 3 4 5 6 7 8 9\n");
}
void set_defaults(int board[9][8])
{
board[1][2]= 1; board[1][4]= 1; board[1][6]= 1; board[1][8]= 1;
board[2][1]= 1; board[2][3]= 1; board[2][5]= 1; board[2][7]= 1;
board[3][2]= 1; board[3][4]= 1; board[3][6]= 1; board[3][8]= 1;
board[6][1]= 2; board[6][3]= 2; board[6][5]= 2; board[6][7]= 2;
board[7][2]= 2; board[7][4]= 2; board[7][6]= 2; board[7][8]= 2;
board[8][1]= 2; board[8][3]= 2; board[8][5]= 2; board[8][7]= 2;
}
int Enemy(int you)
{
if(you == 1 || you == 3){
return 2;
}
if(you == 2 || you == 4){
return 1;
}
}
void Check_if_bacame_queen(int x, int y, int board[9][8], int turn)
{
if(turn == 1){
if(x == 3){
board[x][y] = 3;
}
}
if(turn == 2){
if(x == 1){
board[x][y] = 4;
}
}
}
int NextChainValid(int board[9][8], int x[4])
{
if(board[x[3]+1][x[4]+1]==Enemy(board[x[3]][x[4]]) || board[x[3]+1][x[4]+1]==Enemy(board[x[3]][x[4]]+2) ){
return 1;
}
else if(board[x[3]+1][x[4]-1]==Enemy(board[x[3]][x[4]]) || board[x[3]+1][x[4]-1] == Enemy(board[x[3]][x[4]]+2)){
return 1;
}
else if(board[x[3]-1][x[4]+1] == Enemy(board[x[3]][x[4]]) || board[x[3]-1][x[4]+1] == Enemy(board[x[3]][x[4]]+2 )){
return 1;
}
else if(board[x[3]-1][x[4]-1] == Enemy(board[x[3]][x[4]]) || board[x[3]-1][x[4]-1] == Enemy(board[x[3]][x[4]] +2) ){
return 1;
}
else{
return 0;
}
}
void get_co(int board[9][8],int turn, int peshka[2])
{
int x[4];
if(turn == 1){
printf("X's turn\n");
}
else{
printf("O's turn\n");
}
printf("Write XYAZ: \n");
scanf("%d %d %d %d",&x[1],&x[2],&x[3],&x[4]);
if(board[x[1]][x[2]]==turn || board[x[1]][x[2]] == turn+2){//check if move is valid
if(turn == 1){
if(x[3] == x[1]+1 && x[4]==x[2]+1 || x[4] == x[2]-1){//checking if the new co of the peshka are valid
board[x[1]][x[2]] = 0; //making them
board[x[3]][x[4]] = turn;
Check_if_bacame_queen(x[3],x[4],board,turn);
}
else if(x[3] == x[1]+2 && x[4] == x[2]+2 || x[4] == x[2]-2){
int x2,y2;
board[x[1]][x[2]] = 0;
board[x[3]][x[4]] = turn;
if(x[1]>x[3]){
x2=x[1]-1;
}
else{
x2 = x[3]-1;
}
if(x[2]>x[4]){
y2=x[2]-1;
}
else{
y2 = x[3]-1;
}
board[x2][y2]=0;
peshka[turn+1]--;//lovering peshkas that enemy has
Check_if_bacame_queen(x[3],x[4],board,turn);
if(NextChainValid(board,x)){
get_co(board,turn,peshka);//recursion
}
}
else{
printf("Invalid move!\n");
}
}//turn 1
else if(turn == 2){
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){//checking if the new co of peshka are valid
board[x[1]][x[2]]=0;
board[x[3]][x[4]]=turn;
Check_if_bacame_queen(x[3],x[4],board,turn);
}
else if(x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){//checking if the new co is
int x2,y2;
board[x[1]][x[2]]=0;
board[x[3]][x[4]]=turn;
if(x[1]>x[3]){
x2 = x[1]-1;
}
else{
x2 = x[3]-1;
}
if(x[2]>x[4]){
y2 = x[2]-1;
}
else{
y2 = x[4]-1;
}
board[x2][y2] = 0;
peshka[turn-1]--;//lovering enemys peshkas
Check_if_bacame_queen(x[3],x[4],board,turn);
if(NextChainValid(board,x)){
get_co(board,turn,peshka);//recursion
}
}
else{
printf("Invalide move!");
}
}
else if(turn == 3){//QUEEN
if(x[3] == x[1]-1 || x[3]==x[1]+1 && x[4]==x[2]+1 || x[4]==x[2]+1){
board[x[1]][x[2]] = 0;
board[x[3]][x[4]] = turn;
Check_if_bacame_queen(x[3],x[4],board,turn);
}
else if(x[3]==x[1]+2 || x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){
int x2,y2;
board[x[1]][x[2]] = 0;
board[x[3]][x[4]] = turn;
if(x[1]>x[3]){
x2 = x[1]-1;
}
else{
x2=x[3]-1;
}
if(x[2]>x[4]){
y2 = x[2]-1;
}
else{
y2 = x[4]-1;
}
board[x2][y2]=0;
peshka[turn-1]--;
Check_if_bacame_queen(x[3],x[4],board,turn);
if(NextChainValid(board,x)){
get_co(board,turn,peshka);//recursion
}
}
else{
printf("Invalide move!");
}
}
else if(turn == 4){
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){
board[x[1]][x[2]] = 0;
board[x[3]][x[4]] = turn;
Check_if_bacame_queen(x[3],x[4],board,turn);
}
else if(x[3]==x[1]+2 && x[4]==x[2]+2 || x[4]==x[2]-2){
int x2,y2;
board[x[1]][x[2]] = 0;
board[x[3]][x[4]] = turn;
if(x[1]>x[3]){
x2 = x[1]-1;
}
else{
x2 = x[3]-1;
}
if(x[2]>x[4]){
y2 = x[2]-1;
}
else{
y2 = x[4]-1;
}
board[x2][y2]=0;
peshka[turn-3]--;
Check_if_bacame_queen(x[3],x[4],board,turn);
if(NextChainValid(board,x)){
get_co(board,turn,peshka);//recursion
}
}
else{
printf("Invalide move!");
}
}
}
}
int Winner(int x, int o){
if(x == 0){
printf("X won!");
return 1;
}
else if(o == 0){
printf("O won!");
return 1;
}
else{
return 0;
}
}
int main()
{
int board[9][8]={0}, turn=2, peshka[2] = {12};
set_defaults(board);
while(Winner(peshka[1],peshka[2])){
print_board(board);
get_co(board,turn,peshka);
if(turn == 2){
turn = 1;
}
else{
turn = 2;
}
}
scanf("!");
}
答案 0 :(得分:1)
运行clang
来编译代码表明,有许多位置可以索引数组的末尾(通常是板,但有时也是其他数组)。请记住,C数组的索引是从0开始,而不是1!
编译器诊断的完整转储如下:
checkers.c:41:53: warning: array index 8 is past the end of the array (which contains 8 elements) [-Warray-bounds]
board[1][2]= 1; board[1][4]= 1; board[1][6]= 1; board[1][8]= 1;
^ ~
checkers.c:39:19: note: array 'board' declared here
void set_defaults(int board[9][8])
^
checkers.c:43:53: warning: array index 8 is past the end of the array (which contains 8 elements) [-Warray-bounds]
board[3][2]= 1; board[3][4]= 1; board[3][6]= 1; board[3][8]= 1;
^ ~
checkers.c:39:19: note: array 'board' declared here
void set_defaults(int board[9][8])
^
checkers.c:45:53: warning: array index 8 is past the end of the array (which contains 8 elements) [-Warray-bounds]
board[7][2]= 2; board[7][4]= 2; board[7][6]= 2; board[7][8]= 2;
^ ~
checkers.c:39:19: note: array 'board' declared here
void set_defaults(int board[9][8])
^
checkers.c:57:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
checkers.c:106:31: warning: '&&' within '||' [-Wlogical-op-parentheses]
if(x[3] == x[1]+1 && x[4]==x[2]+1 || x[4] == x[2]-1){//checking if the new co of the peshka are valid
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~ ~~
checkers.c:106:31: note: place parentheses around the '&&' expression to silence this warning
if(x[3] == x[1]+1 && x[4]==x[2]+1 || x[4] == x[2]-1){//checking if the new co of the peshka are valid
^
( )
checkers.c:111:36: warning: '&&' within '||' [-Wlogical-op-parentheses]
else if(x[3] == x[1]+2 && x[4] == x[2]+2 || x[4] == x[2]-2){
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ ~~
checkers.c:111:36: note: place parentheses around the '&&' expression to silence this warning
else if(x[3] == x[1]+2 && x[4] == x[2]+2 || x[4] == x[2]-2){
^
( )
checkers.c:140:29: warning: '&&' within '||' [-Wlogical-op-parentheses]
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){//checking if the new co of peshka are valid
~~~~~~~~~~~~~^~~~~~~~~~~~~~~ ~~
checkers.c:140:29: note: place parentheses around the '&&' expression to silence this warning
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){//checking if the new co of peshka are valid
^
( )
checkers.c:145:34: warning: '&&' within '||' [-Wlogical-op-parentheses]
else if(x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){//checking if the new co is
~~~~~~~~~~~~~^~~~~~~~~~~~~~~ ~~
checkers.c:145:34: note: place parentheses around the '&&' expression to silence this warning
else if(x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){//checking if the new co is
^
( )
checkers.c:175:47: warning: '&&' within '||' [-Wlogical-op-parentheses]
if(x[3] == x[1]-1 || x[3]==x[1]+1 && x[4]==x[2]+1 || x[4]==x[2]+1){
~~ ~~~~~~~~~~~~~^~~~~~~~~~~~~~~
checkers.c:175:47: note: place parentheses around the '&&' expression to silence this warning
if(x[3] == x[1]-1 || x[3]==x[1]+1 && x[4]==x[2]+1 || x[4]==x[2]+1){
^
( )
checkers.c:180:50: warning: '&&' within '||' [-Wlogical-op-parentheses]
else if(x[3]==x[1]+2 || x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){
~~ ~~~~~~~~~~~~~^~~~~~~~~~~~~~~
checkers.c:180:50: note: place parentheses around the '&&' expression to silence this warning
else if(x[3]==x[1]+2 || x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){
^
( )
checkers.c:210:29: warning: '&&' within '||' [-Wlogical-op-parentheses]
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){
~~~~~~~~~~~~~^~~~~~~~~~~~~~~ ~~
checkers.c:210:29: note: place parentheses around the '&&' expression to silence this warning
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){
^
( )
checkers.c:215:34: warning: '&&' within '||' [-Wlogical-op-parentheses]
else if(x[3]==x[1]+2 && x[4]==x[2]+2 || x[4]==x[2]-2){
~~~~~~~~~~~~~^~~~~~~~~~~~~~~ ~~
checkers.c:215:34: note: place parentheses around the '&&' expression to silence this warning
else if(x[3]==x[1]+2 && x[4]==x[2]+2 || x[4]==x[2]-2){
^
( )
checkers.c:106:34: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3] == x[1]+1 && x[4]==x[2]+1 || x[4] == x[2]-1){//checking if the new co of the peshka are valid
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:106:50: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3] == x[1]+1 && x[4]==x[2]+1 || x[4] == x[2]-1){//checking if the new co of the peshka are valid
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:109:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:111:39: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3] == x[1]+2 && x[4] == x[2]+2 || x[4] == x[2]-2){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:111:57: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3] == x[1]+2 && x[4] == x[2]+2 || x[4] == x[2]-2){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:121:25: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[2]>x[4]){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:129:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:140:32: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){//checking if the new co of peshka are valid
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:140:48: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){//checking if the new co of peshka are valid
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:143:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:145:37: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){//checking if the new co is
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:145:53: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){//checking if the new co is
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:156:25: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[2]>x[4]){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:160:26: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
y2 = x[4]-1;
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:164:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:175:50: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3] == x[1]-1 || x[3]==x[1]+1 && x[4]==x[2]+1 || x[4]==x[2]+1){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:175:67: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3] == x[1]-1 || x[3]==x[1]+1 && x[4]==x[2]+1 || x[4]==x[2]+1){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:178:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:180:53: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3]==x[1]+2 || x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:180:69: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3]==x[1]+2 || x[3]==x[1]-2 && x[4]==x[2]+2 || x[4]==x[2]-2){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:191:25: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[2]>x[4]){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:195:26: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
y2 = x[4]-1;
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:199:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:210:32: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:210:48: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[3]==x[1]-1 && x[4]==x[2]+1 || x[4]==x[2]-1){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:213:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:215:37: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3]==x[1]+2 && x[4]==x[2]+2 || x[4]==x[2]-2){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:215:53: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
else if(x[3]==x[1]+2 && x[4]==x[2]+2 || x[4]==x[2]-2){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:226:25: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
if(x[2]>x[4]){
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:230:26: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
y2 = x[4]-1;
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:234:44: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
Check_if_bacame_queen(x[3],x[4],board,turn);
^ ~
checkers.c:94:5: note: array 'x' declared here
int x[4];
^
checkers.c:263:22: warning: suggest braces around initialization of subobject [-Wmissing-braces]
int board[9][8]={0}, turn=2, peshka[2] = {12};
^
{}
checkers.c:266:28: warning: array index 2 is past the end of the array (which contains 2 elements) [-Warray-bounds]
while(Winner(peshka[1],peshka[2])){
^ ~
checkers.c:263:5: note: array 'peshka' declared here
int board[9][8]={0}, turn=2, peshka[2] = {12};
^
45 warnings generated.