[가위바위보]
툴 버전 : Visual Studio 2022
Rand을 활용한 가위바위보 게임
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /* 랜덤을 활용한 가위바위보 게임 만들기 */ #include<stdio.h> #include<stdlib.h> #include<time.h> int main(void) { int Player = 0; int Computer = 0; int Victory = 0; int A_draw = 0; int Defeat = 0; int rand(void); while (1) { printf("바위는(1) 가위는(2) 보는(3) 결과(0): "); scanf_s("%d", &Player); srand((int)time(NULL)); if (Player==0) { break; } if (Player != 1 && Player != 2 && Player != 3) { printf("바위는(1) 가위는(2) 보는(3) 중에 선택해주세요.\n"); } if (Player == 1 || Player == 2 || Player == 3) { switch (Player) { case 1: printf("당신은 바위선택,"); break; case 2: printf("당신은 가위선택,"); break; case 3: printf("당신은 보 선택,"); break; } switch (Computer = rand() % 3 + 1) { case 1: printf("컴퓨터는 바위선택,"); break; case 2: printf("컴퓨터는 가위선택,"); break; case 3: printf("컴퓨터는 보 선택,"); break; } if (Player == Computer) { printf("비겼습니다.\n"); A_draw += 1; } else if ((Player == 1 && Computer == 2) || (Player == 2 && Computer == 3) || (Player == 3 && Computer == 1)) { printf("이겼습니다.\n"); Victory += 1; } else if ((Player == 1 && Computer == 3) || (Player == 2 && Computer == 1) || (Player == 3 && Computer == 2)) { printf("졌습니다.\n"); Defeat += 1; } } }//while printf("게임 결과 %d승 %d무 %d패 \n", Victory, A_draw, Defeat); return 0; }//main | cs |
728x90
'프로그래밍 > C 언어(코딩)' 카테고리의 다른 글
C언어] 숫자 슬롯 머신 게임 (배열) (1) | 2023.12.19 |
---|---|
C언어] 숫자 베이스볼 게임 (함수화) (0) | 2023.12.18 |
C언어] 복합대입 연산자를 활용한 수의 합 구하기 (1) | 2023.12.17 |
C언어] if문 성적표 만들기 (1) | 2023.12.17 |
C언어] 별찍기 (0) | 2023.12.15 |