modif jeu avec taille variables
This commit is contained in:
parent
84329b651b
commit
0a8aa2b7cb
17
graphics.c
17
graphics.c
@ -37,7 +37,7 @@ void init_graphics(int W, int H) {
|
||||
|
||||
SDL_EnableKeyRepeat(1,0);
|
||||
|
||||
SDL_WM_SetCaption("Puissance 4!!",NULL);
|
||||
SDL_WM_SetCaption("Morpion !!",NULL);
|
||||
|
||||
__init_graphics_is_already_called = 25;
|
||||
printf("Fenetre de %d x %d\n",WIDTH,HEIGHT);
|
||||
@ -56,7 +56,7 @@ void init_graphics(int W, int H) {
|
||||
printf("SDL_ttf absent : affichage dans la console.\n");
|
||||
#endif
|
||||
|
||||
fill_screen(white);
|
||||
fill_screen(black);
|
||||
affiche_auto_on();
|
||||
affiche_all();
|
||||
|
||||
@ -258,6 +258,19 @@ void draw_line(point p1, point p2, couleur color) {
|
||||
if (SDL_AFFICHE_AUTO) affiche_all();
|
||||
}
|
||||
|
||||
void draw_big_line(point p1, point p2, couleur color) {
|
||||
draw_line(p1, p2, color);
|
||||
p1.x = p1.x-1;p2.x = p2.x-1;
|
||||
draw_line(p1, p2, color);
|
||||
p1.x = p1.x+2;p2.x = p2.x+2;
|
||||
draw_line(p1, p2, color);
|
||||
p1.x = p1.x-1;p2.x = p2.x-1;
|
||||
p1.y = p1.y-1;p2.y = p2.y-1;
|
||||
draw_line(p1, p2, color);
|
||||
p1.y = p1.y+2;p2.y = p2.y+2;
|
||||
draw_line(p1, p2, color);
|
||||
}
|
||||
|
||||
void draw_rectangle(point p1, point p2, couleur color) {
|
||||
int xmin, xmax;
|
||||
int ymin, ymax;
|
||||
|
@ -43,6 +43,7 @@ point wait_clic();
|
||||
void fill_screen(couleur color);
|
||||
void draw_pixel(point p, couleur color);
|
||||
void draw_line(point p1, point p2, couleur color);
|
||||
void draw_big_line(point p1, point p2, couleur color);
|
||||
void draw_rectangle(point p1, point p2, couleur color);
|
||||
void draw_fill_rectangle(point p1, point p2, couleur color);
|
||||
void draw_circle(point centre, int rayon, couleur color);
|
||||
|
198
jeu.c
198
jeu.c
@ -1,40 +1,55 @@
|
||||
#include "jeu.h"
|
||||
|
||||
void init_board(int board[10][10]) {
|
||||
for(int i = 0; i < 10; i++) {
|
||||
for(int j = 0; j < 10; j++) {
|
||||
void menu() {
|
||||
// init_graphics(400,400);
|
||||
int size_tab = 11;
|
||||
int nb = 3;
|
||||
int board[15][15];
|
||||
int winner;
|
||||
int r = 1;
|
||||
while(r) {
|
||||
play(board, &winner, size_tab, nb);
|
||||
r = replay(winner);
|
||||
}
|
||||
}
|
||||
|
||||
void init_board(int board[15][15], int size_tab) {
|
||||
for(int i = 0; i < size_tab; i++) {
|
||||
for(int j = 0; j < size_tab; j++) {
|
||||
board[i][j] = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_board(int board[10][10]) {
|
||||
void draw_board(int board[15][15], int size_tab) {
|
||||
point p1, p2;
|
||||
for(int i = 0; i < 10; i++) {
|
||||
for(int j = 0; j < 10; j++) {
|
||||
for(int i = 0; i < size_tab; i++) {
|
||||
for(int j = 0; j < size_tab; j++) {
|
||||
p1.x = i*60+60; p1.y = 0;
|
||||
p2.x = i*60+60; p2.y = 600;
|
||||
draw_line(p1,p2,black);
|
||||
p2.x = i*60+60; p2.y = 60*size_tab;
|
||||
draw_big_line(p1,p2,lightgrey);
|
||||
p1.y = i*60+60; p1.x = 0;
|
||||
p2.y = i*60+60; p2.x = 600;
|
||||
draw_line(p1,p2,black);
|
||||
p2.y = i*60+60; p2.x = 60*size_tab;
|
||||
draw_big_line(p1,p2,lightgrey);
|
||||
// draw_player(board, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_player(int board[10][10] ,int i, int j) {
|
||||
void draw_player(int board[15][15] ,int i, int j) {
|
||||
point p = {i*60+30, j*60+30};
|
||||
if(board[i][j] == 0)
|
||||
draw_cross(p, 60, blue);
|
||||
draw_cross(p, 58, blue);
|
||||
if(board[i][j] == 1)
|
||||
draw_circle(p, 30, red);
|
||||
draw_circle(p, 29, red);
|
||||
}
|
||||
|
||||
void play(int board[10][10]) {
|
||||
int* winner = malloc(sizeof(int));
|
||||
void play(int board[15][15], int *winner, int size_tab, int nb) {
|
||||
init_graphics(60*size_tab,60*size_tab);
|
||||
init_board(board, size_tab);
|
||||
draw_board(board, size_tab);
|
||||
int joueur = 1;
|
||||
while(fin(board, winner)) {
|
||||
while(end(board, winner, size_tab, nb)) {
|
||||
point p = wait_clic();
|
||||
int pos[2];
|
||||
pos[0] = p.x/60;
|
||||
@ -45,14 +60,70 @@ void play(int board[10][10]) {
|
||||
}
|
||||
draw_player(board, pos[0], pos[1]);
|
||||
}
|
||||
free(winner);
|
||||
}
|
||||
|
||||
int fin(int board[10][10], int *winner) {
|
||||
|
||||
int end(int board[15][15], int *winner, int size_tab, int nb) {
|
||||
for(int i = 0; i < size_tab; i++) {
|
||||
for(int j = 0; j < size_tab; j++) {
|
||||
if(nb == 3) {
|
||||
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]) {
|
||||
*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]) {
|
||||
*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]) {
|
||||
*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]) {
|
||||
*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];
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int k = 0;
|
||||
for(int i = 0; i < 10; i++) {
|
||||
for(int j = 0; j < 10; j++) {
|
||||
for(int i = 0; i < size_tab; i++) {
|
||||
for(int j = 0; j < size_tab; j++) {
|
||||
if(board[i][j]!=-1)
|
||||
k++;
|
||||
}
|
||||
@ -62,4 +133,87 @@ int fin(int board[10][10], int *winner) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int replay(int winner) {
|
||||
init_graphics(300,200);
|
||||
point pm; pm.y = 150;
|
||||
if(winner==-1) {
|
||||
pm.x = 100;
|
||||
aff_pol("Match Nul", 20, pm, white);
|
||||
}
|
||||
if(winner==0) {
|
||||
pm.x = 42;
|
||||
aff_pol("Joueur bleu a gagne !", 20, pm, blue);
|
||||
}
|
||||
if(winner==1) {
|
||||
pm.x = 40;
|
||||
aff_pol("Joueur rouge a gagne !", 20, pm, red);
|
||||
}
|
||||
point p = {101,86};
|
||||
aff_pol("Rejouer ?", 20, p, white);
|
||||
point p1 = {100,85}, p2 = {200,60};
|
||||
draw_rectangle(p1, p2, white);
|
||||
p.y = 51;
|
||||
aff_pol("Quitter ?", 20, p, white);
|
||||
p1.y = 50; p2.y = 25;
|
||||
draw_rectangle(p1, p2, white);
|
||||
while(1) {
|
||||
p = wait_clic();
|
||||
if(p.x < 200 && p.x > 100) {
|
||||
if(p.y < 85 && p.y > 60)
|
||||
return 1;
|
||||
if(p.y < 50 && p.y > 25)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// int test_x(int board[15][15], int i, int j, int nbtest){
|
||||
// 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];
|
||||
// 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];
|
||||
// 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];
|
||||
// else
|
||||
// exit(0);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// int test_y(int board[15][15], int i, int j, int nbtest){
|
||||
// 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];
|
||||
// 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];
|
||||
// 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];
|
||||
// else
|
||||
// exit(0);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// int test_xy(int board[15][15], int i, int j, int nbtest){
|
||||
// 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];
|
||||
// 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];
|
||||
// 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];
|
||||
// else
|
||||
// exit(0);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
// int test_yx(int board[15][15], int i, int j, int nbtest){
|
||||
// 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];
|
||||
// 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];
|
||||
// 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];
|
||||
// else
|
||||
// exit(0);
|
||||
// return 1;
|
||||
// }
|
||||
|
24
jeu.h
24
jeu.h
@ -2,14 +2,28 @@
|
||||
#define JEU_H
|
||||
#include "graphics.h"
|
||||
|
||||
void init_board(int[10][10]);
|
||||
void menu();
|
||||
|
||||
void draw_board(int[10][10]);
|
||||
void init_board(int[15][15], int);
|
||||
|
||||
void draw_player(int[10][10] ,int, int);
|
||||
void draw_board(int[15][15], int);
|
||||
|
||||
void play(int[10][10]);
|
||||
void draw_player(int[15][15] ,int, int);
|
||||
|
||||
int fin(int[10][10], int*);
|
||||
void play(int[15][15], int*, int, int);
|
||||
|
||||
int end(int[15][15], int*, int, int);
|
||||
|
||||
int replay(int winner);
|
||||
|
||||
void menu();
|
||||
|
||||
int test_x(int[15][15], int, int, int);
|
||||
|
||||
int test_y(int[15][15], int, int, int);
|
||||
|
||||
int test_xy(int[15][15], int, int, int);
|
||||
|
||||
int test_yx(int[15][15], int, int, int);
|
||||
|
||||
#endif
|
10
main.c
10
main.c
@ -1,14 +1,6 @@
|
||||
#include "jeu.h"
|
||||
|
||||
int main() {
|
||||
int board[10][10];
|
||||
init_graphics(600,600);
|
||||
fill_screen(white);
|
||||
init_board(board);
|
||||
// board[2][6] = 1;
|
||||
// board[3][8] = 2;
|
||||
draw_board(board);
|
||||
play(board);
|
||||
wait_escape();
|
||||
menu();
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user