C#/수업내용

0614 / 2048 만들기

버스창문 2022. 6. 14. 17:22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study04_2048
{
    public class Game2048
    {
        int[] Board;
        int position;
        Random random = new Random();
        bool isGameOver = false;
        public bool IsGameOver
        {
            get { return isGameOver; }
        }
        public Game2048()
        {
            this.Board = new int[4];
            this.position = (this.random.Next()) % 4;
            this.Board[this.position] = 4;
            this.NewNumber();
            this.Print();
        }
        public void Left()
        {
            
            for(int i = 0; i < this.Board.Length; i++)
            {
                for(int j = 0; j < this.Board.Length-1; j++)
                {
                    if (this.Board[j] == 0)
                    {
                        int temp = this.Board[j];
                        this.Board[j] = this.Board[j + 1];
                        this.Board[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < this.Board.Length; i++)
            {
                for (int j = 0; j < this.Board.Length - 1; j++)
                {
                    if (this.Board[j] == this.Board[j+1])
                    {
                        this.Board[j] = this.Board[j] + this.Board[j + 1];
                        this.Board[j + 1] = 0;
                    }
                }
            }
            this.NewNumber();
            this.Print();
        }
        public void Right()
        {
            
            for (int i = 0; i < this.Board.Length; i++)
            {
                for (int j = 0; j < this.Board.Length - 1; j++)
                {
                    if (this.Board[3-j] == 0)
                    {
                        int temp = this.Board[2-j];
                        this.Board[2-j] = this.Board[3-j];
                        this.Board[3-j] = temp;
                    }
                }
            }
            for (int i = 0; i < this.Board.Length; i++)
            {
                for (int j = 0; j < this.Board.Length - 1; j++)
                {
                    if (this.Board[2-j] == this.Board[3-j])
                    {
                        this.Board[3 - j] = this.Board[3 - j] + this.Board[2 - j];
                        this.Board[2 - j] = 0;
                    }
                }
            }
            this.NewNumber();
            this.Print();
        }
        public void NewNumber()
        {
            int check = 0;
            Random random = new Random();
            
            while (true)
            {
                this.position = (random.Next()) % 4;
                for (int i = 0; i < this.Board.Length; i++)
                {
                    if (this.Board[i] == 0)
                    {
                        check++;
                    }
                }
                if (check == 0)
                {
                    GameOver();
                    return;
                }
                if (this.Board[position] == 0)
                {
                    break;
                }
            }
            this.Board[this.position] = 2;
        }
        public void Print()
        {
            Console.WriteLine("[{0}][{1}][{2}][{3}]", this.Board[0], this.Board[1], this.Board[2], this.Board[3]);
        }
        public void GameOver()
        {
            Console.WriteLine("게임오버");
            isGameOver = true;
        }
    }
}

한줄짜리 2048

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

namespace study04_2048
{
    public class Game2048Full
    {

        int[,] Board;
        int xPosition;
        int yPosition;
        Random random = new Random();
        bool isGameOver = false;
        public bool IsGameOver
        {
            get { return isGameOver; }
        }
        public Game2048Full()
        {
            this.Board = new int[4, 4];
            this.xPosition = (this.random.Next()) % 4;
            this.yPosition = (this.random.Next()) % 4;
            this.Board[this.xPosition, this.yPosition] = 4;
            this.NewNumber();
            this.Print();
        }
        public void Left()
        {
            int check = 0;
            int check1 = 0;
            int check2 = 0;
            for(int x = 1; x < this.Board.GetLength(0); x++)
            {
                for(int y = 0; y < this.Board.GetLength(1); y++)
                {
                    if (Board[x, y] != 0)
                    {
                        if ((Board[x, y] == Board[x - 1, y]) || (Board[x - 1, y] == 0))
                        {
                            check++;
                        }
                    }
                }
            }
            if(check == 0)
            {
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1); j++)
                    {
                        if (this.Board[i, j] == 0)
                        {
                            check1++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[j, i] == Board[j + 1, i])
                        {
                            check2++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[i, j] == Board[i, j + 1])
                        {
                            check2++;
                        }
                    }
                }
                if (check1 == 0)
                {
                    if (check2 == 0)
                    {
                        GameOver();
                        return;
                    }
                    return;
                }
                return;
            }
            for (int y = 0; y < this.Board.GetLength(1);y++) {
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(0) - 1; j++)
                    {
                        if (this.Board[j,y] == 0)
                        {
                            int temp = this.Board[j,y];
                            this.Board[j,y] = this.Board[j + 1,y];
                            this.Board[j + 1,y] = temp;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(0) - 1; j++)
                    {
                        if (this.Board[j,y] == this.Board[j + 1,y])
                        {
                            this.Board[j,y] = this.Board[j,y] + this.Board[j + 1,y];
                            this.Board[j + 1,y] = 0;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(0) - 1; j++)
                    {
                        if (this.Board[j, y] == 0)
                        {
                            int temp = this.Board[j, y];
                            this.Board[j, y] = this.Board[j + 1, y];
                            this.Board[j + 1, y] = temp;
                        }
                    }
                }
            }
            Console.Clear();
            this.NewNumber();          
            this.Print();
        }
        public void Right()
        {
            int check = 0;
            int check1 = 0;
            int check2 = 0;
            for (int x = 0; x < this.Board.GetLength(0)-1; x++)
            {
                for (int y = 0; y < this.Board.GetLength(1); y++)
                {
                    if (Board[x, y] != 0)
                    {
                        if ((Board[x, y] == Board[x + 1, y]) || (Board[x +1, y] == 0))
                        {
                            check++;
                        }
                    }
                }
            }
            if (check == 0)
            {
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1); j++)
                    {
                        if (this.Board[i, j] == 0)
                        {
                            check1++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[j, i] == Board[j + 1, i])
                        {
                            check2++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[i, j] == Board[i, j + 1])
                        {
                            check2++;
                        }
                    }
                }
                if (check1 == 0)
                {
                    if (check2 == 0)
                    {
                        GameOver();
                        return;
                    }
                    return;
                }
                return;
            }
            for (int y = 0; y < this.Board.GetLength(1); y++) {
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(0) - 1; j++)
                    {
                        if (this.Board[3 - j,y] == 0)
                        {
                            int temp = this.Board[2 - j,y];
                            this.Board[2 - j,y] = this.Board[3 - j,y];
                            this.Board[3 - j,y] = temp;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(0) - 1; j++)
                    {
                        if (this.Board[2 - j,y] == this.Board[3 - j,y])
                        {
                            this.Board[3 - j,y] = this.Board[3 - j,y] + this.Board[2 - j,y];
                            this.Board[2 - j,y] = 0;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(0) - 1; j++)
                    {
                        if (this.Board[3 - j, y] == 0)
                        {
                            int temp = this.Board[2 - j, y];
                            this.Board[2 - j, y] = this.Board[3 - j, y];
                            this.Board[3 - j, y] = temp;
                        }
                    }
                }
            }
            Console.Clear();
            this.NewNumber();
            this.Print();
        }
        public void Up()
        {
            int check = 0;
            int check1 = 0;
            int check2 = 0;
            for (int x = 0; x < this.Board.GetLength(0); x++)
            {
                for (int y = 1; y < this.Board.GetLength(1); y++)
                {
                    if (Board[x, y] != 0)
                    {
                        if ((Board[x, y] == Board[x, y-1]) || (Board[x, y-1] == 0))
                        {
                            check++;
                        }
                    }
                }
            }
            if (check == 0)
            {
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1); j++)
                    {
                        if (this.Board[i, j] == 0)
                        {
                            check1++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[j, i] == Board[j + 1, i])
                        {
                            check2++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[i, j] == Board[i, j + 1])
                        {
                            check2++;
                        }
                    }
                }
                if (check1 == 0)
                {
                    if (check2 == 0)
                    {
                        GameOver();
                        return;
                    }
                    return;
                }
                return;
            }
            for (int x = 0; x < this.Board.GetLength(0); x++)
            {
                for (int i = 0; i < this.Board.GetLength(1); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (this.Board[x,j] == 0)
                        {
                            int temp = this.Board[x,j];
                            this.Board[x,j] = this.Board[x,j + 1];
                            this.Board[x,j + 1] = temp;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(1); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (this.Board[x,j] == this.Board[x,j + 1])
                        {
                            this.Board[x,j] = this.Board[x,j] + this.Board[x,j + 1];
                            this.Board[x,j + 1] = 0;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(1); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (this.Board[x, j] == 0)
                        {
                            int temp = this.Board[x, j];
                            this.Board[x, j] = this.Board[x, j + 1];
                            this.Board[x, j + 1] = temp;
                        }
                    }
                }
            }
            Console.Clear();
            this.NewNumber();
            this.Print();
        }
        public void Down()
        {
            int check = 0;
            int check1 = 0;
            int check2 = 0;
            for (int x = 0; x < this.Board.GetLength(0); x++)
            {
                for (int y = 0; y < this.Board.GetLength(1)-1; y++)
                {
                    if (Board[x, y] != 0)
                    {
                        if ((Board[x, y] == Board[x , y+1]) || (Board[x , y+1] == 0))
                        {
                            check++;
                        }
                    }
                }
            }
            if (check == 0)
            {
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1); j++)
                    {
                        if (this.Board[i, j] == 0)
                        {
                            check1++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[j, i] == Board[j + 1, i])
                        {
                            check2++;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(0); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (Board[i, j] == Board[i, j + 1])
                        {
                            check2++;
                        }
                    }
                }
                if (check1 == 0)
                {
                    if (check2 == 0)
                    {
                        GameOver();
                        return;
                    }
                    return;
                }
                return;
            }
            for (int x = 0; x < this.Board.GetLength(0); x++)
            {
                for (int i = 0; i < this.Board.GetLength(1); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (this.Board[x,3 - j] == 0)
                        {
                            int temp = this.Board[x,2 - j];
                            this.Board[x,2 - j] = this.Board[x,3 - j];
                            this.Board[x,3 - j] = temp;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(1); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (this.Board[x,2 - j] == this.Board[x,3 - j])
                        {
                            this.Board[x,3 - j] = this.Board[x,3 - j] + this.Board[x,2 - j];
                            this.Board[x,2 - j] = 0;
                        }
                    }
                }
                for (int i = 0; i < this.Board.GetLength(1); i++)
                {
                    for (int j = 0; j < this.Board.GetLength(1) - 1; j++)
                    {
                        if (this.Board[x, 3 - j] == 0)
                        {
                            int temp = this.Board[x, 2 - j];
                            this.Board[x, 2 - j] = this.Board[x, 3 - j];
                            this.Board[x, 3 - j] = temp;
                        }
                    }
                }
            }
            Console.Clear();
            this.NewNumber();
            this.Print();
        }
        public void NewNumber()
        {
            Random random = new Random();

            while (true)
            {
                this.xPosition = (random.Next()) % 4;
                this.yPosition = (random.Next()) % 4;
                
                if (this.Board[this.xPosition, this.yPosition] == 0)
                {
                    break;
                }
            }
            this.Board[this.xPosition, this.yPosition] = 2;
        }
        public void Print()
        {
            Console.WriteLine("[{0,4}][{1,4}][{2,4}][{3,4}]\n" +
                              "[{4,4}][{5,4}][{6,4}][{7,4}]\n" +
                              "[{8,4}][{9,4}][{10,4}][{11,4}]\n" +
                              "[{12,4}][{13,4}][{14,4}][{15,4}]"
                              , this.Board[0, 0], this.Board[1, 0], this.Board[2, 0], this.Board[3, 0]
                              , this.Board[0, 1], this.Board[1, 1], this.Board[2, 1], this.Board[3, 1]
                              , this.Board[0, 2], this.Board[1, 2], this.Board[2, 2], this.Board[3, 2]
                              , this.Board[0, 3], this.Board[1, 3], this.Board[2, 3], this.Board[3, 3]);
        }
        public void GameOver()
        {
            Console.WriteLine("게임오버");
            isGameOver = true;
        }
    }
}

 

2048 풀버전

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

namespace study04_2048
{
    public class App
    {
        Game2048Full game = new Game2048Full();
        ConsoleKeyInfo key;
        public App()
        {
            while (!game.IsGameOver)
            {
                key = Console.ReadKey();
                if (key.Key == ConsoleKey.LeftArrow)
                {
                    game.Left();
                }
                else if (key.Key == ConsoleKey.RightArrow)
                {
                    game.Right();
                }
                else if (key.Key == ConsoleKey.UpArrow)
                {
                    game.Up();
                }
                else if (key.Key == ConsoleKey.DownArrow)
                {
                    game.Down();
                }
                else
                {
                    game.Print();
                }
            }
        }
    }
}

실행코드

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

0621  (0) 2022.06.21
0620 몬스터 잡고 아이템 얻고 인벤토리에 저장  (0) 2022.06.20
0615 늑대 마구마구 때리기  (0) 2022.06.15
0614 인벤토리  (0) 2022.06.14
늑대때리기  (0) 2022.06.10