コード例 #1
0
        public static void TurnCarLeft(MonsterTruck mTruck)
        {
            switch (mTruck.Direction)
            {
            case "E":
                mTruck.Direction = "N";
                break;

            case "W":
                mTruck.Direction = "S";
                break;

            case "N":
                mTruck.Direction = "W";
                break;

            case "S":
                mTruck.Direction = "E";
                break;
            }
        }
コード例 #2
0
        public static void DriveCarBackwards(MonsterTruck mTruck)
        {
            switch (mTruck.Direction)
            {
            case "E":
                mTruck.XPosition -= 1;
                break;

            case "W":
                mTruck.XPosition += 1;
                break;

            case "N":
                mTruck.YPosition -= 1;
                break;

            case "S":
                mTruck.YPosition += 1;
                break;
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Timmyeriksson/CarSimulator
        static void Main(string[] args)
        {
            Console.WriteLine(defineRoomText);

            var roomsize = Console.ReadLine();

            Room room = new Room();

            bool validatorRoom = room.CheckValidAxis(roomsize);

            while (validatorRoom == false)
            {
                Console.WriteLine("Something went wrong. Try again. Example input: 8 6");

                var newTry = Console.ReadLine();

                validatorRoom = room.CheckValidAxis(newTry);
            }

            Console.WriteLine("You have entered a room with the size " + room.XAxis + " " + room.YAxis + ". Please type the starting position of the car with integers, as well " +
                              "as the direction it will be facing with a N (north), S (south), W (west), or E (east) all separated with spaces. For example: 2 3 N");

            var carPosition = Console.ReadLine();

            //Assumption here that if implementing new models (i.e off-road) there should be a prompt asking which model the user wants to use. However, it feels fine for now to just
            //go straight for the monster truck
            MonsterTruck mTruck = new MonsterTruck();

            bool validatorCar = mTruck.CheckValidPosition(carPosition, room);

            while (validatorCar == false)
            {
                Console.WriteLine("Something went wrong. Try again. Example input: 2 3 N");

                var newTry = Console.ReadLine();

                validatorCar = mTruck.CheckValidPosition(newTry, room);
            }

            Console.WriteLine("Your car is positioned " + mTruck.XPosition + " " + mTruck.YPosition + " facing " + mTruck.Direction + ". " +
                              "Use commands in series to drive. F goes forward. L turns 90 degrees left. R turns 90 degrees right. B goes back without turning. Example input: FFFRFFLBB");

            var driveCommand = Console.ReadLine();

            Driving driving = new Driving();

            var validResult = driving.ValidDriving(driveCommand, mTruck);

            while (validResult == false)
            {
                Console.WriteLine("Try again. The only acceptable commands are F L R B");

                var newTry = Console.ReadLine();

                validResult = driving.ValidDriving(newTry, mTruck);
            }

            var drivingResult = driving.DrivingCar(mTruck, room);

            if (drivingResult)
            {
                Console.WriteLine("Simulation successful. Position: " + mTruck.XPosition + " " + mTruck.YPosition + " facing " + mTruck.Direction);
            }
            else
            {
                Console.WriteLine("Simulation failed. Car crashed at " + mTruck.XPosition + " " + mTruck.YPosition + " facing " + mTruck.Direction);
            }

            Console.ReadKey();
        }