10/18 c언어 기초반 난수생성하기,가위바위보 프로그램 만들기
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
//가위바위보 게임 만들기
int user,cpu;
printf("0: 가위 ,1: 바위 , 2: 보 하나를 내시오 \n");
scanf("%d" , &user);
srand(time(NULL)); // 난수 초기화
//유저
if (user == 0){
printf("유저: 가위\n");
}else if(user == 1){
printf("유저: 바위\n");
}else if(user == 2){
printf("유저: 보\n");
}else{
printf("잘못된 수 입니다.");
}
//컴퓨터
cpu = rand() % 3;
if (cpu == 0){
printf("컴퓨터: 가위\n");
}else if(cpu == 1){
printf("컴퓨터: 바위\n");
}else if(cpu == 2){
printf("컴퓨터: 보\n");
}else{
printf("잘못된 수 입니다.");
}
//승부 판별기
if(cpu == user){
printf("결과 : 비겻습니다.\n");
}else if(user == 0 && cpu == 2 || user == 1 && cpu ==0 || user == 2 && cpu ==1){
printf("결과: 이겼습니다.\n");
}else{
printf("결과: 졌습니다.\n");
}
}
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.