fin du jeu

This commit is contained in:
21801009 2022-06-21 22:23:08 +02:00
parent 0a8aa2b7cb
commit 82a6fa5087
3 changed files with 187 additions and 113 deletions

292
jeu.c
View File

@ -1,35 +1,137 @@
#include "jeu.h" #include "jeu.h"
void menu() { void menu() {
// init_graphics(400,400); init_graphics(400,450);
int size_tab = 11; point p = {166,51};
int nb = 3; aff_pol("Quitter", 20, p, darkred);
int board[15][15]; point p1 = {100,50}, p2 = {300,25};
int winner; draw_rectangle(p1, p2, red);
p1.y = 75; p2.y = 100;
draw_rectangle(p1, p2, white);
p1.y = 125; p2.y = 150;
draw_rectangle(p1, p2, orchid);
p1.y = 175; p2.y = 200;
draw_rectangle(p1, p2, lightgreen);
p1.y = 225; p2.y = 250;
draw_rectangle(p1, p2, magenta);
p1.y = 275; p2.y = 300;
draw_rectangle(p1, p2, lightblue);
p.x = 100; p.y = 400;
aff_pol("Morpion", 50, p, gold);
p.x = 145; p.y = 300;
aff_pol("Tic-Tac-Toe", 20, p, lightblue);
p.x = 167, p.y = 250;
aff_pol("10x10", 20, p, magenta);
p.y = 200;
aff_pol("15x15", 20, p, lightgreen);
p.x = 180; p.y = 150;
aff_pol("6x7", 20, p, orchid);
p.x = 167; p.y = 100;
aff_pol("Regles", 20, p, white);
int r = 1; int r = 1;
int board[15][15];
int size_tab_x;
int size_tab_y;
int nb;
int winner;
while(1) {
p = wait_clic();
if(p.x < 250 && p.x > 150) {
if(p.y < 50 && p.y > 25) exit(0);
if(p.y < 100 && p.y > 75) {
init_graphics(550, 450);
p.x = 10, p.y = 425;
aff_pol("Le but du jeu du morpion est d'aligner 3 jetons de la meme couleur", 15, p, white);
p.y = 375;
aff_pol(" en ligne horizontale, verticale ou diagonale.", 15, p, white);
p.y = 325;
aff_pol("Il existe plusieurs variantes :", 15, p, white);
p.y = 275;
aff_pol(" - 10x10 : il faut aligner 5 jetons dans une table de 10*10 cases,", 15, p, white);
p.y = 225;
aff_pol(" - 15x15 : il faut aligner 6 jetons dans une table de 15*15 cases,", 15, p, white);
p.y = 175;
aff_pol(" - 6x7 : il faut aligner 4 jetons dans une table de 7*6 cases.", 15, p, white);
p.y = 125;
aff_pol("Cliquez n'importe ou pour revenir au menu", 15, p, white);
p = wait_clic();
init_graphics(400,450);
p.x = 166; p.y = 51;
aff_pol("Quitter", 20, p, darkred);
p1.x = 100;p1.y = 50; p2.x = 300; p2.y = 25;
draw_rectangle(p1, p2, red);
p1.y = 75; p2.y = 100;
draw_rectangle(p1, p2, white);
p1.y = 125; p2.y = 150;
draw_rectangle(p1, p2, orchid);
p1.y = 175; p2.y = 200;
draw_rectangle(p1, p2, lightgreen);
p1.y = 225; p2.y = 250;
draw_rectangle(p1, p2, magenta);
p1.y = 275; p2.y = 300;
draw_rectangle(p1, p2, lightblue);
p.x = 100; p.y = 400;
aff_pol("Morpion", 50, p, gold);
p.x = 145; p.y = 300;
aff_pol("Tic-Tac-Toe", 20, p, lightblue);
p.x = 167, p.y = 250;
aff_pol("10x10", 20, p, magenta);
p.y = 200;
aff_pol("15x15", 20, p, lightgreen);
p.x = 170; p.y = 150;
aff_pol("6x7", 20, p, orchid);
p.x = 167; p.y = 100;
aff_pol("Regles", 20, p, white);
}
if(p.y < 150 && p.y > 125) {
size_tab_x = 7;
size_tab_y = 6;
nb = 3;
break;
}
if(p.y < 200 && p.y > 175) {
size_tab_x = 15;
size_tab_y = 15;
nb = 5;
break;
}
if(p.y < 250 && p.y > 225) {
size_tab_x = 10;
size_tab_y = 10;
nb = 4;
break;
}
if(p.y < 300 && p.y > 275) {
size_tab_x = 3;
size_tab_y = 3;
nb = 2;
break;
}
}
}
while(r) { while(r) {
play(board, &winner, size_tab, nb); play(board, &winner, size_tab_x, size_tab_y, nb);
r = replay(winner); r = replay(winner);
} }
} }
void init_board(int board[15][15], int size_tab) { void init_board(int board[15][15], int size_tab_x, int size_tab_y) {
for(int i = 0; i < size_tab; i++) { for(int i = 0; i < size_tab_x; i++) {
for(int j = 0; j < size_tab; j++) { for(int j = 0; j < size_tab_y; j++) {
board[i][j] = -1; board[i][j] = -1;
} }
} }
} }
void draw_board(int board[15][15], int size_tab) { void draw_board(int board[15][15], int size_tab_x, int size_tab_y) {
point p1, p2; point p1, p2;
for(int i = 0; i < size_tab; i++) { for(int i = 0; i < size_tab_x; i++) {
for(int j = 0; j < size_tab; j++) { for(int j = 0; j < size_tab_y; j++) {
p1.x = i*60+60; p1.y = 0; p1.x = i*60+60; p1.y = 0;
p2.x = i*60+60; p2.y = 60*size_tab; p2.x = i*60+60; p2.y = 60*size_tab_y;
draw_big_line(p1,p2,lightgrey); draw_big_line(p1,p2,lightgrey);
p1.y = i*60+60; p1.x = 0; p1.y = i*60+60; p1.x = 0;
p2.y = i*60+60; p2.x = 60*size_tab; p2.y = i*60+60; p2.x = 60*size_tab_x;
draw_big_line(p1,p2,lightgrey); draw_big_line(p1,p2,lightgrey);
// draw_player(board, i, j); // draw_player(board, i, j);
} }
@ -44,12 +146,12 @@ void draw_player(int board[15][15] ,int i, int j) {
draw_circle(p, 29, red); draw_circle(p, 29, red);
} }
void play(int board[15][15], int *winner, int size_tab, int nb) { void play(int board[15][15], int *winner, int size_tab_x, int size_tab_y, int nb) {
init_graphics(60*size_tab,60*size_tab); init_graphics(60*size_tab_x,60*size_tab_y);
init_board(board, size_tab); init_board(board, size_tab_x, size_tab_y);
draw_board(board, size_tab); draw_board(board, size_tab_x, size_tab_y);
int joueur = 1; int joueur = 1;
while(end(board, winner, size_tab, nb)) { while(end(board, winner, size_tab_x, size_tab_y, nb)) {
point p = wait_clic(); point p = wait_clic();
int pos[2]; int pos[2];
pos[0] = p.x/60; pos[0] = p.x/60;
@ -62,59 +164,23 @@ void play(int board[15][15], int *winner, int size_tab, int nb) {
} }
} }
int end(int board[15][15], int *winner, int size_tab, int nb) { int end(int board[15][15], int *winner, int size_tab_x, int size_tab_y, int nb) {
for(int i = 0; i < size_tab; i++) { for(int i = 0; i < size_tab_x; i++) {
for(int j = 0; j < size_tab; j++) { for(int j = 0; j < size_tab_y; j++) {
if(nb == 3) { if(board[i][j] != -1 ) {
if(i+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j]) { if(i+nb < size_tab_x && test_x(board, i, j, nb)) {
*winner = board[i][j]; *winner = board[i][j];
return 0; return 0;
} }
if(j+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3]) { if(j+nb < size_tab_y && test_y(board, i, j, nb)) {
*winner = board[i][j]; *winner = board[i][j];
return 0; return 0;
} }
if(i+size_tab < size_tab && j+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3]) { if(i+nb < size_tab_x && j+nb < size_tab_y && test_xy(board, i, j, nb)) {
*winner = board[i][j]; *winner = board[i][j];
return 0; return 0;
} }
if(i+size_tab < size_tab && j-size_tab > 0 && board[i][j] != -1 && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3]) { if(i+nb < size_tab_x && j-nb > 0 && test_yx(board, i, j, nb)) {
*winner = board[i][j];
return 0;
}
}
if(nb == 4) {
if(i+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j] && board[i][j]==board[i+4][j]) {
*winner = board[i][j];
return 0;
}
if(j+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3] && board[i][j]==board[i][j+4]) {
*winner = board[i][j];
return 0;
}
if(i+size_tab < size_tab && j+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3] && board[i][j]==board[i+4][j+4]) {
*winner = board[i][j];
return 0;
}
if(i+size_tab < size_tab && j-size_tab > 0 && board[i][j] != -1 && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3] && board[i][j]==board[i+4][j-4]) {
*winner = board[i][j];
return 0;
}
}
if(nb == 5) {
if(i+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j] && board[i][j]==board[i+4][j] && board[i][j]==board[i+5][j]) {
*winner = board[i][j];
return 0;
}
if(j+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3] && board[i][j]==board[i][j+4] && board[i][j]==board[i][j+5]) {
*winner = board[i][j];
return 0;
}
if(i+size_tab < size_tab && j+size_tab < size_tab && board[i][j] != -1 && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3] && board[i][j]==board[i+4][j+4] && board[i][j]==board[i+5][j+5]) {
*winner = board[i][j];
return 0;
}
if(i+size_tab < size_tab && j-size_tab > 0 && board[i][j] != -1 && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3] && board[i][j]==board[i+4][j-4] && board[i][j]==board[i+5][j-5]) {
*winner = board[i][j]; *winner = board[i][j];
return 0; return 0;
} }
@ -122,8 +188,8 @@ int end(int board[15][15], int *winner, int size_tab, int nb) {
} }
} }
int k = 0; int k = 0;
for(int i = 0; i < size_tab; i++) { for(int i = 0; i < size_tab_x; i++) {
for(int j = 0; j < size_tab; j++) { for(int j = 0; j < size_tab_y; j++) {
if(board[i][j]!=-1) if(board[i][j]!=-1)
k++; k++;
} }
@ -170,50 +236,58 @@ int replay(int winner) {
return 0; return 0;
} }
// int test_x(int board[15][15], int i, int j, int nbtest){ int test_x(int board[15][15], int i, int j, int nbtest){
// if(nbtest == 5) if(nbtest == 5)
// return board[i][j] != -1 && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j] && board[i][j]==board[i+4][j] && board[i][j]==board[i+5][j]; return board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j] && board[i][j]==board[i+4][j] && board[i][j]==board[i+5][j];
// if(nbtest == 4) if(nbtest == 4)
// return board[i][j] != -1 && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j] && board[i][j]==board[i+4][j]; return board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j] && board[i][j]==board[i+4][j];
// if(nbtest == 3) if(nbtest == 3)
// return board[i][j] != -1 && board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j]; return board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j] && board[i][j]==board[i+3][j];
// else if(nbtest == 2)
// exit(0); return board[i][j]==board[i+1][j] && board[i][j]==board[i+2][j];
// return 1; else
// } exit(0);
return 1;
}
// int test_y(int board[15][15], int i, int j, int nbtest){ int test_y(int board[15][15], int i, int j, int nbtest){
// if(nbtest == 5) if(nbtest == 5)
// return board[i][j] != -1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3] && board[i][j]==board[i][j+4] && board[i][j]==board[i][j+5]; return board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3] && board[i][j]==board[i][j+4] && board[i][j]==board[i][j+5];
// if(nbtest == 4) if(nbtest == 4)
// return board[i][j] != -1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3] && board[i][j]==board[i][j+4]; return board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3] && board[i][j]==board[i][j+4];
// if(nbtest == 3) if(nbtest == 3)
// return board[i][j] != -1 && board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3]; return board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2] && board[i][j]==board[i][j+3];
// else if(nbtest == 2)
// exit(0); return board[i][j]==board[i][j+1] && board[i][j]==board[i][j+2];
// return 1; else
// } exit(0);
return 1;
}
// int test_xy(int board[15][15], int i, int j, int nbtest){ int test_xy(int board[15][15], int i, int j, int nbtest){
// if(nbtest == 5) if(nbtest == 5)
// return board[i][j] != -1 && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3] && board[i][j]==board[i+4][j+4] && board[i][j]==board[i+5][j+5]; return board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3] && board[i][j]==board[i+4][j+4] && board[i][j]==board[i+5][j+5];
// if(nbtest == 4) if(nbtest == 4)
// return board[i][j] != -1 && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3] && board[i][j]==board[i+4][j+4]; return board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3] && board[i][j]==board[i+4][j+4];
// if(nbtest == 3) if(nbtest == 3)
// return board[i][j] != -1 && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3]; return board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+3];
// else if(nbtest == 2)
// exit(0); return board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2];
// return 1; else
// } exit(0);
return 1;
}
// int test_yx(int board[15][15], int i, int j, int nbtest){ int test_yx(int board[15][15], int i, int j, int nbtest){
// if(nbtest == 5) if(nbtest == 5)
// return board[i][j] != -1 && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3] && board[i][j]==board[i+4][j-4] && board[i][j]==board[i+5][j-5]; return board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3] && board[i][j]==board[i+4][j-4] && board[i][j]==board[i+5][j-5];
// if(nbtest == 4) if(nbtest == 4)
// return board[i][j] != -1 && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3] && board[i][j]==board[i+4][j-4]; return board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3] && board[i][j]==board[i+4][j-4];
// if(nbtest == 3) if(nbtest == 3)
// return board[i][j] != -1 && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3]; return board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-3];
// else if(nbtest == 2)
// exit(0); return board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2];
// return 1; else
// } exit(0);
return 1;
}

8
jeu.h
View File

@ -4,15 +4,15 @@
void menu(); void menu();
void init_board(int[15][15], int); void init_board(int[15][15], int, int);
void draw_board(int[15][15], int); void draw_board(int[15][15], int, int);
void draw_player(int[15][15] ,int, int); void draw_player(int[15][15] ,int, int);
void play(int[15][15], int*, int, int); void play(int[15][15], int*, int, int, int);
int end(int[15][15], int*, int, int); int end(int[15][15], int*, int, int, int);
int replay(int winner); int replay(int winner);

BIN
morpion

Binary file not shown.