示例#1
0
        private void Evade()
        {
            bool hor = false;
            bool ver = false;
            int vDir = 1;
            int hDir = 1;
            bool success = true;
            Square nextSquare = null;

            foreach (Player player in players)
            {
                if (player.playerNumber == myNumber)
                    continue;

                if (player.position.X == players[myNumber].position.X)
                {
                    if ((player.position.Y < players[myNumber].position.Y) && (player.direction == 2))
                    {
                        ver = true;
                        vDir = 1;

                    }
                    else if ((player.position.Y > players[myNumber].position.Y) && (player.direction == 0))
                    {
                        ver = true;
                        vDir = -1;
                    }
                }

                if (player.position.Y == players[myNumber].position.Y)
                {
                    if ((player.position.X < players[myNumber].position.X) && (player.direction == 1))
                    {
                        hor = true;
                        hDir = 1;
                    }
                    else if ((player.position.X > players[myNumber].position.X) && (player.direction == 3))
                    {
                        hor = true;
                        hDir = -1;
                    }
                }
            }

            //how to evade
            if (ver)
            {
                int x = (int)players[myNumber].position.X;
                int y = (int)players[myNumber].position.Y;

                if(x == 0){
                    while ((squares[GetIndex(x+1, y)].getSType() != 0) || (squares[GetIndex(x+1, y)].hasPlayer()))
                    {
                        if ((y == 0) || (y == mapSize - 1))
                        {
                            success = false;
                            break;
                        }
                        y = y + vDir;
                    }
                    if (success)
                    {
                        nextSquare = squares[GetIndex(x + 1, y)];
                    }
                }
                else if (x == mapSize - 1)
                {
                    while ((squares[GetIndex(x-1, y)].getSType() != 0) || (squares[GetIndex(x-1, y)].hasPlayer()))
                    {
                        if ((y == 0) || (y == mapSize - 1))
                        {
                            success = false;
                            break;
                        }
                        y = y + vDir;
                    }
                    if (success)
                    {
                        nextSquare = squares[GetIndex(x - 1, y)];
                    }
                }
                else
                {
                    while (((squares[GetIndex(x - 1, y)].getSType() != 0) || (squares[GetIndex(x - 1, y)].hasPlayer())) && ((squares[GetIndex(x + 1, y)].getSType() != 0) || (squares[GetIndex(x + 1, y)].hasPlayer())))
                    {
                        if ((y == 0) || (y == mapSize - 1))
                        {
                            success = false;
                            break;
                        }
                        y = y + vDir;
                    }
                    if (success)
                    {
                        if ((squares[GetIndex(x - 1, y)].getSType() == 0) && (!squares[GetIndex(x - 1, y)].hasPlayer()))
                        {
                            nextSquare = squares[GetIndex(x - 1, y)];
                        }
                        else if ((squares[GetIndex(x + 1, y)].getSType() == 0) && (!squares[GetIndex(x + 1, y)].hasPlayer()))
                        {
                            nextSquare = squares[GetIndex(x + 1, y)];
                        }
                    }
                }

            }
            //////////////////
            success = true;
            if (hor)
            {
                int x = (int)players[myNumber].position.X;
                int y = (int)players[myNumber].position.Y;

                if (y == 0)
                {
                    while ((squares[GetIndex(x, y+1)].getSType() != 0) || (squares[GetIndex(x, y+1)].hasPlayer()))
                    {
                        if ((x == 0) || (x == mapSize - 1))
                        {
                            success = false;
                            break;
                        }
                        x = x + hDir;
                    }
                    if (success)
                    {
                        nextSquare = squares[GetIndex(x, y+1)];
                    }
                }
                else if (y == mapSize - 1)
                {
                    while ((squares[GetIndex(x, y-1)].getSType() != 0) || (squares[GetIndex(x, y-1)].hasPlayer()))
                    {
                        if ((x == 0) || (x == mapSize - 1))
                        {
                            success = false;
                            break;
                        }
                        x = x + hDir;
                    }
                    if (success)
                    {
                        nextSquare = squares[GetIndex(x, y-1)];
                    }
                }
                else
                {
                    while (((squares[GetIndex(x , y-1)].getSType() != 0) || (squares[GetIndex(x, y-1)].hasPlayer())) && ((squares[GetIndex(x, y+1)].getSType() != 0) || (squares[GetIndex(x, y+1)].hasPlayer())))
                    {
                        if ((x == 0) || (x == mapSize - 1))
                        {
                            success = false;
                            break;
                        }
                        x = x + hDir;
                    }
                    if (success)
                    {
                        if ((squares[GetIndex(x, y - 1)].getSType() == 0) && (!squares[GetIndex(x, y - 1)].hasPlayer()))
                        {
                            nextSquare = squares[GetIndex(x, y - 1)];
                        }
                        else if ((squares[GetIndex(x, y + 1)].getSType() == 0) && (!squares[GetIndex(x, y + 1)].hasPlayer()))
                        {
                            nextSquare = squares[GetIndex(x, y + 1)];
                        }
                    }
                }
            }

            if (nextSquare != null)
            {
                Navigator navg = new Navigator(mapSize);
                AStar ast = new AStar(squares, mapSize);

                List<StarNode> path = ast.calculatePath(players[myNumber].currentSquare, nextSquare, players[myNumber].direction);
                foreach (StarNode node in path)
                {
                    sqrPath.Add(node.mySqr);
                }
                commands = navg.generateCommandList(path, players[myNumber].direction);
                opMode = "evade";
            }
            else
            {
                //call shoot method
            }
        }
