Beispiel #1
0
        public bool attack()
        {
            Boolean state = true;

            com = new Communicator();
            com.sendMessage_ToServer("SHOOT#");
            Console.WriteLine("Shooting.");
            return(state);
        }
        static void Main(string[] args)
        {
            Communicator com = new Communicator();

            if (com.sendMessage_ToServer("JOIN#"))
            {
                GameUpdater updater = new GameUpdater();
                Thread      update  = new Thread(updater.update);
                update.Start();
            }
        }
        static void Main(string[] args)
        {
            Communicator com = new Communicator();
            if (com.sendMessage_ToServer("JOIN#")) {
                 GameUpdater updater = new GameUpdater();
                 Thread update = new Thread(updater.update);
                 update.Start();

                // com.sendMessage_ToServer("RIGHT#");

            }
        }
Beispiel #4
0
        public void stear()
        {
            com = new Communicator();

            players = gamemap.getAllPlayers();
            coin    = gamemap.getCoinPiles();
            life    = gamemap.getLifepacks();
            int x_current = players[this.indexOfHomePlayer].getCoordinates().getXCoordinate();
            int y_current = players[this.indexOfHomePlayer].getCoordinates().getYCoordinate();

            Console.WriteLine("X:" + players[this.indexOfHomePlayer].getCoordinates().getXCoordinate());
            Console.WriteLine("Y:" + players[this.indexOfHomePlayer].getCoordinates().getYCoordinate());
            int max = 40;
            int x_target;
            int y_target;

            int closestCoinpileIndex = 0;
            int closestLifepackIndex = 0;


            if (coin.Count != 0 && gamemap.getNumberOfAliveCoinPilies() != 0)
            {
                if (players[this.indexOfHomePlayer].getHealth() <= 50 && gamemap.getNumberOfAliveLifePacks() != 0)
                {
                    for (int i = 0; i < life.Count; i++)
                    {
                        Console.WriteLine("Life Pack alive:" + life.ElementAt(i).isAlive() + "   " + life.ElementAt(i).getCoordinates().getCoordinatesAsAString());
                        if (life.ElementAt(i).isAlive())
                        {
                            x_target = life.ElementAt(i).getCoordinates().getXCoordinate();
                            y_target = life.ElementAt(i).getCoordinates().getYCoordinate();
                            if (max >= this.utilityFunction(x_target, y_target, x_current, y_current))
                            {
                                max = this.utilityFunction(x_target, y_target, x_current, y_current);
                                closestLifepackIndex = i;
                            }
                        }
                    }

                    x_target = life.ElementAt(closestLifepackIndex).getCoordinates().getXCoordinate();
                    y_target = life.ElementAt(closestLifepackIndex).getCoordinates().getYCoordinate();
                }
                else
                {
                    for (int i = 0; i < coin.Count; i++)
                    {
                        Console.WriteLine("Coin Pile alive:" + coin.ElementAt(i).isAlive());

                        if (coin.ElementAt(i).isAlive())
                        {
                            x_target = coin.ElementAt(i).getCoordinates().getXCoordinate();
                            y_target = coin.ElementAt(i).getCoordinates().getYCoordinate();
                            if (max >= this.utilityFunction(x_target, y_target, x_current, y_current))
                            {
                                max = this.utilityFunction(x_target, y_target, x_current, y_current);
                                closestCoinpileIndex = i;
                            }
                        }
                    }



                    x_target = coin.ElementAt(closestCoinpileIndex).getCoordinates().getXCoordinate();
                    y_target = coin.ElementAt(closestCoinpileIndex).getCoordinates().getYCoordinate();
                }

                int[] utilityValues = { -1, -1, -1, -1 };
                x_current = players[this.indexOfHomePlayer].getCoordinates().getXCoordinate();
                y_current = players[this.indexOfHomePlayer].getCoordinates().getYCoordinate();
                //shoot arround players
                if ((gamemap.getCell(x_current - 1, y_current).Contains("P")) && (x_current - 1 >= 0))
                {
                    this.attack();
                }
                if ((gamemap.getCell(x_current + 1, y_current).Contains("P")) && (x_current + 1 < 20))
                {
                    this.attack();
                }

                if ((gamemap.getCell(x_current, y_current - 1).Contains("P")) && (y_current - 1 >= 0))
                {
                    this.attack();
                }
                if ((gamemap.getCell(x_current, y_current + 1).Contains("P")) && (y_current + 1 < 20))
                {
                    this.attack();
                }

                //===================================================
                if (x_current + 1 < this.mapSize)
                {
                    if (gamemap.getCell(x_current + 1, y_current).Equals("Empty") || gamemap.getCell(x_current + 1, y_current).Split('_')[0].Equals("C") || gamemap.getCell(x_current + 1, y_current).Split('_')[0].Equals("L") || gamemap.getCell(x_current + 1, y_current).Split('_')[0].Equals("P"))
                    {
                        com.sendMessage_ToServer("RIGHT#");
                        utilityValues[0] = this.utilityFunction(x_target, y_target, x_current + 1, y_current);
                        Console.WriteLine("RIGHT :" + utilityValues[0]);
                    }
                    Console.WriteLine(gamemap.getCell(x_current + 1, y_current));
                }
                x_current = players[this.indexOfHomePlayer].getCoordinates().getXCoordinate();
                y_current = players[this.indexOfHomePlayer].getCoordinates().getYCoordinate();

                if (y_current + 1 < this.mapSize)
                {
                    if (gamemap.getCell(x_current, y_current + 1).Equals("Empty") || gamemap.getCell(x_current, y_current + 1).Split('_')[0].Equals("C") || gamemap.getCell(x_current, y_current + 1).Split('_')[0].Equals("L") || gamemap.getCell(x_current, y_current + 1).Split('_')[0].Equals("P"))
                    {
                        com.sendMessage_ToServer("DOWN#");
                        utilityValues[1] = this.utilityFunction(x_target, y_target, x_current, y_current + 1);
                        Console.WriteLine("DOWN: " + utilityValues[1]);
                    }
                    Console.WriteLine(gamemap.getCell(x_current, y_current + 1));
                }
                x_current = players[this.indexOfHomePlayer].getCoordinates().getXCoordinate();
                y_current = players[this.indexOfHomePlayer].getCoordinates().getYCoordinate();

                if (x_current - 1 >= 0)
                {
                    if (gamemap.getCell(x_current - 1, y_current).Equals("Empty") || gamemap.getCell(x_current - 1, y_current).Split('_')[0].Equals("C") || gamemap.getCell(x_current - 1, y_current).Split('_')[0].Equals("L") || gamemap.getCell(x_current - 1, y_current).Split('_')[0].Equals("P"))
                    {
                        com.sendMessage_ToServer("LEFT#");
                        utilityValues[2] = this.utilityFunction(x_target, y_target, x_current - 1, y_current);
                        Console.WriteLine("Left " + utilityValues[2]);
                    }
                    Console.WriteLine(gamemap.getCell(x_current - 1, y_current));
                }
                x_current = players[this.indexOfHomePlayer].getCoordinates().getXCoordinate();
                y_current = players[this.indexOfHomePlayer].getCoordinates().getYCoordinate();

                if (y_current - 1 >= 0)
                {
                    if (gamemap.getCell(x_current, y_current - 1).Equals("Empty") || gamemap.getCell(x_current, y_current - 1).Split('_')[0].Equals("C") || gamemap.getCell(x_current, y_current - 1).Split('_')[0].Equals("L") || gamemap.getCell(x_current, y_current - 1).Split('_')[0].Equals("P"))
                    {
                        if (gamemap.getCell(x_current, y_current - 1).Contains("P"))
                        {
                            this.attack();
                            utilityValues[3] = this.utilityFunction(x_target, y_target, x_current, y_current - 1);
                        }
                        else
                        {
                            com.sendMessage_ToServer("UP#");
                            utilityValues[3] = this.utilityFunction(x_target, y_target, x_current, y_current - 1);
                            Console.WriteLine("Up " + utilityValues[3]);
                        }
                    }
                    Console.WriteLine(gamemap.getCell(x_current, y_current - 1));
                }


                int bestMove = -1;
                int min      = 40;
                //  Boolean state = false;
                for (int i = 0; i < utilityValues.Length; i++)
                {
                    if (utilityValues[i] != -1 && min >= utilityValues[i])
                    {
                        min      = utilityValues[i];
                        bestMove = i;
                    }
                }
                x_current = players[this.indexOfHomePlayer].getCoordinates().getXCoordinate();
                y_current = players[this.indexOfHomePlayer].getCoordinates().getYCoordinate();
                if (x_current == 19 || y_current == 19 || x_current == 0 || y_current == 0)
                {
                    if (x_current == 19)
                    {
                        com.sendMessage_ToServer("LEFT#");
                    }
                    if (y_current == 19)
                    {
                        com.sendMessage_ToServer("UP#");
                    }
                    if (x_current == 0)
                    {
                        com.sendMessage_ToServer("RIGHT#");
                    }
                    if (y_current == 0)
                    {
                        com.sendMessage_ToServer("DOWN#");
                    }
                }
                else
                {
                    switch (bestMove)
                    {
                    case 0: com.sendMessage_ToServer("RIGHT#");
                        Console.WriteLine("CHOOSED MOVE: RIGHT");
                        break;

                    case 1: com.sendMessage_ToServer("DOWN#");
                        Console.WriteLine("CHOOSED MOVE: DOWN");
                        break;

                    case 2: com.sendMessage_ToServer("LEFT#");
                        Console.WriteLine("CHOOSED MOVE: LEFT");
                        break;

                    case 3: com.sendMessage_ToServer("UP#");
                        Console.WriteLine("CHOOSED MOVE: UP");
                        break;

                    default: com.sendMessage_ToServer("SHOOT#");
                        Console.WriteLine("CHOOSED MOVE: SHOOT");
                        break;
                    }

                    this.attack();
                }
            }
        }
