Il gioco consiste nel colpire un bersaglio su un campo di battaglia. L’obiettivo è visualizzato con una ‘O’ mentre il tuo mirino è ‘> <'.
==== ROUND 1 ====
>.< . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . O . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
Le coordinate del bersaglio sono inviate sulla pipe ‘shotPipeOut’ nel formato x,y# Ad esempio 7,6# indica che il bersaglio si trova alla riga 7 e colonna 6 (attento che la numerazione parte da 0). Se visualizziamo (da un altro terminale) il contenuto della pipe otteniamo:
$ cat shotPipeOut
7,6#
Il mirino è inizialmente in 0,0. Sulla pipe ‘shotPipeIn’ devi inviare lo spostamento RELATIVO del mirino per colpire l’obiettivo. Il formato è lo stesso di prima: x,y# Ad esempio se invii -4,3# stai spostando il mirino di tre righe in alto e 3 colonna a destra.
Ad esempio:
==== ROUND 2 ====
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . O . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . >.< . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
Inviando (da un altro terminale) lo shift -4,3:
$ cat > shotPipeIn
-4,3#
Si ottiene:
Shift: -4,3
... SHOOT!
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . >X< . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
Gotcha!! good!
Per vincere devi scrivere un programma in grado di superare 10 round di seguito!
Ecco il programma eseguibile shot per architetture 32 bit e 64 bit.
Salve, questa è la mia soluzione dell’esercizio, purtroppo non riesco a provarla in quanto nel momento in cui lancio shot (versione 32 bit) mi stampa “bash: ./shot: Permission denied”. Grazie mille
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #define ROUNDS 10 #define PIPEIN "shotPipeIn" #define PIPEOUT "shotPipeOut" #define DIM 10 int main(){ int fw,fr,i,j; int xb,yb, //coordinate del bersaglio int xm,ym; //coordinate del mirino int xs,ys; //coordinate dello spostamento char buffer[DIM]; xm=0; ym=0; // se non esistono già creo le pipe mkfifo(PIPEIN,0666); mkfifo(PIPEOUT,0666); // apro le pipe fw=open(PIPEIN,O_WRONLY); fr=open(PIPEOUT,O_RDONLY); if(fw<0 || fr<0){ perror("errore apertura pipe"); exit(EXIT_FAILURE); } for(i=0;i<ROUNDS;i++){ j = 0; // leggo e salvo il valore corrispondente al bersaglio while (read(fr, buffer + j, 1) && buffer[j] != '#') j++; sscanf(buffer, "%d,%d#", &xb,&yb); //calcolo lo spostamento xs=xb-xm; ys=yb-ym; //creo la stringa contenente la mossa da fare snprintf(buffer, DIM, "%d,%d#", xs, ys); //scrivo sulla pipe la stringa appena creata write(fw, buffer, strlen(buffer)); //aggiorno la posizione del mirino con quella del bersaglio xm=xb; ym=yb; } //chiudo le pipe close(fw); close(fr); return EXIT_SUCCESS; }Evidentemente non hai reso il file eseguibile:
chmod +x ./shot