From 6948091f9a9aef66d21f48051547dc7eb209052a Mon Sep 17 00:00:00 2001 From: "benjamin.sitbon" <benjamin.sibon@etu.hesge.ch> Date: Mon, 24 May 2021 14:22:56 +0200 Subject: [PATCH] =?UTF-8?q?op=C3=B6rationnel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- display.c | 7 ++++++- game.c | 6 ++++-- game.h | 2 +- one_armed_bandit.c | 42 ++++++++++++++++++++++++++++++++++++------ utilities.c | 2 +- wheel.c | 4 ++-- 6 files changed, 50 insertions(+), 13 deletions(-) diff --git a/display.c b/display.c index 9f70ded..e3da824 100644 --- a/display.c +++ b/display.c @@ -64,7 +64,12 @@ 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); - for (int i=0; i<4; 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_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); + } + for (int i=0; i<mas->player_wallet; i++){ SDL_Rect coin_rect_pos={700, 400-10*i, coin_rect.w, coin_rect.h}; SDL_RenderCopy(renderer, coin_texture, NULL, &coin_rect_pos); } diff --git a/game.c b/game.c index acc79dd..a5fa54b 100644 --- a/game.c +++ b/game.c @@ -23,8 +23,10 @@ wheel create_wheel(int pixel,int speed){ } -void new_machine(machine* mas, int nb_wheels){ +void new_machine(machine* mas, int nb_wheels,int player, int machine){ for (int i = 0; i < nb_wheels; i++) { - mas->wheels[i] = create_wheel(96,nb_wheels-i); + mas->wheels[i] = create_wheel(96,0); + mas->player_wallet = player; + mas->machine_wallet = machine; } } diff --git a/game.h b/game.h index 95b2ba6..a61cec6 100644 --- a/game.h +++ b/game.h @@ -21,6 +21,6 @@ int insert_coin(); int set_wheel_speed(int index); int next_wheel(); wheel create_wheel(int pixel,int speed); -void new_machine(machine* mas, int nb_wheels); +void new_machine(machine* mas, int nb_wheels,int player, int machine); #endif diff --git a/one_armed_bandit.c b/one_armed_bandit.c index 5cacb92..d24a9bc 100644 --- a/one_armed_bandit.c +++ b/one_armed_bandit.c @@ -17,13 +17,26 @@ +int how_many_points(machine mas){ + int count = 0; + int bubble = mas.wheels[0].current_px; + for(int i =0; i<WHEEL_NB; i++){ + if(bubble == mas.wheels[i].current_px) count += 1; + } + if(mas.wheels[1].current_px == mas.wheels[2].current_px) count += 1; + + return count; +} + + int main() { - int wheel_turn = 0; + int wheel_turn = 4; + bool paid = false; pthread_t threads[4]; machine mas; - new_machine(&mas, 3); + new_machine(&mas, 3,10,30); for(int i = 0; i<3; i++){ run_thread(&threads[i],wheel_func,&mas.wheels[i]); @@ -59,16 +72,33 @@ int main() break; case SDLK_SPACE: wait_key_release(); - if(wheel_turn <3){ + if(wheel_turn <WHEEL_NB){ stop_wheel(&mas.wheels[wheel_turn]); wheel_turn += 1; } + if(wheel_turn == WHEEL_NB && paid == false){ + paid = true; + int howmany = how_many_points(mas); + if(howmany == 2){ + mas.player_wallet += 2; + mas.machine_wallet -= 2; + } + else if(howmany > 2){ + mas.player_wallet += mas.machine_wallet/2; + mas.machine_wallet -= mas.machine_wallet/2; + } + } break; case SDLK_s: wait_key_release(); - wheel_turn =0; - for(int i = 0; i<3; i++){ - mas.wheels[i].speed = 3-i; + if(mas.player_wallet > 0){ + paid = false; + mas.player_wallet -= 1; + mas.machine_wallet += 1; + wheel_turn =0; + for(int i = 0; i<WHEEL_NB; i++){ + mas.wheels[i].speed = WHEEL_NB-i; + } } break; } diff --git a/utilities.c b/utilities.c index 091520b..650144b 100644 --- a/utilities.c +++ b/utilities.c @@ -64,7 +64,7 @@ void wait_key_release() if (SDL_PollEvent(&event)) { if (event.type==SDL_KEYUP) { - printf("key released\n"); + //printf("key released\n"); break; } } diff --git a/wheel.c b/wheel.c index 944a536..1055717 100644 --- a/wheel.c +++ b/wheel.c @@ -38,8 +38,8 @@ int wheel_delay_ms(wheel this){ } int stop_wheel(wheel* this){ - while (this->current_px % 128 != 128-32) { // 32 is the offset to center the item - wheel_spin_1_tick(this); + while (this->current_px % 128 != 96) { // 32 is the offset to center the item + //wheel_spin_1_tick(this); wait_until(wheel_delay_ms(*this)); } this->speed = 0; -- GitLab