예제 #1
0
        public ArrayList getPossibleMoves(Desk desk, short currentPlayer, bool firstMoveOnly = false)
        {
            int[,] fields = desk.getFields();
            int       x, y, size = desk.getSize();
            ArrayList possibleMoves = new ArrayList();
            bool      forcedMoves   = false;
            short     opositePlayer = getOppositePlayer(currentPlayer);

            ArrayList playerFields     = null;
            ArrayList playerFieldsCopy = new ArrayList();

            //get current player fields
            if (currentPlayer == GameVar.PLAYER_WHITE)
            {
                playerFields = desk.getPlayerFields(GameVar.PLAYER_WHITE);
            }
            else if (currentPlayer == GameVar.PLAYER_BLACK)
            {
                playerFields = desk.getPlayerFields(GameVar.PLAYER_BLACK);
            }

            //make player_fields_copy, because while move inspected player_fields change
            foreach (var field in playerFields)
            {
                playerFieldsCopy.Add(field);
            }

            int playerFieldsCnt = playerFieldsCopy.Count;

            for (int i = 0; i < playerFieldsCnt; i++)           //loop player fields
            {
                x = (int)(playerFieldsCopy[i] as Array).GetValue(0);
                y = (int)(playerFieldsCopy[i] as Array).GetValue(1);

                //get possible jumps
                ArrayList possible_jumps = getPossibleJumps(desk, x, y, currentPlayer);
                if (possible_jumps.Count > 0)
                {
                    if (!forcedMoves)      //forced jump found, do not inspect normal moves anymore
                    {
                        possibleMoves.Clear();
                        forcedMoves = true;
                    }
                    foreach (Move jump in possible_jumps)
                    {
                        addMove(possibleMoves, jump);      //add jump move to results
                    }
                    if (firstMoveOnly)
                    {
                        return(possibleMoves);
                    }
                }

                if (!forcedMoves)      //inspect normal moves only when no forced jump found
                {
                    int newX = 0;
                    int newY = 0;

                    //loop normal moves directions
                    for (int k = 0; k < directionsNormal[currentPlayer].Length; k++)
                    {
                        if (directionsNormal[currentPlayer][k] == GameVar.DIR_UP)
                        {
                            newX = x;
                            newY = y + 1;
                        }
                        else if (directionsNormal[currentPlayer][k] == GameVar.DIR_UP_LEFT)
                        {
                            newX = x - 1;
                            newY = y + 1;
                        }
                        else if (directionsNormal[currentPlayer][k] == GameVar.DIR_UP_RIGHT)
                        {
                            newX = x + 1;
                            newY = y + 1;
                        }
                        else if (directionsNormal[currentPlayer][k] == GameVar.DIR_DOWN)
                        {
                            newX = x;
                            newY = y - 1;
                        }
                        else if (directionsNormal[currentPlayer][k] == GameVar.DIR_DOWN_LEFT)
                        {
                            newX = x - 1;
                            newY = y - 1;
                        }
                        else if (directionsNormal[currentPlayer][k] == GameVar.DIR_DOWN_RIGHT)
                        {
                            newX = x + 1;
                            newY = y - 1;
                        }
                        if (GameVar.isValidField(newX, newY, deskSize) && fields[newX, newY] == GameVar.FIELD_EMPTY)
                        {
                            addMove(possibleMoves, x, y, newX, newY);      //add normal move to results

                            if (firstMoveOnly)
                            {
                                return(possibleMoves);
                            }
                        }
                    }
                }
            }
            return(possibleMoves);
        }
예제 #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);
        }