Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

행복한 개구리

C# 21.03.19.수업내용 본문

C#/수업내용

C# 21.03.19.수업내용

HappyFrog 2021. 3. 19. 13:29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study004
{
    class GameLauncher
    {
        public int[] arrPlate;

        //생성자
        public GameLauncher()
        {

        }


        public void Init()
        {
            Console.WriteLine("초기화 합니다.");
        }

        public void CreatePlate()
        {
            Console.WriteLine("새로운 판을 생성합니다.");

            this.arrPlate = new int[4];
            this.arrPlate[1] = 2;
        }




        //빈 자리 중 한 칸에 랜덤하게 2 또는 4가 생성됨
        public void CreateBlock()
        {
            Random rand = new Random();
            int num = rand.Next(0, 101);


            int i = 0;
            while (i < 1)
            {
                Random rand2 = new Random();
                int blockIndex = rand2.Next(0, this.arrPlate.Length);
                
                if (this.arrPlate[blockIndex] == 0)
                {
                    if (num > 50)
                    {
                        this.arrPlate[blockIndex] = 4;
                        Console.WriteLine("{0}인덱스에 4 할당됨", blockIndex);
                        break;
                    }
                    else
                    {
                        this.arrPlate[blockIndex] = 2;
                        Console.WriteLine("{0}인덱스에 2 할당됨", blockIndex);
                        break;
                    }
                }
            }

            Console.WriteLine("================결과================");
            for(int a = 0; a < this.arrPlate.Length; a++)
            {
                Console.WriteLine("{0}인덱스 {1}", a, this.arrPlate[a]);
            } 
        }


        //게임 시작
        public void StartGame()
        {
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                Console.WriteLine(keyInfo.Key);
                if (keyInfo.Key == ConsoleKey.LeftArrow)
                {
                    Console.WriteLine("<");
                }
                else if (keyInfo.Key == ConsoleKey.RightArrow)
                {
                    Console.WriteLine("->");
                }
                else if (keyInfo.Key == ConsoleKey.Escape)
                {
                    this.EndGame();
                    break;
                }
            }
        }

        public void EndGame()
        {

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

namespace Study004
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            GameLauncher game = new GameLauncher();
            game.CreatePlate();            
            game.CreateBlock();
        }
    }
}

3시간걸림ㅎ..

================================================

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

namespace Study004
{
    class GameLauncher
    {
        public int[] arrPlate;

        //생성자
        public GameLauncher()
        {

        }


        public void Init()
        {
            Console.WriteLine("초기화 합니다.");
        }

        public void CreatePlate()
        {
            Console.WriteLine("새로운 판을 생성합니다.");

            this.arrPlate = new int[4];
            this.arrPlate[1] = 2;
        }






        //빈 자리 중 한 칸에 랜덤하게 2 또는 4가 생성됨
        public void CreateBlock()
        {
            Random rand = new Random();
            int num = rand.Next(0, 101);


            int i = 0;
            while (i < 1)
            {
                Random rand2 = new Random();
                int blockIndex = rand2.Next(0, this.arrPlate.Length);

                if (this.arrPlate[blockIndex] == 0)
                {
                    if (num > 50)
                    {
                        this.arrPlate[blockIndex] = 4;

                        break;
                    }
                    else
                    {
                        this.arrPlate[blockIndex] = 2;

                        break;
                    }
                }
            }


            for (int a = 0; a < this.arrPlate.Length; a++)
            {
                Console.Write("{0, 5}", this.arrPlate[a]);
            }
            Console.WriteLine();
        }



