骑士飞行棋笔记

 主函数:

 

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

namespace 骑士飞行棋
{
    class Program
    {
        public static int[] map = new int[100];//初始化地图,大小为100
        public static int[] person = new int[2] { 0, 0 };//设置角色,角色A和B其地图位置为他的值
        public static string[] playerName = new string[2];//初始化一个大小为2数组,用于存放用户A和B的名字
        public static bool[] result = new bool[2] { false, false };

        static void Main(string[] args)
        {

            ShowUI();
            ShowMap();
            Console.WriteLine("请输入玩家A的姓名");
            playerName[0] = Console.ReadLine();//把玩家A的姓名放在数组playerName下标为0中
            while (playerName[0] == "")//判断玩家A的姓名是否为空
            {
                Console.WriteLine("玩家A姓名不得为空,请重新输入");//如果玩家姓名为空,提示用户重新输入
                playerName[0] = Console.ReadLine();//把用户输入的玩家A姓名重新放在playerName[0] 中
            }
            Console.WriteLine("请输入玩家B的姓名");
            playerName[1] = Console.ReadLine();
            while (playerName[1] == "" || playerName[1] == playerName[0])
            {
                if (playerName[1] == "")
                {
                    Console.WriteLine("玩家B姓名不得为空,请重新输入");
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能与玩家A[{0}]的姓名相同,请重新输入", playerName[0]);
                }

                playerName[1] = Console.ReadLine();
            }
            Console.Clear();.//当玩家输入完可用的用户名后,清屏
            ShowUI();//显示游戏头
            Console.WriteLine("对战开始......");
            Console.WriteLine("{0}的士兵用A表示", playerName[0]);//显示玩家的名字
            Console.WriteLine("{0}的士兵用B表示", playerName[1]);
            DrawMap();
            while (person[0] <= 99 && person[1] <= 99)//判断用户是否在地图上
            {
                
                #endregion
                if (result[0] == false)//当result[0]为false玩家A掷骰子
                {
                    ZhiShaiZi(0);
                }
                else    //当result[0]为false玩家A掷骰子暂停掷骰子,暂停操作
                {
                    result[0] = false;
                }
                if (person[0]==99)//判断玩家A的坐标为99
                {
                    Console.Clear();//如果为99清屏
                    Win();//调用win方法,显示胜利
                    break;//跳出掷骰子
                }
                if (result[1] == false)//玩家B掷骰子
                {
                    ZhiShaiZi(1);
                }
                else
                {
                    result[1] = false;
                }
                if (person[1] == 99)
                {
                    Console.Clear();
                    Win();
                    break;
                }


            }
        }

 

初始化地图头ShowUI()方法:

        /// <summary>
        /// 出事画地图的头
        /// </summary>
        public static void ShowUI()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("*****************************************");
            Console.WriteLine("*                                       *");
            Console.WriteLine("*               骑士飞行棋              *");
            Console.WriteLine("*                                       *");
            Console.WriteLine("*****************************************");
            Console.ForegroundColor = ConsoleColor.White;
        }

改变地图数组的值,每一种值代表一种符号的ShowMap()方法:
            1...幸运轮盘,显示组用户就 ◎
            2...地雷,显示给用户就是 ☆
            3...暂停,显示给用户就是 ▲
            4...时空隧道,显示组用户就 卐

        /// <summary>
        /// 改变数组map的值,每一种值代表一种符号
        /// </summary>
        public static void ShowMap()
        {
            //....1...幸运轮盘,显示组用户就 ◎
            //....2...地雷,显示给用户就是 ☆
            //....3...暂停,显示给用户就是 ▲
            //....4...时空隧道,显示组用户就 卐
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运转盘
            for (int i = 0; i < luckyturn.Length; i++)
            {
                map[luckyturn[i]] = 1;
            }
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
            for (int i = 0; i < landMine.Length; i++)
            {
                map[landMine[i]] = 2;
            }
            int[] pause = { 9, 27, 60, 93 };//暂停
            for (int i = 0; i < pause.Length; i++)
            {
                map[pause[i]] = 3;
            }
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                map[timeTunnel[i]] = 4;
            }
        }

