Skip to content
Snippets Groups Projects
Commit 8c5ff2f6 authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

conditions de fin mais parfois bugué

parent 3f8fea2e
Branches
Tags
No related merge requests found
...@@ -56,7 +56,7 @@ void *display_func(void *param){ ...@@ -56,7 +56,7 @@ void *display_func(void *param){
// printf("\ndisplay_func"); // printf("\ndisplay_func");
machine* mas = (machine *) (param); machine* mas = (machine *) (param);
while(1){ while(mas->wheels[0].end){
SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect); SDL_RenderCopy(renderer, one_arm_texture, NULL, &one_arm_rect);
......
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
#include "game.h" #include "game.h"
void new_machine(machine* mas, int nb_wheels,int player, int machine){
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->wheels[i] = create_wheel(96,0);
mas->player_wallet = player; mas->player_wallet = player;
mas->machine_wallet = machine; mas->machine_wallet = machine;
sem_init(&mas->semaphore,0,0);
mas->wheels[i] = create_wheel(96,0,&mas->semaphore,end);
} }
} }
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
#include "wheel.h" #include "wheel.h"
typedef struct machine_a_sous{ 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;
} machine; } machine;
void new_machine(machine* mas, int nb_wheels,int player, int machine); void new_machine(machine* mas, int nb_wheels,int player, int machine,bool* end);
#endif #endif
/** // Template of one-armed bandit lab
* One Armed Bandit // KB main, thread handling
* 24.05.21
* Authors : Sitbon benjamin, Beck Léonard
**/
// Keyboard thread and game initialization
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <pthread.h> #include <pthread.h>
#include <semaphore.h>
#include "utilities.h" #include "utilities.h"
#include "display.h" #include "display.h"
#include "threads.h" #include "game.h"
#include "one_armed_bandit.h" #include "one_armed_bandit.h"
#include "threads.h"
...@@ -35,10 +32,11 @@ int main() ...@@ -35,10 +32,11 @@ int main()
{ {
int wheel_turn = 4; int wheel_turn = 4;
bool paid = false; bool paid = false;
bool quit=false;
pthread_t threads[4]; pthread_t threads[4];
machine mas; machine mas;
new_machine(&mas, 3,10,30); new_machine(&mas, 3,10,30,&quit);
for(int i = 0; i<3; i++){ for(int i = 0; i<3; i++){
run_thread(&threads[i],wheel_func,&mas.wheels[i]); run_thread(&threads[i],wheel_func,&mas.wheels[i]);
...@@ -49,7 +47,7 @@ int main() ...@@ -49,7 +47,7 @@ int main()
SDL_Event event; SDL_Event event;
bool quit=false;
do { do {
SDL_WaitEvent(&event); // passive waiting on an event SDL_WaitEvent(&event); // passive waiting on an event
switch (event.type) switch (event.type)
...@@ -67,9 +65,6 @@ int main() ...@@ -67,9 +65,6 @@ int main()
switch(event.key.keysym.sym) switch(event.key.keysym.sym)
{ {
case SDLK_ESCAPE: case SDLK_ESCAPE:
for(int i =0; i<4; i++){
pthread_kill(threads[i]);
}
quit=true; quit=true;
break; break;
case SDLK_SPACE: case SDLK_SPACE:
...@@ -100,6 +95,7 @@ int main() ...@@ -100,6 +95,7 @@ int main()
wheel_turn =0; wheel_turn =0;
for(int i = 0; i<WHEEL_NB; i++){ for(int i = 0; i<WHEEL_NB; i++){
mas.wheels[i].speed = WHEEL_NB-i; mas.wheels[i].speed = WHEEL_NB-i;
sem_post(&mas.semaphore);
} }
} }
break; break;
......
// Small library to ease the use of pthread // Small library to ease the use of pthread
// TODO: MALLOOOOC
#define __GNU_SOURCE #define __GNU_SOURCE
#include <stdio.h> #include <stdio.h>
...@@ -41,6 +39,7 @@ int run_thread_and_wait (void* (*fn )(void*), void *arg){ ...@@ -41,6 +39,7 @@ int run_thread_and_wait (void* (*fn )(void*), void *arg){
int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt){ int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, int t_cnt){
pthread_t* t = malloc(t_cnt*sizeof(pthread_t)); pthread_t* t = malloc(t_cnt*sizeof(pthread_t));
if (!t) return -1;
for(int i = 0; i<t_cnt; i++){ for(int i = 0; i<t_cnt; i++){
int err = run_thread(&t[i], fn, (*arg) + size_of_arg_type*i); int err = run_thread(&t[i], fn, (*arg) + size_of_arg_type*i);
...@@ -58,6 +57,7 @@ int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type, ...@@ -58,6 +57,7 @@ int run_threads_and_wait(void* (*fn )(void*), void** arg, int size_of_arg_type,
int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt){ int run_threads_and_wait_single_param(void* (*fn )(void*), void* arg, int t_cnt){
pthread_t *t = malloc(t_cnt*sizeof(pthread_t)); pthread_t *t = malloc(t_cnt*sizeof(pthread_t));
if (!t) return -1;
for(int i = 0; i<t_cnt; i++){ for(int i = 0; i<t_cnt; i++){
int err = run_thread(&t[i], fn, arg); int err = run_thread(&t[i], fn, arg);
......
...@@ -4,10 +4,12 @@ ...@@ -4,10 +4,12 @@
#include "wheel.h" #include "wheel.h"
wheel create_wheel(int pixel,int speed){ wheel create_wheel(int pixel,int speed,sem_t* sem,bool* ending){
wheel ret; wheel ret;
ret.current_px = pixel; ret.current_px = pixel;
ret.speed = speed; ret.speed = speed;
ret.semaphore = sem;
ret.end = ending;
pthread_mutex_init(&ret.lock,NULL); pthread_mutex_init(&ret.lock,NULL);
return ret; return ret;
} }
...@@ -15,21 +17,26 @@ wheel create_wheel(int pixel,int speed){ ...@@ -15,21 +17,26 @@ wheel create_wheel(int pixel,int speed){
void *wheel_func(void *param){ void *wheel_func(void *param){
// printf("\nwheel_func"); // printf("\nwheel_func");
wheel* id = (wheel *) (param); wheel* this = (wheel *) (param);
while(1){ while(this->end){
wheel_spin_1_tick(id); wheel_spin_1_tick(this);
wait_until(wheel_delay_ms(*id)); wait_until(wheel_delay_ms(*this));
} }
} }
int wheel_spin_1_tick(wheel* this){ int 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 ;
pthread_mutex_unlock(&this->lock); pthread_mutex_unlock(&this->lock);
} }
else{
sem_wait(this->semaphore);
} }
}
int wheel_delay_ms(wheel this){ int wheel_delay_ms(wheel this){
if (this.speed) { if (this.speed) {
......
...@@ -2,30 +2,38 @@ ...@@ -2,30 +2,38 @@
#define WHEEL_H #define WHEEL_H
#include <pthread.h> #include <pthread.h>
#include <stdbool.h>
#include <semaphore.h>
typedef struct roue{ typedef struct roue{
int speed;
// speed is either 1 (the fastest), 2, 3 (the slowest) or 0 (stopped) // speed is either 1 (the fastest), 2, 3 (the slowest) or 0 (stopped)
int current_px; int speed;
// stores the position of the wheel, used to determine what item is visible // stores the position of the wheel, used to determine what item is visible
pthread_mutex_t lock; int current_px;
// used to synchronize cache with memory so display is always up to date // used to synchronize cache with memory so display is always up to date
pthread_mutex_t lock;
sem_t* semaphore;
bool* end;
} wheel; } wheel;
wheel create_wheel(int pixel,int speed); wheel create_wheel(int pixel,int speed,sem_t* sem,bool* ending);
void *wheel_func(void *param);
//function run by each wheel thread, turns the wheel //function run by each wheel thread, turns the wheel
void *wheel_func(void *param);
int wheel_spin_1_tick(wheel* this);
// spins the wheel // spins the wheel
int wheel_spin_1_tick(wheel* this);
int wheel_delay_ms(wheel this);
// calculates the delay based on the speed // calculates the delay based on the speed
int wheel_delay_ms(wheel this);
int stop_wheel(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);
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment