2022-06-21 15:53:54 +02:00
|
|
|
#include "jeu.h"
|
|
|
|
|
2022-06-21 20:08:52 +02:00
|
|
|
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++) {
|
2022-06-21 17:23:25 +02:00
|
|
|
board[i][j] = -1;
|
2022-06-21 15:53:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 20:08:52 +02:00
|
|
|
void draw_board(int board[15][15], int size_tab) {
|
2022-06-21 15:53:54 +02:00
|
|
|
point p1, p2;
|
2022-06-21 20:08:52 +02:00
|
|
|
for(int i = 0; i < size_tab; i++) {
|
|
|
|
for(int j = 0; j < size_tab; j++) {
|
2022-06-21 15:53:54 +02:00
|
|
|
p1.x = i*60+60; p1.y = 0;
|
2022-06-21 20:08:52 +02:00
|
|
|
p2.x = i*60+60; p2.y = 60*size_tab;
|
|
|
|
draw_big_line(p1,p2,lightgrey);
|
2022-06-21 15:53:54 +02:00
|
|
|
p1.y = i*60+60; p1.x = 0;
|
2022-06-21 20:08:52 +02:00
|
|
|
p2.y = i*60+60; p2.x = 60*size_tab;
|
|
|
|
draw_big_line(p1,p2,lightgrey);
|
2022-06-21 17:23:25 +02:00
|
|
|
// draw_player(board, i, j);
|
2022-06-21 15:53:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 20:08:52 +02:00
|
|
|
void draw_player(int board[15][15] ,int i, int j) {
|
2022-06-21 15:53:54 +02:00
|
|
|
point p = {i*60+30, j*60+30};
|
2022-06-21 17:23:25 +02:00
|
|
|
if(board[i][j] == 0)
|
2022-06-21 20:08:52 +02:00
|
|
|
draw_cross(p, 58, blue);
|
2022-06-21 17:23:25 +02:00
|
|
|
if(board[i][j] == 1)
|
2022-06-21 20:08:52 +02:00
|
|
|
draw_circle(p, 29, red);
|
2022-06-21 17:23:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 20:08:52 +02:00
|
|
|
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);
|
2022-06-21 17:23:25 +02:00
|
|
|
int joueur = 1;
|
2022-06-21 20:08:52 +02:00
|
|
|
while(end(board, winner, size_tab, nb)) {
|
2022-06-21 17:23:25 +02:00
|
|
|
point p = wait_clic();
|
|
|
|
int pos[2];
|
|
|
|
pos[0] = p.x/60;
|
|
|
|
pos[1] = p.y/60;
|
|
|
|
if(board[pos[0]][pos[1]]==-1) {
|
|
|
|
board[pos[0]][pos[1]] = joueur;
|
|
|
|
joueur = 1-joueur;
|
|
|
|
}
|
|
|
|
draw_player(board, pos[0], pos[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 20:08:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 17:23:25 +02:00
|
|
|
int k = 0;
|
2022-06-21 20:08:52 +02:00
|
|
|
for(int i = 0; i < size_tab; i++) {
|
|
|
|
for(int j = 0; j < size_tab; j++) {
|
2022-06-21 17:23:25 +02:00
|
|
|
if(board[i][j]!=-1)
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(k>=100) {
|
|
|
|
*winner = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
2022-06-21 20:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
// }
|