숫자 야구

프로그래밍 2022. 12. 25. 15:54
728x90
SMALL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Hitter
{
    int[] Setdata;
    public Hitter()
    {
        Setdata = new int[3]; //세 칸짜리 배열 만들기
    }

    public int[] BatAve()
    {
        Random rand = new Random();

        Console.WriteLine("타자의 타율은 *할 *푼 *리 입니다.");
        Console.WriteLine("타자의 타율 3자리 숫자 * * *를 맞혀보세요.");
        Console.WriteLine("");

        for (int i = 0; i < Setdata.Length; i++)
        {
            Setdata[i] = rand.Next(0, 9); // 랜덤으로 숫자 세자리 생성
            for(int j=0; j<i; j++)
            { 
                if(Setdata[i] == Setdata[j])
                {
                    i--;
                }
            }
        } 
        return Setdata;
    }
}

class Pitcher
{
    private String GetStr;
    private int[] GetData;
    private char[] Char;

    public Pitcher()
    {
        GetStr = " ";
        GetData = new int[3];
        Char = new char[3];
    }

    public int[] Pitching()
    {
        Console.WriteLine("0~9 사이의 숫자 중에 중복되지 않게 3자리 숫자를 입력하세요 예) 367");
        GetStr = Console.ReadLine();

        for (int i =0; i<3; i++)
        {
            Char[i] = GetStr[i];
            GetData[i] = int.Parse(Char[i].ToString());
        }
        int sum = GetData[0] + GetData[1] + GetData[2];
        return GetData;
    }
}

class Referee
{
    private int Strike;
    public int Ball;
    public int STRIKE
    {
        get
        {
            return Strike;
        }
    }

    public int[] Judgement(int[] _GetNumber, int[] _SetNumber)
    {
        Strike = 0;
        Ball = 0;
        int[] Result = new int[2];

        Console.WriteLine("-------------------------------");
        for (int i=0;i<_GetNumber.Length; i++)
        {
            for(int j=0;j<_SetNumber.Length;j++)
            {
                if (_GetNumber[i] == _SetNumber[j])
                {
                    if (_GetNumber[i] == _SetNumber[i])
                    {
                        Strike += 1;
                        Result[0] = Strike;
                    }
                    else
                    {
                        Ball += 1;
                        Result[1] = Ball;
                    }
                }
            }
        }
        return Result;
    }

    public void CheckResult(int[] _Result)
    {
        int sum = 0;
        sum = Strike + Ball;
            
        if(Strike == 0 && Ball == 0)
        {
            Console.WriteLine("맞는 숫자가 하나도 없습니다.");
        }
        else
        {
            Console.WriteLine("맞는 숫자가" + sum + "개 있습니다.");
        }
        Console.WriteLine("strike : " + Strike);
        Console.WriteLine("Ball :" + Ball);
        Console.WriteLine("");
    }

    public void ShowFinalResult(bool _IsEnd, int[] _SetNumber)
    {
        if(true == _IsEnd)
        {
            Console.WriteLine("스트라이크 아웃");
        }
        else
        {
            Console.WriteLine("홈런을 맞았네여");
        }
        Console.WriteLine("타자의 타율은" + _SetNumber[0] + "할" + _SetNumber[1] + "푼" + _SetNumber[2] + "리 입니다.");

    }

    public void ShowMsg(string _Msg)
    {
        Console.WriteLine(_Msg);
    }
}

class Program
{
    static void Main(string[] args)
    {
        int[] SetNumber;
        int[] GetNumber;
        int[] BallCount;
        bool IsEnd = false;

        Pitcher NewPitcher = new Pitcher();
        Hitter NewHitter = new Hitter();
        Referee NewReferee = new Referee();

        SetNumber = NewHitter.BatAve();

        for (int i=0;i<10;i++)
        {
            NewReferee.ShowMsg((i + 1) + "번째 투구입니다.");
            GetNumber = NewPitcher.Pitching();
            BallCount = NewReferee.Judgement(GetNumber, SetNumber);
            NewReferee.CheckResult(BallCount);

            if(NewReferee.STRIKE ==3)
            {
                IsEnd = true;
                break;
            }
        }
        NewReferee.ShowFinalResult(IsEnd, SetNumber);
    }
}

728x90

설정

트랙백

댓글