示例#2
0
        //get health packs
        private void GetHealth(TimeSpan time)
        {
            if (lifePacks.Count == 0)
                return;

            Navigator navg = new Navigator(mapSize);
            List<StarNode> bestPath;
            navg.GetBestHealth(squares, lifePacks.ToArray(), players[myNumber].currentSquare, players[myNumber].direction, out bestPath, out targetPack);

            commands = navg.generateCommandList(bestPath, players[myNumber].direction);
            if ((commands.Count > 0) && (bestPath.Count > 0))
            {
                foreach (StarNode node in bestPath)
                {
                    sqrPath.Add(node.mySqr);
                }
            }
            opMode = "life";
        }
示例#3
0
        //move to the centermost cell
        private void MoveToCenter()
        {
            int x = mapSize/2;
            int y = mapSize/2;
            int index = x + ( y * mapSize );
            Square center = squares[index];

            int add = 1;
            bool succ = true;

            while (center.getSType() != 0)
            {
                x = x + add;
                add = -(add+1);

                if((x<0)||(x>mapSize-1)){
                    succ = false;
                    break;
                }
                index = x + (y * mapSize);
                center = squares[index];
            }

            if (!succ)
            {
                succ = true;
                while (center.getSType() != 0)
                {
                    y = y + add;
                    add = -(add + 1);

                    if ((y < 0) || (y > mapSize - 1))
                    {
                        succ = false;
                        break;
                    }
                    index = x + (y * mapSize);
                    center = squares[index];
                }
            }

            if (succ)
            {
                AStar ast = new AStar(squares, mapSize);
                List<StarNode> path = ast.calculatePath(players[myNumber].currentSquare, center, players[myNumber].direction);
                foreach (StarNode node in path)
                {
                    sqrPath.Add(node.mySqr);
                }
                Navigator navg = new Navigator(mapSize);
                commands = navg.generateCommandList(path, players[myNumber].direction);
                opMode = "center";
            }
        }
示例#4
0
        //get coin piles
        private void GetCoins(TimeSpan time)
        {
            if ((coinPiles.Count == 0))
                return;

            Navigator navg = new Navigator(mapSize);
            List<StarNode> bestPath;
            navg.GetBestCoin(squares, coinPiles.ToArray(), players[myNumber].currentSquare, players[myNumber].direction, out bestPath, out targetCoin);

            commands = navg.generateCommandList(bestPath, players[myNumber].direction);
            if ((commands.Count > 0)&&(bestPath.Count >0))
            {
                foreach (StarNode node in bestPath)
                {
                    sqrPath.Add(node.mySqr);
                }
            }
            opMode = "coin";
            //Console.WriteLine("Got command");
        }