ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2048게임
    C#/수업과제 2022. 6. 15. 13:22
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _2048Game_practice
    {
        internal class App
        {
            public App()
            {
                Game game = new Game();
                game.Start();
                game.Print();
                game.StartMoveLeft();
                game.Print();
                game.AddNum();
                game.Print();
                Console.WriteLine();
    
                game.StartMoveLeft();
                game.Print();
                game.AddNum();
                game.Print();
                Console.WriteLine();
    
                game.StartMoveLeft();
                game.Print();
                game.AddNum();
                game.Print();
                Console.WriteLine();
    
                game.StartMoveLeft();
                game.Print();
                game.AddNum();
                game.Print();
                Console.WriteLine();
    
                game.StartMoveLeft();
                game.Print();
                game.AddNum();
                game.Print();
    
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _2048Game_practice
    {
        public class Game
        {
            public int[] board;
            private Random rand;
    
            //생성자
            public Game()
            {
                this.board = new int[4];
                this.rand = new Random();
            }
    
            public void Start()
            {    
                int index = this.rand.Next(0, 4);
                this.board[index] = 2;
            }
    
            public void AddNum()
            {
    
                int len = 0;
    
                for (int i = 0; i < this.board.Length; i++)
                {
                    int num = this.board[i];
                    if (num == 0)
                    {
                        //Console.WriteLine(i);
                        len++;
                    }
                }
    
                int[] temp = new int[len];
                int idex = 0;
                for (int i = 0; i < this.board.Length; i++)
                {
                    int num = this.board[i];
                    if (num == 0)
                    {
                        temp[idex++] = i;
                    }
                }
    
                int tempIdx = this.rand.Next(temp.Length);
    
                this.board[temp[tempIdx]] = 2;
    
                Console.WriteLine("addnum");
    
            }
    
            public void StartMoveLeft()
            {
                int index = -1;     //지역변수 초기화 
                int num = -1;
                //몇회 MoveLeftOnce를 호출 할것인가?
    
                for (int i = 0; i < this.board.Length; i++)
                {
                    num = this.board[i];
                    if (num != 0)
                    {
                        index = i;
                    }
                }
    
                for (int i = 0; i < index; i++)
                {
                    {
                        this.MoveLeftOnce();
                    }
    
                }
                Console.WriteLine("<----------------");
               
            }
    
    
            public void MoveLeftOnce()
            {
                for (int i = 0; i < this.board.Length; i++)
                {
                    int num = this.board[i];
                    if (num != 0)
                    {
                        if ((i != 0) && (this.board[i-1] == 0))
                        {
                            Console.WriteLine("{0}인덱스에 있는 {1}값을 {2}인덱스로 이동", i, num, i - 1);
                            this.board[i - 1] = num;
                            this.board[i] = 0;
                        }
                        else if ((i !=0) && (this.board[i] == this.board[i - 1]))
                        {
                            Console.WriteLine("{0}인덱스에 있는 {1}값을 {2}인덱스로 이동", i, num, i - 1);
                            this.board[i-1] *= 2;
                            this.board[i] = 0;
                        }
                    }
                }
            }
    
    
            public void Print()
            {
                int printIndex = 0;
                for(int i = 0; i < 4; i++)
                    {
                    Console.Write("{0}  ", this.board[printIndex]);
                    printIndex++;
                    }
                Console.WriteLine("");
                //0 0 0 0
            }
    
        }
    }

    'C# > 수업과제' 카테고리의 다른 글

    텍스트 Rpg만들기  (0) 2022.06.21
    데이터 저장 연습과제  (0) 2022.06.20
    2차원 배열의 늑대와 영웅  (0) 2022.06.15
    for문 연습 문제  (0) 2022.06.10
    디아블로 아이템 출력하기  (0) 2022.06.09
Designed by Tistory.