C# 游戏排序算法管理

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

namespace SGGame
{
    public enum SORT_METHOD
    {
        NONE,

BY_CARD_SCORE = 1,
        BY_CARD_WORTH,
        BY_CARD_EXP,
        BY_CARD_REFINE,

BY_EQUIP_SCORE, // use as default
        BY_EQUIP_WORTH,
    }

public class SortMethod
    {
        private Dictionary<SORT_METHOD, IComparer<SlotInfo>> Methods;

private static SortMethod Instance;
        public  static IComparer<SlotInfo> Comparer
        {
            get
            {
                if (Instance == null)
                {
                    Instance = new SortMethod();
                    Instance.Methods = new Dictionary<SORT_METHOD, IComparer<SlotInfo>>();
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_SCORE,    new SortMultiple(new SortByFormat(),   new SortByStar(),     new SortByScore(),    new SortByLevel()));
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_WORTH,    new SortMultiple(new SortByCoinCard(), new SortDECByStar(),  new SortDECByLevel(), new SortDECByID()));
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_EXP,      new SortMultiple(new SortByExpCard(),  new SortDECByStar(),  new SortDECByLevel(), new SortDECByID()));
                    Instance.Methods.Add(SORT_METHOD.BY_CARD_REFINE, new SortMultiple(new SortDECByStar(), new SortDECByLevel(), new SortDECByID()));
                    Instance.Methods.Add(SORT_METHOD.BY_EQUIP_SCORE,   new SortMultiple(new SortByStar(),     new SortBySlot()));
                    Instance.Methods.Add(SORT_METHOD.BY_EQUIP_WORTH, new SortMultiple(new SortDECByStar(), new SortDECByLevel(), new SortDECByID()));
                    //Instance.Methods.Add(SORT_METHOD.BY_EQUIP_PIECE, new SortMultiple(new SortEquipByEnough(), new SortEquipByStar(), new SortEquipByCan(), new SortEquipPieceCount()));
                }

return Instance.Methods[GameManager.ProxyManager.MainProxy.SortMethod];
            }
        }
    }

public class SortBySlot   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return x.Slot - y.Slot; } }
    public class SortByCode   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return x.Code - y.Code; } }
    public class SortByScore  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as CardInfo).Score              - (x as CardInfo).Score; } }
    public class SortByClass  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo).BaseData.attribute - (y as CardInfo).BaseData.attribute; } }
    public class SortByLevel  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo) == null ? (y as EquipInfo).Level - (x as EquipInfo).Level : (y as CardInfo).Level - (x as CardInfo).Level; } }
    public class SortByCost   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as CardInfo).Cost               - (x as CardInfo).Cost;  } }
    public class SortByFormat : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return x.Slot > Global.MAX_FIGHT_POS_COUNT && y.Slot > Global.MAX_FIGHT_POS_COUNT ? 0 : x.Slot - y.Slot; } }
    public class SortByStar   : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            if (x.GetType() == typeof(CardInfo))
                return (y as CardInfo).BaseData.start - (x as CardInfo).BaseData.start;

if (x.GetType() == typeof(EquipInfo))
                return (y as EquipInfo).BaseData.star - (x as EquipInfo).BaseData.star;

return 0;
        }
    }

public class SortDECByStar : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            if (x.GetType() == typeof(CardInfo))
                return (x as CardInfo).BaseData.start-(y as CardInfo).BaseData.start ;

if (x.GetType() == typeof(EquipInfo))
                return (x as EquipInfo).BaseData.star-(y as EquipInfo).BaseData.star;

return 0;
        }
    }

public class SortByCoinCard  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo).BaseData.IsCoinCard() ? (y as CardInfo).BaseData.IsCoinCard() ? 0 : -1 : (y as CardInfo).BaseData.IsCoinCard() ? 1 : 0; } }
    public class SortByExpCard   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo).BaseData.IsExpCard() ? (y as CardInfo).BaseData.IsExpCard() ? 0 : -1 : (y as CardInfo).BaseData.IsExpCard() ? 1 : 0; } }
    public class SortDECByLevel  : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo) == null ? (x as EquipInfo).Level-(y as EquipInfo).Level  : (x as CardInfo).Level-(y as CardInfo).Level ; } }
    public class SortDECByID     : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (x as CardInfo) == null ? (x as EquipInfo).BaseData.id - (y as EquipInfo).BaseData.id : (x as CardInfo).BaseData.ID - (y as CardInfo).BaseData.ID; } }

