在C中我的战舰计划中放置船只时遇到问题

时间:2011-11-07 08:10:30

标签: c

我现在已经挣扎了大约2个小时。出于某种原因,不是将字符放在构成我的板的〜上,而是将字符放置在不是我输入的坐标的随机点中。希望有人能帮助我指导正确的方向。

void place_ships(int board[ROWS][COLS])
{

    int ship_type = 1, ship_length = 0, row = 0, col = 0, direction = 0, count = 0;

    for (ship_type = 1; ship_type < 6; ship_type++)
    {
                    if (ship_type == 1)
                    {
                        printf("Please enter where you would like to place your Carrier (5 spots).\n\n");
                        ship_length = 5;
                    }
                    else if (ship_type == 2)
                    {
                        printf("Please enter where you would like to place your Battleship (4 spots).\n\n");
                        ship_length = 4;
                    }
                    else if (ship_type == 3)
                    {
                        printf("Please enter where you would like to place your Submarine (3 spots).\n\n");
                        ship_length = 3;
                    }
                    else if (ship_type == 4)
                    {
                        printf("Please enter where you would like to place your Cruiser (3 spots).\n\n");
                        ship_length = 3;
                    }
                    else if (ship_type == 5)
                    {
                        printf("Please enter where you would like to place your Destroyer (2 spots).\n\n");
                        ship_length = 2;
                    }

        printf("Choose the direction of your ship. (0 is vertical, 1 is horizontal)\n\n");
        scanf("%d",&direction);

            printf("Please choose ROW number.\n");
            scanf("%d",&row);
            printf("Please choose COLUMN number.\n");
            scanf("%d",&col);

            if (direction == 0)
            {

                    for (count = 0; count < ship_length; count++)
                    {
                            if (ship_type == 1)
                            {
                                board[row+count][col] = 'c';
                                print_board (board, row, col);
                            }
                            else if (ship_type == 2)
                            {
                                board[row+count][col] = 'b';
                            }
                            else if (ship_type == 3)
                            {
                                board[row+count][col] = 's';
                            }
                            else if (ship_type == 4)
                            {
                                board[row+count][col] = 'r';
                            }
                            else if (ship_type == 5)
                            {
                                board[row+count][col] = 'd';
                            }
                    }
            }

            if (direction == 1)
            {
                    for (count = 0; count < ship_length; count++)
                    {
                            if (ship_type == 1)
                            {
                                board[row][col+count] = 'c';
                            }
                            else if (ship_type == 2)
                            {
                                board[row][col+count] = 'b';
                            }
                            else if (ship_type == 3)
                            {
                                board[row][col+count] = 's';
                            }
                            else if (ship_type == 4)
                            {
                                board[row][col+count] = 'r';
                            }
                            else if(ship_type == 5)
                            {
                                board[row][col+count] = 'd';
                            }
                        }
                }
        }
}

印刷板功能是:

void print_board (char board[ROWS][COLS], int num_rows, int num_cols)
{
    int row_index = 0, col_index = 0;

    for (row_index = 0; row_index < num_rows; row_index++)
    {
        for (col_index = 0; col_index < num_cols; col_index++)
        {
            printf ("%c ", board[row_index][col_index]);
        }
        putchar ('\n');
    }
}
好的,所以这就是我改变它的原因。我需要在标题中定义结构吗?我又是一个很大的初学者......

void place_ships(int board[ROWS][COLS])
{

    int ship_type = 0, ship_length = 0, row = 0, col = 0, direction = 0, count = 0;
    char * typestr[] = { "Ship type 0", "Carrier", "Battleship", "Submarine", "Cruiser", "Destroyer" };
    char* tag = "0cbsrd"; 

    for (ship_type = 0; ship_type < 6; ship_type++)
    {
        ship_length = 7 - ship_type;
        prinf("Please enter where you would like to place %s (%s) spots).\n\n",typstr[ship_type], ship_length);

        printf("Choose the direction of your ship. (0 is vertical, 1 is horizontal)\n\n");
        scanf("%d",&direction);

        printf("Please choose ROW number.\n");
        scanf("%d",&row);
        printf("Please choose COLUMN number.\n");
        scanf("%d",&col);

        if (direction == 0)
        {
            board[row+count][col] = tag[ship_type];
        }
        else
        {
            board[row][col+count] = tag[ship_type];
        }
    }
}

3 个答案:

答案 0 :(得分:2)

我看到一些可能会给你带来麻烦的潜在问题:

scanf有点棘手,因为它在读出数字后也会将新行字符留在缓冲区中。您可能希望使用fgets()代替从键盘读取,然后在字符串上使用sscanf来提取数字(或atoi)。

当您写入矩阵时,您需要确保在用户输入坐标后需要检测船舶,以及检查船舶是否适合的方向。

if ( direction == vertical )
{
  ok = ( row + ship_length < ROWS ) // assuming row index [0, ROWS-1]
}
else 
{
  ok = ( col + ship_length < COLS )
}

并且就像其他已经提到过的人一样,请使用枚举来获取常量值 e.g。

enum directions { vertical, horizontal };

答案 1 :(得分:1)

一个简单的switch语句,而不是if-else列表:

switch (ship_type)
{
case 1:
    board[row+count][col] = 'c';
    break;
case 2:
    board[row+count][col] = 'b';
    break;
case 3:
    board[row+count][col] = 's';
    break;
case 4:
    board[row+count][col] = 'r';
    break;
case 5:
    board[row+count][col] = 'd';
    break;
}

实际上,更好的解决方案可能是这样的:

char ship_symbols[] = { 'c', 'b', 's', 'r', 'd' };
board[row+count][col] = ship_symbols[ship_type - 1];  /* -1 because array indexes start at 0 */

答案 2 :(得分:1)

一些改进建议:

  • 如果可以计算某些内容,请不要使用if()内容。

    即,而不是

                    if (ship_type == 1)
                    {
                        printf("Please enter where you would like to place your Carrier (5 spots).\n\n");
                        ship_length = 5;
                    }
                                    ...
                    else if (ship_type == 5)
                    {
                        printf("Please enter where you would like to place your Destroyer (2 spots).\n\n");
                        ship_length = 2;
                    }
    

    更好地使用

    ship_length = 7 - ship_type;
    printf("Please enter where you would like to place your %s (%s spots).\n\n", typestr[ship_type], ship_length);
    

    如果您已在某处定义

     char * typestr[] = { "Ship type 0", "Carrier", "Battleship", "Submarine", "Cruiser", "Destroyer" };
    

    而不是

    if (ship_type == 1)
    {
        board[row+count][col] = 'c';
        print_board (board, row, col);
    }
    ...
    

    使用

    if (direction == 0) {
        board[row+count][col] = tag[ship_type];
    } else {
        board[row][col+count] = tag[ship_type];
    }
    print_board (board, row, col);
    

    char* tag = "0cbsrd";
    
  • 读入数据后添加一些输出。如果您确实已阅读了正确的数据,则会显示此信息。

  • 您的阵列边界是否正确?