From a79ef408bb2153c9d34b5f2268e1e2ec5ec1487b Mon Sep 17 00:00:00 2001 From: "leonard.beck" <leonard.beck@etu.hesge.ch> Date: Mon, 24 May 2021 18:25:24 +0200 Subject: [PATCH] comments --- display.c | 6 ++++-- game.c | 9 ++++----- game.h | 2 +- makefile | 7 ++----- one_armed_bandit.c | 31 ++++++++++++++++--------------- one_armed_bandit.h | 7 ++++--- wheel.c | 7 ++++--- wheel.h | 14 +++++++------- 8 files changed, 42 insertions(+), 41 deletions(-) diff --git a/display.c b/display.c index 7272bbd..28a59e4 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 448c3f0..94eaf45 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 1ae9a0c..9a15c20 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 058181e..97f122b 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 ca4bf73..50d7db9 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 ca932cf..126a727 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 92619b2..757f9cf 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 2e5bd2a..0f39617 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 -- GitLab