Esempio n. 1
0
        public void loadXML(String filename)
        {
            moves = new ArrayList();
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode     xmlNode, fieldsOverNode, fieldsRemoveNode;
            ArrayList   playerData;

            int[] from, to;

            // load XML
            xmlDoc.Load(filename);
            xmlNode = xmlDoc.DocumentElement;
            if (!xmlNode.Name.Equals("game"))
            {
                throw new Exception("Wrong start XML node");
            }

            // read players
            playerData          = readXMLPlayerNode(xmlNode.ChildNodes[0], "playerWhite");
            playerWhiteName     = playerData[0].ToString();
            playerWhiteControls = getPlayerControlsFromString((String)playerData[1]);
            playerData          = readXMLPlayerNode(xmlNode.ChildNodes[1], "playerBlack");
            playerBlackName     = playerData[0].ToString();
            playerBlackControls = getPlayerControlsFromString((String)playerData[1]);

            // read moves
            xmlNode = xmlNode.ChildNodes[2];
            if (!xmlNode.Name.Equals("moves"))
            {
                throw new Exception("Wrong moves XML node");
            }
            foreach (XmlNode moveNode in xmlNode.ChildNodes)
            {
                from = readXMLPositionNode(moveNode.ChildNodes[0], "positionFrom");
                to   = readXMLPositionNode(moveNode.ChildNodes[1], "positionTo");
                Move move = new Move(from[0], from[1], to[0], to[1]);

                fieldsOverNode = moveNode.ChildNodes[2];
                if (!fieldsOverNode.Name.Equals("fieldsOver"))
                {
                    throw new Exception("Wrong fieldsOver XML node");
                }
                foreach (XmlNode fieldOverNode in fieldsOverNode.ChildNodes)
                {
                    from = readXMLPositionNode(fieldOverNode, "position");
                    move.addOverField(from[0], from[1]);
                }

                fieldsRemoveNode = moveNode.ChildNodes[3];
                if (!fieldsRemoveNode.Name.Equals("fieldsRemove"))
                {
                    throw new Exception("Wrong fieldsOver XML node");
                }
                foreach (XmlNode fieldRemoveNode in fieldsRemoveNode.ChildNodes)
                {
                    from = readXMLPositionNode(fieldRemoveNode, "position");
                    move.addRemoveField(from[0], from[1]);
                }
                moves.Add(move);
            }
        }
Esempio n. 2
0
        public ArrayList getPossibleJumps(Desk desk, int x, int y, short currentPlayer)
        {
            ArrayList possibleJumps = new ArrayList();
            ArrayList jumpsFound    = new ArrayList();
            Move      newMove;

            int[,] fields = desk.getFields();
            int xIncrement = 0;
            int yIncrement = 0;

            int[] targetCoords;
            short opposite_player = getOppositePlayer(currentPlayer);

            //loop all possible diretions for jump
            for (int i = 0; i < directionsJump.Length; i++)
            {
                if (directionsJump[i] == GameVar.DIR_UP_LEFT)
                {
                    xIncrement = -1;
                    yIncrement = 1;
                }
                else if (directionsJump[i] == GameVar.DIR_UP)
                {
                    xIncrement = 0;
                    yIncrement = 1;
                }
                else if (directionsJump[i] == GameVar.DIR_UP_RIGHT)
                {
                    xIncrement = 1;
                    yIncrement = 1;
                }
                else if (directionsJump[i] == GameVar.DIR_RIGHT)
                {
                    xIncrement = 1;
                    yIncrement = 0;
                }
                else if (directionsJump[i] == GameVar.DIR_LEFT)
                {
                    xIncrement = -1;
                    yIncrement = 0;
                }
                else if (directionsJump[i] == GameVar.DIR_DOWN_LEFT)
                {
                    xIncrement = -1;
                    yIncrement = -1;
                }
                else if (directionsJump[i] == GameVar.DIR_DOWN)
                {
                    xIncrement = 0;
                    yIncrement = -1;
                }
                else if (directionsJump[i] == GameVar.DIR_DOWN_RIGHT)
                {
                    xIncrement = 1;
                    yIncrement = -1;
                }

                //get target coords of possible jump
                targetCoords = canJump(fields, x, y, xIncrement, yIncrement, currentPlayer);
                if (targetCoords == null)      //no jump possible
                {
                    continue;
                }
                int targetCoordsX = targetCoords[0];
                int targetCoordsY = targetCoords[1];

                //create new (master) move, make it on desk and inspect again from new position - in case multiple jump
                newMove = new Move(x, y, targetCoordsX, targetCoordsY);
                newMove.addRemoveField(targetCoordsX - xIncrement, targetCoordsY - yIncrement);
                desk.makeMove(newMove);
                jumpsFound = getPossibleJumps(desk, targetCoordsX, targetCoordsY, currentPlayer);
                desk.undoMove(newMove);

                //multiple jump found
                if (jumpsFound.Count > 0)
                {
                    foreach (Move jump in jumpsFound)      //loop multiple jump fractals, add for master jump
                    {
                        jump.addOverField(jump.getFrom()[0], jump.getFrom()[1]);
                        jump.addRemoveField(jump.getFrom()[0] - xIncrement, jump.getFrom()[1] - yIncrement);
                        jump.setFrom(x, y);
                        possibleJumps.Add(jump);
                    }
                }
                else
                {
                    possibleJumps.Add(newMove);       //no (more) multijumps, add master jump to results
                }
            }
            return(possibleJumps);
        }