Beispiel #5
0
        public void update()
        {
            int s = 0;  // s=1  initial message Recived

            // s=2  start message Recived..


            while (true)
            {
                connection = tcpListener.AcceptSocket();
                if (connection.Connected)
                {
                    stm = new NetworkStream(connection);
                    byte[] buffer = new byte[5000];
                    int    k;
                    try
                    {
                        k = stm.Read(buffer, 0, buffer.Length);
                    }
                    catch (IOException e) {
                        Console.WriteLine("Remote Host  Closed ");
                        continue;
                    }

                    String message = ascii.GetString(buffer, 0, k);
                    char   value;
                    if (!message.Equals(""))
                    {
                        value = message.ToCharArray()[0];
                    }
                    else
                    {
                        value = 'U';
                    }

                    if (message.Equals("CELL_OCCUPIED#"))
                    {
                        Console.WriteLine("CELL OCCUPIED....");
                        com = new Communicator();
                        com.sendMessage_ToServer("SHOOT#");
                        continue;
                    }

                    switch (value)
                    {
                    case 'I': this.initialString = message;
                        s++;
                        break;

                    case 'S': this.startString = message;
                        s++;
                        if (s == 2)
                        {
                            map      = new Map(initialString, startString);
                            solution = new Player_Solution(map);
                        }
                        break;

                    case 'G': this.gameString = message;
                        map.MapUpdate(message);
                        if (switcher <= 10)
                        {
                            solution.stear();
                            switcher++;
                            Console.WriteLine("Stear MODE");
                        }
                        else if (switcher > 10 && switcher <= 20)
                        {
                            bool shoot = solution.attack();
                            switcher++;
                            Console.WriteLine("Attack MODE");
                            if (shoot == false)
                            {
                                switcher = 1010;
                                Console.WriteLine("SWITCHED STEAR MODE");
                            }
                        }
                        else
                        {
                            switcher = 0;
                        }
                        break;

                    case 'C': this.coinPileString = message;
                        map.ItemUpdate(message);
                        break;

                    case 'L': this.medipackString = message;
                        map.ItemUpdate(message);
                        break;

                    default: Console.WriteLine("Unknown Message.");
                        break;
                    }

                    Console.WriteLine(message);
                }
                else
                {
                    Console.WriteLine("Internet fail.. ");
                    continue;
                }
            }
        }
        private void Stear()
        {
            while (true) {

                com = new Communicator();
                if (System.Console.ReadKey(true).Key == ConsoleKey.UpArrow) {

                    com.sendMessage_ToServer("UP#");
                    Console.WriteLine("Sending Message UP");
                }
                else if (System.Console.ReadKey(true).Key == ConsoleKey.DownArrow)
                {
                    com.sendMessage_ToServer("DOWN#");
                    Console.WriteLine("Sending Message DOWN");
                }
                else if (System.Console.ReadKey(true).Key == ConsoleKey.LeftArrow) {
                    com.sendMessage_ToServer("LEFT#");
                    Console.WriteLine("Sending Message LEFT");
                }
                else if (System.Console.ReadKey(true).Key == ConsoleKey.RightArrow) {
                    com.sendMessage_ToServer("RIGHT#");
                    Console.WriteLine("Sending Message RIGHT");
                }
                else if (System.Console.ReadKey(true).Key == ConsoleKey.Spacebar) {
                    com.sendMessage_ToServer("SHOOT#");
                    Console.WriteLine("Sending Message SHOOT");

                }

            }
        }