blog/c/bonuses/04.md

183 lines
5.2 KiB
Markdown
Raw Normal View History

---
id: seminar-04
title: 4th seminar
description: |
Robot in a maze.
last_update:
date: 2023-03-13
---
:::warning[caution]
Deadline for the submission of the bonus is **March 23th 24:00**.
:::
:::tip
In case you have any questions, feel free to reach out either by email, Discord
or just by submitting an issue [here](https://gitlab.fi.muni.cz/xfocko/kb/-/issues/new).
:::
For this bonus you can get 3 K₡ and another 0.5 K₡ for the bonus part of it.
[Source](pathname:///files/c/bonuses/04.tar.gz)
## Introduction
In this task you are given a 2D map for a robot. The map contains multiple markers:
- `^v<>` - which denote the directions the robot will be facing when he steps into
the cell or starts on it.
- `K` - denotes the key.
- `T` - denotes the treasure.
In case robot lands at the beginning on unknown field, e.g. `.` in the tests, he
faces the direction that is given through the parameter.
Your task is to write the `walk` function that returns end result of the walk.
Walk can end in multiple ways:
- `FOUND_TREASURE` - when you find the treasure
- `FOUND_KEY` - when you find the key
- `OUT_OF_BOUNDS` - when the robot falls off the map
- `INFINITE_LOOP` - in case you will implement the bonus
- `NONE` - which is used right now as a default return in the skeleton, has no meaning
later on
## Hard requirement
There is only one hard requirement that tests cannot check.
**You are not allowed to use any indexing related to map or your current position**
**in your implementation.**
Reason for this requirement is for you to get used to working with pointers. And
for the implementation of this task it is much easier to use just the pointers.
## Example of run
For a better understanding of your task, I will describe a simple walk with corresponding
function call.
```c
const char *map = (
">.v"
".K<"
"..."
);
walk(map, &map[6], '^', 3, 3);
```
For this call, you should return `FOUND_KEY`. Let us walk through the walk ;)
1. Robot is placed at the bottom left corner, there is no direction specified, so
he follows the direction given by parameter (upwards, denoted as `N`(orth),
so that we can differentiate markers on the map with the robot when using printing
function).
```
>.v
.K<
N..
```
2. Moves up:
```
>.v
NK<
...
```
3. Moves up (now covers `>`), changes direction to right:
```
E.v
.K<
...
```
4. Moves to right:
```
>Ev
.K<
...
```
5. Moves to right, faces south:
```
>.S
.K<
...
```
6. Moves down, faces west:
```
>.v
.KW
...
```
7. Moves left, founds key, returns `FOUND_KEY`:
```
>.v
.W<
...
```
## Bonus part
For the bonus part you are supposed to return `INFINITE_LOOP` in case the robot
is stuck in the infinite loop. There are three tests for it. If you pass only the
easy and medium one, you can get 0.25 K₡ for doing your best and trying it out. :)
## Easter eggs
- Statistics
| Language | Files | Lines | Blanks | Comments | Code | Complexity |
| ---------------- | ----: | ----: | -----: | -------: | ---: | ---------: |
| _C_ | 4 | 458 | 34 | 58 | 366 | 33 |
| `test_maze.c` | | 225 | 9 | 0 | 216 | 4 |
| `sol.maze.c` | | 141 | 15 | 28 | 98 | 24 |
| `maze.c` | | 84 | 8 | 30 | 46 | 5 |
| `main.c` | | 8 | 2 | 0 | 6 | 0 |
| _C Header_ | 1 | 33 | 3 | 19 | 11 | 0 |
| `maze.h` | | 33 | 3 | 19 | 11 | 0 |
| _CMake_ | 1 | 25 | 4 | 6 | 15 | 2 |
| `CMakeLists.txt` | | 25 | 4 | 6 | 15 | 2 |
| **Total** | 6 | 516 | 41 | 83 | 392 | 35 |
- Majority of the line count in solution is caused by the formatting :)
- Included headers can be interpreted as hints, same goes for the unimplemented
`static` functions which you can use, but **are not required**.
- Given `CMakeLists.txt` will generate 2 binaries, `test_maze` and `maze`.
- `test_maze` runs the tests you are given.
- `maze` runs the `main.c`, where you can debug, print mazes and whatever else
you want.
- I keep only one copy of `cut.h` in my repository, so you need to download it from
[here](https://gitlab.fi.muni.cz/pb071/cut/-/jobs/159010/artifacts/file/1header/cut.h) and place it into the directory where you have your source code.
- Or you can use the one you have from the latest homework, git will keep it
only once, so it doesn't take up more space.
- I would recommend cloning this repository and copying the `maze` directory to
your own repository, since there are multiple files and it may be easier for you.
In case you have any questions, feel free to reach out to me.
## Submitting
For submitting the bonus assignment you can follow the same procedure as for
submitting the homeworks, that is:
1. On branch `main` add the provided skeleton.
2. Checkout new branch `seminar-bonus-04`.
3. Add your solution to the newly created branch.
4. Create a MR to the `main` branch with me (`@xfocko`) as the reviewer.