/*
문제
'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다.
게임은 NxN 정사각 보드위에서 진행되고, 몇몇 칸에는 사과가 놓여져 있다. 보드의 상하좌우 끝에 벽이 있다. 게임이 시작할때 뱀은 맨위 맨좌측에 위치하고 뱀의 길이는 1 이다. 뱀은 처음에 오른쪽을 향한다.
뱀은 매 초마다 이동을 하는데 다음과 같은 규칙을 따른다.

먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다.
만약 벽이나 자기자신의 몸과 부딪히면 게임이 끝난다.
만약 이동한 칸에 사과가 있다면, 그 칸에 있던 사과가 없어지고 꼬리는 움직이지 않는다.
만약 이동한 칸에 사과가 없다면, 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다. 즉, 몸길이는 변하지 않는다.
사과의 위치와 뱀의 이동경로가 주어질 때 이 게임이 몇 초에 끝나는지 계산하라.

입력
첫째 줄에 보드의 크기 N이 주어진다. (2 ≤ N ≤ 100) 다음 줄에 사과의 개수 K가 주어진다. (0 ≤ K ≤ 100)
다음 K개의 줄에는 사과의 위치가 주어지는데, 첫 번째 정수는 행, 두 번째 정수는 열 위치를 의미한다. 사과의 위치는 모두 다르며, 맨 위 맨 좌측 (1행 1열) 에는 사과가 없다.
다음 줄에는 뱀의 방향 변환 횟수 L 이 주어진다. (1 ≤ L ≤ 100)
다음 L개의 줄에는 뱀의 방향 변환 정보가 주어지는데, 정수 X와 문자 C로 이루어져 있으며. 게임 시작 시간으로부터 X초가 끝난 뒤에 왼쪽(C가 'L') 또는 오른쪽(C가 'D')로 90도 방향을 회전시킨다는 뜻이다. X는 10,000 이하의 양의 정수이며, 방향 전환 정보는 X가 증가하는 순으로 주어진다.
*/

using System;
using System.IO;
using System.Timers;

class Program
{
    public static void Main(string[] args)
    {
        if(args.Length == 1)
        {
            SNAKENM nm = new SNAKENM();
            nm.RUN("EX");
        }
        else
        {
            SNAKENM nm = new SNAKENM();
            nm.RUN();
        }
    }
}       
class SNAKENM
{
    static int MAPY = 100;
    static int MAPX = 100;

    const int LEFT = 1000;
    const int RIGHT = 1001;
    const int DOWN = 1002;
    const int UP = 1003;
    const int ROTATION_DEGREE_90_RIGHT = 10;
    const int ROTATION_DEGREE_90_LEFT = 20;
    const int EXIT_EVENT = -99;
    const int APPLE_EVENT = 2;
    const int APPLE_DELETE_EVENT = 3;

    int[] TIMER_EVENT = new int[1024];
    int[] TIMER_EVENT_DETAIL = new int[1024];
    int TIMER_EVENT_CNT=0;
    int timer_index = 0;

    int[,] APPLE = new int[MAPY,MAPX];
    int draw_apple_event = 0;
    int[,] BOARD = new int[MAPY,MAPX];
    int[] BOARD_POSITION_X = new int[MAPX];
    int[] BOARD_POSITION_Y = new int[MAPY];
    int position_index=0;
    int apple_delete_event_index=0;
    int APPLE_CNT=0;

    int xpos = 1;
    int ypos = 1;
    int direction = LEFT;
    int timer = 0;

    Timer tm = new Timer();

