Střelba na terče
-
načíst terče z textového souboru předaného v prvním argumentu programu
TargetsArray* targets_load(const char* path);
- terče reprezentovat pomocí struktury
- souřadnice jsou typu
int
- radius je typu
float
- barva může mít maximálně 15 znaků
- na řádku může být pouze jeden terč
- ošetřit neexistenci souboru a různé chyby nesplňující formát (viz testy):
# targets.txt pocet_tercu terc0_x terc0_y terc0_radius terc0_barva terc1_x terc1_y terc1_radius terc1_barva ...
-
načíst střely z binárního souboru předaného v prvním argumentu programu
MissilesArray* missiles_load(const char* path) { // 1. otevreme soubor path pro binarni cteni // 2. zkontrolujeme, zda se podarilo nacist // 3. nacist ze souboru bajt s poctem prvku N (a zkontrolovat) // 4. alokovat strukturu MissilesArray (sizeof(MissilesArray) // 5. alokovat pamet pro N items typu Missile // 6. cyklus pres vsechny prvky N // 7. precteme 2x uint32_t a ulozime do struktury (a kontrolujeme) // 8. zkontrolujeme, ze je konec souboru // 9. vratime pointer na missiles }
- střely také reprezentovat pomocí struktury
- ošetřit různé chyby při čtení (kratší soubor, delší soubor, ...)
- první bajt soubor obsahuje počet terčů
- každý terč je pak reprezentován dvěma čísly o velikosti 4 bajty (lépe použít
uint32_t
)
Obsah souboru: (lze najet myší na skupinky bajtů)
$ xxd -g 1 01_missiles.bin
041 unsigned bajt
počet střel 19 00 00 00unsigned int (uint32_t) v low endian
0x00000019 = 25
X souřadnice nulté střely 46 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice nulté střely 50 00 00 00unsigned int (uint32_t) v low endian
X souřadnice první střely 5a 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice první střely 96 00 00 00unsigned int (uint32_t) v low endian
X souřadnice druhé střely 64 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice druhé střely 37 00 00 00unsigned int (uint32_t) v low endian
X souřadnice třetí střely 8c 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice třetí střely
počet střel 19 00 00 00unsigned int (uint32_t) v low endian
0x00000019 = 25
X souřadnice nulté střely 46 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice nulté střely 50 00 00 00unsigned int (uint32_t) v low endian
X souřadnice první střely 5a 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice první střely 96 00 00 00unsigned int (uint32_t) v low endian
X souřadnice druhé střely 64 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice druhé střely 37 00 00 00unsigned int (uint32_t) v low endian
X souřadnice třetí střely 8c 00 00 00unsigned int (uint32_t) v low endian
Y souřadnice třetí střely
-
vykreslit terče a střely do textového SVG obrázku
result.svg
bool render_svg(const char *path, const TargetsArray *targets, const MissilesArray *missiles) { // 1. otevreme soubor pro textovy zapis // 2. zkontrolovat // 3. projdeme vsechny targets a vytiskneme <circle> // 4. projdeme vsechny missiles a vytiskneme <circle> // 5. zavrit soubor }
SVG obrázek musí byt v tomto formátu:
<svg xmlns="http://www.w3.org/2000/svg"> <!-- targets --> <circle cx="90" cy="70" r="40.000000" fill="red" /> <circle cx="160" cy="90" r="60.000000" fill="#ffaabb" /> <!-- missiles --> <circle cx="125" cy="70" r="5" fill="black" /> <circle cx="80" cy="90" r="5" fill="black" /> <circle cx="150" cy="100" r="5" fill="black" /> <circle cx="55" cy="140" r="5" fill="black" /> </svg>
-
vypočitání zásahu a uložení do binárního souboru
score.dat
void calculate_score(TargetsArray *targets, const MissilesArray *missiles) { // projdeme vsechny terce // projdeme vsechny strely // spocitame vzdalenost stredu terce od strely pomoci Pythagorovy vety // zjistime, jestli je to v terci - tj. vzdalenost je mensi nebo rovna polomeru // inkrementujeme pocet zasahu ve strukture Terce } bool save_score(const char *path, TargetsArray *targets) { // 1. otevreme soubor pro binarni zapis // 2. zkontrolujeme // 3. projdeme vsechny strely // 4. pokud je nenulovy pocet zasahu, tak ulozime index terce (1 `uint8_t`) + pocet zasahu (`uint32_t`) }
-
každý terč s nenulovým počtem zásahu bude uložen do souboru
-
index terče uložen jako
unsigned char
-
počet zásahů uložen jako
unsigned int
$ xxd -g 1 score.dat
-
001 unsigned bajt
index nultého terče 02 00 00 00unsigned int (uint32_t) v low endian
0x00000002 = 2
nultý terč má dva zásahy 011 unsigned bajt
index prvního terče 01 00 00 00unsigned int (uint32_t) v low endian
0x00000001 = 1
první terč má jeden zásah
index nultého terče 02 00 00 00unsigned int (uint32_t) v low endian
0x00000002 = 2
nultý terč má dva zásahy 011 unsigned bajt
index prvního terče 01 00 00 00unsigned int (uint32_t) v low endian
0x00000001 = 1
první terč má jeden zásah