2 * Quoridor server and related libraries
3 * Copyright (C) 2009-2010 Martin Rexa
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.
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.
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/.
18 package cz.xelfi.quoridor.statistics;
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;
26 import java.util.HashMap;
32 public class OpeningTree {
33 Map<String, OpeningTreeNode> nodes = new HashMap<String, OpeningTreeNode>();
37 root = new OpeningTreeNode("ROOT");
38 nodes.put("ROOT", root);
41 public OpeningTreeNode getRoot(){
45 public OpeningTreeNode getNode(String hashCode){
46 return nodes.get(hashCode);
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();
57 for(CommentedMove move: game.getMoves()){
58 if(!move.getMove().equals(Move.RESIGN)){
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);
67 node = new OpeningTreeNode(hashCode);
68 nodes.put(hashCode, node);
71 mirrorNode = nodes.get(mirrorHashCode);
72 if(mirrorHashCode.equals(hashCode)){
73 if(mirrorNode == null){
77 if(mirrorNode == null){
78 mirrorNode = new OpeningTreeNode(mirrorHashCode);
79 nodes.put(mirrorHashCode, mirrorNode);
81 mirrorNode.addGame(game);
83 if(parentNode != null)
84 parentNode.addChild(move.getMove(),node);
85 if(mirrorParentNode != null){
86 if(parentNode.equals(mirrorParentNode) && node.equals(mirrorNode))
89 mirrorParentNode.addChild(move.getMove().getMirrorMove(),mirrorNode);
93 node.addFinishedGame(game);
94 if(!node.equals(mirrorNode))
95 mirrorNode.addFinishedGame(game);