`
louisling
  • 浏览: 143852 次
  • 性别: Icon_minigender_1
  • 来自: ZhuHai
社区版块
存档分类
最新评论

MatchFinder

    博客分类:
  • Java
阅读更多
public class MatchFinder {
    //static List<FileInfo> matchNames = new ArrayList<FileInfo>();
    static List<String> nonMatchNames = new ArrayList<String>();

    public static void main(String[] args) throws Exception {
        List<MatchInfo> matchNames = new ArrayList<MatchInfo>();

        //enumerate(new File("I:/_backup/_bad"));
        //enumerate(new File("D:/_bad"));
        //enumerate(new File("E:/badmintons"), matchNames); 
        //enumerate(new File("F:/badmintons"), matchNames);
        
        /**/
        readIndexFromFile("I:/_backup/_bad/bad.index.txt", matchNames);
/*        readIndexFromFile("F:/louis/badminton-index/Home.BY.E.txt", matchNames);
        readIndexFromFile("F:/louis/badminton-index/Home.Louis.E.txt", matchNames);
        readIndexFromFile("F:/louis/badminton-index/Home.Louis.F.txt", matchNames);*/

        //enumerate(new File("F:/_bad/badmintons"), matchNames);        
        Collections.sort(matchNames); //, new MatchInfoCompator()
        Log.prt(matchNames);
        
        //writeToExcel(matchNames, "c:/tmp/matches.xls");
    }
    
    static void writeToExcel(List<MatchInfo> matchNames, String fileName) {
        FileOutputStream fos = null;
        try {
            MWorkbook workbook = new MWorkbook();

            //write data
            MSheet sheet = workbook.createSheet("Match");
            MRow row = sheet.createRow(0);
            //Create Header
            String[] headers = {"Year", "Match", "Item", "Player1", "Player2", "File Name", "Absolute Path"};
            for (int i = 0; i < headers.length; i++) {
                MCell cell = row.createCell((short) i);
                cell.setMCellValue(headers[i], true);
            }

            for (int i = 0; i < matchNames.size(); i ++ ) {
                MatchInfo info = matchNames.get(i);
                row = sheet.createRow(1 + i);
                
                MCell cell = row.createCell((short) 0);                
                cell.setMCellValue(info.year);
                
                cell = row.createCell((short) 1);
                cell.setMCellValue(info.match);
                
                cell = row.createCell((short) 2);
                cell.setMCellValue(info.item);
                
                cell = row.createCell((short) 3);
                cell.setMCellValue(info.player1);
                
                cell = row.createCell((short) 4);
                cell.setMCellValue(info.player2);
                
                cell = row.createCell((short) 5);
                cell.setMCellValue(info.fileName);
                
                cell = row.createCell((short) 6);
                cell.setMCellValue(info.absolutePath);
            }
                        
            //Set column to proper size
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn((short) i);
            }
                       
            //Save to file
            fos = new FileOutputStream(fileName);
            workbook.write(fos);            
        }
        catch (FileNotFoundException e1) {
            Log.error(e1);
        }
        catch (IOException e2) {
            Log.error(e2);
        }
        finally {
            if (fos != null)
                try {
                    fos.close();
                }
                catch (IOException e) {
                    Log.error(e);
                }
        }
    }

    public static void enumerate(File directory, List<MatchInfo> matchNames) throws Exception {
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory())
                enumerate(file, matchNames);
            else
                search(file, matchNames);
        }
    }

    static void search(File file, List<MatchInfo> matchNames) throws IOException {
        String fileName = file.getName();
        if (!isValidMatchFile(fileName)) {
            //Log.prt("*** " + fileName);
            nonMatchNames.add(fileName);
            return;
        }

        int itemIndex = itemIndex(fileName);
        int vsIndex = vsIndex(fileName);

        //itemIndex should >=6, vsIndex should >=11, eg. 1999 X MS A VS B.avi         
        if (itemIndex < 6 || vsIndex < 11)
            Log.prt("*** " + file.getAbsolutePath());
        else {
            //String matchName = getMatchName(fileName);
            matchNames.add(new MatchInfo(file.getAbsolutePath(), fileName));
        }
    }

    /** Valid match file name: (2001) China (MS) Alice (VS) Bob(.)rmvb */
    static boolean isValidMatchFile(String fileName) {
        if (fileName.endsWith(".txt"))
            return false;
        try {
            //If is a full file Name that contains path
            if (fileName.contains("\\")) {
                fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
            }

            String year = fileName.substring(0, 4);
            Integer.parseInt(year);
        } catch (Exception e) {
            //Log.prt("*** " + fileName);
            return false;
        }

        return true;
    }

    static void readIndexFromFile(String indexFileName, List<MatchInfo> matchNames) throws FileNotFoundException {
        File file = new File(indexFileName);
        Scanner in = new Scanner(new FileInputStream(file));
        //int lineNumber = 0;
        while (in.hasNextLine()) {
            //lineNumber++;
            String absolutePath = in.nextLine();

            if (!isValidMatchFile(absolutePath)) {
                //Log.prt("*** " + absolutePath);
                continue;
            }

            //If is a full file Name that contains path
            String fileName = absolutePath;
            if (absolutePath.contains("\\")) {
                fileName = absolutePath.substring(absolutePath.lastIndexOf("\\") + 1);
            }

            int itemIndex = itemIndex(fileName);
            int vsIndex = vsIndex(fileName);
            //itemIndex should >=6, vsIndex should >=11, eg. 1999 X MS A VS B.avi         
            if (itemIndex < 6 || vsIndex < 11)
                Log.prt("*** " + file.getAbsolutePath());
            else {
                MatchInfo matchInfo = new MatchInfo(absolutePath, fileName);
                getMatchInfo(matchInfo);
                matchNames.add(matchInfo);
            }
        }
        in.close();
    }

    public static void getMatchInfo(MatchInfo matchInfo) {
        //String s = "1997 All England MD Kang kyun-Ha tae Kwon VS Michael Sogaard-Jon holst.flv";
        String fileName = matchInfo.fileName;
        
        //1) Year
        String year = fileName.substring(0, 4), match = "*", item = "*";
        matchInfo.year = year;
        
        //2) Match        
        int index = itemIndex(fileName);
        if (index > -1) {
            match = fileName.substring(5, index).trim();
            matchInfo.match = match;
        }
        
        //3) Item
        if (index > -1) {
            item = fileName.substring(index + 1, index + 3);
            matchInfo.item = item;
        }
        
        //4) Players       
        int vsIndex = vsIndex(fileName);
        try {
            if (vsIndex > -1) {
                matchInfo.player1 = fileName.substring(index + 4, vsIndex).trim();
                
                int lastPoint = fileName.lastIndexOf(".");
                if (lastPoint > 0)
                    matchInfo.player2 = fileName.substring(vsIndex + 4, lastPoint);
            }
        } catch (Exception e) {
            Log.error(e);
            Log.prt(fileName);
        }

        //Log.prt("%s, %s, %s, %s, VS, %s\n", year, match, item, a, b);
        //String ss = String.format("%s, %s, %s, %s, VS, %s", year, match, item, a, b);
    }

    public static int vsIndex(String source) {
        source = source.toUpperCase();
        int index = source.indexOf(" VS ");
        return index;
    }

    public static int itemIndex(String source) {
        source = source.toUpperCase();
        int index = source.indexOf(" MS ");
        if (index < 0)
            index = source.indexOf(" WS ");
        if (index < 0)
            index = source.indexOf(" MD ");
        if (index < 0)
            index = source.indexOf(" WD ");
        if (index < 0)
            index = source.indexOf(" XD ");
        return index;
    }
}


public class MatchInfo implements Comparable<MatchInfo> {
    public String absolutePath;
    public String fileName;
    public boolean isMatch = true;

    public String year;
    public String match;
    public String item;
    public String player1;
    public String player2;
    public String description;

    public MatchInfo(String absolutePath, String fileName) {
        super();
        this.absolutePath = absolutePath;
        this.fileName = fileName;
    }

    public MatchInfo(String absolutePath, String fileName, boolean isMatch) {
        super();
        this.absolutePath = absolutePath;
        this.fileName = fileName;
        this.isMatch = isMatch;
    }

    public MatchInfo(String absolutePath, String fileName, boolean isMatch, String year, String match, String item, String player1,
            String player2, String description) {
        super();
        this.absolutePath = absolutePath;
        this.fileName = fileName;
        this.isMatch = isMatch;
        this.year = year;
        this.match = match;
        this.item = item;
        this.player1 = player1;
        this.player2 = player2;
        this.description = description;
    }

    @Override
    public String toString() {
        return fileName;
    }

    @Override
    public int compareTo(MatchInfo o) {
        return this.fileName.compareTo(o.fileName);
    }
}


public class MatchInfoCompator implements Comparator<MatchInfo> {

    @Override
    public int compare(MatchInfo o1, MatchInfo o2) {
        int x = BaseUtils.compareTo(o1.year, o2.year);
        if (x == 0)
            x = BaseUtils.compareTo(o1.match, o2.match);
        if (x == 0)
            x = BaseUtils.compareTo(o1.item, o2.item);
        if (x == 0)
            x = BaseUtils.compareTo(o1.player1, o2.player1);
        if (x == 0)
            x = BaseUtils.compareTo(o1.player2, o2.player2);
        
        return x;
    }
}
分享到:
评论

相关推荐

    matchfinder:用于与治疗对照组或连续治疗寻找匹配项的R包

    寻星工具matchfinder用于根据可预测性找到最佳匹配。 一般想法:如果可以预测要处理的单位和要控制的单位,则给定的匹配是不好的。主要概念首先让我们讨论匹配对的情况: 我们有一组T处理单元和C控制单元,总共N = T...

    MatchFinder:Matchfinder是一项服务,可以为给定的参与者,主题组和首选项找到公平的比赛

    MatchFinder 是一个旨在帮助组织者为参与者、特定主题或兴趣组以及个人偏好寻找公正比赛的服务。这个服务的核心功能是通过算法来匹配不同条件下的参与者,确保比赛的公平性和参与者的满意度。HTML 标签的提及可能...

    Unity3D三消类游戏水果乐园完整源码下载.zip

    源码中可能包含专门处理游戏网格和元素交换的类,如`GridManager`和`Tile`,以及执行消除逻辑的`MatchFinder`。 在游戏界面,Unity3D利用其内置的图形渲染系统来展示水果元素。每个水果通常被表示为一个游戏对象,...

    Java连连看项目工程(源码+类图+最终程序),Eclipse开发

    关键类可能包括`GameBoard`(游戏板)、`Pair`(匹配对)、`Player`(玩家)、`MatchFinder`(匹配查找器)等。源码分析可以帮助我们学习如何在Java中实现这些功能,例如使用条件语句和循环来实现匹配逻辑,事件监听...

    Unity连连看小游戏

    项目中提供的代码可能是组织良好的C#类,如GameBoard类用于表示游戏面板,MatchFinder类用于执行匹配查找,还有可能的GameManager类负责游戏逻辑和状态管理。这些类之间通过面向对象的设计模式进行通信,确保代码的...

    c源码 7-zip压缩算法代码.rar

    - `MatchFinder`类:实现字典匹配算法,寻找最佳匹配。 - `HuffmanEncoder`类:构建和使用霍夫曼编码。 - `Encoder`类:整体负责压缩流程,调用其他组件完成实际工作。 在阅读和学习7-Zip的C源码时,你可以关注以下...

    unity消除游戏源码-天天萌泡泡.rar

    在“天天萌泡泡”中,源码可能会包含一个自定义的`MatchFinder`类,它实现了这些匹配逻辑。 接着,游戏中的泡泡元素可能通过Unity的精灵(Sprite)系统进行管理。每个泡泡都是一个游戏对象(GameObject),包含了...

    GemsDrop:涉及相似宝石匹配行的Android游戏

    在Java编程中,游戏逻辑通常由多个类组成,包括但不限于GameBoard类(用于存储和管理游戏板状态)、Gem类(代表单个宝石对象)和MatchFinder类(负责查找并消除匹配的宝石)。这些类之间通过继承、组合和接口实现...

    dhbwStarbattle

    作为玩家,您可以通过MatchFinder-Settings搜索比赛。 每玩一场游戏,您就可以赚钱,花在商店的新物品上。 游戏本身是2D平台射击游戏,您可以在其中作为宇航员四处飞行,并且必须与对手玩家战斗。 计划额外的游戏...

    TestGame_Match3_CS

    在`TestGame_Match3_CS`中,我们可能会看到多个类,如`GameBoard`、`Tile`和`MatchFinder`等,它们分别代表游戏棋盘、单个方块和匹配查找算法。这些类通过继承、封装和多态性来实现复杂的游戏逻辑。 1. **游戏棋盘...

Global site tag (gtag.js) - Google Analytics