배열 연습
2023. 7. 25.

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

namespace Starcraft
{
    internal class Item
    {
       public string itemName;
        public Item(string itemName) {
            this.itemName = itemName;

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class App
    {
        //생성자
        public App()
        {
            //아이템 배열 변수 items정의
            Item[] items;
            //items변수에 크기가 5인 아이템 배열 인스턴스 생성 후 할당
            items = new Item[5];
            //인덱스 0,1,2에 해당하는 각요소에 Item 인스턴스 생성 후 할당
            items[0] = new Item("장검");
            items[1] = new Item("단검");
            items[2] = new Item("활");
            //Item 인스턴스 생성할 때 생성자 매개변수로 아이템의 이름을 인수로 전달
            //아이템 배열의 각 요소의 이름과 index를 출력
            //배열의 요소값이 null 이라면 [   ] 출력
            for(int i=0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    Console.WriteLine("{0}. {1}", i, items[i].itemName);
                }
                else
                    Console.WriteLine("{0}. [   ]",i);                
            }


        }

    }
}

 

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

namespace Starcraft
{
    internal class Inventory
    {
        //멤버 변수
        
        public string itemName { get; set; }
        private Item[] items;
        public int maxCapacity;//최대 수용량
        int index;
        //생성자
        public Inventory(int maxCapacity)
        {
            this.maxCapacity = maxCapacity;
            //아이템을 그룹화, 관리
            items = new Item[this.maxCapacity];
            //최대 수용량 만큼 담을수있는 배열을 items 변수에 저장
            Console.WriteLine("인벤토리가 생성되었습니다.(최대수용량: {0})\n",maxCapacity);
        }

        //메서드


        //아이템 넣기
        public void AddItem(Item itemName)
        {
            //this.itemName = itemName;
            if (this.index > maxCapacity-1)
            {
                Console.WriteLine("공간이 부족 합니다.");
                return;
            }


            //items[index] = new Item(itemName);
            //위와 같이 작성하면 add할떄마다 계속 생성된다.
            this.items[index] = itemName;
            index++;
            Console.WriteLine("{0}이 추가되었습니다.\n", itemName.itemName);

        }
        public Item SearchItem(string searchName)//아이템 검색
        {        
            for(int i = 0; i<maxCapacity -1; i++)
            {
                if (items[i] != null)
                {
                    if (this.items[i].itemName == searchName)
                    {
                        Console.WriteLine("{0}이 인벤토리에 있습니다.", this.items[i].itemName);
                        break;
                    }
                    
                }
                else
                {
                    Console.WriteLine("찾는 아이템이 없습니다.");
                }
            }
            return this.items[index];
        }

        public void PrintAllItem()//모든 아이템 출력
        {

            for (int i = 0; i < maxCapacity - 1; i++)
            {
                if (items[i] != null)
                { Console.WriteLine(items[i].itemName); }
                else
                { Console.WriteLine("[  ]"); }
            }
        }


    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class App
    {
        //생성자
        public App()
        {
            Inventory inventory = new Inventory(10); //최대수용량이 10인 인벤토리 생성
            inventory.PrintAllItem();
            inventory.AddItem(new Item("장검"));
            inventory.PrintAllItem();
           // inventory.SearchItem("단검");

            inventory.AddItem(new Item("단검"));
            inventory.PrintAllItem();
            inventory.SearchItem("단검");

        }

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

namespace Starcraft
{
    internal class Item
    {
       public string itemName;
        public Item(string itemName) {
            this.itemName = itemName;

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

namespace _2048
{
    internal class App
    {
       public App()
        {
            Game game = new Game();
            game.GameStart();
        }

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

namespace _2048
{
    internal class Game
    {
        //멤버 변수
        ConsoleKeyInfo info;
        int[] arr = new int[4];
        int num;
        //생성자
        public Game()
        {

        }

        public void GameStart()
        {
            Console.WriteLine("게임시작\n");

            PrintBoard();

            info = Console.ReadKey();
            Console.WriteLine("{0}", info.Key);
            MoveRight(info);

            info = Console.ReadKey();
            Console.WriteLine("{0}", info.Key);
            MoveRight(info);

            info = Console.ReadKey();
            Console.WriteLine("{0}", info.Key);
            MoveRight(info);

            info = Console.ReadKey();
            Console.WriteLine("{0}", info.Key);
            MoveRight(info);

        }
        void PrintBoard()
        {
            //보드 만들기 연습
           
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("{0}", arr[i]);
                //for (int j = 0; j < arr.Length; j++)
                //   Console.Write("{0}", arr[j]);
            }
            Console.WriteLine("\n");
        }

        void MoveLeft(ConsoleKeyInfo infoKey)
        {//왼쪽 방향키 입력시 왼쪽으로 이동
           
            if (info.Key == ConsoleKey.LeftArrow)
            {
              //  Console.WriteLine("<-");
            }

        }
        void MoveRight(ConsoleKeyInfo infoKey)
        {//오른쪽 방향키 입력시 오른쪽으로 이동

            if (info.Key == ConsoleKey.RightArrow)
            {
                if (arr[num] != 0)
                {
                    //  Console.WriteLine("<-");

                    arr[num + 1] = arr[num];
                    arr[num] = 0;
                    num++;
                    PrintBoard();
                }
                else {
                    arr[0] = 1;
                  //  arr[num + 1] = arr[num];
                 //   arr[num] = 0;
                  //  num++;
                    PrintBoard();

                }
             //   arr[0] = 1;
              //  PrintBoard();

              //  arr[1] = arr[0];
             //   arr[0] = 0;
             //   PrintBoard();

              //  arr[2] = arr[1];
             //   arr[1] = 0;
              //  PrintBoard();

            }

        }
    }
}

'C# 기초' 카테고리의 다른 글

namespace, 컬렉션,  (0) 2023.07.26
다차원배열  (0) 2023.07.25
배열 예제  (0) 2023.07.24
구조체, 추상 클래스, 인터페이스, 1차원 배열  (0) 2023.07.24
상속, virtual, override, overload, 연결(chaining), casting  (0) 2023.07.24
myoskin