        public void LeftAlignment() //왼쪽 정렬;
        {

            for (int i = 0; i < this.arrPlate.Length; i++)  //배열 전체 한번씩 돌리는 반복문
            {
                for (int j = 0; j < this.arrPlate.Length; j++)
                {
                    if (j < this.arrPlate.Length - 1)   //배열[3]이 아닌 범위에서
                    {
                        if (this.arrPlate[j] == 0 && this.arrPlate[j + 1] != 0) //한 수와 그 오른쪽 배열자리를 비교해 i자리가 비어있고 i+1이 값이 있다면 i+1인덱스 값을 i로 이동(왼쪽으로 이동)
                        {
                            this.arrPlate[j] = this.arrPlate[j + 1];
                            this.arrPlate[j + 1] = 0;
                        }
                        else if (this.arrPlate[j] != 0 && this.arrPlate[j + 1] != 0)
                        {
                            if (this.arrPlate[j] == this.arrPlate[j + 1])
                            {
                                this.arrPlate[j] += this.arrPlate[j + 1];
                                this.arrPlate[j + 1] = 0;
                            }
                        }
                    }
                    else
                    {
                        this.arrPlate[i] = this.arrPlate[i];
                    }
                }
            }
            for (int i = 0; i < this.arrPlate.Length; i++)
            {
                Console.Write("{0, 5}", this.arrPlate[i]);
            }
            Console.WriteLine();
            Console.WriteLine("==============생성==============");
        }

        public void isFull()
        {
            for (int i = 0; i < this.arrPlate.Length; i++)
            {
                if (this.arrPlate[i] == this.arrPlate[i + 1])
                {
                    break;
                }
                else
                {
                    continue;
                }
            }
        }

        public void RightAlignment()    //오른쪽 정렬
        {

            for (int i = this.arrPlate.Length - 1; i >= 0; i--)
            {
                for (int j = this.arrPlate.Length - 1; j >= 0; j--)
                {
                    if (j > 0)
                    {
                        if (this.arrPlate[j] == 0 && this.arrPlate[j - 1] != 0)
                        {
                            this.arrPlate[j] = this.arrPlate[j - 1];
                            this.arrPlate[j - 1] = 0;
                        }
                        else if (this.arrPlate[j] != 0 && this.arrPlate[j - 1] != 0)
                        {
                            if (this.arrPlate[j] == this.arrPlate[j - 1])
                            {
                                this.arrPlate[j] += this.arrPlate[j - 1];
                                this.arrPlate[j - 1] = 0;
                            }
                        }
                    }
                    else
                    {
                        this.arrPlate[i] = this.arrPlate[i];
                    }
                }
            }
            for (int i = 0; i < this.arrPlate.Length; i++)
            {
                Console.Write("{0, 5}", this.arrPlate[i]);
            }
            Console.WriteLine();
            Console.WriteLine("==============생성==============");
        }




        //게임 시작
        public void StartGame()
        {
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                Console.WriteLine(keyInfo.Key);
                if (keyInfo.Key == ConsoleKey.LeftArrow)
                {
                    Console.WriteLine("==============결과==============");
                    Console.WriteLine("<-");
                    this.LeftAlignment();
                    this.CreateBlock();
                    
                }
                else if (keyInfo.Key == ConsoleKey.RightArrow)
                {
                    Console.WriteLine("==============결과==============");
                    Console.WriteLine("->");
                    this.RightAlignment();
                    this.CreateBlock();
                    
                }
                else if (keyInfo.Key == ConsoleKey.Escape)
                {
                    this.EndGame();
                    break;
                }
            }
        }

        public void EndGame()
        {
            Console.WriteLine("==============GAME_OVER==============");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study004
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");
                                
            
            GameLauncher game = new GameLauncher();
            game.CreatePlate();            
            game.CreateBlock();

            game.StartGame();
            
        }
    }
}

 

================================================

 

'C# > 수업내용' 카테고리의 다른 글

C# 21.03.23.수업내용  (0) 2021.03.23
C# 21.03.22.수업내용  (0) 2021.03.22
C# 21.03.18.수업내용  (0) 2021.03.18
C# 21.03.17.수업내용  (0) 2021.03.17
C#21.03.16.수업내용  (0) 2021.03.16