statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:51:17 +0100
branchstatistics-and-elo
changeset 179 c5fbddc4c590
parent 178 4b78d4f028b3
child 264 d60370059c3c
permissions -rw-r--r--
Renaming Board.board2HashCode to getCode()
jaroslav@178
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2010 Martin Rexa
jaroslav@178
    25
 */
jaroslav@178
    26
jaroslav@178
    27
package cz.xelfi.quoridor.statistics;
jaroslav@178
    28
jaroslav@178
    29
import cz.xelfi.quoridor.Board;
jaroslav@178
    30
import cz.xelfi.quoridor.Move;
jaroslav@178
    31
import cz.xelfi.quoridor.webidor.Game;
jaroslav@178
    32
import cz.xelfi.quoridor.webidor.CommentedMove;
jaroslav@178
    33
import cz.xelfi.quoridor.IllegalPositionException;
jaroslav@178
    34
import java.util.Map;
jaroslav@178
    35
import java.util.HashMap;
jaroslav@178
    36
jaroslav@178
    37
/**
jaroslav@178
    38
 *
jaroslav@178
    39
 * @author Martin Rexa
jaroslav@178
    40
 */
jaroslav@178
    41
public class OpeningTree {
jaroslav@178
    42
    Map<String, OpeningTreeNode> nodes = new HashMap<String, OpeningTreeNode>();
jaroslav@178
    43
    OpeningTreeNode root;
jaroslav@178
    44
jaroslav@178
    45
    public OpeningTree(){
jaroslav@178
    46
        root = new OpeningTreeNode("ROOT");
jaroslav@178
    47
        nodes.put("ROOT", root);
jaroslav@178
    48
    }
jaroslav@178
    49
jaroslav@178
    50
    public OpeningTreeNode getRoot(){
jaroslav@178
    51
        return root;
jaroslav@178
    52
    }
jaroslav@178
    53
jaroslav@178
    54
    public OpeningTreeNode getNode(String hashCode){
jaroslav@178
    55
        return nodes.get(hashCode);
jaroslav@178
    56
    }
jaroslav@178
    57
jaroslav@178
    58
    public void processGame(Game game) throws IllegalPositionException{
jaroslav@178
    59
        OpeningTreeNode node = root;
jaroslav@178
    60
        OpeningTreeNode parentNode;
jaroslav@178
    61
        OpeningTreeNode mirrorNode = root;
jaroslav@178
    62
        OpeningTreeNode mirrorParentNode;
jaroslav@178
    63
        Board board = Board.empty();
jaroslav@178
    64
        Board mirrorBoard = Board.empty();
jaroslav@178
    65
        root.addGame(game);
jaroslav@178
    66
        for(CommentedMove move: game.getMoves()){
jaroslav@178
    67
            if(!move.getMove().equals(Move.RESIGN)){
jaroslav@178
    68
                parentNode = node;
jaroslav@178
    69
                mirrorParentNode = mirrorNode;
jaroslav@178
    70
                board = board.apply(move.getMove());
jaroslav@178
    71
                mirrorBoard = mirrorBoard.apply(move.getMove().getMirrorMove());
jaroslav@179
    72
                String hashCode = board.getCode();
jaroslav@179
    73
                String mirrorHashCode = mirrorBoard.getCode();
jaroslav@178
    74
                node = nodes.get(hashCode);
jaroslav@178
    75
                if(node == null){
jaroslav@178
    76
                    node = new OpeningTreeNode(hashCode);
jaroslav@178
    77
                    nodes.put(hashCode, node);
jaroslav@178
    78
                }
jaroslav@178
    79
                node.addGame(game);
jaroslav@178
    80
                mirrorNode = nodes.get(mirrorHashCode);
jaroslav@178
    81
                if(mirrorHashCode.equals(hashCode)){
jaroslav@178
    82
                    if(mirrorNode == null){
jaroslav@178
    83
                        mirrorNode = node;
jaroslav@178
    84
                    }
jaroslav@178
    85
                }else{
jaroslav@178
    86
                    if(mirrorNode == null){
jaroslav@178
    87
                        mirrorNode = new OpeningTreeNode(mirrorHashCode);
jaroslav@178
    88
                        nodes.put(mirrorHashCode, mirrorNode);
jaroslav@178
    89
                    }
jaroslav@178
    90
                    mirrorNode.addGame(game);
jaroslav@178
    91
                }
jaroslav@178
    92
                if(parentNode != null)
jaroslav@178
    93
                    parentNode.addChild(move.getMove(),node);
jaroslav@178
    94
                if(mirrorParentNode != null){
jaroslav@178
    95
                    if(parentNode.equals(mirrorParentNode) && node.equals(mirrorNode))
jaroslav@178
    96
                        ;
jaroslav@178
    97
                    else
jaroslav@178
    98
                        mirrorParentNode.addChild(move.getMove().getMirrorMove(),mirrorNode);
jaroslav@178
    99
                }
jaroslav@178
   100
            }
jaroslav@178
   101
        }
jaroslav@178
   102
        node.addFinishedGame(game);
jaroslav@178
   103
        if(!node.equals(mirrorNode))
jaroslav@178
   104
            mirrorNode.addFinishedGame(game);
jaroslav@178
   105
    }
jaroslav@178
   106
jaroslav@178
   107
}