조건
더보기
1) 3사람이 돌아가면서 쏜다
2) 7발의 총알이 들어있는 총을 번갈아 가면서 쏜다.
3) 초기에 총알은 하나의 번호로 설정되고
4) 교대로 총을 발사하면서 랜덤 수 발생
5) 초기 설정된 총알 위치의 번호와 일치하는 번호가 생성되면 사망
6) 기타 다른 기능이나 요소 및 재미 등은 자유롭게 설계 구현함.
코드
원래 소스파일, 헤더파일.. 분리해서 작성하는데 귀찮아서 한 파일에 다 때려박았다 (어차피 코드 길이 짧아서 괜찮을 듯..?)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef char element[100];
typedef struct ListNode {
element data;
struct ListNode* link;
}ListNode;
ListNode* insert_first(ListNode* head, const element data) {
ListNode* node = (ListNode*)malloc(sizeof(ListNode));
if (!node) {
fprintf(stderr, "메모리 할당 에러\n");
exit(1);
}
strcpy(node->data, data);
if (head == NULL) {
head = node;
node->link = node;
}
else {
node->link = head->link;
head->link = node;
}
return head;
}
void delete_node(ListNode** head, ListNode** current) {
ListNode* temp = *current;
if (*head == (*head)->link) {
free(*current);
*current = NULL;
*head = NULL;
return;
}
ListNode* prev = *head;
while (*current != prev->link) {
prev = prev->link;
}
if (*current == *head)
*head = prev;
prev->link = (*current)->link;
*current = (*current)->link;
free(temp);
}
void print_list(ListNode* head) {
ListNode* p;
if (head == NULL) return;
if (head->link == head) {
printf("%s\n", head->data);
return;
}
p = head->link;
do {
printf("%s, ", p->data);
p = p->link;
} while (p != head);
printf("%s\n", p->data);
}
int count_list(ListNode* head) {
int count = 1;
ListNode* p;
if (head == NULL) return 0;
for (ListNode* p = head->link; p != head; p = p->link)
count++;
return count;
}
inline int get_bullet_num() {
return rand() % 7 + 1;
}
inline int check_input(int sel) {
if (sel == 1 || sel == 0) return 1;
printf("잘못된 입력입니다. 다시 입력해주세요\n");
return 0;
}
int main() {
srand((unsigned)time(NULL));
char* userList[] = { "KIM", "PARK", "CHOI" };
int bullet, user, sel1, sel2;
ListNode* current;
ListNode* head = NULL;
for (int i = 0; i < 3; i++)
head = insert_first(head, userList[i]);
current = head->link;
bullet = get_bullet_num();
while (1) {
printf("현재 %s님 차례입니다.\n", current->data);
while(1) {
printf("실리더를 돌립니다...\n");
user = get_bullet_num();
while (1) {
printf("그대로 쏘겠습니까(1)? 다시 돌리겠습니까(0)? ");
scanf("%d", &sel1);
if (check_input(sel1))break;
}
if (sel1) break;
else {
printf("다시 ");
continue;
}
}
if (bullet == user) {
printf("%s님은 사망하셨습니다.\n\n", current->data);
delete_node(&head, ¤t);
if (count_list(head) <= 1) break;
printf("현재 생존자(%d): ", count_list(head));
print_list(head);
while (1) {
printf("이어서 게임을 시작하시겠습니까?(이어서:1, 끝내기: 0) ");
scanf("%d", &sel2);
printf("\n");
if (check_input(sel2))break;
else printf("잘못된 입력입니다. 다시 입력해주세요\n");
}
if (sel2) {
bullet = get_bullet_num();
continue;
}
else break;
}
else {
printf("%s님은 살아남으셨습니다.\n\n", current->data);
current = current->link;
}
}
printf("\n최종 생존자(%d): ", count_list(head));
print_list(head);
while (head != NULL) {
delete_node(&head, &head->link);
}
printf("게임을 종료합니다.\n");
}
실행화면


'자료구조' 카테고리의 다른 글
| [C언어| 연결리스트] 연결리스트로 표현한 다항식 (0) | 2025.05.12 |
|---|---|
| [C언어| 스택(stack)] 미로 게임 만들기 (자동 생성 및 키보드 입력을 통한 탈출) (0) | 2025.05.02 |
| [쉽게 풀어 쓴 자료구조] 4장 연습문제 EXERCISE (programming) (0) | 2025.04.02 |