logo CodeStepByStep logo

Toboggan

Write a method named Toboggan that simulates a Toboggan sliding down a repeating ski slope and returns the number of trees it would hit. A map of the ski slope is stored in a file whose name is passed to your method as a parameter. The file contains a grid of open squares (.) and trees (#) such as the following file, slope.txt:

..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#

You should simulate the Toboggan's movement down this slope. The Toboggan starts on the square in the top-left corner and needs to reach the bottom (below the bottom-most row on your map). The Toboggan repeatedly jumps right 3, down 1 until it reaches the bottom. If the Toboggan hits the right edge of the slope, it wraps around to the left edge. So for the example input file above, the Toboggan's path would be the following, where O represents a square the Toboggan touched without hitting a tree and X represents a square the Toboggan touched and did hit a tree:

O.##.......
#..O#...#..
.#....X..#.
..#.#...#O#
.X...##..#.
..#.X#.....
.#.#.#.O..#
.#........X
#.X#...#...
#...#X....#
.#..#...X.#

Therefore the call of Toboggan("slope.txt") would return 7 since it hit 7 trees on its way to the bottom of the slope.

You may assume that the file exists and is readable, that it follows the format described above, and that the slope is of size at least 2x2.

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.