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/.
19 package cz.xelfi.quoridor.statistics;
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;
27 import java.util.HashMap;
33 public class OpeningTree {
34 Map<String, OpeningTreeNode> nodes = new HashMap<String, OpeningTreeNode>();
38 root = new OpeningTreeNode("ROOT");
39 nodes.put("ROOT", root);
42 public OpeningTreeNode getRoot(){
46 public OpeningTreeNode getNode(String hashCode){
47 return nodes.get(hashCode);
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();
58 for(CommentedMove move: game.getMoves()){
59 if(!move.getMove().equals(Move.RESIGN)){
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);
68 node = new OpeningTreeNode(hashCode);
69 nodes.put(hashCode, node);
72 mirrorNode = nodes.get(mirrorHashCode);
73 if(mirrorHashCode.equals(hashCode)){
74 if(mirrorNode == null){
78 if(mirrorNode == null){
79 mirrorNode = new OpeningTreeNode(mirrorHashCode);
80 nodes.put(mirrorHashCode, mirrorNode);
82 mirrorNode.addGame(game);
84 if(parentNode != null)
85 parentNode.addChild(move.getMove(),node);
86 if(mirrorParentNode != null){
87 if(parentNode.equals(mirrorParentNode) && node.equals(mirrorNode))
90 mirrorParentNode.addChild(move.getMove().getMirrorMove(),mirrorNode);
94 node.addFinishedGame(game);
95 if(!node.equals(mirrorNode))
96 mirrorNode.addFinishedGame(game);