Skip to content
Snippets Groups Projects
Commit a79ef408 authored by leonard.beck's avatar leonard.beck :zzz:
Browse files

comments

parent 332f87b1
No related branches found
No related tags found
No related merge requests found
...@@ -48,7 +48,7 @@ void *display_func(void *param){ ...@@ -48,7 +48,7 @@ void *display_func(void *param){
SDL_Window* window = init_display(); SDL_Window* window = init_display();
// printf("\ndisplay_func"); // printf("\ndisplay_func");
machine* mas = (machine *) (param); machine* mas = (machine *) (param);
while(!*mas->wheels[0].end){ while(!*mas->wheels[0].end){
...@@ -57,6 +57,7 @@ void *display_func(void *param){ ...@@ -57,6 +57,7 @@ void *display_func(void *param){
SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h}; SDL_Rect coin_rect_pos={700, 1020, coin_rect.w, coin_rect.h};
SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos);
// renders coins
for (int i=0; i<mas->machine_wallet; i++){ 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_Rect coin_rect_pos={700, 1010-10*i, coin_rect.w, coin_rect.h};
SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos);
...@@ -66,7 +67,8 @@ void *display_func(void *param){ ...@@ -66,7 +67,8 @@ void *display_func(void *param){
SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); 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); pthread_mutex_lock(&mas->wheels[i].lock);
int current_px = mas->wheels[i].current_px; int current_px = mas->wheels[i].current_px;
pthread_mutex_unlock(&mas->wheels[i].lock); pthread_mutex_unlock(&mas->wheels[i].lock);
......
...@@ -5,10 +5,9 @@ ...@@ -5,10 +5,9 @@
void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end){ void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end){
for (int i = 0; i < nb_wheels; i++) { for (int i = 0; i < nb_wheels; i++) {
mas->player_wallet = player;
mas->player_wallet = player; mas->machine_wallet = machine;
mas->machine_wallet = machine; sem_init(&mas->semaphore,0,0);
sem_init(&mas->semaphore,0,0); mas->wheels[i] = create_wheel(96,0,&mas->semaphore,end);
mas->wheels[i] = create_wheel(96,0,&mas->semaphore,end);
} }
} }
...@@ -12,7 +12,7 @@ typedef struct machine_a_sous{ ...@@ -12,7 +12,7 @@ typedef struct machine_a_sous{
wheel wheels[WHEEL_NB]; wheel wheels[WHEEL_NB];
int player_wallet; int player_wallet;
int machine_wallet; int machine_wallet;
sem_t semaphore; sem_t semaphore; // used to pause the thread whel the wheel is stopped
} machine; } machine;
void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end); void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end);
......
GCC=gcc -O2 -Wall -std=gnu99 -g GCC=gcc -O2 -std=gnu99 -g #-Wall
LIBS=-lSDL2 -lSDL2_image -lpthread LIBS=-lSDL2 -lSDL2_image -lpthread
BIN=one_armed_bandit BIN=one_armed_bandit
...@@ -10,13 +10,10 @@ $(BIN): $(OBJS) ...@@ -10,13 +10,10 @@ $(BIN): $(OBJS)
$(GCC) $^ -o $@ $(LIBS) $(GCC) $^ -o $@ $(LIBS)
run: $(BIN) run: $(BIN)
./$< ./$<
%.o: %.c $(HEADERS) %.o: %.c $(HEADERS)
$(GCC) $< -c $(GCC) $< -c
clean: clean:
rm -f $(BIN) $(OBJS) rm -f $(BIN) $(OBJS)
// 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 <stdio.h>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include "utilities.h" #include "utilities.h"
#include "display.h" #include "display.h"
#include "game.h" #include "game.h"
#include "one_armed_bandit.h"
#include "threads.h" #include "threads.h"
#include "one_armed_bandit.h"
...@@ -43,21 +43,21 @@ int main() ...@@ -43,21 +43,21 @@ int main()
int wheel_turn = 4; int wheel_turn = 4;
bool paid = false; bool paid = false;
bool quit=false; bool quit=false;
pthread_t threads[4]; pthread_t threads[WHEEL_NB + 1];
machine mas; machine mas;
new_machine(&mas, 3,10,30,&quit); new_machine(&mas, WHEEL_NB,10,30,&quit);
for(int i = 0; i<3; i++){ for(int i = 0; i<WHEEL_NB; i++){
run_thread(&threads[i],wheel_func,&mas.wheels[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; SDL_Event event;
do { do {
SDL_WaitEvent(&event); // passive waiting on an event SDL_WaitEvent(&event); // passive waiting on an event
switch (event.type) switch (event.type)
...@@ -84,7 +84,7 @@ int main() ...@@ -84,7 +84,7 @@ int main()
wheel_turn += 1; wheel_turn += 1;
} }
if(wheel_turn == WHEEL_NB && paid == false){ if(wheel_turn == WHEEL_NB && paid == false){
paid = true; paid = true; // has the player recieved his money
int howmany = how_many_points(mas); int howmany = how_many_points(mas);
if(howmany == 2){ if(howmany == 2){
mas.player_wallet += 2; mas.player_wallet += 2;
...@@ -98,6 +98,7 @@ int main() ...@@ -98,6 +98,7 @@ int main()
break; break;
case SDLK_s: case SDLK_s:
wait_key_release(); wait_key_release();
// prevents inserting coins when game just started
if(mas.player_wallet > 0 && stopped_wheels(mas) > 0){ if(mas.player_wallet > 0 && stopped_wheels(mas) > 0){
paid = false; paid = false;
mas.player_wallet -= 1; mas.player_wallet -= 1;
......
...@@ -10,17 +10,18 @@ ...@@ -10,17 +10,18 @@
/\/\ /\/\
| | / \
(____|_____|____) (____|_____|____)
| o o | | o o |
|__^__| |__^__|
\#####/ \#####/
\###/ \###/
\#/ \#/
*/ */
// returns the score based on the positionning of the wheels // returns the score based on the positionning of the wheels
int how_many_points(machine mas); int how_many_points(machine mas);
int stopped_wheels(machine mas); // returns how many wheels are stopped
\ No newline at end of file int stopped_wheels(machine mas);
...@@ -23,10 +23,11 @@ void *wheel_func(void *param){ ...@@ -23,10 +23,11 @@ void *wheel_func(void *param){
wheel_spin_1_tick(this); wheel_spin_1_tick(this);
wait_until(wheel_delay_ms(*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) { if (this->speed) {
pthread_mutex_lock(&this->lock); pthread_mutex_lock(&this->lock);
this->current_px = (this->current_px + 2) % 896 ; this->current_px = (this->current_px + 2) % 896 ;
...@@ -41,10 +42,10 @@ int wheel_spin_1_tick(wheel* this){ ...@@ -41,10 +42,10 @@ int wheel_spin_1_tick(wheel* this){
int wheel_delay_ms(wheel this){ int wheel_delay_ms(wheel this){
if (this.speed) { if (this.speed) {
return this.speed * 2; 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 while (this->current_px % 128 != 96) { // 96 is the offset to center the item
wait_until(wheel_delay_ms(*this)); wait_until(wheel_delay_ms(*this));
} }
......
...@@ -22,18 +22,18 @@ typedef struct roue{ ...@@ -22,18 +22,18 @@ typedef struct roue{
wheel create_wheel(int pixel,int speed,sem_t* sem,bool* ending); 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); void *wheel_func(void *param);
// spins the wheel // spins the wheel
int wheel_spin_1_tick(wheel* this); void wheel_spin_1_tick(wheel* this);
// calculates the delay based on the speed // calculates the delay based on the speed
int wheel_delay_ms(wheel this); int wheel_delay_ms(wheel this);
// stops the wheel on the right pixel to have a centered item // stops the wheel on the right pixel to have a centered item
int stop_wheel(wheel* this); void stop_wheel(wheel* this);
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment