推箱子

推箱子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Class1
    {
        public static void Main(string[] args)
        {
            //0 空地,1小人,2箱子,-1墙,3-目的地
            int[,] map = new int[5, 5]
            {
                {-1,-1,-1,-1,-1},
                {-1,0,0,0,-1},
                {-1,1,2,3,-1},
                {-1,0,0,0,-1},
                {-1,-1,-1,-1,-1}
            };
            int okx = 2, oky = 3;
            int x=2, y=1; //小人的当前位置
            for (; ; )
            {
                Console.Clear();//清屏
                //显示出来
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        if (map[i, j] == 1)
                        {
                            Console.Write("");
                        }
                        else if (map[i, j] == 2)
                        {
                            Console.Write("");
                        }
                        else if (map[i, j] == -1)
                        {
                            Console.Write("");
                        }
                        else if (map[i, j] == 3)
                        {
                            Console.Write("");
                        }
                        else
                        {
                            Console.Write("  ");
                        }
                    }
                    Console.WriteLine();
                }
                //等待按键
                ConsoleKeyInfo info = Console.ReadKey();
                if (info.Key == ConsoleKey.RightArrow)
                {
                    if (y + 1 > 4)
                    {
                        Console.Write("\a");
                        continue;
                    }
                    if (map[x, y + 1] != -1 )
                    {
                        if (map[x, y + 1] == 2)
                        {
                            if (map[x, y + 2] == -1)
                            {
                                Console.Write("\a");
                                continue;
                            }
                            if (map[x, y + 2] == 3)
                            {
                                Console.Clear();
                                Console.WriteLine("成功");
                                break;
                            }
                            //箱子动
                            int t = map[x, y + 1];
                            map[x, y + 1] = map[x, y + 2];
                            map[x, y + 2] = t;
                            
                        }
                        //人动
                        int temp = map[x, y];
                        map[x, y] = map[x, y + 1];
                        map[x, y + 1] = temp;

                        y++;
                        
                    }
                    else
                    {
                        Console.Write("\a");
                    }
                }
                else if (info.Key == ConsoleKey.UpArrow)
                {

                }
                else if (info.Key == ConsoleKey.LeftArrow)
                {
                    if (y - 1 < 0)
                    {
                        Console.Write("\a");
                        continue;
                    }
                    if (map[x, y - 1] != -1 )
                    {
                        int temp = map[x, y];
                        map[x, y] = map[x, y - 1];
                        map[x, y - 1] = temp;

                        y--;
                    }
                    else
                    {
                        Console.Write("\a");
                    }
                }
                else if (info.Key == ConsoleKey.DownArrow)
                {

                }
            }
        }
    }
}

 

posted on 2015-05-28 11:06  qq蓝光  阅读(216)  评论(0编辑  收藏  举报

导航