public class SortByAttack    : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).Attack - (x as EquipInfo).Attack; } }
    public class SortByHP        : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).HP     - (x as EquipInfo).HP;     } }
    public class SortByPhDef     : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).PhDef  - (x as EquipInfo).PhDef;  } }
    public class SortByMgDef     : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (y as EquipInfo).MgDef  - (x as EquipInfo).MgDef;  } }
    public class SortByact1Gr100 : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (int)(y as EquipInfo).BaseData.Prop[0].max   - (int)(x as EquipInfo).BaseData.Prop[0].max; } }
    public class SortByact1num   : IComparer<SlotInfo> { public int Compare(SlotInfo x, SlotInfo y) { return (int)(y as EquipInfo).BaseData.Prop[0].value - (int)(x as EquipInfo).BaseData.Prop[0].value; } }

public class SortEquipPieceByCan : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            EquipPieceInfo infoX = x as EquipPieceInfo;
            EquipPieceInfo infoY = y as EquipPieceInfo;
            List<int> sources_X = new List<int>();
            List<int> sources_Y = new List<int>();
            sources_X = TableManager.EquipTable.Find((ushort)infoX.BaseData.id).Source;
            sources_Y = TableManager.EquipTable.Find((ushort)infoY.BaseData.id).Source;
            if (sources_X.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoX.BaseData.id && detail.dropType == 6)
                            sources_X.Add((int)floor.ID);
                    }
                }
            }
            if (sources_Y.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoY.BaseData.id && detail.dropType == 6)
                            sources_Y.Add((int)floor.ID);
                    }
                }
            }
            if (sources_X.Count == sources_Y.Count && sources_X.Count==0)
            {
                return 0;
            }
            else
            {
                if (sources_X.Count==0)
                {
                    return -1;
                }
                else if (sources_Y.Count==0)
                {
                    return 1;
                }
                else
                {
                    bool floor_available_x = false;
                    bool floor_available_y = false;
                    for (int i = 0; i < sources_X.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_X[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_X[i])
                        {
                            floor_available_x = floor_latest.ID > sources_X[i];
                            break;
                        }
                    }
                    for (int i = 0; i < sources_Y.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_Y[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_Y[i])
                        {
                            floor_available_y = floor_latest.ID > sources_Y[i];
                            break;
                        }
                    }
                    if (floor_available_x == floor_available_y && (floor_available_x == false || floor_available_x == true))
                    {
                        return 0;
                    }
                    else
                    {
                        if (floor_available_x==true)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
            }
        }
    }

public class SortCardPieceByCan : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            CardPieceInfo infoX = x as CardPieceInfo;
            CardPieceInfo infoY = y as CardPieceInfo;
            List<int> sources_X = new List<int>();
            List<int> sources_Y = new List<int>();
            sources_X = TableManager.CardTable.Find((ushort)infoX.BaseData.ID).Source;
            sources_Y = TableManager.CardTable.Find((ushort)infoY.BaseData.ID).Source;
            if (sources_X.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoX.BaseData.ID && detail.dropType == 5)
                            sources_X.Add((int)floor.ID);
                    }
                }
            }
            if (sources_Y.Count == 0)
            {
                // traverse all loot entry to find sources
                foreach (SmallLevelBase floor in TableManager.SmallLevelTable.MapTable.Values)
                {
                    if (floor.ID >= 3000) break; // donot handle activity dungeons

LootBase loot = TableManager.SmallBonusTable.Find((ushort)floor.rewardGroupID);
                    if (loot == null)
                    {
                        continue;
                    }
                    foreach (LootBase.LootDetail detail in loot.LootDetails)
                    {
                        if (detail.itemId == infoY.BaseData.ID && detail.dropType == 5)
                            sources_Y.Add((int)floor.ID);
                    }
                }
            }
            if (sources_X.Count == sources_Y.Count && sources_X.Count == 0)
            {
                return 0;
            }
            else
            {
                if (sources_X.Count == 0)
                {
                    return -1;
                }
                else if (sources_Y.Count == 0)
                {
                    return 1;
                }
                else
                {
                    bool floor_available_x = false;
                    bool floor_available_y = false;
                    for (int i = 0; i < sources_X.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_X[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_X[i])
                        {
                            floor_available_x = floor_latest.ID > sources_X[i];
                            break;
                        }
                    }
                    for (int i = 0; i < sources_Y.Count; i++)
                    {
                        SmallLevelBase floor_latest = GameManager.ProxyManager.MainProxy.Missions.Latest(sources_Y[i] / 1000);
                        if (floor_latest != null && floor_latest.ID > sources_Y[i])
                        {
                            floor_available_y = floor_latest.ID > sources_Y[i];
                            break;
                        }
                    }
                    if (floor_available_x == floor_available_y && (floor_available_x == false || floor_available_x == true))
                    {
                        return 0;
                    }
                    else
                    {
                        if (floor_available_x == true)
                        {
                            return -1;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
            }
        }
    }

public class SortEquipPieceCount : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            EquipPieceInfo infoX = x as EquipPieceInfo;
            EquipPieceInfo infoY = y as EquipPieceInfo;
            if (infoX.Count == infoY.Count)
            {
               return infoX.BaseData.id-infoY.BaseData.id;
            }
            else
            {
                //return GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoX.BaseData.id) - GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoY.BaseData.id);
                return infoY.Count-infoX.Count;
            }
        }
    }