    public void RUN()
    {
        DATA_READ();
        DATA_INIT();
        TIME_INIT();

        ConsoleKeyInfo keyInfo;
        while(true)
        {
            keyInfo=Console.ReadKey(true);
            if(keyInfo.Key == ConsoleKey.Escape) break;
        }
    }
    void TIME_TICK(object sender, ElapsedEventArgs e)
    {
        timer++;

        if(direction == LEFT) xpos++;
        else if(direction == RIGHT) xpos--;
        else if(direction == UP) ypos--;
        else if(direction == DOWN) ypos++;

        if( (BOARD[ypos,xpos]==EXIT_EVENT) || (BOARD[ypos,xpos]==1))
        {
            tm.Stop();
            tm=null;
            Console.WriteLine("[TIME]:" + timer.ToString("00000"));
            Environment.Exit(0);
        }

        //APPLE EVENT
        if(APPLE[ypos,xpos]==APPLE_EVENT)
        {
            draw_apple_event = APPLE_EVENT;
            APPLE[ypos,xpos]=APPLE_DELETE_EVENT;

            if(direction == LEFT) xpos--;
            else if(direction == RIGHT) xpos++;
            else if(direction == UP) ypos++;
            else if(direction == DOWN) ypos--;
        }
        else if(APPLE[ypos,xpos]==APPLE_DELETE_EVENT)
        {
            draw_apple_event = APPLE_DELETE_EVENT;

            //마지막꼬리의 값을 없앤다.
            int xx = BOARD_POSITION_X[apple_delete_event_index];
            int yy = BOARD_POSITION_Y[apple_delete_event_index];
            BOARD[yy,xx] = 0;
            apple_delete_event_index++;
        }
        else
        {
            draw_apple_event = 0;
        }

        BOARD[ypos,xpos]=1;
        BOARD_POSITION_X[position_index] = xpos;
        BOARD_POSITION_Y[position_index] = ypos;
        position_index++;

        if(timer == TIMER_EVENT[timer_index])
        {
            if(TIMER_EVENT_DETAIL[timer_index]==ROTATION_DEGREE_90_LEFT)
            {
                if(direction==LEFT) direction = UP;
                else if(direction==UP) direction = RIGHT;
                else if(direction==RIGHT) direction = DOWN;
                else if(direction==DOWN) direction = LEFT;
            }
            else if(TIMER_EVENT_DETAIL[timer_index]==ROTATION_DEGREE_90_RIGHT)
            {
                if(direction==LEFT) direction = DOWN;
                else if(direction==UP) direction = LEFT;
                else if(direction==RIGHT) direction = UP;
                else if(direction==DOWN) direction = RIGHT;
            }
            timer_index++;
        }

        DRAW();
    }
    public void RUN(string EX)
    {
        DATA_INIT(EX);
        TIME_INIT();

        ConsoleKeyInfo keyInfo;
        while(true)
        {
            keyInfo=Console.ReadKey(true);
            if(keyInfo.Key == ConsoleKey.Escape) break;
        }
    }
    void DATA_READ()
    {
        string inputText = "";
        int index = 0;

        while(true)
        {
            if(index == 0)
            {
                Console.WriteLine("1. MAPY * MAPX");
                inputText = Console.ReadLine();

                MAPX=Convert.ToInt32(inputText) + 2;
                MAPY=Convert.ToInt32(inputText) + 2;
            }
            else if(index == 1)
            {
                Console.WriteLine("2. APPLE_CNT");
                inputText = Console.ReadLine();

                APPLE_CNT=Convert.ToInt32(inputText);
            }
            else if(index == 2)
            {
                for(int ii=0; ii<APPLE_CNT; ii++)
                {
                    Console.WriteLine("3. APPLE_POSITION:[" + (ii+1).ToString("00") + "]");
                    inputText = Console.ReadLine();

                    string[] position = (inputText).Split(' ');
                    if(position.Length != 2)
                    {
                        Console.WriteLine("invalid data error!!");
                        Environment.Exit(0);
                    }

                    int yy = Convert.ToInt32(position[0]) + 1;
                    int xx = Convert.ToInt32(position[1]) + 1;

                    APPLE[yy,xx]=APPLE_EVENT;
                }
            }
            else if(index == 3)
            {
                Console.WriteLine("4. TIMER_EVENT_CNT");
                inputText = Console.ReadLine();

                TIMER_EVENT_CNT=Convert.ToInt32(inputText);
            }
            else if(index == 4)
            {
                for(int ii=0; ii<TIMER_EVENT_CNT; ii++)
                {
                    Console.WriteLine("5. TIMER EVENT DETAIL:[" + (ii+1).ToString("00") + "]");
                    inputText = Console.ReadLine();     

                    string[] position = (inputText).Split(' ');
                    if(position.Length != 2)
                    {
                        Console.WriteLine("invalid data error!!");
                        Environment.Exit(0);
                    }

                    TIMER_EVENT[ii] = Convert.ToInt32(position[0]);
                    if(position[1] == "L") TIMER_EVENT_DETAIL[ii] = ROTATION_DEGREE_90_LEFT;
                    else if(position[1] == "D") TIMER_EVENT_DETAIL[ii] = ROTATION_DEGREE_90_RIGHT;
                }
                break;
            }
            index++;
        }
    }
    void DATA_INIT(string EX)
    {
        MAPX = 10 + 2;
        MAPY = 10 + 2;

        APPLE[1,5]=APPLE_EVENT;
        APPLE[1,3]=APPLE_EVENT;
        APPLE[1,2]=APPLE_EVENT;
        APPLE[1,6]=APPLE_EVENT;
        APPLE[1,7]=APPLE_EVENT;

        TIMER_EVENT[0]=8;
        TIMER_EVENT_DETAIL[0]=ROTATION_DEGREE_90_RIGHT;

        TIMER_EVENT[1]=10;
        TIMER_EVENT_DETAIL[1]=ROTATION_DEGREE_90_RIGHT;

        TIMER_EVENT[2]=11;
        TIMER_EVENT_DETAIL[2]=ROTATION_DEGREE_90_RIGHT;

        TIMER_EVENT[3]=13;
        TIMER_EVENT_DETAIL[2]=ROTATION_DEGREE_90_LEFT;

        for(int ii=0; ii<MAPY; ii++)
        {
        BOARD[ii,0]=-99;
        BOARD[ii,MAPX-1]=-99;
        }
        for(int kk=0; kk<MAPX; kk++)
        {
        BOARD[0,kk]=-99;
        BOARD[MAPY-1,kk]=-99;
        }
        BOARD[ypos,xpos]=1;
        BOARD_POSITION_X[position_index] = xpos;
        BOARD_POSITION_Y[position_index] = ypos;
        position_index++;
    }
    void DATA_INIT()
    {
        for(int ii=0; ii<MAPY; ii++)
        {
            BOARD[ii,0]=-99;
            BOARD[ii,MAPX-1]=-99;
        }
        for(int kk=0; kk<MAPX; kk++)
        {
        BOARD[0,kk]=-99;
        BOARD[MAPY-1,kk]=-99;
        }
        BOARD[ypos,xpos]=1;
    }
    void TIME_INIT()
    {
        tm.Elapsed += new ElapsedEventHandler(TIME_TICK);
        tm.Interval = 1000;
        tm.Start();
    }   
    void DRAW()
    {
        string mmmText = "";

        mmmText += "[" + timer.ToString("00000") + "]" + ",EVENT[" + draw_apple_event.ToString("00000") + "]" + ",X[" + ypos.ToString("00000") + "]" + ",Y[" + xpos.ToString("00000") + "]" + "\n";
        for(int ii=0; ii<MAPY; ii++)
        {
            for(int kk=0; kk<MAPX; kk++)
            {
                if(BOARD[ii,kk]==1) mmmText += "[**]";
                else mmmText += ii.ToString("00") + kk.ToString("00");
            }
            mmmText += "\n";
        }
        Console.WriteLine(mmmText);
        Console.WriteLine("");
    }
}

 

 

파일첨부)

snake2.cs
0.01MB

+ Recent posts