예제 코드
반복
룰제시,입력,예외처리
연산
랜덤한숫자생성
연산
결과
출력
결과출력(승패)+전적
다시할것인지 확인 y,n (return;)
using System;
using System.Net.Http.Headers;
using System.Text;
using static System.Console;
using static System.Threading.Thread;
namespace RockPaperScissorsGame_V2
{
class Program
{
enum RSP { 가위 = 1, 바위, 보}
enum Result { 승, 무, 패}
static RSP Input()
{
RSP Player;
while (true)
{
ForegroundColor = ConsoleColor.White;
WriteLine("#######################################");
WriteLine("# 1.가위, 2,바위, 3.보 중 선택하시오. #");
WriteLine("#######################################");
string s = ReadLine();
if (s == "1")
{
Player = RSP.가위;
}
else if (s == "2")
{
Player = RSP.바위;
}
else if (s == "3")
{
Player = RSP.보;
}
else
{
Clear();
WriteLine("잘못된 입력입니다. 다시 입력해주세요 : ");
continue;
}
break;
}
return Player;
}
static Result GameResult(RSP Player, RSP Com, ref int win, ref int draw, ref int losw)
{
WriteLine($"플레이어 : {Player} vs 컴퓨터 : {Com}");
if (Player == Com)//비김
{
WriteLine($"컴퓨터 : {Com} VS 플레이어 : {Player}");
Sleep(1000);
ForegroundColor = ConsoleColor.Green;
WriteLine("---------------------------------");
WriteLine(" 무승부");
WriteLine("---------------------------------");
return Result.무;
}
else if ((Player == RSP.바위 && Com == RSP.가위) || (Player == RSP.가위 && Com == RSP.보)
|| (Player == RSP.보 && Com == RSP.바위)) //이김
{
WriteLine($"컴퓨터 : {Com} VS 플레이어 : {Player}");
Sleep(1000);
ForegroundColor = ConsoleColor.Blue;
WriteLine("---------------------------------");
WriteLine(" 플레이어 승리");
WriteLine("---------------------------------");
return Result.승;
}
else //짐
{
WriteLine($"컴퓨터 : {Com} VS 플레이어 : {Player}");
Sleep(1000);
ForegroundColor = ConsoleColor.Red;
WriteLine("---------------------------------");
WriteLine(" 플레이어 패배");
WriteLine("---------------------------------");
return Result.패;
}
}
static void Main(string[] args)
{
//선언부
Random r = new Random();
RSP Player, Com;
//전적
int win = 0, draw = 0, lose = 0;
ForegroundColor = ConsoleColor.Yellow;
SetCursorPosition(5, 5);
WriteLine("가위바위보게임에오신걸환영합니다가위바위보게임에오신걸환");
SetCursorPosition(5, 6);
WriteLine("위 영");
SetCursorPosition(5, 7);
WriteLine("바 가위 바위 보 게임에 오신걸 환영합니다 합");
SetCursorPosition(5, 8);
WriteLine("위 니");
SetCursorPosition(5, 9);
WriteLine("보게임에오신걸환영합니다가위바위보게임에오신걸환영합니다");
WriteLine();
while (true)
{
Com = (RSP)r.Next(1, 2 + 1);//컴퓨터 세팅
//입력부
Player = Input();
//연산+출력
//result = Gameresult(Player,Com)
Result result = GameResult(Player, Com,ref win, ref draw, ref lose);
//switch(result)
//{
// case Result.승:
// win++;
// break;
// case Result.무:
// draw++;
// break;
// case Result.패:
// lose++;
// break;
// default:
// break;
//}
//전적확인
System.Threading.Thread.Sleep(500);
ForegroundColor = ConsoleColor.White;
WriteLine("---------------------------------");
WriteLine($" 전적 : ({win + draw + lose}전) {win}승 {draw}무 {lose}패");
WriteLine("---------------------------------");
//게임시작
while (true)
{
System.Threading.Thread.Sleep(700);
WriteLine("한번 더 진행하겠습니까? ( Y / N)");//T,N,기타
WriteLine("---------------------------------");
string again = ReadLine().ToUpper();
if (again == "Y")
{
break;//재시작
}
else if (again == "N")
{
return;//게임종료
}
else
{
WriteLine("한번 더 진행하겠습니까? ( Y / N)");
continue;//예외처리
}
}
Clear();
}
}
}
}
결과
<Frist Round>
// #######################################
// # 1.가위, 2,바위, 3.보 중 선택하시오. #
// #######################################
// 2
// 플레이어 : 바위 vs 컴퓨터 : 가위
// 컴퓨터 : 가위 VS 플레이어 : 바위
// ---------------------------------
// 플레이어 승리
// ---------------------------------
// ---------------------------------
// 전적 : (1전) 1승 0무 0패
// ---------------------------------
// 한번 더 진행하겠습니까? ( Y / N)
// ---------------------------------
<Second Round>
// #######################################
// # 1.가위, 2,바위, 3.보 중 선택하시오. #
// #######################################
// 3
// 플레이어 : 보 vs 컴퓨터 : 보
// 컴퓨터 : 보 VS 플레이어 : 보
// ---------------------------------
// 무승부
// ---------------------------------
// ---------------------------------
// 전적 : (2전) 1승 1무 0패
// ---------------------------------
// 한번 더 진행하겠습니까? ( Y / N)
//---------------------------------
<Third Round>
// #######################################
// # 1.가위, 2,바위, 3.보 중 선택하시오. #
// #######################################
// 1
// 플레이어 : 가위 vs 컴퓨터 : 바위
// 컴퓨터 : 바위 VS 플레이어 : 가위
// ---------------------------------
// 플레이어 패배
// ---------------------------------
// ---------------------------------
// 전적 : (3전) 1승 1무 1패
// ---------------------------------
// 한번 더 진행하겠습니까? ( Y / N)
// ---------------------------------
// <Ending the Game>
// 한번 더 진행하겠습니까? ( Y / N)
// ---------------------------------
// N
'C# > 구현' 카테고리의 다른 글
C# - 숫자야구게임(4자리) 구현하기 (0) | 2024.06.19 |
---|---|
C# - 별먹기 구현하기 (0) | 2024.06.18 |
C# - 가위바위보 구현하기 (0) | 2024.06.17 |
C# - 주사위 앞, 뒤 숫자 구현하기 (0) | 2024.06.17 |
C# - 구구단 구현하기 (0) | 2024.06.16 |