Ejemplo n.º 1
0
        private List <Spot> KingSpots(Spot[,] map, Spot currentSpot, Chess c)
        {
            List <Spot> spots = new List <Spot>();

            if (currentSpot.index.x < map.GetLength(0) - 1 && !IsFriendly(map[currentSpot.index.x + 1, currentSpot.index.y]))
            {
                spots.Add(map[currentSpot.index.x + 1, currentSpot.index.y]);
            }
            if (currentSpot.index.x > 0 && !IsFriendly(map[currentSpot.index.x - 1, currentSpot.index.y]))
            {
                spots.Add(map[currentSpot.index.x - 1, currentSpot.index.y]);
            }
            if (currentSpot.index.y < map.GetLength(1) - 1 && !IsFriendly(map[currentSpot.index.x, currentSpot.index.y + 1]))
            {
                spots.Add(map[currentSpot.index.x, currentSpot.index.y + 1]);
            }
            if (currentSpot.index.y > 0 && !IsFriendly(map[currentSpot.index.x, currentSpot.index.y - 1]))
            {
                spots.Add(map[currentSpot.index.x, currentSpot.index.y - 1]);
            }

            if (currentSpot.index.x < map.GetLength(0) - 1 && currentSpot.index.y < map.GetLength(1) - 1 &&
                !IsFriendly(map[currentSpot.index.x + 1, currentSpot.index.y + 1]))
            {
                spots.Add(map[currentSpot.index.x + 1, currentSpot.index.y + 1]);
            }
            if (currentSpot.index.x > 0 && currentSpot.index.y < map.GetLength(1) - 1 &&
                !IsFriendly(map[currentSpot.index.x - 1, currentSpot.index.y + 1]))
            {
                spots.Add(map[currentSpot.index.x - 1, currentSpot.index.y + 1]);
            }
            if (currentSpot.index.x < map.GetLength(0) - 1 && currentSpot.index.y > 0 &&
                !IsFriendly(map[currentSpot.index.x + 1, currentSpot.index.y - 1]))
            {
                spots.Add(map[currentSpot.index.x + 1, currentSpot.index.y - 1]);
            }
            if (currentSpot.index.x > 0 && currentSpot.index.y > 0 &&
                !IsFriendly(map[currentSpot.index.x - 1, currentSpot.index.y - 1]))
            {
                spots.Add(map[currentSpot.index.x - 1, currentSpot.index.y - 1]);
            }

            if (white)
            {
                if (currentSpot.index.x == 3 && currentSpot.index.y == 0 &&
                    map[map.GetLength(0) - 1, 0].occupier.role == Role.rook &&
                    IsFriendly(map[map.GetLength(0) - 1, 0]))
                {
                    if (!map[map.GetLength(0) - 1, 0].occupier.IsThreatened(c) &&
                        !IsThreatened(c))
                    {
                        spots.Add(map[map.GetLength(0) - 2, 0]);
                    }
                }
                if (currentSpot.index.x == 3 && currentSpot.index.y == 0 &&
                    map[0, 0].occupier.role == Role.rook &&
                    IsFriendly(map[0, 0]))
                {
                    if (!map[0, 0].occupier.IsThreatened(c) &&
                        !IsThreatened(c))
                    {
                        spots.Add(map[1, 0]);
                    }
                }
            }
            else
            {
                if (currentSpot.index.x == 4 && currentSpot.index.y == map.GetLength(1) - 1 &&
                    map[map.GetLength(0) - 1, map.GetLength(1) - 1].occupier.role == Role.rook &&
                    IsFriendly(map[map.GetLength(0) - 1, map.GetLength(1) - 1]))
                {
                    if (!map[map.GetLength(0) - 1, map.GetLength(1) - 1].occupier.IsThreatened(c) &&
                        !IsThreatened(c))
                    {
                        spots.Add(map[map.GetLength(0) - 2, map.GetLength(1) - 1]);
                    }
                }
                if (currentSpot.index.x == 4 && currentSpot.index.y == map.GetLength(1) - 1 &&
                    map[0, map.GetLength(1) - 1].occupier.role == Role.rook &&
                    IsFriendly(map[0, map.GetLength(1) - 1]))
                {
                    if (!map[0, map.GetLength(1) - 1].occupier.IsThreatened(c) &&
                        !IsThreatened(c))
                    {
                        spots.Add(map[1, map.GetLength(1) - 1]);
                    }
                }
            }

            return(spots);
        }
