diff --git a/display.c b/display.c index 7272bbddf766f68536dc59f970b74142f40f0f0b..28a59e472f616f31d5cb33b34e37828af148f66e 100644 --- a/display.c +++ b/display.c @@ -48,7 +48,7 @@ void *display_func(void *param){ SDL_Window* window = init_display(); // printf("\ndisplay_func"); machine* mas = (machine *) (param); - + while(!*mas->wheels[0].end){ @@ -57,6 +57,7 @@ void *display_func(void *param){ SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h}; SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); + // renders coins for (int i=0; i<mas->machine_wallet; i++){ SDL_Rect coin_rect_pos={700, 1010-10*i, coin_rect.w, coin_rect.h}; SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); @@ -66,7 +67,8 @@ void *display_func(void *param){ SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); } - for(int i=0; i<3; i++){ + // renders wheels + for(int i=0; i<WHEEL_NB; i++){ pthread_mutex_lock(&mas->wheels[i].lock); int current_px = mas->wheels[i].current_px; pthread_mutex_unlock(&mas->wheels[i].lock); diff --git a/game.c b/game.c index 448c3f0a27d3143f703341af85ad43f081a0b5a7..94eaf450c427b6d3aa1eb430fdd84795cd5fc67a 100644 --- a/game.c +++ b/game.c @@ -5,10 +5,9 @@ void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end){ for (int i = 0; i < nb_wheels; i++) { - - mas->player_wallet = player; - mas->machine_wallet = machine; - sem_init(&mas->semaphore,0,0); - mas->wheels[i] = create_wheel(96,0,&mas->semaphore,end); + mas->player_wallet = player; + mas->machine_wallet = machine; + sem_init(&mas->semaphore,0,0); + mas->wheels[i] = create_wheel(96,0,&mas->semaphore,end); } } diff --git a/game.h b/game.h index 1ae9a0c97ce237b4cf819d311bdb7e9741e277c9..9a15c20471262a0038acc0b27ba1359563d64db1 100644 --- a/game.h +++ b/game.h @@ -12,7 +12,7 @@ typedef struct machine_a_sous{ wheel wheels[WHEEL_NB]; int player_wallet; int machine_wallet; - sem_t semaphore; + sem_t semaphore; // used to pause the thread whel the wheel is stopped } machine; void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end); diff --git a/makefile b/makefile index 058181e70e10f21eb6e50f8920d0e84f454d0c9d..97f122b2acde47966cb098bf6624a662e7b9a0bd 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -GCC=gcc -O2 -Wall -std=gnu99 -g +GCC=gcc -O2 -std=gnu99 -g #-Wall LIBS=-lSDL2 -lSDL2_image -lpthread BIN=one_armed_bandit @@ -10,13 +10,10 @@ $(BIN): $(OBJS) $(GCC) $^ -o $@ $(LIBS) run: $(BIN) - ./$< + ./$< %.o: %.c $(HEADERS) $(GCC) $< -c clean: rm -f $(BIN) $(OBJS) - - - diff --git a/one_armed_bandit.c b/one_armed_bandit.c index ca4bf7364d6e429f7ef9b3588020fed6eaf0d779..50d7db9c16abd67c99b881f0fe4810ee0cf88135 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -1,18 +1,18 @@ -// Template of one-armed bandit lab -// KB main, thread handling - - +/** +* One Armed Bandit +* 24.05.21 +* Authors : Sitbon Benjamin, Beck Léonard +**/ #include <stdio.h> - #include <assert.h> #include <unistd.h> #include <pthread.h> #include "utilities.h" #include "display.h" #include "game.h" -#include "one_armed_bandit.h" #include "threads.h" +#include "one_armed_bandit.h" @@ -43,21 +43,21 @@ int main() int wheel_turn = 4; bool paid = false; bool quit=false; - pthread_t threads[4]; + pthread_t threads[WHEEL_NB + 1]; machine mas; - new_machine(&mas, 3,10,30,&quit); - - for(int i = 0; i<3; i++){ + new_machine(&mas, WHEEL_NB,10,30,&quit); + + for(int i = 0; i<WHEEL_NB; i++){ run_thread(&threads[i],wheel_func,&mas.wheels[i]); } - - run_thread(&threads[3],display_func,&mas); - + run_thread(&threads[WHEEL_NB],display_func,&mas); + + SDL_Event event; - + do { SDL_WaitEvent(&event); // passive waiting on an event switch (event.type) @@ -84,7 +84,7 @@ int main() wheel_turn += 1; } if(wheel_turn == WHEEL_NB && paid == false){ - paid = true; + paid = true; // has the player recieved his money int howmany = how_many_points(mas); if(howmany == 2){ mas.player_wallet += 2; @@ -98,6 +98,7 @@ int main() break; case SDLK_s: wait_key_release(); + // prevents inserting coins when game just started if(mas.player_wallet > 0 && stopped_wheels(mas) > 0){ paid = false; mas.player_wallet -= 1; diff --git a/one_armed_bandit.h b/one_armed_bandit.h index ca932cf73e8d025cd46e7c6e0783079f6c4bd5e9..126a7272096fa1af6cbc94de3646168081bb5128 100644 --- a/one_armed_bandit.h +++ b/one_armed_bandit.h @@ -10,17 +10,18 @@ /\/\ - | | + / \ (____|_____|____) | o o | |__^__| \#####/ \###/ \#/ - + */ // returns the score based on the positionning of the wheels int how_many_points(machine mas); -int stopped_wheels(machine mas); \ No newline at end of file +// returns how many wheels are stopped +int stopped_wheels(machine mas); diff --git a/wheel.c b/wheel.c index 92619b2361f13724d3a0f23be73fc1559584e2e9..757f9cf64b20d4de8b2e49630be82401a1ad8f7d 100644 --- a/wheel.c +++ b/wheel.c @@ -23,10 +23,11 @@ void *wheel_func(void *param){ wheel_spin_1_tick(this); wait_until(wheel_delay_ms(*this)); } + return NULL; } -int wheel_spin_1_tick(wheel* this){ +void wheel_spin_1_tick(wheel* this){ if (this->speed) { pthread_mutex_lock(&this->lock); this->current_px = (this->current_px + 2) % 896 ; @@ -41,10 +42,10 @@ int wheel_spin_1_tick(wheel* this){ int wheel_delay_ms(wheel this){ if (this.speed) { return this.speed * 2; - } + } return 2; } -int stop_wheel(wheel* this){ +void stop_wheel(wheel* this){ while (this->current_px % 128 != 96) { // 96 is the offset to center the item wait_until(wheel_delay_ms(*this)); } diff --git a/wheel.h b/wheel.h index 2e5bd2a95bda987d8810195118e7d2ed5092e40c..0f3961749fbdcd84c604fb6891d1699ba97713e7 100644 --- a/wheel.h +++ b/wheel.h @@ -22,18 +22,18 @@ typedef struct roue{ wheel create_wheel(int pixel,int speed,sem_t* sem,bool* ending); -//function run by each wheel thread, turns the wheel +// function run by each wheel thread, turns the wheel void *wheel_func(void *param); - + // spins the wheel -int wheel_spin_1_tick(wheel* this); - +void wheel_spin_1_tick(wheel* this); + // calculates the delay based on the speed int wheel_delay_ms(wheel this); - + // stops the wheel on the right pixel to have a centered item -int stop_wheel(wheel* this); - +void stop_wheel(wheel* this); + #endif