statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 179 c5fbddc4c590
child 266 15fcdfc4cd4a
permissions -rw-r--r--
Changing headers to GPLv3
     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 package cz.xelfi.quoridor.statistics;
    19 
    20 import cz.xelfi.quoridor.Board;
    21 import cz.xelfi.quoridor.Move;
    22 import cz.xelfi.quoridor.webidor.Game;
    23 import cz.xelfi.quoridor.webidor.CommentedMove;
    24 import cz.xelfi.quoridor.IllegalPositionException;
    25 import java.util.Map;
    26 import java.util.HashMap;
    27 
    28 /**
    29  *
    30  * @author Martin Rexa
    31  */
    32 public class OpeningTree {
    33     Map<String, OpeningTreeNode> nodes = new HashMap<String, OpeningTreeNode>();
    34     OpeningTreeNode root;
    35 
    36     public OpeningTree(){
    37         root = new OpeningTreeNode("ROOT");
    38         nodes.put("ROOT", root);
    39     }
    40 
    41     public OpeningTreeNode getRoot(){
    42         return root;
    43     }
    44 
    45     public OpeningTreeNode getNode(String hashCode){
    46         return nodes.get(hashCode);
    47     }
    48 
    49     public void processGame(Game game) throws IllegalPositionException{
    50         OpeningTreeNode node = root;
    51         OpeningTreeNode parentNode;
    52         OpeningTreeNode mirrorNode = root;
    53         OpeningTreeNode mirrorParentNode;
    54         Board board = Board.empty();
    55         Board mirrorBoard = Board.empty();
    56         root.addGame(game);
    57         for(CommentedMove move: game.getMoves()){
    58             if(!move.getMove().equals(Move.RESIGN)){
    59                 parentNode = node;
    60                 mirrorParentNode = mirrorNode;
    61                 board = board.apply(move.getMove());
    62                 mirrorBoard = mirrorBoard.apply(move.getMove().getMirrorMove());
    63                 String hashCode = board.getCode();
    64                 String mirrorHashCode = mirrorBoard.getCode();
    65                 node = nodes.get(hashCode);
    66                 if(node == null){
    67                     node = new OpeningTreeNode(hashCode);
    68                     nodes.put(hashCode, node);
    69                 }
    70                 node.addGame(game);
    71                 mirrorNode = nodes.get(mirrorHashCode);
    72                 if(mirrorHashCode.equals(hashCode)){
    73                     if(mirrorNode == null){
    74                         mirrorNode = node;
    75                     }
    76                 }else{
    77                     if(mirrorNode == null){
    78                         mirrorNode = new OpeningTreeNode(mirrorHashCode);
    79                         nodes.put(mirrorHashCode, mirrorNode);
    80                     }
    81                     mirrorNode.addGame(game);
    82                 }
    83                 if(parentNode != null)
    84                     parentNode.addChild(move.getMove(),node);
    85                 if(mirrorParentNode != null){
    86                     if(parentNode.equals(mirrorParentNode) && node.equals(mirrorNode))
    87                         ;
    88                     else
    89                         mirrorParentNode.addChild(move.getMove().getMirrorMove(),mirrorNode);
    90                 }
    91             }
    92         }
    93         node.addFinishedGame(game);
    94         if(!node.equals(mirrorNode))
    95             mirrorNode.addFinishedGame(game);
    96     }
    97 
    98 }