Ejemplo n.º 2
0
        private List <Spot> CrossSpots(Spot[,] map, Spot s)
        {
            List <Spot> list = new List <Spot>();

            bool[] dirOccupied = new bool[4];
            for (int i = 1; i < 8; i++)
            {
                try
                {
                    if (!dirOccupied[0])
                    {
                        if (!IsFriendly(map[s.index.x + i, s.index.y]))
                        {
                            list.Add(map[s.index.x + i, s.index.y]);
                        }
                        if (map[s.index.x + i, s.index.y].IsOccupied())
                        {
                            dirOccupied[0] = true;
                        }
                    }
                }
                catch {}
                try
                {
                    if (!dirOccupied[1])
                    {
                        if (!IsFriendly(map[s.index.x - i, s.index.y]))
                        {
                            list.Add(map[s.index.x - i, s.index.y]);
                        }
                        if (map[s.index.x - i, s.index.y].IsOccupied())
                        {
                            dirOccupied[1] = true;
                        }
                    }
                }
                catch {}
                try
                {
                    if (!dirOccupied[2])
                    {
                        if (!IsFriendly(map[s.index.x, s.index.y - i]))
                        {
                            list.Add(map[s.index.x, s.index.y - i]);
                        }
                        if (map[s.index.x, s.index.y - i].IsOccupied())
                        {
                            dirOccupied[2] = true;
                        }
                    }
                }
                catch {}
                try
                {
                    if (!dirOccupied[3])
                    {
                        if (!IsFriendly(map[s.index.x, s.index.y + i]))
                        {
                            list.Add(map[s.index.x, s.index.y + i]);
                        }
                        if (map[s.index.x, s.index.y + i].IsOccupied())
                        {
                            dirOccupied[3] = true;
                        }
                    }
                }
                catch {}
            }
            return(list);
        }
Ejemplo n.º 3
0
        private Spot MoveCursor(ConsoleKey key, List <Spot> spots)
        {
            float       distance = float.PositiveInfinity;
            List <Spot> wanted   = new List <Spot>();
            Spot        bestSpot = markedSpot;

            switch (key)
            {
            case ConsoleKey.RightArrow:
                for (int i = 0; i < spots.Count; i++)
                {
                    if (spots[i].index.x > markedSpot.index.x)
                    {
                        wanted.Add(spots[i]);
                    }
                }
                for (int i = 0; i < wanted.Count; i++)
                {
                    if (markedSpot.position.Distance(wanted[i].position) < distance)
                    {
                        distance = markedSpot.position.Distance(wanted[i].position);
                        bestSpot = wanted[i];
                    }
                }
                return(bestSpot);

            case ConsoleKey.LeftArrow:
                for (int i = 0; i < spots.Count; i++)
                {
                    if (spots[i].index.x < markedSpot.index.x)
                    {
                        wanted.Add(spots[i]);
                    }
                }
                for (int i = 0; i < wanted.Count; i++)
                {
                    if (markedSpot.position.Distance(wanted[i].position) < distance)
                    {
                        distance = markedSpot.position.Distance(wanted[i].position);
                        bestSpot = wanted[i];
                    }
                }
                return(bestSpot);

            case ConsoleKey.UpArrow:
                for (int i = 0; i < spots.Count; i++)
                {
                    if (spots[i].index.y < markedSpot.index.y)
                    {
                        wanted.Add(spots[i]);
                    }
                }
                for (int i = 0; i < wanted.Count; i++)
                {
                    if (markedSpot.position.Distance(wanted[i].position) < distance)
                    {
                        distance = markedSpot.position.Distance(wanted[i].position);
                        bestSpot = wanted[i];
                    }
                }
                return(bestSpot);

            case ConsoleKey.DownArrow:
                for (int i = 0; i < spots.Count; i++)
                {
                    if (spots[i].index.y > markedSpot.index.y)
                    {
                        wanted.Add(spots[i]);
                    }
                }
                for (int i = 0; i < wanted.Count; i++)
                {
                    if (markedSpot.position.Distance(wanted[i].position) < distance)
                    {
                        distance = markedSpot.position.Distance(wanted[i].position);
                        bestSpot = wanted[i];
                    }
                }
                return(bestSpot);
            }
            return(bestSpot);
        }
