看看H5游戏引擎 各种技巧

最近想学习下Html5游戏开发,了解了几个H5游戏引擎,Egret、Cocos2d-js,PharseJs、LayaAir,用哪一种的开发者都有,各有各的优势,各有各的坑。

于是初步看了Egret和Cocos-Js,Egret是用TS(typescript,微软出的一个语言)来开发,然后编译成js的,核心也是由js开发的,对于html5的支持很好,开发html5游戏效率和性能也是很高的;Cocos-Js的母体语言是C++,通过js binding实现H5,从性能上不如Egret,但它对于Native的支持则比Egret强。从开发的角度来看Cocos-js坑很多,很多问题需要修改底层源码,Egret的Bug也不少,开发轻量级的PC Html5游戏自然没有问题,只是对Native的效率还不够。



new 发布于 2016-11-1 06:01

UGUI不规则形状按钮 各种技巧

using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
public class RaycastFilter : MonoBehaviour, ICanvasRaycastFilter
{
 private RectTransform rectTransform;
 private Image image;
 public void Awake()
 {
  collider2D = GetComponent<BoxCollider2D>();
  image = GetComponent<Image>();
 }
 public bool IsRaycastLocationValid(Vector2 screenPosition, Camera raycastEventCamera) //uGUI callback
 {
  if (image == null)
   return true;
  Vector2 localPoint;
  RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPosition, raycastEventCamera, out localPoint);
  var normalized = new Vector2(
   (localPoint.x + rectTransform.pivot.x*rectTransform.rect.width)/rectTransform.rect.width,
   (localPoint.y + rectTransform.pivot.y*rectTransform.rect.height)/rectTransform.rect.width);
  Rect rect = image.sprite.textureRect;
  var x = Mathf.FloorToInt(rect.x + rect.width * normalized.x);
  var y = Mathf.FloorToInt(rect.y + rect.height * normalized.y);
  try
  {
   return image.sprite.texture.GetPixel(x,y).a > 0.2f;
  }
  catch (UnityException e)
  {
   Debug.LogError("Mask texture not readable, set your sprite to Texture Type 'Advanced' and check 'Read/Write Enabled'");
   Destroy(this);
   return false;
  }
 }
}
标签: uGUI

new 发布于 2016-3-3 05:51

11步让你成为更优秀的程序员 各种技巧

1.永远不要复制代码



不惜任何代价避免重复的代码。如果一个常用的代码片段出现在了程序中的几个不同地方,重构它,把它放到一个自己的函数里。重复的代码会导致你的同事在读你的代码时产生困惑。而重复的代码如果在一个地方修改,在另外一个地方忘记修改,就会产生到处是bug,它还会使你的代码体积变得臃肿。现代的编程语言提供了很好的方法来解决这些问题,例如,下面这个问题在以前很难解决,而如今使用lambdas却很好实现:






现在我们重构含有部分相同代码的函数,用delegate模式重写它们:






2. 留意你开始分心的时候



当你发现自己在浏览facebook或微博、而不是在解决问题,这通常是一种你需要短暂休息的信号。离开办公桌,去喝一杯咖啡,或去跟同事聊5分钟。尽管这样做看起来有点反直觉,但长久去看,它会提高你的工作效率。



3. 不要匆忙赶任务而放弃原则



当带着压力去解决一个问题或修改一个bug,你很容易失去自制,发现自己匆匆忙忙,甚至完全忘了一直坚持的重要的测试过程。这通常会导致更多的问题,会让你在老板或同事眼里显得很不专业。



4. 测试你完成的代码



你知道你的代码能做什么,而且试了一下,它确实好用,但你实际上需要充分的验证它。分析所有可能的边界情况,测试在所有可能的条件下它都能如期的工作。如果有参数,传递一些预期范围外的值。传递一个null值。如果可能,让同事看看你的代码,问他们能否弄坏它。单元测试是到达这种目的的常规方法。



5. 代码审查



提交你的代码之前,找个同事一起坐下来,向他解释你做了哪些修改。通常,这样做的过程中你就能发现代码中的错误,而不需要同事说一句话。这比自己审查自己的代码要有效的多得多。



