Commit e1c47d68 authored by Baptiste Lehuede's avatar Baptiste Lehuede
Browse files

Merge branch 'main' into 'Secondary'

# Conflicts:
#   README.md
parents 07686e92 f0878f48
1.bmp

6.08 KB

......@@ -3,7 +3,11 @@ project(pickomino C)
set(CMAKE_C_STANDARD 11)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
include_directories(.)
add_executable(maintestsecondary
SecondaryFunctions.c)
add_executable(pickomino maingraph.c window.c init.c input.c Secondaryfunctions.c)
target_link_libraries(pickomino ${SDL2_LIBRARIES})
......@@ -5,20 +5,21 @@
#ifndef PICKOMINO_GRAPHICALFUNCTIONS_H
#define PICKOMINO_GRAPHICALFUNCTIONS_H
#include "initialisation.h"
#include <stdbool.h>
//Graphical functions//
//Le joueur selectionne le dés à conserver//
void selDicesToKeep(Dice listDices[NB_DICES], ...);
Dice *selDicesToKeep(Dice listDices[NB_DICES],bool *debut_program,SDL_Event event);
//Le joueur selectionne le Pickomino à prendre, puis le pickomino est pris//
void selAndTakePickomino(Game game, Dice listDices[NB_DICES]);
int selAndTakePickomino(Game *game, int val_total_dice, bool *debut_program,SDL_Event event, int num_joueur);
//Le joueur choisit de relancer ou prendre un pickomino//
Choice makeACHoice();
Choice makeAChoice(Game *game, Dice listDices[NB_DICES], SDL_Event event, bool *debut_program, int val_total_dice, int numerojoueur);
//Le joueur lance les dès//
int ClicktoThrow();
Dice *ClicktoThrow(Dice listDices[NB_DICES], bool *debut_program, SDL_Event event);
//Le joueur indique le nombre de joueur//
int whriteNbOfPlayer();
......@@ -27,11 +28,9 @@ int whriteNbOfPlayer();
int whriteNbOfIAs();
//Le joueur écrit son pseudo//
char whriteName();
char writeName();
//Le joueur indique le niveau de l'IA//
int whriteLvlIA();
#endif //PICKOMINO_GRAPHICALFUNCTIONS_H
//
// Created by bardaghji on 05/04/2022.
//
#include "SecondaryFunctions.h"
#include "initialisation.h"
#include <stdio.h>
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void setOnFirst(StackPickomino *l){
l->current = l->first;
}
void setOnLast(StackPickomino *l){
l->current = l->last;
}
int isEmpty(StackPickomino *l){
return (l->first == NULL && l->last == NULL);
}
void setOnNext(StackPickomino *l){
l->current = l->current->next;
}
void setOnPrevious(StackPickomino *l){
l->current = l->current->previous;
}
Pickomino *createPickomino(int val, int warms) {
Pickomino * new = malloc(sizeof(Pickomino));
if(new==NULL){
printf("error allocation Pickomino\n");
EXIT_FAILURE;
}
else {
new->val = val;
new->warms = warms;
new ->returned = 0;
new->previous = NULL;
new->next = NULL;
return new;
}
}
StackPickomino * createStack() {
StackPickomino *new = malloc(sizeof(StackPickomino));
if (new == NULL) {
printf("error allocation Pickomino\n");
EXIT_FAILURE;
} else {
new->current = NULL;
new->first = NULL;
new->last = NULL;
}
}
void addPickominoToMainStack(Pickomino * p, StackPickomino * mainStack){
if(isEmpty(mainStack)){
mainStack->first=p;
mainStack->last=p;
}
else {
setOnFirst(mainStack);
if (mainStack->first->val > p->val) {
p->next = mainStack->first;
mainStack->first->previous = p;
p->previous = NULL;
mainStack->first = p;
} else {
setOnLast(mainStack);
if (mainStack->current->val < p->val){
p->next = NULL;
mainStack->current->next = p;
p->previous = mainStack->current;
mainStack->last = p;
}
setOnFirst(mainStack);
while (mainStack->current->next != NULL) {
if (mainStack->current->val > p->val && mainStack->current->previous->val < p->val) {
p->previous = mainStack->current->previous;
p->next = mainStack->current;
mainStack->current->previous->next = p;
mainStack->current->previous = p;
}
setOnNext(mainStack);
}
}
}
setOnFirst(mainStack);
}
Player * createPlayer(StackPickomino * playerStack, char pseudo[12], HorIA info) {
int i = 0;
Player *newPlayer = malloc(sizeof(Player));
if (newPlayer == NULL) {
printf("error allocating player\n");
EXIT_FAILURE;
}
newPlayer->stack = playerStack;
for (i=0; i<12; i++){
newPlayer->pseudo[i] = pseudo[i];
}
if (info == IA) {
newPlayer->horIA = IA;
}
else {
newPlayer->horIA = human;
return newPlayer;
}
}
void addPlayer(Player * p, Game * g){
if(g->nbplayers>MAX_PLAYERS){
printf("can't add player\n");
exit(1);
}
g->listPlayers[g->nbplayers] = p;
g->nbplayers+=1;
}
Dice * throwDices(Dice listDice[NB_DICES]) {
srand(time(NULL));
int i = 0;
for(i; i < NB_DICES; i++) {
if (listDice[i].sel == 0){
listDice[i].val = ((rand() % 6) + 1);
}
}
return listDice;
}
Dice * keepDice(Dice listDice[NB_DICES], int val_choice){
int i;
for(i = 0; i < NB_DICES; i++) {
if (listDice[i].val == val_choice){
listDice[i].sel = 1;
}
}
return listDice;
}
void addPickominoToPlayer(StackPickomino * playerStack, Pickomino * p){
if(isEmpty(playerStack)){
playerStack->first=p;
playerStack->last=p;
p->previous = NULL;
p->next = NULL;
}
else {
setOnLast(playerStack);
p->next = NULL;
playerStack->current->next = p;
p->previous = playerStack->current;
playerStack->last = p;
}
}
Dice * resetDices(Dice listDice[NB_DICES]){
int i;
for(i = 0; i < NB_DICES; i++) {
listDice[i].sel = 0;
listDice[i].val = 0;
}
return listDice;
}
Game * createGame(){
Game * g = malloc(sizeof(Game));
if (g == NULL) {
printf("error allocation Pickomino\n");
EXIT_FAILURE;
} else {
g->nbplayers = 0;
}
}
int calculDice(Dice listDice[NB_DICES]){
int i;
int val_total = 0;
for(i = 0; i < NB_DICES; i++) {
if(listDice[i].sel == 1) {
if (listDice[i].val == 6) {
val_total += 5;
} else {
val_total += listDice[i].val;
}
}
}
return val_total;
}
void losePickomino(StackPickomino * playerStack, StackPickomino * mainStack){
setOnLast(playerStack);
Pickomino * p = playerStack->current;
if(playerStack->current == playerStack->first){
playerStack->first = NULL;
playerStack->last = NULL;
playerStack->current = NULL;
}
else{
playerStack->current->previous->next = NULL;
playerStack->last = playerStack->current->previous;
setOnFirst(playerStack);
}
addPickominoToMainStack(p,mainStack);
setOnLast(mainStack);
while(mainStack->current->returned == 1){
setOnPrevious(mainStack);
}
if(p->val < mainStack->current->val){
mainStack->current->returned = 1;
}
}
void takePickominoFromPlayer1ToPlayer2(StackPickomino * playerStack1, StackPickomino * playerStack2){
setOnLast(playerStack1);
Pickomino * p = playerStack1->current;
if(playerStack1->current == playerStack1->first){
playerStack1->first = NULL;
playerStack1->last = NULL;
playerStack1->current = NULL;
}
else{
playerStack1->current->previous->next = NULL;
playerStack1->last = playerStack1->current->previous;
setOnFirst(playerStack1);
}
addPickominoToPlayer(playerStack2,p);
}
void takePickominoFromMainStack(StackPickomino * mainStack, StackPickomino * playerStack, int val_search){
setOnFirst(mainStack);
while(mainStack->current->val != val_search){
setOnNext(mainStack);
}
Pickomino * p_save = mainStack->current;
if(mainStack->current->previous == NULL && mainStack->current->next == NULL){
mainStack->last = NULL;
mainStack->first = NULL;
}else{
if(mainStack->current->previous == NULL){
mainStack->current->next->previous = NULL;
mainStack->first = mainStack->current->next;
}else{
if(mainStack->current->next == NULL){
mainStack->current->previous->next = NULL;
mainStack->last = mainStack->current->previous;
}else{
mainStack->current->previous->next = mainStack->current->next;
mainStack->current->next->previous = mainStack->current->previous;
}
}
}
addPickominoToPlayer(playerStack,p_save);
}
//
// Created by eii on 24/02/2022.
//
#ifndef PICKOMINO_SECONDARYFUNCTIONS_H
#define PICKOMINO_SECONDARYFUNCTIONS_H
#include "initialisation.h"
//INIT FUNCTIONS //
Pickomino * createPickomino(int val, int warms);
StackPickomino * createStack();
void addPickominoToMainStack(Pickomino * p, StackPickomino * mainStack);
Player * createPlayer(StackPickomino * playerStack, char pseudo[12], HorIA info);
void addPlayer(Player * p, Game * g);
//GAME FUNCTIONS //
//lance les dés//
Dice * throwDices(Dice listDice[NB_DICES]);
//Declare le dés comme conservé//
Dice * keepDice(Dice listDice[NB_DICES], int val_choice);
//Ajoute un pickomino à un joueur//
void addPickominoToPlayer(StackPickomino * stack, Pickomino * p);
//Réinitialise les dés//
Dice * resetDices(Dice listDice[NB_DICES]);
//Créée une partie//
Game * createGame();
//Calcul la valeur des dés gardés//
int calculDice(Dice listDice[NB_DICES]);
//Prend le dernier pickomino d'un joueur et le met dans la pile principale,//
// quand le joueur perd son tour//
void losePickomino(StackPickomino * playerStack, StackPickomino * mainStack);
//Prend le dernier pickomino d'un joueur et le donne à un autre joueur//
void takePickominoFromPlayer1ToPlayer2(StackPickomino * playerStack1, StackPickomino * playerStack2);
//Prend un pickomino dans la pile principale et le donne au joueur//
void takePickominoFromMainStack(StackPickomino * mainStack, StackPickomino * playerStack, int val);
#endif //PICKOMINO_SECONDARYFUNCTIONS_H
//
// Created by kilyanben on 18/04/22.
//
#include "window.h"
#include <SDL2/SDL.h>
#include "init.h"
SDL_Window *window=NULL;
SDL_Renderer *renderer=NULL;
SDL_Texture *De1 = NULL;
SDL_Texture *De2 = NULL;
SDL_Texture *De3 = NULL;
SDL_Texture *De4 = NULL;
SDL_Texture *De5 = NULL;
SDL_Texture *De6 = NULL;
SDL_Texture *Pick21=NULL;
SDL_Texture *Pick22=NULL;
SDL_Texture *Pick23=NULL;
SDL_Texture *Pick24=NULL;
SDL_Texture *Pick25=NULL;
SDL_Texture *Pick26=NULL;
SDL_Texture *Pick27=NULL;
SDL_Texture *Pick28=NULL;
SDL_Texture *Pick29=NULL;
SDL_Texture *Pick30=NULL;
SDL_Texture *Pick31=NULL;
SDL_Texture *Pick32=NULL;
SDL_Texture *Pick33=NULL;
SDL_Texture *Pick34=NULL;
SDL_Texture *Pick35=NULL;
SDL_Texture *Pick36=NULL;
SDL_Texture *Avatar=NULL;
SDL_Texture *BoutonLanceDe=NULL;
//
// Created by kilyanben on 18/04/22.
//
#ifndef GRAPHICALFUNCTIONS_H_INIT_H
#define GRAPHICALFUNCTIONS_H_INIT_H
#endif //GRAPHICALFUNCTIONS_H_INIT_H
#include <SDL2/SDL.h>
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *De1;
SDL_Texture *De2;
SDL_Texture *De3;
SDL_Texture *De4;
SDL_Texture *De5;
SDL_Texture *De6;
SDL_Texture *Pick21;
SDL_Texture *Pick22;
SDL_Texture *Pick23;
SDL_Texture *Pick24;
SDL_Texture *Pick25;
SDL_Texture *Pick26;
SDL_Texture *Pick27;
SDL_Texture *Pick28;
SDL_Texture *Pick29;
SDL_Texture *Pick30;
SDL_Texture *Pick31;
SDL_Texture *Pick32;
SDL_Texture *Pick33;
SDL_Texture *Pick34;
SDL_Texture *Pick35;
SDL_Texture *Pick36;
SDL_Texture *Avatar;
SDL_Texture *BoutonLanceDe;
\ No newline at end of file
//
// Created by eii on 24/02/2022.
//
#ifndef PICKOMINO_INITIALISATION_H
#define PICKOMINO_INITIALISATION_H
#define MAX_PLAYERS 6
#define NB_DICES 8
typedef enum {nochoice,TakePickomino, ThrowAgain}Choice;
typedef struct Pickomino{
int val;
int warms;
int returned;
struct Pickomino * next;
struct Pickomino * previous;
} Pickomino;
typedef struct {
Pickomino * first;
Pickomino * current;
Pickomino * last;
} StackPickomino;
typedef enum {human,IA}HorIA;
typedef struct{
StackPickomino * stack;
char pseudo[12];
HorIA horIA;
}Player;
typedef struct{
int nbplayers;
Player * listPlayers[MAX_PLAYERS];
StackPickomino * mainStack;
int difficultyIA;
}Game;
typedef struct{
int val;
int sel;
}Dice;
#endif //PICKOMINO_INITIALISATION_H
//
// Created by kilyanben on 05/04/2022.
//
#include "input.h"
#include "init.h"
#include "window.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include "GraphicalFunctions.h"
#include "Secondaryfunctions.h"
Dice *ClicktoThrow(Dice ListDice[NB_DICES], bool *debut_program, SDL_Event event) {
Dice *ListDiceFinal;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
*debut_program = false;
}
else if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
if (event.button.x >= 1700 && event.button.x <= 1828 && event.button.y >= 820 && event.button.y <= 948) {
ListDiceFinal = throwDices(ListDice);
return ListDiceFinal;
}
}
}
}
}
Dice *selDicesToKeep(Dice listDices[NB_DICES], bool *debut_program, SDL_Event event){
int w=78;
int h=75;
int ecart=103;
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
*debut_program= false;
}
else if (event.type== SDL_MOUSEBUTTONDOWN){
if(event.button.x >=700 && event.button.x<=700+w && event.button.y>=820 && event.button.y<=820+h && listDices[0].sel==0){
return keepDice(listDices,listDices[0].val);
}
else if(event.button.x >=700+ecart && event.button.x<=700+ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[1].sel==0){
return keepDice(listDices,listDices[1].val);
}
else if(event.button.x >=700+2*ecart && event.button.x<=700+2*ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[2].sel==0){
return keepDice(listDices,listDices[2].val);
}
else if(event.button.x >=700+3*ecart && event.button.x<=700+3*ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[3].sel==0){
return keepDice(listDices,listDices[3].val);
}
else if(event.button.x >=700+4*ecart && event.button.x<=700+4*ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[4].sel==0){
return keepDice(listDices,listDices[4].val);
}
else if(event.button.x >=700+5*ecart && event.button.x<=700+5*ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[5].sel==0){
return keepDice(listDices,listDices[5].val);
}
else if(event.button.x >=700+6*ecart && event.button.x<=700+6*ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[6].sel==0){
return keepDice(listDices,listDices[6].val);
}
else if(event.button.x >=700+7*ecart && event.button.x<=700+7*ecart+w && event.button.y>=820 && event.button.y<=820+h && listDices[7].sel==0){
return keepDice(listDices,listDices[7].val);
}
}
}
}
/* MAX 4 joueurs avec cette fonction, NE PAS OUBLIER D'AJOUTER LE FAIT QU'UNE PERSONNE PEUT N'AVOIR AUCUN PICKOMINO*/
int selAndTakePickomino(Game *game, int val_total_dice, bool *debut_program,SDL_Event event, int num_joueur) {
int w = 29;
int h = 60;
int ecart = 59;
while (SDL_WaitEventTimeout(&event,30000)) {
if (event.type == SDL_QUIT) {
*debut_program = false;
} else if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.x >= 200 && event.button.x <= 200 + w && event.button.y >= 500 &&
event.button.y <= 500 + h && estpresentetpasretourne(game->mainStack, 21) == 1 &&
val_total_dice >= 21) {
takePickominoFromMainStack(game->mainStack, game->listPlayers[num_joueur - 1]->stack, 21);
return 1;
}
if (event.button.x >= 200 + ecart && event.button.x <= 200 + w + ecart && event.button.y >= 500 &&
event.button.y <= 500 + h && estpresentetpasretourne(game->mainStack, 22) == 1 &&
val_total_dice >= 22) {
takePickominoFromMainStack(game->mainStack, game->listPlayers[num_joueur - 1]->stack, 22);
return 1;
}
if (event.button.x >= 200 + 2 * ecart && event.button.x <= 200 + 2 * ecart