Ejemplo n.º 4
0
        private void HandleInput(ConsoleKey key)
        {
            List <Spot> chooseAbles;

            if (currentState == State.choosingPlayer)
            {
                chooseAbles = (whiteTurn) ? chooseAbles = teams[0].AvailablePlayers() : chooseAbles = teams[1].AvailablePlayers();
            }
            else
            {
                chooseAbles = currentSoldier.AvailableSpots(this);
            }
            bool hasChoice = (chooseAbles.Count == 0) ? false : true;

            if (hasChoice)
            {
                markedSpot = (markedSpot == null) ? chooseAbles[0] : markedSpot;
            }



            switch (key)
            {
            case ConsoleKey.RightArrow:
                if (hasChoice)
                {
                    if (choiceIndex + 1 == chooseAbles.Count)
                    {
                        choiceIndex = 0;
                    }
                    else
                    {
                        choiceIndex++;
                    }
                    MarkSpot(MoveCursor(key, chooseAbles));
                    //MarkSpot(chooseAbles[choiceIndex]);
                }
                else
                {
                    Console.SetCursorPosition(0, 0);
                }
                break;

            case ConsoleKey.LeftArrow:
                if (hasChoice)
                {
                    if (choiceIndex - 1 < 0)
                    {
                        choiceIndex = chooseAbles.Count - 1;
                    }
                    else
                    {
                        choiceIndex--;
                    }
                    MarkSpot(MoveCursor(key, chooseAbles));
                    //MarkSpot(chooseAbles[choiceIndex]);
                }
                else
                {
                    Console.SetCursorPosition(0, 0);
                }
                break;

            case ConsoleKey.UpArrow:
                if (hasChoice)
                {
                    MarkSpot(MoveCursor(key, chooseAbles));
                }
                break;

            case ConsoleKey.DownArrow:
                if (hasChoice)
                {
                    MarkSpot(MoveCursor(key, chooseAbles));
                }
                break;

            case ConsoleKey.Enter:
                if (markedSpot == null)
                {
                }
                else if (currentState == State.choosingPlayer)
                {
                    if (markedSpot.occupier == null)
                    {
                        Console.WriteLine("no occupier");
                    }
                    currentSoldier = markedSpot.occupier;
                    currentState   = State.choosingAct;
                    UnMark();
                }
                else if (currentState == State.choosingAct)
                {
                    if (map[markedSpot.index.x, markedSpot.index.y].IsOccupied())
                    {
                        map[markedSpot.index.x, markedSpot.index.y].occupier.alive = false;
                    }

                    map[currentSoldier.currentSpot.index.x, currentSoldier.currentSpot.index.y].occupier = null;
                    map[currentSoldier.currentSpot.index.x, currentSoldier.currentSpot.index.y].ClearRole();
                    map[markedSpot.index.x, markedSpot.index.y].occupier = currentSoldier;

                    currentSoldier.currentSpot = map[markedSpot.index.x, markedSpot.index.y];
                    currentSoldier             = null;

                    currentState = State.choosingPlayer;
                    whiteTurn    = !whiteTurn;
                    UnMark();
                }
                choiceIndex = 0;
                break;

            case ConsoleKey.Escape:
                if (currentState == State.choosingAct)
                {
                    currentState = State.choosingPlayer;
                }
                else
                {
                    Console.SetCursorPosition(0, 0);
                }
                break;

            case ConsoleKey.F3:
                showAll = !showAll;
                if (showAll)
                {
                    ShowSpots(chooseAbles);
                }
                else
                {
                    HideSpots(chooseAbles);
                }
                break;

            default:
                Console.SetCursorPosition(0, 0);
                break;
            }
        }
Ejemplo n.º 5
0
 private void UnMark()
 {
     markedSpot.Paint(markedSpot.color);
     markedSpot = null;
 }