0
#define _CRT_SECURE_NO_WARNINGS
#define MAX_LENGHT 70
#include <stdio.h>

int main() {
    int nm_lives,x=0, path_lenght, nm_moves, a=0,b=0,y=0;
    char player, positions_bomb[MAX_LENGHT], positions_treasure[MAX_LENGHT], sum_treasure = 0;



    printf("================================\n");
    printf("         Treasure Hunt!\n");
    printf("================================\n\n");
    printf("PLAYER Configuration\n");
    printf("--------------------\n");
    printf("Enter a single character to represent the player: ");
    scanf(" %c", &player);
    
    while (x == 0) {
        printf("Set the number of lives: ");
        scanf(" %d", &nm_lives);
        if (nm_lives >= 1 && nm_lives <= 10) {
            x = 1;
        }
        else {
            printf("     Must be between 1 and 10!\n");
        }
    }

    printf("Player configuration set-up is complete\n\n");
    
    
    printf("GAME Configuration\n");
    printf("------------------\n");
    x = 0;
    while (x == 0) {
        printf("Set the path length (a multiple of 5 between 10-70): ");
        scanf("%d", &path_lenght);
        if (path_lenght >= 10 && path_lenght <= 70) {
            x = 1;
        }
        else {
            printf("     Must be a multiple of 5 and between 10-70!!!\n");
        }
    }
    x = 0;
    while (x == 0) {
        printf("Set the limit for number of moves allowed: ");
        scanf("%d", &nm_moves);
        if (nm_moves >= 3 && nm_moves <= 26) {
            x = 1;
            printf("\n");
        }
        else {
            printf("     Value must be between 3 and 26\n");
        }
    }
    x = 0;
    printf("BOMB Placement\n");
    printf("--------------\n");
    printf("Enter the bomb positions in sets of 5 where a value\nof 1 = BOMB, and 0 = NO BOMB.Space - delimit your input.\n(Example: 1 0 0 1 1) NOTE: there are %d to set!\n", path_lenght);
    for (x = 0; x < path_lenght; x=x+5) {
        
        printf("   Positions [ %d- %d]: ",a, b );
        for(x=0;x<path_lenght;x=x+5)
            for (y = a; y <= b; y++) {
                scanf("%c", &positions_bomb[x]);
            }
            a += 1;
            b = a + 4;

    }
    printf("BOMB placement set\n\n");
    a = 0, b =0;
    printf("TREASURE Placement\n");
    printf("------------------\n");
    printf("Enter the treasure placements in sets of 5 where a value\nof 1 = TREASURE, and 0 = NO TREASURE.Space - delimit your input.\n(Example: 1 0 0 1 1) NOTE: there are %d to set!\n", path_lenght);
    x = 0;
    for (x = 0; x < path_lenght; x = x + 5) {

        printf("   Positions [ %d- %d]: ", a, b);
        scanf(" %c %c %c %c %c", &positions_treasure[x], &positions_treasure[x+1], &positions_treasure[x+2], &positions_treasure[x+3], &positions_treasure[x+4]);
        
        
        a += 1;
        b = a + 4;

    }
    
    printf("TREASURE placement set\n\n");
    printf("GAME configuration set-up is complete...\n\n");
    printf("------------------------------------\n");
    printf("TREASURE HUNT Configuration Settings\n");
    printf("------------------------------------\n");


    printf("Player: \n");
    printf("   Symbol     : %c\n", player);
    printf("   Lives      : %d\n", nm_lives);
    printf("   Treasure   : [ready for gameplay]\n");
    printf("   History    : [ready for gameplay]\n\n");
    printf("Game:\n");
    printf("   Path Length: %d\n", path_lenght);
    printf("   Bombs      : ");
    for (x = 0; x < path_lenght; x = x++) {
        printf(" %c", positions_bomb[x]);
    }
    printf("   Treasure   : \n\n");
    printf("====================================\n");
    printf("~ Get ready to play TREASURE HUNT! ~\n");
    printf("====================================\n");


    


    


    return 0;
} 

ввод бомб и сокровищ желтым отмечен ввод информации

вывод бомб и сокровищ

как принять места нахождения бомб и сокровищ именнотак как показано на картинке 1, а также как вывести информацию о них как показанно на картинке 2 сижу над этой проблемой два дня, вроде как все прогуглил, но похожих вопросов еще не было.

1 ответ 1

0

Советы по коду:

  1. Быть внимательным, а то много мелких ошибок.
  2. Проще работать с массивом чисел, чем с массивом символов.
  3. Считывать строку за раз, как было сделано во-втором случае.
  4. Если работать всё таки со символами, то не забывать обрабатывать пробельные символы, например, символ переноса строки.
// Объявление массивов
int positions_bomb[MAX_LENGHT], positions_treasure[MAX_LENGHT];

// Ввод
for (x = 0; x < path_lenght; x=x+5) {
    printf("   Positions [%2d-%2d]: ", x+1, x+5);
    scanf("%d %d %d %d %d", &positions_bomb[x], &positions_bomb[x+1], &positions_bomb[x+2], &positions_bomb[x+3], &positions_bomb[x+4]);
}

// Вывод
printf("   Bombs      : ");
for (x = 0; x < path_lenght; ++x) {
    printf("%d", positions_bomb[x]);
}
printf("\n");

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.