我正在制作一个可以罚款的战舰游戏程序,我试图包含一个循环,通知玩家他们已经输入了相同的坐标,要求他们再次输入坐标以及循环通知他们船舶坐标函数中的无效坐标,但每次执行此操作时程序都会卡在输入坐标的循环中。这是程序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void originalboard(int board[][5])
{
int line, column;
for (line = 0; line < 5; line++)
for (column = 0; column < 5; column++)
board[line][column] = -1;
}
void displboard(int board[][5])
{
//variables declared for storing value in l/c
int line, column;
printf(" __1__ __2__ __3__ __4__ __5__");
printf("\n");
//diff symbols appear based on ship is hit or its miss when new grid generated
for (line = 0; line < 5; line++) {
printf("%d", line + 1);
for (column = 0; column < 5; column++) {
if (board[line][column] == -1) {
printf("|__~__|");
}
else if (board[line][column] == 0) {
printf("|__*__|");
}
else if (board[line][column] == 1) {
printf("|__X__|");
}
}
printf("\n");
}
}
void Shipsintialize(int ships[][2]) {
srand(time(NULL));
int ship, last;
for (ship = 0; ship < 3; ship++) {
ships[ship][0] = rand() % 5;
ships[ship][1] = rand() % 5;
//check if this shot was not tried
//if the shot was tried, just get out of the 'do while' loop when draws a pair that was not tried
for (last = 0; last < ship; last++) {
if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))
do {
ships[ship][0] = rand() % 5;
ships[ship][1] = rand() % 5;
} while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));
}
}
}
//coordinate values inputted and called from giveshot funct
void shipcoordinate(int shot[2])
{
//2 arrays used; coordinate require 2 values
while (shot[0] && shot[1] <=5)
{
printf("Line: ");
scanf("%d", &shot[0]);
shot[0]--;
printf("Column: ");
scanf("%d", &shot[1]);
shot[1]--;
}
if (shot[0] && shot[1] >=6)
{
printf("invalid coordinate");
}
}
int hitship(int shot[2], int ships[][2])
{
int ship;
for (ship = 0; ship < 3; ship++) {
if (shot[0] == ships[ship][0] && shot[1] == ships[ship][1]) {
printf("You hit a ship with the shot (%d,%d)\n", shot[0] + 1, shot[1] + 1);
return 1; //1 returned to give "X" symbol
}
}
return 0;
}
void tip(int shot[2], int ships[][2], int attempt)
{
int line = 0,
column = 0,
row;
//count how many ships there are in line/column
for (row = 0; row < 3; row++) {
if (ships[row][0] == shot[0])
line++;
if (ships[row][1] == shot[1])
column++;
}
printf("\nAttempt %d: \nline %d -> %d ships\ncolumn %d -> %d ships\n", attempt, shot[0] + 1, line, shot[1] + 1, column);
}
//condition of board changed here
void changeBoard(int shot[2], int ships[][2], int board[][5]) {
//if ship hit symbol "X"
if (hitship(shot, ships))
board[shot[0]][shot[1]] = 1;
else
//if miss or aka 0 "*" is used
board[shot[0]][shot[1]] = 0;
if (shot[0] && shot[1] >= 6)
{
}
}
int main() {
//initializing parameter of game
int board[5][5];
int ships[3][2];
int shot[2];
int attempts = 0,
hits = 0;
originalboard(board);
Shipsintialize(ships);
printf("\n");
//do while loop for when 3 ships are hit end game
do {
displboard(board);
shipcoordinate(shot);
attempts++;
if (hitship(shot, ships)) {
tip(shot, ships, attempts);
hits++;
}
else
tip(shot, ships, attempts);
changeBoard(shot, ships, board);
} while (hits != 3);
printf("\n\n\nFinished game. You were able to hit the three ships in %d attempts\n\n", attempts);
displboard(board);
}