From e4d549530c34ad198debfc21bce6a8f97992af26 Mon Sep 17 00:00:00 2001 From: Nazar Sendetskyi Date: Tue, 14 Jun 2022 07:43:35 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20'du7?= =?UTF-8?q?'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du7/Makefile | 14 ++++ du7/README.md | 16 ++++ du7/game | Bin 0 -> 39400 bytes du7/main.c | 110 ++++++++++++++++++++++++++++ du7/main.o | Bin 0 -> 13136 bytes du7/snake.h | 114 +++++++++++++++++++++++++++++ du7/snake.o | Bin 0 -> 7432 bytes du7/world.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++ du7/world.h | 122 +++++++++++++++++++++++++++++++ du7/world.o | Bin 0 -> 19480 bytes 10 files changed, 574 insertions(+) create mode 100644 du7/Makefile create mode 100644 du7/README.md create mode 100644 du7/game create mode 100644 du7/main.c create mode 100644 du7/main.o create mode 100644 du7/snake.h create mode 100644 du7/snake.o create mode 100644 du7/world.c create mode 100644 du7/world.h create mode 100644 du7/world.o diff --git a/du7/Makefile b/du7/Makefile new file mode 100644 index 0000000..d71d436 --- /dev/null +++ b/du7/Makefile @@ -0,0 +1,14 @@ +CFLAGS=-std=c99 -Wall -g + +all: game + +%.o: %.c + gcc $(CFLAGS) -c $< -o $@ + +clean: + rm *.o + rm game + +game: main.o world.o snake.o + gcc -rdynamic -g main.o world.o snake.o -lcurses -lm -o game + diff --git a/du7/README.md b/du7/README.md new file mode 100644 index 0000000..79a2d67 --- /dev/null +++ b/du7/README.md @@ -0,0 +1,16 @@ +# Snake Master + +Implement Snake game. + +Make the game to pass the automatic tests and make the game to be nice. + +## Files + +Please do not change file names. + +- `snake.h`: you implementation should follow this header. +- `snake.c`: implemement the game according to the documentation in header file in this file to pass automatic tests. +- `Makefile`: rules to build the game. +- `main.c`: trivial main function that runs the game, modify this file to change the appereance of the game and the initial state. +- `world.c`: world game loop and ASCII graphics library (do not change). +- `world.h`: world library interface (do not change). diff --git a/du7/game b/du7/game new file mode 100644 index 0000000000000000000000000000000000000000..7d83081af0e37f732b3bd97d8da3df46bbbb136f GIT binary patch literal 39400 zcmeHw3wTu3wf~-(?Boo2O=c33K!5>3K_G;efPf~z;2`0lkP1|t49VnCl1!Wj4=WK3 zQRYS}@oI}LRlIHg*W0TtZU44ZMe)&st$M-Rs;%0l3f`npFGa1nt!?ga?Z@QIA=BRP z{_p?${{QdeMmC?6PB>*bf ze3pyzM0N?w1fC&iGQUy+Mw!r>L?B>cumiEC(<6XjJZehchX#%O4kBi}q)E#)4)F%>qcdOxb5mLqqO++ihm zSjlPVQvTFX`;*L||M^Ov6?&0JhgX~#v&wBya#pxZ@oPwB(6R1xtp2}K-dZJhwo<5J zR`{|cGYxfl*Flc_@?Skvv$N|}dFk50T3_lXbj)aJ-h9QJ87=kGTbkSA+ox}zf5r4G z=9EXn<+DUS*+pegpSoss4MVdWC7NzdN*5}e@^AUYrs$meew=yNJM+TFi(2eUKZzXI zI;c$Ip@ZaA6AQcm=S%QUywqm4eQT>9FWX%>*=}d{4A-t*Mv#q|Fy6>$;=JO->eFwJUE=%s z+0m@_nViCRAF7>Gxql%0k5fPR=Ki1eGLteRT?_^&zr(2 z5d)qYPS4yl^sNvZj(=?${+rU!8`9{xIt~BRY52dEhF+gW&x2|BUra+kn}*(+hJI_B za%ZHWea`~g{oXY6iZtckE$A-hrGWs)JP8=x@(b7~=B0#*z?ZfF`iNfP=LeX62o(qM*TltaRKpcjzm zDkZM&E4_)8hH@ZYWYw_h$9Y22aX3E1yqr5Duq*Q#D$ zSBfV~cGjcl`>v1#5rZK4O-0{2OVV>yy`EO|PURm}ZT5kpd!|YLPbFXzl$|fDa<5VJ zpDF&2mHl*G(Xm$XdzC#QNoEvJkpFeur|%h54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hg3skA%~)Nq1?wQG475}c3sT!eV&(<1zD(8+<1k`MH6i(7Ggh}5%fIFp8%ExAX_BC{nbvGf z#aj9~SuR(JXm!fOH;npVa!Dl+*c^>2$B?g6mw3oOt)XZ%*cf7B&G)aX-PRi3ifRwv ztlLoNU%5`(BUqnoNcCB0d$Xm(iveWV;RB<9xEj(GkF_Q&X{XE8YmcUdEUc$(57L+) zY{4%UsfMX8+yHxruPiH%?r6nQDqt)kVUtdwo}oxPE5|Bos2tB=l;h`!_E03Y!$jH~ zZ*HldjyC}$SJjdg_zk0x33d$AO{~0rM;nTe5WlvFoUNfq6u(-eAOYk>u);|KD%IW+ zW96b-l_Oc+7zRp-XsC{r$LM#Na#Xt+++n(GSb3;P-Q1e$p-p2_hrG*437tk!!PaId zlj7)MtQ>b(v6@$2AKDym1ZS|V5w~N7Hn%l|bvn2i7sytPiKSS@1rH7rBK)^Vv_gWZ z8Rnw$(Cc#=(whh98(VZOo>^31L7GvG*n@zB;u^& zKTTMHgjrs%3u@@qj0#%i>9J^RaT3<`Kd8#ra6ri+{1S)N{vfD$gfUtF{i*;BYn33` zNWRnZ^Z*(i&G@Gxb@_TdQbYY672>7C>VNvA0DYrG%j?b?5{ff-?B0*Kw@_HRqL$5Mem#^h@{lBE-mnemLy;Q>!N-@ckj8*;- z(5MKlpLv1M;T30QT>-SVKRroqmiH<-4bM@g>2RsEhW`lMEU(vTHT0TfDXHZ&JOr9q zzG8wD(6IM!l>bAEyq>pd*kzfN=yGK#*lNQfuh)k)%+-tvTK)GUC9m6`RVqdW&nQ#_ z-F_N>1qrI6mOoo5b2NNeqRF$he7Sx%B(K+{HJq%jXRW|m{%K_7;a{`q_3KBgrG%AV z;~M@RZ22a6z0Q5;zbH?SDpKFo +#include +#include +#include +#include "snake.h" +// This file contains functions for drawing the the game and changing the state + +// Start is called one in the beginning +void* init_game(){ + // Allocate memory for the state + struct state* st = calloc(1,(sizeof(struct state))); + return st; +} + +// The first event +// Initialize game state +// - borders +// - snake position +// - food position +void init_snake(struct event* world, struct state* st){ + int cy = world->height/2; + int cx = world->width/2 - 5; + for (int i = 0; i < 5; i++){ + st->snake = add_snake(st->snake,cx + i ,cy); + } + + int h = world->height; + int w = world->width; + for (int i = 0; i < 5; i++){ + st->foodx[i] = rand() % w; + st->foody[i] = rand() % h; + } + // Initial game vector + st->sx = 1; + st->sy = 0; +} + +// Step is called in a loop once in interval. +// It should modify the state and draw it. +int world_event(struct event* w,void* game){ + // Get state pointer + struct state* st = game; + if (w->type == EVENT_START){ + // Called on beginning + init_snake(w,st); + } + // Called on key press + else if (w->type == EVENT_KEY){ + int key = w->key; + // Modifies vector of snake movement + if (key == KEY_RIGHT){ + st->sx = 1; + st->sy = 0; + } + else if (key == KEY_LEFT){ + st->sx = -1; + st->sy = 0; + } + else if (key == KEY_DOWN){ + st->sx = 0; + st->sy = 1; + } + else if (key == KEY_UP){ + st->sx = 0; + st->sy = -1; + } + } + // Called on esc key + else if (w->type == EVENT_ESC){ + // Non zero means finish the loop and stop the game. + return 1; + } + // Called on interval timeout + else if (w->type == EVENT_TIMEOUT){ + clear_screen(); + // Copy screen size + st->width = w->width; + st->height = w->height; + // Change game state + int r = step_state(st); + char ms[200]; + sprintf(ms,"r %d\n",r); + set_message(ms,9,9); + // Draw snake + struct snake* sn = st->snake; + while (sn != NULL){ + set_cell('x',sn->x,sn->y); + sn = sn->next; + } + // Draw food + for (int i = 0 ; i < FOOD_COUNT; i++){ + if (st->foodx[i] >= 0 && st->foody[i] >= 0){ + set_cell('*',st->foodx[i],st->foody[i]); + } + } + // Stop the game + if (r){ + char message[] = "Koniec"; + set_message(message,10,10); + } + } + return 0; +} + + +int main(int argc, char** argv){ + start_world(init_game,world_event,free); + return 0; +} diff --git a/du7/main.o b/du7/main.o new file mode 100644 index 0000000000000000000000000000000000000000..5e0a2a409e8439e458308171ecab9e1577af744c GIT binary patch literal 13136 zcmb_idvF}ZneW-%k!GdU!+KbfAF?1AY-~Mj3%_mT$C?!g%UBW^FpO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t}NTw-WXs`LTN7Xc>QJ`hO`k z;01HkjX?Y^W z%MrS7C6=|?KmHVsKL1+#C18s_oofsrll`lCQTxYp2$e(ohbaCVoQE7!=;C+YRQWUb z|8~It3epiR<_}|0^LOL|AW-OTxZxnp0ca`Nze*g`9^eAb9|4Bs9>cYF|V4homCas{OwJKUMx8rh)GwJIp_xAxe8Wys7#|lVhZCngYW! oVq6lcJ+3MB-Uo^urGG$s41uGl`NNo|vN!19?L~(zHdXuo14yrU)Bpeg literal 0 HcmV?d00001 diff --git a/du7/snake.h b/du7/snake.h new file mode 100644 index 0000000..4d1ff74 --- /dev/null +++ b/du7/snake.h @@ -0,0 +1,114 @@ +#ifndef snake_h_INCLUDED +#define snake_h_INCLUDED + +// Number of food items on the plane +#define FOOD_COUNT 5 + +/** + * One part of the snake; + * + * The snake is a linked list; + */ + +struct snake { + // x position of the snake part + int x; + // y position of the snake part + int y; + // Pointer to the next snake part. + // The last part of the snake has NULL pointer to the next part. + struct snake* next; +}; + +// End game reason constants, return value of step_state +enum endgame { + // Continue the game + END_CONTINUE = 0, + // Snake hit a wall + END_WALL, + // Snake hit itself + END_SNAKE, + // No food left + END_FOOD, + // Other reason to end + END_USER +}; + +/** + * State of the game. + * + * The state consists of the snake, its speed and food on the plane. + * + * The snake is a linked list of snake parts. + * + * Speed vector is a vector added to the last head position to create a new head. + * + * Food are points on the plane. Food with negative coordinates meads food is already eaten. + */ + +struct state { + // Snake as a linked list + struct snake* snake; + // X of the food positions + int foodx[FOOD_COUNT]; + // Y of the food positions + int foody[FOOD_COUNT]; + int sx; + int sy; + int width; + int height; +}; + +/** + * Add a new snake part with given position. The new snake part becomes the new head. + * + * @param head of the snake. + * @param x coordinate of the new head; + * @param y coordinate of the new head. + * @return new head of the snake. + */ +struct snake* add_snake(struct snake* snake,int x,int y); + +/** + * Remove the last snake part. + * The last snake part should always have NULL next pointer. + * + * @param head of the snake. + * @return new head of the snake. + */ +struct snake* remove_snake(struct snake* snake); + +/** + * Finds out if given coordinates are part of the snake. + * @param snake + * @param x coordinate to search in snake + * @param y coordinate to search in snake + * @return True, if there is a snake part with coordinates x,y. False otherwise + * + */ +int is_snake(struct snake* snake,int x, int y); +/** + * Remove and free each snake part; + * @param head of the snake. + */ +void free_snake(struct snake* sn); +/** + * Change game state. + * + * The function shoud calculate new posision of the snake head + * from the current position and speed vector. + * Then it should modify snake parst or food coordinates according to the rules: + * + * - If the new position is on the snake, end the game, return END_SNAKE. + * - If the new position is on the food, mark food as eaten + * (set its coordinates to -1) and add new snake part on the position of the food. If there is no food left, return END_FOOD. else return END_CONTINUE. + * - If the new position is on the plane, add new snake part on the new position and remove the last part of the snake, return END_CONTINUE. + * + * @param current state of the game + * @return reason to end the game according to enum endgame. + */ +int step_state(struct state* state); + + +#endif // snake_h_INCLUDED + diff --git a/du7/snake.o b/du7/snake.o new file mode 100644 index 0000000000000000000000000000000000000000..d83d0a1c49bfb560f63ecf3daf5eb5ce22d9c02c GIT binary patch literal 7432 zcmb_hdu&_P89&$8CwA&2cH{I#A9JA;=!oO601bWMKHNGbYeR`eQ^8W#zKN|ScCfG0 zq+5YCygW-&^AFQdG&+qx_6JnsuVpLiD$vxTNhmyo_@kPF7PO*tQxQyx_7MBNbH3wT zpQf9HcBGv1eZTK{zH`pK=Q_{Cy7##ZgQ;e)oota)C}USvEz(gzM%fD1!oKux_(vjM z{wzLmJrpmWzE&>Hl@qfU1EBJI^m7e$G`5dWuh+zQ#(PX>>E61})HzEzEBbURY;rt`rnx)ZH1I1wD*8za61?CF)gy1^xV` z(!2lFjEg8wE@m-tJqmNW0!HwsW`Sgdm1Ca}T)`P#sYD~jOc}D1x|OKA9J{I*F;!6M zK$YtXOi1}XS6xQdHQitMjiLRFJBsN~Y zEcXT)6Fu#M)%*Mg5bm^sc|%LVBc+OC$M1+bTKH_zs5R_AR6)`O}Z_Z0N5)parQ6_j1626N4y^G3v+ZsWw7{G4%`>HX>(?Zb1##1Sbis;bw5Ful>HDZ#k$zzg@5SwPO7k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yNous(!Wf4y9ihQj}d-Y)9)udso^IGAJg!)l;?!zUp;4E(ezcF-_dYY&kr^H zK2-YiA~iaP%aVP3?tgA za|Pe$qHTsAaoS!Ig#Op)bcv?N$M13U`AzK~09)NBhE?&mXpVK=cF#jkmcJ+28gmL+aszZGBZ&?0iE_PTu!NS4Y!zEYYfd(0nm*YnQ; zru#(848>145AzbbX~33>KSuG@KEe3-jM3wl$fwE^SkZn5zcyj}iX)%YyvAg$ZK zCVqL7zxRN++LWCd%OFv7RSAe0vUg%g>$*P$43}CfQ`J5MGG|lsR~X(i(5G&HgW^vs zMH+Sc*8yLu{%5KFV`PW*55S+YSL?D=Y&7|06ZT+3iQ(NBA>AIgiFU*QVQd +#include +#include +#include +#include + +int TIMEOUT; + +void abort_game(const char* message){ + endwin(); + puts(message); + exit(1); +} + +void check_bounds(const char* source,int x, int y){ + char msg[200]; + if (x < 0 || x >= COLS){ + sprintf(msg,"%s:: width %d is out of bounds (0,%d)",source,x,COLS); + abort_game(msg); + } + if (y < 0 || y >= LINES){ + sprintf(msg,"%s:: height %d is out of bounds (0,%d)",source,y,LINES); + abort_game(msg); + } +} + +void clear_screen(){ + // Clear screen + mvaddch(0,0,' '); + int screenchars = LINES*COLS; + for (int j = 1; j < screenchars;j++ ){ + addch(' '); + } +} + +void game_speed(int value){ + if (value < 0){ + abort_game("world_seed:: cannot be negative\n"); + } + TIMEOUT =value; +} + +void set_message(const char* message,int x,int y) { + int l = strlen(message); + for (int i = 0; i < l; i++){ + check_bounds("set_message",x+i,y); + set_cell(message[i],x+i,y); + } +} + +void assert_message(int event,const char* message){ + if (event == 0){ + abort_game(message); + } +} + + +void set_cell(int character,int x,int y) { + check_bounds("set_cell",x,y); + set_color_cell(character,x,y,COLOR_WHITE,COLOR_BLACK); +} + +void set_color_cell(int character,int x,int y,short front_color,short back_color){ + check_bounds("set_color_cell",x,y); + if (has_colors()){ + int pair = COLOR_COUNT * front_color + back_color; + attron(COLOR_PAIR(pair)); + mvaddch(y,x,character); + attroff(COLOR_PAIR(pair)); + } + else{ + mvaddch(y,x,character); + } +} + +int start_world(void* (*init_game)(),int (*world_event)(struct event* event,void* game),void (*destroy_game)(void*)){ + srand(time(NULL)); + int r = 1; + // Speed global variable + TIMEOUT = 100; + if (initscr() == NULL){ + // TODO Which Error? + puts("Curses Error."); + return -1; + } + noecho(); // Nevypisuj vstup na obrazovku + cbreak(); // Zabudni starý vstup + nodelay(stdscr,TRUE); // Nečakaj na stlačenie + keypad(stdscr,TRUE); // Aktivuje šípky + curs_set(FALSE); // Neviditeľný kurzor + /* Get all the mouse events */ + mousemask(ALL_MOUSE_EVENTS, NULL); + MEVENT mouse_event; + if (has_colors()){ // Zistenie či terminál podporuje farby + start_color(); + for (int i = 0; i < COLOR_COUNT;i++){ + for (int j = 0; j < COLOR_COUNT;j++){ + init_pair(i * COLOR_COUNT + j, i,j); + } + } + } + else { + puts("No colors!\n"); + } + void* game = NULL; + if (init_game != NULL){ + game = init_game(); + assert_message(game != NULL,"init_game:: should return non null pointer"); + } + timeout(TIMEOUT); + // Initial step + struct event event; + memset(&event,0,sizeof(struct event)); + event.height = LINES; + event.width = COLS; + event.type = EVENT_START; + clock_t start_time = clock(); + clock_t last_timeout = start_time; + clock_t next_timeout = last_timeout + TIMEOUT; + event.time_ms = start_time; + // Start event + r = world_event(&event,game); + refresh(); + while (!r) { + memset(&event,0,sizeof(struct event)); + event.height = LINES; + event.width = COLS; + event.key = getch(); + // No key was pressed + if (event.key == ERR){ + event.type = EVENT_TIMEOUT; + last_timeout = clock(); + next_timeout = last_timeout + TIMEOUT; + } + // Mouse event + else if (event.key == KEY_MOUSE ){ + event.type = EVENT_MOUSE; + if(getmouse(&mouse_event) == OK){ + event.mouse_x = mouse_event.x; + event.mouse_y = mouse_event.y; + if(mouse_event.bstate & BUTTON1_PRESSED){ + event.mouse_left = 1; + } + if(mouse_event.bstate & BUTTON2_PRESSED){ + event.mouse_middle = 1; + } + if(mouse_event.bstate & BUTTON3_PRESSED){ + event.mouse_right = 1; + } + } + } + else if (event.key == KEY_RESIZE) { + event.type = EVENT_RESIZE; + } + else{ + event.type = EVENT_KEY; + if (event.key == 27){ + int k = getch(); + if (k == -1){ + // Esc Was pressed + event.type = EVENT_ESC; + } + else { + // Alt was pressed + event.key = k; + event.alt_key = 1; + } + } + } + // Draw new world + event.time_ms = clock(); + r = world_event(&event,game); + refresh(); + event.time_ms = clock(); + // set new timeout + int nt = next_timeout - event.time_ms; + //printf("%d\n",nt); + if (nt > 0){ + timeout(nt); + } + else { + timeout(TIMEOUT); + next_timeout = event.time_ms + TIMEOUT; + } + } + memset(&event,0,sizeof(struct event)); + event.height = LINES; + event.width = COLS; + event.type = EVENT_END; + event.time_ms = clock(); + world_event(&event,game); + if (destroy_game != NULL){ + destroy_game(game); + } + endwin(); + return r; +}; diff --git a/du7/world.h b/du7/world.h new file mode 100644 index 0000000..e7a3fdb --- /dev/null +++ b/du7/world.h @@ -0,0 +1,122 @@ +#ifndef _WORLD_H_ +#define _WORLD_H_ + +#include + +/** + * World represented as a rectangular matrix of colorful characters. + * + * Point [0,0] is displayed the upper left corner of the screen. + * + */ + +enum event_type { + EVENT_START, + EVENT_TIMEOUT, + EVENT_KEY, + EVENT_MOUSE, + EVENT_RESIZE, + EVENT_ESC, + EVENT_END, +}; + +struct event { + /** + * Last width of the screen. + */ + int width; + /** + * Last height of the screen. + */ + int height; + /** + * Last pressed key or Curses event. + * + * Special event values: + * ERR if timeout, + * KEY_RESIZE if screen resize + * KEY_EVENT, other event, + * KEY_MOUSE, mouse clicked + * + * Key values: + * + * ' ' Space + * KEY_DOWN Arrow down + * KEY_UP Arrow up + * KEY_LEFT Arrow left + * KEY_RIGHT Arrow right + * KEY_A1 Upper left of keypad + * KEY_A3 Upper right of keypad + * KEY_B2 Center of keypad + * KEY_C1 Lower left of keypad + * KEY_C3 Lower right of keypad + * + * KEY_ENTER + * KEY_BACKSPACE + */ + int key; + int alt_key; + enum event_type type; + int mouse_x; + int mouse_y; + int mouse_left; + int mouse_right; + int mouse_middle; + long int time_ms; +}; + +/** + * Sets cell to a state. + * @param event + * @param x coordinate of cell + * @param y coordinate of cell + * @param new state of the cell + */ +void set_cell(int character,int x,int y); + +/** + * COLOR_BLACK 0 + * COLOR_RED 1 + * COLOR_GREEN 2 + * COLOR_YELLOW 3 + * COLOR_BLUE 4 + * COLOR_MAGENTA 5 + * COLOR_CYAN 6 + * COLOR_WHITE 7 + */ + +#define COLOR_COUNT 8 + +void set_color_cell(int character,int x,int y,short front_color,short back_color); + + +/** + * + * @param event + * @param number of commandline arguments + * @param init_world + * @param destroy_world + * + * void init_world(struct event* w); + * Initializes user state. + * Free user state. + * @param event + */ + +int start_world(void* (*init_game)(),int (*world_event)(struct event* event,void* game),void (*destroy_game)(void* game)); +/** + * Set timeout interval in miliseconds + */ +void game_speed(int value); + +/* + * Prints a message in screen on a position x,y + */ + +void set_message(const char* message,int x,int y); +/* + * Clears screen + */ +void clear_screen(); + +#endif diff --git a/du7/world.o b/du7/world.o new file mode 100644 index 0000000000000000000000000000000000000000..12199285148d734ebb327722c42439bdd2fe1d94 GIT binary patch literal 19480 zcmb_j4SZC^wV#{KB%AL*2oMRfAYk~&2ZG2~AbczY$)6K*B(UmUHec;{hnPla9fx>SHDM;LhDbXlr%?{ zDvLUw!?rZF2>aC{`e)Sc#a;*Nr^_BVTr~ZYAEkPY4w4;t7A-rBmy}6;7o1^Kdf1~C zbd{o*tXA~+v84BWkGBs!dUoVs;+;?kR?PLDzxCway#pIhZav?*^*rmyuisOPN`-x0 z-t$S}+=0R#7|Jur#Hr+llhCU(%1qfjRz6$bwc!-Bv?o8K%X>0;Ecx;Pt^H+v^?_cY z!s;!0iWMC`+O^?i@@0w*&D{||L%YyMG%df#_^a1>03$+!{YKKbNYaobl9H)A>;!9I$u**}nIoX7NGPCTaN0zsXIC4PfG&1+ zea|jOTTbfpNm3$NBEz1!CL2fFU*p?(B6J2ASd4bdaD<>-Bfl4=IwUWU*EMy zjxE1RpGY3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