根据数组map的值得不同的画地图上的符号方法:

        /// <summary>
        /// 画地图上的符号
        /// </summary>
        /// <param name="pos">传过来的坐标</param>
        /// <returns></returns>
        public static string DrawHang(int pos)
        {
            string temp = "";//声明一个字符串temp,以便于存地图符号,返回给调用的方法
            if (person[0] == person[1] && person[0] == pos)//当A和B值相等并且在地图上
            {
                temp = "<>";//地图符号画<>
            }
            else if (person[0] == pos)//A的值如果等于传过来的坐标的值,就在此坐标画A
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                temp = "";
            }
            else if (person[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                temp = "";
            }
            else
            {
                switch (map[pos])
                {
                    case 0: Console.ForegroundColor = ConsoleColor.Gray;//当传过来的坐标等于0时,画□,颜色灰色
                        temp = ""; break;
                    case 1: Console.ForegroundColor = ConsoleColor.Red;//当传过来的坐标等于0时,画◎,颜色红色
                        temp = ""; break;
                    case 2: Console.ForegroundColor = ConsoleColor.Blue;
                        temp = ""; break;
                    case 3: Console.ForegroundColor = ConsoleColor.Magenta;
                        temp = ""; break;
                    case 4: Console.ForegroundColor = ConsoleColor.Green;
                        temp = ""; break;

                }
            }
            return temp;//返回符号
        }

画地图的方法:(要调用话符号方法)

第一行有30个符号:坐标0-29

第一竖行有5个符号:坐标:30-34

第二行有30个符号:坐标35-64//应从64开始遍历到35:

第二竖行有5个符号:坐标:65-69

第三行有30个符号:坐标70-99

