1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java Thu Jan 07 22:34:17 2010 +0100
1.3 @@ -0,0 +1,107 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2010 Martin Rexa
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.statistics;
1.31 +
1.32 +import cz.xelfi.quoridor.Board;
1.33 +import cz.xelfi.quoridor.Move;
1.34 +import cz.xelfi.quoridor.webidor.Game;
1.35 +import cz.xelfi.quoridor.webidor.CommentedMove;
1.36 +import cz.xelfi.quoridor.IllegalPositionException;
1.37 +import java.util.Map;
1.38 +import java.util.HashMap;
1.39 +
1.40 +/**
1.41 + *
1.42 + * @author Martin Rexa
1.43 + */
1.44 +public class OpeningTree {
1.45 + Map<String, OpeningTreeNode> nodes = new HashMap<String, OpeningTreeNode>();
1.46 + OpeningTreeNode root;
1.47 +
1.48 + public OpeningTree(){
1.49 + root = new OpeningTreeNode("ROOT");
1.50 + nodes.put("ROOT", root);
1.51 + }
1.52 +
1.53 + public OpeningTreeNode getRoot(){
1.54 + return root;
1.55 + }
1.56 +
1.57 + public OpeningTreeNode getNode(String hashCode){
1.58 + return nodes.get(hashCode);
1.59 + }
1.60 +
1.61 + public void processGame(Game game) throws IllegalPositionException{
1.62 + OpeningTreeNode node = root;
1.63 + OpeningTreeNode parentNode;
1.64 + OpeningTreeNode mirrorNode = root;
1.65 + OpeningTreeNode mirrorParentNode;
1.66 + Board board = Board.empty();
1.67 + Board mirrorBoard = Board.empty();
1.68 + root.addGame(game);
1.69 + for(CommentedMove move: game.getMoves()){
1.70 + if(!move.getMove().equals(Move.RESIGN)){
1.71 + parentNode = node;
1.72 + mirrorParentNode = mirrorNode;
1.73 + board = board.apply(move.getMove());
1.74 + mirrorBoard = mirrorBoard.apply(move.getMove().getMirrorMove());
1.75 + String hashCode = Board.board2HashCode(board);
1.76 + String mirrorHashCode = Board.board2HashCode(mirrorBoard);
1.77 + node = nodes.get(hashCode);
1.78 + if(node == null){
1.79 + node = new OpeningTreeNode(hashCode);
1.80 + nodes.put(hashCode, node);
1.81 + }
1.82 + node.addGame(game);
1.83 + mirrorNode = nodes.get(mirrorHashCode);
1.84 + if(mirrorHashCode.equals(hashCode)){
1.85 + if(mirrorNode == null){
1.86 + mirrorNode = node;
1.87 + }
1.88 + }else{
1.89 + if(mirrorNode == null){
1.90 + mirrorNode = new OpeningTreeNode(mirrorHashCode);
1.91 + nodes.put(mirrorHashCode, mirrorNode);
1.92 + }
1.93 + mirrorNode.addGame(game);
1.94 + }
1.95 + if(parentNode != null)
1.96 + parentNode.addChild(move.getMove(),node);
1.97 + if(mirrorParentNode != null){
1.98 + if(parentNode.equals(mirrorParentNode) && node.equals(mirrorNode))
1.99 + ;
1.100 + else
1.101 + mirrorParentNode.addChild(move.getMove().getMirrorMove(),mirrorNode);
1.102 + }
1.103 + }
1.104 + }
1.105 + node.addFinishedGame(game);
1.106 + if(!node.equals(mirrorNode))
1.107 + mirrorNode.addFinishedGame(game);
1.108 + }
1.109 +
1.110 +}