public class SortCardPieceCount : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            CardPieceInfo infoX = x as CardPieceInfo;
            CardPieceInfo infoY = y as CardPieceInfo;
            if (infoX.Count == infoY.Count)
            {
                return infoX.BaseData.ID - infoY.BaseData.ID;
            }
            else
            {
                //return GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoX.BaseData.id) - GameManager.ProxyManager.MainProxy.EquipPieces.GetEntryCount(infoY.BaseData.id);
                return infoY.Count - infoX.Count;
            }
        }
    }

public class SortCardPiece : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            CardPieceInfo infoX = x as CardPieceInfo;
            CardPieceInfo infoY = y as CardPieceInfo;
            if ( infoX.Enough && !infoY.Enough) return -1;
            if (!infoX.Enough &&  infoY.Enough) return 1;

return infoY.BaseData.start - infoX.BaseData.start;
        }
    }

public class SortEquipPiece : IComparer<SlotInfo>
    {
        public int Compare(SlotInfo x, SlotInfo y)
        {
            EquipPieceInfo infoX = x as EquipPieceInfo;
            EquipPieceInfo infoY = y as EquipPieceInfo;
            if ( infoX.Enough && !infoY.Enough) return -1;
            if (!infoX.Enough &&  infoY.Enough) return 1;

return infoY.BaseData.star - infoX.BaseData.star;
        }
    }

public class SortMultiple : IComparer<SlotInfo>
    {
        private List<IComparer<SlotInfo>> ComparerList;

public SortMultiple(IComparer<SlotInfo> comparer1, IComparer<SlotInfo> comparer2) : this(comparer1, comparer2, null, null) {}
        public SortMultiple(IComparer<SlotInfo> comparer1, IComparer<SlotInfo> comparer2, IComparer<SlotInfo> comparer3) : this(comparer1, comparer2, comparer3, null) {}
        public SortMultiple(IComparer<SlotInfo> comparer1, IComparer<SlotInfo> comparer2, IComparer<SlotInfo> comparer3, IComparer<SlotInfo> comparer4)
        {
            ComparerList = new List<IComparer<SlotInfo>>();
            if (comparer1 != null) ComparerList.Add(comparer1);
            if (comparer2 != null) ComparerList.Add(comparer2);
            if (comparer3 != null) ComparerList.Add(comparer3);
            if (comparer4 != null) ComparerList.Add(comparer4);

ComparerList.Add(new SortByCode()); // set code sorter as default sort method
        }

public int Compare(SlotInfo x, SlotInfo y)
        {
            int result = 0;
            foreach (IComparer<SlotInfo> comparer in ComparerList)
            {
                if (comparer == null) break;

result = comparer.Compare(x, y);
                if (result != 0) break;
            }

return result;
        }
    }
}

时间: 2024-10-29 19:08:40

C# 游戏排序算法管理的相关文章

排序算法的体验游戏

今天监考排座位时出现了一些混乱. 我要求他们按顺序每列N人坐好.若学号是连续的,1到N号第1列,N+1到2N号第2列,这个好说.乱就乱在,参加考试的同学的学号不连续,中间有断号.规则说来简单,若2号.3号空缺,4号需要直接坐在1号后面. 由于手头没有名单,这对于每位同学而言,确实困难,35号必须要看到前面紧临的31号坐定才能确定自己的位置.这是一个必须顺序完成的任务.一时间,学生们陷入相互等待,在教室后面缩成一堆,谁也不动.最后,我和班干部采用了逐个叫号的方式才将坐位排定. 坐定之后,我对同学们

算法(第4版)-2.1 初级排序算法

