0
我想制作一个战列舰程序。到目前为止,我的程序要求用户1输入他/她想要他们的船只的位置。然后用户2猜测他们认为船只在哪里。遇到麻烦在c中的战列舰程序while循环
我试图让我的程序重新提示用户2,如果他们没有第一次打所有玩家1的船。
我试过在几个地方放一段while循环while循环,但每次我的程序崩溃时,while循环现在也会使它崩溃。似乎没有任何工作。
#include <stdio.h>
int main(void)
{
int board[2][2];
int i, j; //initialize loop variables
int i2, j2; //initialize 2nd loop variables
int i3, j3; // 3rd loop variables
printf(" User 1: Enter a '1' where you want to place your ship and '0' where you do not.\n");
/* these loops prompt the user to enter a 0 or 1 for each space in the 2d array depending on where they want their ship*/
for(i = 0; i <= 1 ; i++)
{
for(j = 0 ; j <= 1 ; j++)
{
printf("space[%d][%d]: ", i, j);
scanf("%d", &board[i][j]);
}
}
while(board[i][j] == 1)
{
/*used to prompt the user2 as long as user1 still has ships left*/
int board2[2][2];
printf("User 2: Enter a '1' where you think User 1 placed their ship and '0' where \nyou do not.\n");
/* Asks user2 for their guesses */
for(i2 = 0 ; i2 <= 1 ; i2++)
{
for(j2 = 0 ; j2 <= 1 ; j2++)
{
printf("space[%d][%d]:", i2, j2);
scanf("%d", &board2[i2][j2]);
}
}
for(i3 = 0 ; i3 <= 1 ; i3++)
{
//compares user1 input to user2 guess
for(j3 = 0 ; j3 <= 1 ; j3++)
{
if(board[i3][j3] == 1 && board2[i3][j3] == 1)
{
printf("Hit!\n"); // if the inputs match display "hit"
board[i][j] = 0;
}
else
{
printf("Miss!\n"); // if no hit display miss
}
}
}
}
return 0;
}
'while(board [i] [j] == 1){':出入界限。我认为无用的代码。 – BLUEPIXY
while(board [i] [j] == 1){:我同意这个部分现在是无用的,但我不知道在哪里放置while循环来让它重新提示用户2。 – nicky
User2只需要输入预期的船的位置。并判断它。 – BLUEPIXY