/// <summary>
        /// 画地图
        /// </summary>
        /// <returns></returns>
        public static string DrawHang(int pos)
        public static void DrawMap()
        {
            //画第一行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            Console.WriteLine();
            //第一画竖行
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            //画第二行
            for (int i = 64; i > 34; i--)
            {
                Console.Write(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            Console.WriteLine();
            //画第二竖
            for (int i = 65; i < 70; i++)
            {
                Console.WriteLine(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            //画第三行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawHang(i));//调用画符号方法,把坐标i传给方法
            }
            Console.WriteLine();
        }

玩家踩到幸运轮盘时,选择请选择1----交换位置,2----轰炸对方的方法:

方法只返回数字1或则数字2

        /// <summary>
        /// 玩家踩到幸运转盘时判断选的是否为提示数字的方法
        /// </summary>
        /// <param name="pos">传过来的提示字符串</param>
        /// <returns></returns>
           public static int LucyPan(string msg)
        {
            while (true)
            {
                try
                {
                    Console.WriteLine(msg);
                    int num = int.Parse(Console.ReadLine());//转换用户输入的字符串
                    if (num == 1 || num == 2)//若用户输入的为1或则2,返回num
                    {

                        return num;
                    }
                    else
                    {
                        Console.WriteLine("您输入的数字不合法,请重新输入;");
                        Console.WriteLine(msg);
                        continue;
                    }
                }

                catch
                {
                    Console.WriteLine("输入有误,请重新输入...");
                }
            }

        }

掷骰子ZhiSaiZi(int plyerpos)方法:

注意:当玩家的下标为plyerpos时那么另一个玩家的下标为[1-plyerpos]

        /// <summary>
        /// 掷骰子方法
        /// </summary>
        /// <param name="plyerpos">传过来的玩家姓名的数组下标标</param>
        /// <returns></returns>
        public static void ZhiShaiZi(int plyerpos)
        {
            #region
            string msg = "";
            Random r=new Random();
            int posNumber=r.Next(1,7);
            Console.WriteLine("玩家{0}按任意开始掷骰子", playerName[plyerpos]);
            Console.ReadKey(true);
            Console.WriteLine("玩家{0}掷出了{1}", playerName[plyerpos], posNumber);
            Console.WriteLine("按任意键开始行动....");
            Console.ReadKey(true);
            person[plyerpos] += posNumber;
            CheckPos();
            if (person[plyerpos] == person[1 - plyerpos])
            {
                person[1 - plyerpos] = 0;
            }
            else
            {
                switch (map[person[plyerpos]])
                {
                    //正常行动,方块
                    case 0:
                        msg = string.Format( "{0}行动完毕", playerName[plyerpos]); break;
                    //幸运转盘,
                    case 1:
                        msg = string.Format("{0}走到了幸运轮盘,请选择1----交换位置,2----轰炸对方", 

playerName[plyerpos]);
                        int num = LucyPan(msg);//调用LucyPan的方法,把msg传给LucyPan,返回1或则2,
                        if (num == 1)//当返回1的时候,玩家调换位置
                        {
                            int temp = person[plyerpos];
                            person[plyerpos] = person[1 - plyerpos];
                            person[1 - plyerpos] = temp;
                            msg = string.Format("玩家{0}选择了与玩家{1}交换位置", playerName[plyerpos], 

playerName[1 - plyerpos]);
                        }
                        else //当num为2的时候,玩家的对家退6格
                        {
                            person[1 - plyerpos] -= 6;
                            CheckPos();//检查玩家是否在地图上
                            msg = string.Format("玩家{0}选择了轰炸对方,玩家{1}退6格", playerName[plyerpos], 

playerName[1 - plyerpos]);
                        }
                        break;
                    //地雷,返回6格
                    case 2:
                        person[plyerpos] -= 6;
                        CheckPos();//检查玩家是否在地图上
                        msg = string.Format("恭喜玩家{0}踩到了万年不遇的地雷,退6格", playerName[plyerpos]); 

break;
                    //暂停一次
                    case 3:
                        result[plyerpos] = true;//在主函数里面为true时,不调用ZhiSaiZi方法
                        msg = string.Format("玩家{0}暂停一次", playerName[plyerpos]); break;
                    //时空隧道,前进10格
                    case 4:
                        person[plyerpos] += 10;
                        CheckPos();//检查玩家是否在地图上
                        msg = string.Format("玩家{0}极其猥琐,竟然穿越了10格", playerName[plyerpos]); break;
                }
            }
            Console.Clear();
            DrawMap();
            Console.WriteLine(msg);

            #endregion

        }

检查用户是否在地图上CheckPos()方法:

        public static void CheckPos()
        {
            if (person[0]>99)//当玩家A的坐标大于99时
            {
                person[0] = 99;//把玩家A的坐标设为99
            }
            if (person[1] > 99)//当玩家B的坐标大于99时
            {
                person[1] = 99;//把玩家B的坐标设为99
            }
            if (person[0] <0)//当玩家A的坐标小于0时
            {
                person[0] = 0;//把玩家A的坐标设为0
            }
            if (person[1] <0)//当玩家B的坐标小于0时
            {
                person[1] = 0;//把玩家B的坐标设为0
            }
        }

胜利win()方法:

public static void Win()
        {
             Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("");
            Console.WriteLine("                    ■                  ◆               ■        ■");
            Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
            Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
            Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
            Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
            Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
            Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
            Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
            Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
            Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
            Console.ResetColor();
        
        }

 注意:随即产生1-6的数字方法

            Random r=new Random();
            int posNumber=r.Next(1,7);

 



转载于:https://www.cnblogs.com/guohuiru/archive/2012/11/14/2769272.html

### 光流法C++源代码解析与应用 #### 光流法原理 光流法是一种在计算机视觉领域中用于追踪视频序列中运动物体的方法。它基于亮度不变性假设,即场景中的点在时间上保持相同的灰度值,从而通过分析连续帧之间的像素变化来估计运动方向和速度。在数学上,光流场可以表示为像素位置和时间的一阶导数,即Ex、Ey(空间梯度)和Et(时间梯度),它们共同构成光流方程的基础。 #### C++实现细节 在给定的C++源代码片段中,`calculate`函数负责计算光流场。该函数接收一个图像缓冲区`buf`作为输入,并初始化了几个关键变量:`Ex`、`Ey`和`Et`分别代表沿x轴、y轴和时间轴的像素强度变化;`gray1`和`gray2`用于存储当前帧和前一帧的平均灰度值;`u`则表示计算的光流矢量大小。 #### 图像处理流程 1. **初始化和预处理**:`memset`函数被用来清零`opticalflow`数组,它将保存计算的光流数据。同时,`output`数组被填充为白色,这通常用于可视化结果。 2. **灰度计算**:对每一像素点进行处理,计算其灰度值。这里采用的是RGB通道平均值的计算方法,将每个像素的R、G、B值相加后除以3,得到一个近似灰度值。此步骤确保了计算过程的鲁棒性和效率。 3. **光流向量计算**:通过比较当前帧和前一帧的灰度值,计算每个像素点的Ex、Ey和Et值。这里值得注意的是,光流向量的大小`u`是通过`Et`除以`sqrt(Ex^2 + Ey^2)`得到的,再乘以10进行量化处理,以减少计算复杂度。 4. **结果存储与阈值处理**:计算的光流值被存储在`opticalflow`数组中。如果`u`的绝对值超过10,则认为该点存在显著运动,因此在`output`数组中将对应位置标记为黑色,形成运动区域的可视化效果。 5. **状态更新**:通过`memcpy`函数将当前帧复制到`prevframe`中,为下一次迭代做准备。 #### 扩展应用:Lukas-Kanade算法 除了上述基础的光流计算外,代码还提到了Lukas-Kanade算法的应用。这是一种更高级的光流计算方法,能够提供更精确的运动估计。在`ImgOpticalFlow`函数中,通过调用`cvCalcOpticalFlowLK`函数实现了这一算法,该函数接受前一帧和当前帧的灰度图,以及窗口大小等参数,返回像素级别的光流场信息。 在实际应用中,光流法常用于目标跟踪、运动检测、视频压缩等领域。通过深入理解和优化光流算法,可以进一步提升视频分析的准确性和实时性能。 光流法及其C++实现是计算机视觉领域的一个重要组成部分,通过对连续帧间像素变化的精细分析,能够有效捕捉和理解动态场景中的运动信息
微信小程序作为腾讯推的一种轻型应用形式,因其便捷性与高效性,已广泛应用于日常生活中。以下为该平台的主要特性及配套资源说明: 特性方面: 操作便捷,即开即用:用户通过微信内搜索或扫描二维码即可直接使用,无需额外下载安装,减少了对手机存储空间的占用,也简化了使用流程。 多端兼容,统一开发:该平台支持在多种操作系统与设备上运行,开发者无需针对不同平台进行重复适配,可在一个统一的环境中完成开发工作。 功能丰富,接口完善:平台提供了多样化的API接口,便于开发者实现如支付功能、用户身份验证及消息通知等多样化需求。 社交整合,传播高效:小程序深度嵌入微信生态,能有效利用社交关系链,促进用户之间的互动与传播。 开发成本低,周期短:相比传统应用程序,小程序的开发投入更少,开发周期更短,有助于企业快速实现产品上线。 资源内容: “微信小程序-项目源码-原生开发框架-含效果截图示例”这一资料包,提供了完整的项目源码,并基于原生开发方式构建,确保了代码的稳定性与可维护性。内容涵盖项目结构、页面设计、功能模块等关键部分,配有详细说明与注释,便于使用者迅速理解并掌握开发方法。此外,还附有多个实际运行效果的截图,帮助用户直观了解功能实现情况,评估其在实际应用中的表现与价值。该资源适用于前端开发人员、技术爱好者及希望拓展业务的机构,具有较高的参考与使用价值。欢迎查阅,助力小程序开发实践。资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值