• C# 推箱子(只有一关)


    class Program
    {
    static void ditu(int[,] map) //建立一张地图的函数,10x10的
    {
    for (int i = 0; i < 10; i++)
    {
    for (int j = 0; j < 10; j++)
    {
    if (map[i, j] == 0)
    {
    Console.Write(" ");
    }
    else if (map[i, j] == 1)
    {
    Console.Write("■");
    }
    else if (map[i, j] == 2)
    {
    Console.Write("●");
    }
    else if (map[i, j] == 3)
    {
    Console.Write("★");
    }
    else if (map[i, j] == 4)
    {
    Console.Write("♀");
    }
    }
    Console.WriteLine();
    }

    }
    static void Main(string[] args)
    {
    int[,] Map = new int[10, 10]
    {
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,0,0,0,1,0,0,1},
    {1,4,2,0,0,0,1,0,0,1},
    {1,0,0,0,0,0,1,1,1,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,0,0,1},
    {1,0,0,1,0,0,1,0,0,1},
    {1,3,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1},
    };
    int x = 2, y = 1, t = 0, t1 = 0, tx = 0;
    while (true)
    {
    ditu(Map);
    if (tx == 100) //最后一步提到前面。
    {
    Console.WriteLine("恭喜通第一关。");

    break;
    }
    ConsoleKeyInfo s = Console.ReadKey(); //赋值一个按键的变量
    #region====向上
    if (s.Key.ToString() == "UpArrow")
    {
    if (Map[x - 1, y] == 2 && Map[x - 2, y] == 0)//两次冒泡
    {
    t = Map[x - 2, y];
    Map[x - 2, y] = Map[x - 1, y];
    Map[x - 1, y] = t;

    t1 = Map[x - 1, y];
    Map[x - 1, y] = Map[x, y];
    Map[x, y] = t1;
    x--;
    }
    else if (Map[x - 1, y] == 0)
    {
    t = Map[x - 1, y];
    Map[x - 1, y] = Map[x, y];
    Map[x, y] = t;
    x--;
    }
    else if (Map[x - 1, y] == 1 || (Map[x - 1, y] == 2 && Map[x - 2, y] == 1))
    {
    Console.WriteLine("您未学会穿墙术,请去商城购买!");
    Console.ReadLine(); //撞墙回车
    }
    }
    #endregion
    #region====向下
    if (s.Key.ToString() == "DownArrow")
    {
    if (Map[x + 1, y] == 2 && Map[x + 2, y] == 0)
    {
    t = Map[x + 2, y];
    Map[x + 2, y] = Map[x + 1, y];
    Map[x + 1, y] = t;

    t1 = Map[x + 1, y];
    Map[x + 1, y] = Map[x, y];
    Map[x, y] = t1;
    x++;
    }
    else if (Map[x + 1, y] == 0)
    {
    t = Map[x + 1, y];
    Map[x + 1, y] = Map[x, y];
    Map[x, y] = t;
    x++;
    }
    else if (Map[x + 1, y] == 1 || (Map[x + 1, y] == 2 && Map[x + 2, y] == 1))
    {
    Console.WriteLine("您未学会穿墙术,请去商城购买!");
    Console.ReadLine(); //撞墙回车
    }
    }
    #endregion
    #region====向右
    if (s.Key.ToString() == "RightArrow")
    {
    if (Map[x, y + 1] == 2 && Map[x, y + 2] == 0)
    {
    t = Map[x, y + 2];
    Map[x, y + 2] = Map[x, y + 1];
    Map[x, y + 1] = t;

    t1 = Map[x, y + 1];
    Map[x, y + 1] = Map[x, y];
    Map[x, y] = t1;
    y++;
    }
    else if (Map[x, y + 1] == 0)
    {
    t = Map[x, y + 1];
    Map[x, y + 1] = Map[x, y];
    Map[x, y] = t;
    y++;
    }
    else if (Map[x, y + 1] == 1 || (Map[x, y + 1] == 2 && Map[x, y + 2] == 1))
    {
    Console.WriteLine("您未学会穿墙术,请去商城购买!");
    Console.ReadLine(); //撞墙回车
    }
    }
    #endregion
    #region====向左
    if (s.Key.ToString() == "LeftArrow")
    {
    if (Map[x, y - 1] == 2 && Map[x, y - 2] == 0)
    {
    t = Map[x, y - 2];
    Map[x, y - 2] = Map[x, y - 1];
    Map[x, y - 1] = t;

    t1 = Map[x, y - 1];
    Map[x, y - 1] = Map[x, y];
    Map[x, y] = t1;
    y--;
    }
    else if (Map[x, y - 1] == 0)
    {
    t = Map[x, y - 1];
    Map[x, y - 1] = Map[x, y];
    Map[x, y] = t;
    y--;
    }
    else if (Map[x, y - 1] == 1 || (Map[x, y - 1] == 2 && Map[x, y - 2] == 1))
    {
    Console.WriteLine("您未学会穿墙术,请去商城购买!");
    Console.ReadLine(); //撞墙回车
    }
    }
    #endregion
    #region====结束
    if (Map[x, y - 1] == 2 && Map[x, y - 2] == 3)
    {
    Map[x, y - 2] = Map[x, y - 1];

    Map[x, y - 1] = Map[x, y];
    Map[x, y] = t;
    tx = 100; //随意的赋值一个变量,作为后面IF的运行条件。
    }

    #endregion
    Console.Clear(); //每运行一遍,刷新一次地图。

    }

    Console.ReadLine();
    }
    }

  • 相关阅读:
    while循环和do while循环的基本使用和区别
    less框架简介
    css关联选择器大致类型总结
    渐进增强和优雅降级
    行,行内元素与块级元素有什么不同?
    for循环的大概遍历运用
    JDBC连接mysql数据库并进行简单操作
    Java实现杨辉三角
    replaceAll() 方法
    java抽象类和接口的区别
  • 原文地址:https://www.cnblogs.com/likaixuan/p/4336967.html
Copyright © 2020-2023  润新知