6. 让代码更少



如果你发现写了大量的代码来解决一个简单的问题,你很可能做错了。下面的boolean用法是一个很好的例子:






这时你应该写成这样:






代码越少越好。这会使bug更少,重构可能性更小,出错的几率更小。要适度。可读性同等重要,你可不能这样做而使代码丧失可读性。



7. 为优雅的代码而努力



优雅的代码非常的易读,只用手边很少的代码、让机器做很少的运算就能解决问题。在各种环境中都做到代码优雅是很难的,但经过一段时间的编程,你会对优雅的代码是个什么样子有个初步的感觉。优雅的代码不会通过重构来获得。当你看到优雅的代码是会很高兴。你会为它自豪。例如,下面就是一个我认为是优雅的方式来计算多边形面积的方法:






8. 编写不言自明的代码



勿庸置疑,注释是编程中很重要的一部分,但能够不言自明的代码跟胜一筹,因为它能让你在看代码时就能理解它。函数名变量名要慎重选择,好的变量/方法名字放到语言语义环境中时,不懂编程的人都能看懂。例如:






能自我说明的代码不能代替注释。注释是用来解释“为什么”的,而自我说明的代码是来描述“是什么”的。



9. 不要使用纯数字



直接把数字嵌入代码中是一种恶习,因为无法说明它们是代表什么的。当有重复时更糟糕——相同的数字在代码的多个地方出现。如果只修改了一个,而忘记了其它的。这就导致bug。一定要用一个命名常量来代表你要表达的数字,即使它在代码里只出现一次。



10. 不要做手工劳动



当做一系列动作时,人类总是喜欢犯错误。如果你在做部署工作,并且不是一步能完成的,那你就是在做错事。尽量的让工作能自动化的完成,减少人为错误。当做工作量很大的任务时,这尤其重要。



11. 避免过早优化


当你要去优化一个已经好用的功能代码时,你很有可能会改坏它。优化只能发生在有性能分析报告指示需要优化的时候,通常是在一个项目开发的最后阶段。性能分析之前的优化活动纯属浪费时间,并且会导致bug出现。


来源:http://www.manew.com/thread-47665-1-1.html


new 发布于 2016-3-1 01:50

斗牛算法C# 各种技巧

斗牛算法

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

namespace ConsoleApplication1
{
    public enum Suits
    {
        Spade = 4,
        Heart = 3,
        Diamond = 2,
        Club = 1,
    }

    public struct CardChip
    {
        public Suits suit;
        public int point;
        public string name;
        public byte suitName;
    }

    /// <summary>
    /// 结果类型
    /// </summary>
    public struct CardsResult
    {
        public ResultType type;
        public int resultPoint;
        public CardChip theMax;
    }

    public enum ResultType
    {
        NoCow = 0,
        CowOne,
        CowTwo,
        CowThree,
        CowFour,
        CowFive,
        CowSix,
        CowSeven,
        CowEight,
        CowNine,
        CowTen,
        SilverCow,
        GoldCow,
        FiveSmall,
        Boom
    }

    public class Player
    {
        public int playerID;
        public List<CardChip> cardList = new List<CardChip>();
        public CardsResult Result
        {
            get { return this.CalcResult(); }
        }

        /// <summary>
        /// show1
        /// </summary>
        public void ShowCards()
        {
            Console.WriteLine("Player:" + playerID);
            foreach (CardChip cp in cardList)
            {
                string a = cp.name + "";
                string b = "";
                if (cp.suit == Suits.Heart)
                    b = (char)3 + "";
                else if (cp.suit == Suits.Diamond)
                    b = (char)4 + "";
                else if (cp.suit == Suits.Club)
                    b = (char)5 + "";
                else
                    b = (char)6 + "";

                Console.Write(b + a + ",");
            }
            Console.WriteLine();
        }

