Exemple #1
0
        static void Main()
        {
            double increceSpeed = 0;
            double acceleration = 0.5;

            int playfieldWidth = 5; // shirina na igralnoto pole
            int livesCOunt = 5;
            Console.BufferHeight = Console.WindowHeight = 30;
            Console.BufferWidth = Console.BufferWidth = 50;

            //usercar
            Rock userCar = new Rock();
            userCar.x = 2;//start position
            userCar.y = Console.WindowHeight - 1;//start position
            userCar.c = '@';//symbol car
            userCar.color = ConsoleColor.Yellow;// color
            Random randomGenerator = new Random();

            List<Rock> cars = new List<Rock>();// list of cars with Cars property x ,y, c,colour but in addition we use add option form list property
            while (true)
            {
                if (increceSpeed > 400)
                {
                    increceSpeed += acceleration;
                }

                bool hitted = false;
                //create car
                {//skobite otdelqt newCar ot tuk s newcCar w for cikyla kydeto promenqme y za da dwijime kolichkite
                    Rock newCar = new Rock();
                    newCar.color = ConsoleColor.Green;
                    newCar.x = randomGenerator.Next(0, playfieldWidth);//0-4 beside pW=5
                    newCar.y = 0;//appear on the top
                    newCar.c = '%';
                    cars.Add(newCar);// dobavqme nowata kolichka zapisana w spisyk
                }
                //## Move your car (keyread)// read key = drive car
                if (Console.KeyAvailable)// check is key is pressed if pressed - read key else go down the code
                {
                    ConsoleKeyInfo pressedKey = Console.ReadKey(true);//read key
                    while (Console.KeyAvailable) Console.ReadKey(true);/*read buffer if there are any*/ /*clear buffer keys and read the ast key*/
                    if (pressedKey.Key == ConsoleKey.LeftArrow)
                    {
                        if (userCar.x - 1 >= 0)// we wont get out of the console buffer/field
                        {
                            userCar.x--;
                        }
                    }
                    else if (pressedKey.Key == ConsoleKey.RightArrow)
                    {
                        if (userCar.x + 1 < playfieldWidth)// we wont get out of the console buffer/field
                        {
                            userCar.x++;
                        }
                    }
                }
                //##  Move other Cars
                List<Rock> newList = new List<Rock>(); // zaradi neadekwatnoto dvijenie prawime now spisyk s koli4ki
                // kato swyrshime dobavqneto na nowi elementi na novite kolichki ot stariq spisyk
                // naj nakraq smenqme referenciqta cars = newList; t.e. kym stariq spisyk dobavqme nowi kolichki
                for (int i = 0; i < cars.Count; i++)
                {// uwelichawa Y koordinatite na wsqka kolichka
                    Rock oldCar = cars[i];// wzimame elemnt ot masiwa s kolichkire
                    Rock newCar = new Rock();// nowa kolichka sys sy]ite swojstwa na starata
                    newCar.x = oldCar.x;
                    newCar.y = oldCar.y + 1;//samo za radi towa pisahme wsi4ko ostanalo :D
                    newCar.c = oldCar.c;
                    //## CHeck if Hitted
                    newCar.color = oldCar.color;
                    if (newCar.y == userCar.y && newCar.x == userCar.x)
                    {//prowerka dali nowata kolichka sywpada s mqstoot na nashata ako da maha jiwoti
                        livesCOunt--;
                        hitted = true;// flad za ubyr
                        if (increceSpeed < 400)
                        {
                            increceSpeed += 50;// uweli4awame skorosta pri umirane
                        }

                        if (hitted)
                        {
                            PrintOnPosition(userCar.x, userCar.y, 'X', ConsoleColor.Red);
                        }
                        if (livesCOunt <= 0)
                        {// proverka za priklu4wane na igrata
                            PrintStringOnPosition(8, 7, " GAY OVER ! ! !", ConsoleColor.Red);
                            PrintStringOnPosition(8, 12, " Pres ENTER to exit! ! !", ConsoleColor.Red);

                            Console.ReadLine();// iz4akwasht operator i4akwa da se natisne nqkoj key
                            return; // spirame main metoda
                            // Environment.Exit(0);
                        }
                    }
                    if (newCar.y < Console.WindowHeight)
                    {// za da ne hwyrlq exeption kogato generiranite kolichki stignat  do dolu
                        newList.Add(newCar);
                    }
                }
                cars = newList;// dobavqne na nowite kolichki s novi elementi kym stariq spisyk

                //## Clear the console
                Console.Clear();
                //

                if (hitted)
                {
                    cars.Clear();
                    PrintOnPosition(userCar.x, userCar.y, 'X', ConsoleColor.Red);// pri udyr registrira s druc simvol i cwqt
                }
                else
                {
                    PrintOnPosition(userCar.x, userCar.y, userCar.c, userCar.color);
                }
                foreach (Rock car in cars)
                {//## draw othe cars
                    PrintOnPosition(car.x, car.y, car.c, car.color);
                }
                //## Draw INfo //## score
                PrintStringOnPosition(10, 4, "Lives: " + livesCOunt, ConsoleColor.White);
                PrintStringOnPosition(10, 5, "Speed: " + increceSpeed, ConsoleColor.White);
                PrintStringOnPosition(10, 7, "Speed: " + acceleration, ConsoleColor.White);

                //## Slow fown
                SpeedUP(increceSpeed);

            }
        }
        public void Attack_Rock()
        {
            // setup environment for test
            GameController.StartGame ();
            List<Piece> PL1 = GameController.Players [0].PieceList;
            List<Piece> PL2 = GameController.Players [1].PieceList;
            for (int i = 0; i < PL1.Count; i++)
            {
                PL1 [i].isDead = true;
                PL1 [i].X = -1;
                PL1 [i].Y = -1;
                PL2 [i].isDead = true;
                PL2 [i].X = -1;
                PL2 [i].Y = -1;
            }
            UtilityFunctions.CheckAvaiMoveOfAllPieces ();

            //test piece
            Piece testRock = new Rock (2, 2, true);
            Piece testPawn = null;
            Piece testPawn2 = null;

            //get a pwan from write player for test attack
            foreach (Piece p in PL1)
            {
                if (p.type == PieceType.Pawn)
                {
                    testPawn = p;
                    break;
                }
            }
            testPawn.isDead = false;
            testPawn.X = 2;
            testPawn.Y = 4;
            testRock.AvailableMoves ();
            testRock.Move (2, 4);
            Assert.AreEqual (true, testPawn.isDead);

            //get a pwan from black player for test attack
            foreach (Piece p in PL2)
            {
                if (p.type == PieceType.Pawn)
                {
                    testPawn2 = p;
                    break;
                }
            }
            testPawn2.isDead = false;
            testPawn2.X = 1;
            testPawn2.Y = 4;
            testRock.AvailableMoves ();
            testRock.Move (1, 4);
            Assert.AreEqual (false, testPawn2.isDead);
        }
        public void AvaliableMove_Rock()
        {
            // setup environment for test
            GameController.StartGame ();
            List<Piece> PL1 = GameController.Players [0].PieceList;
            List<Piece> PL2 = GameController.Players [1].PieceList;
            for (int i = 0; i < PL1.Count; i++)
            {
                PL1 [i].isDead = true;
                PL1 [i].X = -1;
                PL1 [i].Y = -1;
                PL2 [i].isDead = true;
                PL2 [i].X = -1;
                PL2 [i].Y = -1;
            }
            UtilityFunctions.CheckAvaiMoveOfAllPieces ();

            //test piece
            Piece testRock = new Rock (2, 2, true);
            testRock.AvailableMoves ();
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 0, 2));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 1, 2));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 3, 2));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 4, 2));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 2, 0));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 2, 1));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 2, 3));
            Assert.IsTrue (hasContain (testRock.AvaliableMove_X, testRock.AvaliableMove_Y, 2, 4));

            Piece testRock2 = new Rock (0, 7, false);
            testRock2.AvailableMoves ();
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 0));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 1));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 2));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 3));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 4));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 5));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 0, 6));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 1, 7));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 2, 7));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 3, 7));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 4, 7));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 5, 7));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 6, 7));
            Assert.IsTrue (hasContain (testRock2.AvaliableMove_X, testRock2.AvaliableMove_Y, 7, 7));
        }
        /// <summary>
        /// Loads the game.
        /// </summary>
        public static void LoadGame()
        {
            string filename = SwinGame.PathToResource ("records.txt");
            string[,] img = {{"Pawn_W","Rock_W","Knight_W","Bishop_W","Queen_W","King_W"},{"Pawn_B","Rock_B","Knight_B","Bishop_B","Queen_B","King_B"}};
            StreamReader sr = new StreamReader (filename);
            _players.Clear ();
            _deadPieceW.Clear ();
            _deadPieceB.Clear ();
            _players.Add (new Player ("", false));
            _players.Add (new Player ("", true));
            _players [0].PieceList.Clear ();
            _players [1].PieceList.Clear ();
            Result = 3;
            //Read names
            _players[0].name = sr.ReadLine();
            _players [1].name = sr.ReadLine ();
            // Modify Pieces Location
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    string carry = "";
                    carry = sr.ReadLine ();
                    Piece p = null;
                    switch (carry [0])
                    {
                    case 'P':
                        p = new Pawn (PieceType.Pawn);
                        p.Image = img [i, 0];
                        break;
                    case 'Q':
                        p = new Queen (PieceType.Queen);
                        p.Image = img [i, 4];
                        break;
                    case 'R':
                        p = new Rock (PieceType.Rock);
                        p.Image = img [i, 1];
                        break;
                    case 'K':
                        p = new King (PieceType.King);
                        p.Image = img [i, 5];
                        break;
                    case 'N':
                        p = new Knight (PieceType.Knight);
                        p.Image = img [i, 2];
                        break;
                    case 'B':
                        p = new Bishop (PieceType.Bishop);
                        p.Image = img [i, 3];
                        break;
                    }

                    p.IsBlack = (i == 1);
                    p.HasBeenMoved = false;
                    if (carry [1] == 'M')
                    {
                        p.HasBeenMoved = true;
                    }
                    if (carry [2] == 'L')
                    {
                        p.isDead = false;
                    }
                    else
                    {
                        p.isDead = true;
                        if (!p.IsBlack)
                        {
                            _deadPieceW.Add (p);
                        }
                        else
                        {
                            _deadPieceB.Add (p);
                        }
                    }
                    if (carry [3] == '-')
                    {
                        p.X = (-1) * (Convert.ToInt32 (carry [4]) - 48);
                        if (carry [5] == '-')
                        {
                            p.Y = (-1) * (Convert.ToInt32 (carry [6]) - 48);
                        }
                        else
                        {
                            p.Y = (Convert.ToInt32 (carry [5]) - 48);
                        }
                    }
                    else
                    {
                        p.X = Convert.ToInt32 (carry [3]) - 48;
                        if (carry [4] == '-')
                        {
                            p.Y = (-1) * (Convert.ToInt32 (carry [5]) - 48);
                        }
                        else
                        {
                            p.Y = (Convert.ToInt32 (carry [4]) - 48);
                        }
                    }
                    GameController.Players [i].PieceList.Add (p);
                }
            }
            SelectedPiece = null;
            string curr = sr.ReadLine();
            int current = Convert.ToInt32 (curr[0]) - 48;
            CurrentPlayerIndex = current;
            CurrentPlayer = Players [current];
            UtilityFunctions.CheckAvaiMoveOfAllPieces ();
            curr = sr.ReadLine ();
            if (curr == "CHECKING")
            {
                GameController.Checking = true;
                if (!MainGameController.CanPreventAttack ())
                {
                    GameController.Result = Math.Abs (GameController.CurrentPlayerIndex - 1);
                    GameController.EndCurrentState (); //Need to be changed later
                }
            }else
            {
                Checking = false;
            }
            AddState (GameState.MainGame);
            sr.Close ();
        }