コード例 #1
0
ファイル: Desk.cs プロジェクト: alestrunda/osetinskadama
        public void setField(int x, int y, short fieldValue)
        {
            if (!GameVar.isValidField(x, y, size))
            {
                throw new Exception("Not valid field " + x + " " + y + " for size" + size);
            }

            short player = 0;

            if (fieldValue == GameVar.FIELD_WHITE)
            {
                player = GameVar.PLAYER_WHITE;
            }
            else if (fieldValue == GameVar.FIELD_BLACK)
            {
                player = GameVar.PLAYER_BLACK;
            }
            else
            {
                throw new Exception("Player not recognized");
            }

            //set desk field
            fields[x, y] = fieldValue;
            addPlayerField(x, y, player);
        }
コード例 #2
0
ファイル: GameRules.cs プロジェクト: alestrunda/osetinskadama
        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);
        }