statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 09:46:43 +0200
changeset 266 15fcdfc4cd4a
parent 264 d60370059c3c
permissions -rw-r--r--
Using maven-license-plugin and updating all missing headers
     1 /**
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Martin Rexa
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.statistics;
    20 
    21 import cz.xelfi.quoridor.Board;
    22 import cz.xelfi.quoridor.Move;
    23 import cz.xelfi.quoridor.webidor.Game;
    24 import cz.xelfi.quoridor.webidor.CommentedMove;
    25 import cz.xelfi.quoridor.IllegalPositionException;
    26 import java.util.Map;
    27 import java.util.HashMap;
    28 
    29 /**
    30  *
    31  * @author Martin Rexa
    32  */
    33 public class OpeningTree {
    34     Map<String, OpeningTreeNode> nodes = new HashMap<String, OpeningTreeNode>();
    35     OpeningTreeNode root;
    36 
    37     public OpeningTree(){
    38         root = new OpeningTreeNode("ROOT");
    39         nodes.put("ROOT", root);
    40     }
    41 
    42     public OpeningTreeNode getRoot(){
    43         return root;
    44     }
    45 
    46     public OpeningTreeNode getNode(String hashCode){
    47         return nodes.get(hashCode);
    48     }
    49 
    50     public void processGame(Game game) throws IllegalPositionException{
    51         OpeningTreeNode node = root;
    52         OpeningTreeNode parentNode;
    53         OpeningTreeNode mirrorNode = root;
    54         OpeningTreeNode mirrorParentNode;
    55         Board board = Board.empty();
    56         Board mirrorBoard = Board.empty();
    57         root.addGame(game);
    58         for(CommentedMove move: game.getMoves()){
    59             if(!move.getMove().equals(Move.RESIGN)){
    60                 parentNode = node;
    61                 mirrorParentNode = mirrorNode;
    62                 board = board.apply(move.getMove());
    63                 mirrorBoard = mirrorBoard.apply(move.getMove().getMirrorMove());
    64                 String hashCode = board.getCode();
    65                 String mirrorHashCode = mirrorBoard.getCode();
    66                 node = nodes.get(hashCode);
    67                 if(node == null){
    68                     node = new OpeningTreeNode(hashCode);
    69                     nodes.put(hashCode, node);
    70                 }
    71                 node.addGame(game);
    72                 mirrorNode = nodes.get(mirrorHashCode);
    73                 if(mirrorHashCode.equals(hashCode)){
    74                     if(mirrorNode == null){
    75                         mirrorNode = node;
    76                     }
    77                 }else{
    78                     if(mirrorNode == null){
    79                         mirrorNode = new OpeningTreeNode(mirrorHashCode);
    80                         nodes.put(mirrorHashCode, mirrorNode);
    81                     }
    82                     mirrorNode.addGame(game);
    83                 }
    84                 if(parentNode != null)
    85                     parentNode.addChild(move.getMove(),node);
    86                 if(mirrorParentNode != null){
    87                     if(parentNode.equals(mirrorParentNode) && node.equals(mirrorNode))
    88                         ;
    89                     else
    90                         mirrorParentNode.addChild(move.getMove().getMirrorMove(),mirrorNode);
    91                 }
    92             }
    93         }
    94         node.addFinishedGame(game);
    95         if(!node.equals(mirrorNode))
    96             mirrorNode.addFinishedGame(game);
    97     }
    98 
    99 }