C_C++

(C언어) 가위 바위 보 게임 Play the rock-paper-scissors game

고니자니 2022. 12. 18. 20:53
반응형

C언어를 이용한 가위 바위 보 게임입니다.

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

int main()
{
	int player;
	int com;
	int draw = 0, win = 0, lose = 0;
	char* str[] = { "","가위","바위","보" };

	srand((unsigned)time(NULL));

	printf("* 가위-바위-보 게임(Play the rock-paper-scissors game) *\n\n");
    
	do
	{
		printf("\n선택>> 가위(1), 바위(2), 보(3), 종료(0) : ");
		com = rand() % 3 + 1;
		scanf("%d", &player);
		if (player <= 0) break;

		if (player < 1 || player>3)
		{
			printf("다시 입력해주세요.\n");
		}
		else if (player == com)
		{
			printf("무승부입니다 (사람:%s, 컴퓨터:%s)\n", str[player], str[com]);
			draw++;
		}
		else if ((player == 1 && com == 2) || (player == 2 && com == 3) || (player == 3 && com == 1))
		{
			printf("졌습니다 (사람:%s, 컴퓨터:%s)\n", str[player], str[com]);
			win++;
		}
		else
		{
			printf("이겼습니다 (사람:%s, 컴퓨터:%s)\n", str[player], str[com]);
			lose++;
		}
	} while (1);
	printf("\n\n");
	printf("사람 : %d승, %d무, %d패 입니다.\n", win, draw, lose);
	printf("컴퓨터: %d승, %d무, %d패 입니다.\n", lose, draw, win);

	return 0;
}

 

 

 

 

728x90
반응형