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