2.1.1 游戏规则 1. 排序成本模型:在研究排序算法时,我们需要计算比较和交换的数量.对于不交换元素的算法,我们会计算访问数组的次数. 2. · 原地排序算法:除了函数调用所需的栈和固定数目的实例变量之外无需额外内存的原地排序算法: · 其他排序算法:需要额外内存空间来储存另一份数组副本. 2.2.2 选择排序 public class Selection { public static void sort(Comparable[] a) { // 将a[]按升序排列 int N = a.l

排序算法系列——归并排序

记录学习点滴,菜鸟成长记 归并排序的英文叫做Merge-Sort,要想明白归并排序算法,还要从“递归”的概念谈起. 1.递归 一般来讲,人在做决策行事的时候是往往是从已知出发,比如,我又要举个不恰当的例子了→_→: 看到漂亮姑娘→喜欢人家→追→女朋友→老婆 但是人家施瓦辛格不是这么想的,人家从小就立志当总统: 要当总统←先当州长←竞选州长要有钱←那得找个有钱妹子←妹子都喜欢明星←身材好能当明星←健身 递归,就像一个人对自己的发展有清晰的规划和坚定的信心一样,他知道每一步会有怎么样的结果,他需要仅

排序算法之鸡尾酒排序

昨天中午北京温度为40度,在中午12:16分我来到篮球场,思考了1分钟决定开站 转球: 我和另外3名队友开始半场, 球传到我手的刹那顿时烫的我持球不稳,顿时问道了淡淡的胶皮味道和烤肉味道的混搭. 这时我来了一个腾空跳投, 球---------爆炸了........ 听新闻说昨天在路上都是 "熟人" 一位老大爷不慎被车刮倒了,大爷二话没说立马爬了起来,围观众人议论纷纷: "大爷人不错","大爷素质真高","大爷身体可真好" 大爷

九大排序算法Java实现

之前学习数据结构与算法时花了三天时间整理九大排序算法,并采用Java语言来实现,今天第一次写博客,刚好可以把这些东西从总结的文档中拿出来与大家分享一下,同时作为自己以后的备忘录. 1.排序算法时间复杂度.稳定性分类: 2.排序算法问题描述与实现 2.1冒泡排序(交换排序-稳定) [问题描述]对于一个int数组,请编写一个冒泡排序算法,对数组元素排序. 问题分析:冒泡排序,顾名思义,从前往后遍历,每次遍历在末尾固定一个最大值. 易错点:每次内层循环结束都会在末尾确定一个元素的位置,因此内层循环的判

四种排序算法PHP实现类

四种排序算法的PHP实现:1) 插入排序(Insertion Sort)的基本思想是: 每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止. 2) 选择排序(Selection Sort)的基本思想是: 每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕. 3) 冒泡排序的基本思想是: 两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止. 4) 快速排序实质上和

算法导论专题一--排序算法

排序算法作为许多程序的中间步骤,是计算机科学中的一个基本操作. 一.问题描述 排序算法输入的是n个数的一个序列<a1,a2…..an>,输出为输入的一个排列<a1’,…..an’>,满足a1’<a2’<….<an’ 简言之就是输入一个序列,输出的是这个数组元素从小到大排列的另一序列. 二.方法思想综述 从算法导论这本书上根据算法的复杂度可以将排序算法分为三种,,.,这两种方法都需要数据间的比较,而不需要. 其中有三种为选择,冒泡,插入. 选择排序:最直观,简单但是

排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)

总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然排序的显著目的是排列数据以显示它,但它往往可以用来解决其他的问题,特别是作为某些成型算法的一部分. 总的来说,排序算法分为两大类:比较排序 和 线性时间排序. 比较排序依赖于比较和交换来将元素移动到正确的位置上.它们的运行时间往往不可能小于O(nlgn). 对于线性时间排序,它的运行时间往往与它处理

一些常见的排序算法

博主做的是关于Unity游戏方面的,虽然平时都没有用过这些排序算法,但还是很有必要学习一下.今天想温故一下,总结一下才能更好的进行下一步的学习. 一.冒泡排序 冒泡排序是一个特别简单的排序算法,代码也很容易实现.我总结起来就是:遍历数组n(数组的长度)次:每次与它下一个元素比较,如果这个元素比它下一个元素小,就交换这两个元素.可以发现,如果遍历一次数组的话可以得到数组的最大值或最小值. 一次遍历数组的结果: 经过多次遍历后: 源码: import java.util.Arrays; public