        /// <summary>
        /// show2
        /// </summary>
        public void ShowCrads2()
        {
            Console.WriteLine("Player:" + playerID);
            foreach (CardChip cp in cardList)
            {
                string a = cp.name + "";
                string b = "";
                if (cp.suit == Suits.Heart)
                    b = (char)3 + "";
                else if (cp.suit == Suits.Diamond)
                    b = (char)4 + "";
                else if (cp.suit == Suits.Club)
                    b = (char)5 + "";
                else
                    b = (char)6 + "";

                Console.WriteLine("┌───┐");
                Console.WriteLine("│" + b + "     │");
                Console.WriteLine("│  " + a.PadRight(4) + "│");
                Console.WriteLine("│     " + b + "│");
                Console.WriteLine("└───┘");
            }
            Console.WriteLine();
        }

        /// <summary>
        /// 排序
        /// </summary>
        public void Sort()
        {
            cardList.Sort(
                delegate(CardChip cc1, CardChip cc2)
                {
                    if (cc1.point == cc2.point)
                    {
                        return cc1.suit.CompareTo(cc2.suit);
                    }
                    return cc1.point.CompareTo(cc2.point);
                });
        }

        /// <summary>
        /// 计算牌点数
        /// </summary>
        /// <returns></returns>
        private CardsResult CalcResult()
        {
            CardsResult result;
            result.type = ResultType.NoCow;
            result.resultPoint = 0;
            result.theMax = new CardChip();

            if (Boom(cardList))
            {
                result.theMax = cardList[cardList.Count - 1];
                result.resultPoint = (int)ResultType.Boom;
                result.type = ResultType.Boom;
                return result;
            }

            if (FiveSmall(cardList))
            {
                result.theMax = cardList[cardList.Count - 1];
                result.resultPoint = (int)ResultType.FiveSmall;
                result.type = ResultType.FiveSmall;
                return result;
            }

            List<CardChip> colorCards = new List<CardChip>();
            List<CardChip> noColorCards = new List<CardChip>();

            for (int i = 0; i < cardList.Count; i++)
            {
                if (cardList[i].point >= 10)
                {
                    colorCards.Add(cardList[i]);
                }
                else
                {
                    noColorCards.Add(cardList[i]);
                }
            }

            switch (colorCards.Count)
            {
                case 0:
                    return NoColorCard(cardList);
                case 1:
                    return OneColorCard(cardList);
                case 2:
                    return TwoColorCard(cardList);
                case 3:
                case 4:
                case 5:
                    return MoreThanThreeColorCard(cardList);
            }
            return result;
        }

