コード例 #1
0
 private bool CoordinatIsNOTFree(Kordinat kordinater)
 {
     if (Grid[kordinater.X, kordinater.Y] == SquareStatus.wall)
     {
         return(true);
     }
     return(false);
 }
コード例 #2
0
 private bool CoordinatIsNOTValid(Kordinat kordinater)
 {
     if (!Players.Any(p => p.Kordinater.X == kordinater.X && p.Kordinater.Y == kordinater.Y) && Grid.GetLength(0) > kordinater.X && Grid.GetLength(1) > kordinater.Y && kordinater.X >= 0 && kordinater.Y >= 0)
     {
         return(false);
     }
     return(true);
 }
コード例 #3
0
        public void SetStartingPositionsForPlayers()
        {
            var startingPositions = new Kordinat[] { new Kordinat {
                                                         X = 0, Y = 0
                                                     }, new Kordinat {
                                                         X = Grid.GetLength(0) - 1, Y = Grid.GetLength(1) - 1
                                                     }, new Kordinat {
                                                         X = 0, Y = Grid.GetLength(1) - 1
                                                     }, new Kordinat {
                                                         X = Grid.GetLength(0) - 1, Y = 0
                                                     } };

            for (int i = 0; i < Players.Count; i++)
            {
                Players[i].SetPlayerStartingPosition(startingPositions[i]);
            }
        }
コード例 #4
0
        public static Kordinat ShosSelectSizeMenueAndGetSelectedSizeAsKordinat()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("Labyrint Height and Width can not be larger than 25 or lesser than 5.\nEnter labyrint height and width below separated by a comma (exampel '9,12')");
                Console.Write(Environment.NewLine + "Enter labyrint size: "); // Prints menue title

                string userInput = Console.ReadLine();

                if (Regex.IsMatch(userInput, "^([5-9]|[1][0-9]|[2][0-5]),([5-9]|[1][0-9]|[2][0-5])$"))
                {
                    var      userInputSplitted = userInput.Split(',');
                    Kordinat selectedGridSize  = new Kordinat();
                    selectedGridSize.X = int.Parse(userInputSplitted[0]);
                    selectedGridSize.Y = int.Parse(userInputSplitted[1]);
                    return(selectedGridSize);
                }
            }
        }
コード例 #5
0
 private Kordinat NewCoordinateFromKeyPress(Kordinat kordinater, ConsoleKeyInfo keyPressed)
 {
     if (keyPressed.Key == ConsoleKey.UpArrow)
     {
         kordinater.Y = kordinater.Y - 1;
     }
     if (keyPressed.Key == ConsoleKey.DownArrow)
     {
         kordinater.Y = kordinater.Y + 1;
     }
     if (keyPressed.Key == ConsoleKey.RightArrow)
     {
         kordinater.X = kordinater.X + 1;
     }
     if (keyPressed.Key == ConsoleKey.LeftArrow)
     {
         kordinater.X = kordinater.X - 1;
     }
     return(kordinater);
 }
コード例 #6
0
        public bool TryMovePlayer(Player player, ConsoleKeyInfo keyPressed)
        {
            Kordinat newKordinat = NewCoordinateFromKeyPress(player.Kordinater, keyPressed); // ta fram kordinaten spelaren försöker flytta till (Baserat på spelarens nuvarande position och vilken pil)

            if (CoordinatIsNOTValid(newKordinat))                                            // kolla om den nya kordinaten är utanför spelplanen
            {
                return(false);                                                               // Inget mer utförs, dvs spelarens pjäs flyttas inte
            }
            if (CoordinatIsEqualToTarget(newKordinat))
            {
                player.MovePlayerToCoordinate(newKordinat);
                PrintGrid(ConsoleColor.Green, player);
                Console.Beep(2000, 500);
                player.Points++;
                Target reachedTarget = Targets.Find(t => t.Kordinater.X == newKordinat.X && t.Kordinater.Y == newKordinat.Y);
                reachedTarget.SetRandomTargetPosition(Grid, Players, Targets);
                return(false);
            }

            if (CoordinatIsNOTFree(newKordinat))  // kolla om den nya kordinaten är en vägg
            {
                var tempColorStorage = player.Color;
                player.Color = ConsoleColor.Red;
                player.MovePlayerToCoordinate(newKordinat);

                PrintGrid(ConsoleColor.Red, player);
                Console.Beep(2000, 500);
                Console.Clear();
                Thread.Sleep(100);

                player.Color = tempColorStorage;
                player.MovePlayerToCoordinate(player.startingPosition);
                return(false);
            }

            player.MovePlayerToCoordinate(newKordinat); // flytta spelaren

            return(true);                               // Har vi kommit hit har vi lyckatas flytta spelaren, returnerar true
        }
コード例 #7
0
        internal static SquareStatus[,] GetGrid(Kordinat cord)
        {
            SquareStatus[,] bigGrid = GenerateBigGridFromFile();

            SquareStatus[,] gameGrid = new SquareStatus[cord.X, cord.Y];

            Random rnd     = new Random();
            int    randomX = rnd.Next(0, 33 - cord.X);
            int    randomY = rnd.Next(0, 33 - cord.Y);

            for (int i = 0; i < cord.X; i++)
            {
                for (int j = 0; j < cord.Y; j++)
                {
                    gameGrid[i, j] = bigGrid[i + randomX, j + randomY];
                }
            }
            gameGrid[0, 0]                   = SquareStatus.free;
            gameGrid[0, cord.Y - 1]          = SquareStatus.free;
            gameGrid[cord.X - 1, 0]          = SquareStatus.free;
            gameGrid[cord.X - 1, cord.Y - 1] = SquareStatus.free;
            return(gameGrid);
        }
コード例 #8
0
 private bool CoordinatIsEqualToTarget(Kordinat newKordinat)
 {
     return(Targets.Any(t => t.Kordinater.X == newKordinat.X && t.Kordinater.Y == newKordinat.Y));
 }
コード例 #9
0
 public void SetPlayerStartingPosition(Kordinat playerStartingPosition)
 {
     startingPosition.X = playerStartingPosition.X;
     startingPosition.Y = playerStartingPosition.Y;
     MovePlayerToCoordinate(startingPosition);
 }
コード例 #10
0
 public void MovePlayerToCoordinate(Kordinat movedCoordinate)
 {
     Kordinater.X = movedCoordinate.X;
     Kordinater.Y = movedCoordinate.Y;
 }