-
(C/C++) 가위바위보 게임 구현하기C언어 2021. 6. 30. 17:50
- 컴퓨터와 가위바위보를 하는 게임을 구현해보자.
- 입력은 숫자로 받고 1(바위), 2(가위), 3(보) 로 정했다.
- 컴퓨터의 수는 랜덤하게 정해지고 승패 결과를 출력하고 누적 승리 수를 체크한다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include <stdio.h>#include <stdlib.h>#include <time.h>// 1: 바위 2: 가위 3: 보void rsp(int n) {static int win = 0;int computer = rand() % 3 + 1;char str[3];switch (computer) {case 1:printf("컴퓨터는 바위를 냈다!\n");break;case 2:printf("컴퓨터는 가위를 냈다!\n");break;case 3:printf("컴퓨터는 보를 냈다!\n");break;}if (n == 1) {if (computer == 1) {printf("무승부!\n");}else if (computer == 2) {win++;printf("Player WIN!\n");}else {printf("Computer WIN!\n");}}else if (n == 2) {if (computer == 2) {printf("무승부!\n");}else if (computer == 3) {win++;printf("Player WIN!\n");}else {printf("Computer WIN!\n");}}else {if (computer == 3) {printf("무승부!\n");}else if (computer == 1) {win++;printf("Player WIN!\n");}else {printf("Computer WIN!\n");}}printf("누적 승리 : %d\n", win);printf("===========================\n");}void main() {srand(time(NULL));int num;for (int i = 0; i < 10; i++) {printf("바위(1) 가위(2) 보(3) : ");scanf("%d", &num);rsp(num);}}cs - 실행 결과
3번밖에 못이겼다.. 'C언어' 카테고리의 다른 글
(C / C++) 파일 입출력 해보기(업다운게임) (0) 2021.07.01 Call by value vs Call by reference (0) 2021.06.30 (C/C++) 문자열 맞추기 게임 구현하기! (0) 2021.06.29 (C/C++) 폭탄게임(지뢰찾기) 구현해보기! (0) 2021.06.29