        /// <summary>
        /// 炸弹
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private bool Boom(List<CardChip> list)
        {
            //(b|c|d)==a
            if ((list[1].point | list[2].point | list[3].point) == list[0].point)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 五小
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private bool FiveSmall(List<CardChip> list)
        {
            int res = list[0].point + list[1].point + list[2].point + list[3].point + list[4].point;
            if (res <= 10)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 三四五花
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private CardsResult MoreThanThreeColorCard(List<CardChip> list)
        {
            CardsResult cdResult;
            cdResult.theMax = list[list.Count - 1];
            int res = (list[0].point + list[1].point) % 10;
            if (res == 0)
            {
                cdResult.type = ResultType.GoldCow;
                cdResult.resultPoint = 10;
            }
            else
            {
                cdResult.resultPoint = res;
                cdResult.type = (ResultType)res;
            }

            return cdResult;
        }

        /// <summary>
        /// 二花
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private CardsResult TwoColorCard(List<CardChip> list)
        {
            CardsResult cdResult;
            cdResult.theMax = list[list.Count - 1];

            if ((list[0].point + list[1].point) % 10 == 0)
            {
                cdResult.resultPoint = list[2].point;
                cdResult.type = (ResultType)list[2].point;
            }
            else if ((list[0].point + list[2].point) % 10 == 0)
            {
                cdResult.resultPoint = list[1].point;
                cdResult.type = (ResultType)list[1].point;
            }
            else if ((list[1].point + list[2].point) % 10 == 0)
            {
                cdResult.resultPoint = list[0].point;
                cdResult.type = (ResultType)list[0].point;
            }
            else if ((list[0].point + list[1].point + list[2].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
            }
            else
            {
                cdResult.resultPoint = 0;
                cdResult.type  = ResultType.NoCow;
            }

            return cdResult;
        }

        /// <summary>
        /// 一花
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private CardsResult OneColorCard(List<CardChip> list)
        {
            CardsResult cdResult;
            int temp = 0;
            if ((list[0].point + list[1].point) % 10 == 0)
            {
                temp = (list[2].point + list[3].point) % 10;
                if (temp == 0)
                {
                    cdResult.resultPoint = 10;
                    cdResult.type = ResultType.CowTen;
                }
                else
                {
                    cdResult.resultPoint = temp;
                    cdResult.type = (ResultType)temp;
                }
            }
            else if ((list[0].point + list[2].point) % 10 == 0)
            {
                temp = (list[1].point + list[3].point) % 10;
                if (temp == 0)
                {
                    cdResult.resultPoint = 10;
                    cdResult.type = ResultType.CowTen;
                }
                else
                {
                    cdResult.resultPoint = temp;
                    cdResult.type = (ResultType)temp;
                }
            }
            else if ((list[0].point + list[3].point) % 10 == 0)
            {
                temp = (list[1].point + list[2].point) % 10;
                if (temp == 0)
                {
                    cdResult.resultPoint = 10;
                    cdResult.type = ResultType.CowTen;
                }
                else
                {
                    cdResult.resultPoint = temp;
                    cdResult.type = (ResultType)temp;
                }
            }
            else if ((list[1].point + list[2].point) % 10 == 0)
            {
                temp = (list[0].point + list[3].point) % 10;
                if (temp == 0)
                {
                    cdResult.resultPoint = 10;
                    cdResult.type = ResultType.CowTen;
                }
                else
                {
                    cdResult.resultPoint = temp;
                    cdResult.type = (ResultType)temp;
                }
            }
            else if ((list[1].point + list[3].point) % 10 == 0)
            {
                temp = (list[0].point + list[2].point) % 10;
                if (temp == 0)
                {
                    cdResult.resultPoint = 10;
                    cdResult.type = ResultType.CowTen;
                }
                else
                {
                    cdResult.resultPoint = temp;
                    cdResult.type = (ResultType)temp;
                }
            }
            else if ((list[2].point + list[3].point) % 10 == 0)
            {
                temp = (list[0].point + list[1].point) % 10;
                if (temp == 0)
                {
                    cdResult.resultPoint = 10;
                    cdResult.type = ResultType.CowTen;
                }
                else
                {
                    cdResult.resultPoint = temp;
                    cdResult.type = (ResultType)temp;
                }
            }
            else if ((list[0].point + list[1].point + list[2].point) % 10 == 0)
            {
                cdResult.resultPoint = list[3].point;
                cdResult.type = (ResultType)list[3].point;
            }
            else if ((list[0].point + list[1].point + list[3].point) % 10 == 0)
            {
                cdResult.resultPoint = list[2].point;
                cdResult.type = (ResultType)list[2].point;
            }
            else if ((list[0].point + list[2].point + list[3].point) % 10 == 0)
            {
                cdResult.resultPoint = list[1].point;
                cdResult.type = (ResultType)list[1].point;
            }
            else if ((list[1].point + list[2].point + list[3].point) % 10 == 0)
            {
                cdResult.resultPoint = list[0].point;
                cdResult.type = (ResultType)list[0].point;
            }
            else
            {
                cdResult.resultPoint = 0;
                cdResult.type = ResultType.NoCow;
            }
            cdResult.theMax = list[list.Count - 1];
            return cdResult;
        }

        /// <summary>
        /// 无花
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private CardsResult NoColorCard(List<CardChip> list)
        {
            CardsResult cdResult;
            cdResult.theMax = list[list.Count - 1];

            if ((list[0].point + list[1].point + list[2].point) % 10 == 0 && (list[3].point + list[4].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[0].point + list[1].point + list[2].point) % 10 == 0 && (list[3].point + list[4].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[3].point + list[4].point) % 10;
                cdResult.type = (ResultType)((list[3].point + list[4].point) % 10);
                return cdResult;
            }

            if ((list[0].point + list[1].point + list[3].point) % 10 == 0 && (list[2].point + list[4].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[0].point + list[1].point + list[3].point) % 10 == 0 && (list[2].point + list[4].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[2].point + list[4].point) % 10;
                cdResult.type = (ResultType)((list[2].point + list[4].point) % 10);
                return cdResult;
            }

            if ((list[0].point + list[1].point + list[4].point) % 10 == 0 && (list[2].point + list[3].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[0].point + list[1].point + list[4].point) % 10 == 0 && (list[2].point + list[3].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[2].point + list[3].point) % 10;
                cdResult.type = (ResultType)((list[2].point + list[3].point) % 10);
                return cdResult;
            }

            if ((list[0].point + list[2].point + list[3].point) % 10 == 0 && (list[1].point + list[4].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[0].point + list[2].point + list[3].point) % 10 == 0 && (list[1].point + list[4].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[1].point + list[4].point) % 10;
                cdResult.type = (ResultType)((list[1].point + list[4].point) % 10);
                return cdResult;
            }

            if ((list[0].point + list[2].point + list[4].point) % 10 == 0 && (list[1].point + list[3].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[0].point + list[2].point + list[4].point) % 10 == 0 && (list[1].point + list[3].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[1].point + list[3].point) % 10;
                cdResult.type = (ResultType)((list[1].point + list[3].point) % 10);
                return cdResult;
            }

            if ((list[1].point + list[2].point + list[3].point) % 10 == 0 && (list[0].point + list[4].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[1].point + list[2].point + list[3].point) % 10 == 0 && (list[0].point + list[4].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[0].point + list[4].point) % 10;
                cdResult.type = (ResultType)((list[0].point + list[4].point) % 10);
                return cdResult;
            }

            if ((list[1].point + list[2].point + list[4].point) % 10 == 0 && (list[0].point + list[3].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[1].point + list[2].point + list[4].point) % 10 == 0 && (list[0].point + list[3].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[0].point + list[3].point) % 10;
                cdResult.type = (ResultType)((list[0].point + list[3].point) % 10);
                return cdResult;
            }

            if ((list[2].point + list[3].point + list[4].point) % 10 == 0 && (list[0].point + list[1].point) % 10 == 0)
            {
                cdResult.resultPoint = 10;
                cdResult.type = ResultType.CowTen;
                return cdResult;
            }
            else if ((list[2].point + list[3].point + list[4].point) % 10 == 0 && (list[0].point + list[1].point) % 10 != 0)
            {
                cdResult.resultPoint = (list[0].point + list[1].point) % 10;
                cdResult.type = (ResultType)((list[0].point + list[1].point) % 10);
                return cdResult;
            }

            cdResult.resultPoint = 0;
            cdResult.type = ResultType.NoCow;
            return cdResult;
        }

    }

    public class Card
    {
        /// <summary>
        /// The maximum player count
        /// </summary>
        public const int MAX_PLAYERS = 10;

        /// <summary>
        /// 构造
        /// </summary>
        public Card()
        {

        }

        public void Start()
        {
            //Initialize crads
            List<CardChip> cards = this.InitCards();
            //Random crads
            this.RandomCards(cards);
            //Deal cards to players
            Player[] players = Deal(cards, 5, 5);
            //print to screen
            for (int i = 0; i < players.Length; i++)
            {
                players[i].ShowCards();
            }
            //calculate the result for each player
            Console.WriteLine("-----------------------------------------------------");
            for (int k = 0; k < players.Length; k++)
            {
                CardsResult cr = players[k].Result;
                string str = string.Format("Player{0}:牌型-{1} 点数-{2} 最大牌-{3}{4}", k, (ResultType)cr.type, cr.resultPoint, (char)cr.theMax.suitName, cr.theMax.name);
                Console.WriteLine(str);
            }

            CalcWinner(players);
        }

        /// <summary>
        /// 初始化牌13*4  游戏蛮牛http://www.unitymanual.com/space-uid-13769.html  CSDN博客http://write.blog.csdn.net/postlist
        /// </summary>  
        /// <returns></returns>
        public List<CardChip> InitCards()
        {
            List<CardChip> cards = new List<CardChip>();

            for (int i = 1; i <= 13; i++)
            {
                for (int j = 1; j <= 4; j++)
                {
                    CardChip cc;
                    cc.point = i;
                    cc.suit = (Suits)j;
                    if (j == 1) cc.suitName = 4;
                    else if (j == 2) cc.suitName = 5;
                    else if (j == 3) cc.suitName = 3;
                    else cc.suitName = 6;

                    if (i >= 2 && i <= 10)
                        cc.name = i.ToString();
                    else if (i == 1)
                        cc.name = "A";
                    else if (i == 11)
                        cc.name = "J";
                    else if (i == 12)
                        cc.name = "Q";
                    else
                        cc.name = "K";
                    cards.Add(cc);
                }
            }
            return cards;
        }

        /// <summary>
        /// 洗牌
        /// </summary>
        /// <param name="cds"></param>
        public void RandomCards(List<CardChip> cds)
        {
            Permute(cds);
        }

        /// <summary>
        /// Deal crads
        /// </summary>
        /// <param name="cds">洗好的牌</param>
        /// <param name="players">玩家数</param>
        /// <param name="num">每个玩家的牌数</param>
        /// <returns></returns>
        public Player[] Deal(List<CardChip> cds, int players, int num)
        {
            if (players > MAX_PLAYERS)
            {
                Console.WriteLine("Too much players");
                return null;
            }
            Console.WriteLine("----------------------Deal cards----------------------");
            Player[] playerList = new Player[players];
            for (int i = 0; i < playerList.Length; i++)
            {
                playerList[i] = new Player();
                playerList[i].playerID = i;
                playerList[i].cardList = new List<CardChip>();
            }
            for (int j = 0; j < players * num; j++)
            {
                playerList[j % players].cardList.Add(cds[j]);
            }
            for (int k = 0; k < playerList.Length; k++)
            {
                playerList[k].Sort();
            }
            return playerList;
        }

        /// <summary>
        /// Calculate the winner
        /// </summary>
        /// <param name="players"></param>
        public void CalcWinner(Player[] players)
        {
            List<Player> p = players.ToList<Player>();
            p.Sort(
                delegate(Player p1, Player p2)
                {
                    if (p1.Result.type == p2.Result.type)
                    {
                        if (p1.Result.theMax.point == p2.Result.theMax.point)
                        {
                            return p1.Result.theMax.suit.CompareTo(p2.Result.theMax.suit);
                        }
                        return p1.Result.theMax.point.CompareTo(p2.Result.theMax.point);
                    }
                    return p1.Result.type.CompareTo(p2.Result.type);

                });
            Console.WriteLine("----------------------结果---------------------------");
            for (int k = p.Count - 1; k >= 0; k--)
            {
                CardsResult cr = p[k].Result;
                string str = string.Format("Player{0}:牌型-{1} 点数-{2} 最大牌-{3}{4}", p[k].playerID, (ResultType)cr.type, cr.resultPoint, (char)cr.theMax.suitName, cr.theMax.name);
                Console.WriteLine(str);
            }
            Console.WriteLine("-----------------------------------------------------");
            Console.WriteLine(string.Format("玩家{0}获胜!", p[4].playerID));

        }

        #region Random Cards in Array
        private void Permute<T>(List<T> array)
        {
            Random random = new Random();
            for (int i = 1; i < array.Count; i++)
            {
                Swap<T>(array, i, random.Next(0, i));
            }
        }

        private void Swap<T>(List<T> array, int indexA, int indexB)
        {
            T temp = array[indexA];
            array[indexA] = array[indexB];
            array[indexB] = temp;
        }
        #endregion
    }
}

标签: 斗牛算法

new 发布于 2016-3-1 01:35

洗牌算法 各种技巧

洗牌算法
private void Permute<T>(List<T> array)
        {
            Random random = new Random();
            for (int i = 1; i < array.Count; i++)
            {
                Swap<T>(array, i, random.Next(0, i));
            }
        }

        private void Swap<T>(List<T> array, int indexA, int indexB)
        {
            T temp = array[indexA];
            array[indexA] = array[indexB];
            array[indexB] = temp;
        }

标签: 洗牌

new 发布于 2016-3-1 01:33