C_C++

(C/C++) 카운트다운 게임: 정해진 시간 이내에 숫자 맞히기

고니자니 2024. 1. 6. 09:30
반응형

카운트다운 게임은 일반적으로 특정 숫자에서 시작하여 1씩 감소시키면서 플레이어가 정해진 시간 안에 특정 목표 숫자에 도달하는 게임입니다.

컴퓨터가 시작 숫자를 1에서 10까지의 범위에서 무작위로 선택하고, 사용자가 1을 더하거나 빼는 입력을 받아 목표 숫자에 도달하도록 하는 간단한 카운트다운 게임을 구현한 것입니다. 게임의 제한 시간은 30초로 설정되어 있습니다. 필요에 따라 목표 숫자와 시간 제한을 조절하여 사용할 수 있습니다.

 

여기에서는 C언어와 C++ 언어의 코드가 있으며, 아래쪽에 파이썬 코드의 링크가 있습니다.

 

C언어

#define _CRT_SECURE_NO_WARNINGS  // Visual Studio
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void countdown_game(int target_number, int time_limit) {
    int current_number = rand() % 10 + 1;  // 시작 숫자를 무작위로 선택
    printf("게임 시작! 현재 숫자: %d\n", current_number);

    time_t start_time = time(NULL);  // 게임 시작 시간 기록

    while (current_number != target_number) {
        printf("1을 더하거나 뺀 후 Enter를 눌러주세요.\n");
        char operation;
        scanf(" %c", &operation);

        if (operation == '+') {
            current_number += 1;
        }
        else if (operation == '-') {
            current_number -= 1;
        }
        else {
            printf("올바른 연산을 선택하세요.\n");
            continue;
        }

        printf("현재 숫자: %d\n", current_number);

        // 시간 초과 확인
        time_t current_time = time(NULL);
        if (current_time - start_time > time_limit) {
            printf("시간 초과! 게임 종료.\n");
            break;
        }
    }

    if (current_number == target_number) {
        printf("축하합니다! 목표 숫자 %d에 도달하셨습니다.\n", target_number);
        printf("소요된 시간: %ld초\n", time(NULL) - start_time);
    }
}

int main() {
    srand((unsigned int)time(NULL));
    countdown_game(10, 30);  // 예제로 목표 숫자를 10, 시간 제한을 30초로 설정
    return 0;
}

 

C++ 언어

#include <iostream>
#include <ctime>
#include <cstdlib>

void countdown_game(int target_number, int time_limit) {
    int current_number = rand() % 10 + 1;  // 시작 숫자를 무작위로 선택
    std::cout << "게임 시작! 현재 숫자: " << current_number << std::endl;

    time_t start_time = time(nullptr);  // 게임 시작 시간 기록

    while (current_number != target_number) {
        std::cout << "1을 더하거나 뺀 후 Enter를 눌러주세요." << std::endl;
        char operation;
        std::cin >> operation;

        if (operation == '+') {
            current_number += 1;
        } else if (operation == '-') {
            current_number -= 1;
        } else {
            std::cout << "올바른 연산을 선택하세요." << std::endl;
            continue;
        }

        std::cout << "현재 숫자: " << current_number << std::endl;

        // 시간 초과 확인
        time_t current_time = time(nullptr);
        if (current_time - start_time > time_limit) {
            std::cout << "시간 초과! 게임 종료." << std::endl;
            break;
        }
    }

    if (current_number == target_number) {
        std::cout << "축하합니다! 목표 숫자 " << target_number << "에 도달하셨습니다." << std::endl;
        std::cout << "소요된 시간: " << time(nullptr) - start_time << "초" << std::endl;
    }
}

int main() {
    srand(static_cast<unsigned int>(time(nullptr)));
    countdown_game(10, 30);  // 예제로 목표 숫자를 10, 시간 제한을 30초로 설정
    return 0;
}

 

실행한 결과 화면입니다.

(C/C++) 카운트다운 게임: 정해진 시간 이내에 숫자 맞히기

 

시간이 초과된 경우

(C/C++) 카운트다운 게임: 정해진 시간 이내에 숫자 맞히기

 


 

 

(파이썬) 카운트다운, 정해진 시간 안에 숫자 맞히기 게임

카운트다운 게임은 일반적으로 특정 숫자에서 시작하여 1씩 감소시키면서 플레이어가 정해진 시간 안에 특정 목표 숫자에 도달하는 게임입니다. 여기에는 파이썬 코드로 간단하게 구현했습니

coding-abc.kr

 

728x90
반응형