C#/수업 과제

반환값이 있는 메서드 작성 (scv 미네랄, 가스 캐기, 좌표)

hyunjin-dev-log 2021. 12. 23. 17:15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static int prevX = 0;
        static int prevY = 0;

        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("x좌표 : ");
                int input1 = Convert.ToInt32(Console.ReadLine());
                Console.Write("y좌표 : ");
                int input2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("채취한 자원 : {0}", Move(input1, input2));
                Console.WriteLine();
            }
        }

        static string Move(int x, int y)
        {
            Console.WriteLine("scv가 ({0}, {1})에서 ({2}, {3})로 이동합니다.", prevX, prevY, x, y);
            
            while (x > prevX)
            {
                Console.WriteLine("({0}, {1})", ++prevX, prevY);
            }
            while (x < prevX)
            {
                Console.WriteLine("({0}, {1})", --prevX, prevY);
            }
            while (y > prevY)
            {
                Console.WriteLine("({0}, {1})", prevX, ++prevY);
            }
            while (y < prevY)
            {
                Console.WriteLine("({0}, {1})", prevX, --prevY);
            }

            Console.WriteLine("이동을 완료했습니다.");

            if (prevX == 3 && prevY == 4)
            {
                Console.WriteLine("현재좌표 ({0}, {1})", x, y);
                Console.WriteLine("미네랄을 채취합니다.");
                return "미네랄";
            }
            if (prevX == -4 && prevY == 0)
            {
                Console.WriteLine("현재좌표 ({0}, {1})", x, y);
                Console.WriteLine("가스를 채취합니다.");
                return "가스";
            }
            else
            {
                Console.WriteLine("현재좌표 ({0}, {1})", x, y);
                Console.WriteLine("이곳엔 자원이 없습니다.");
                return null;
            }
        }
    }
}

HelloWorld.exe
0.01MB