Merge branch 'WIP' into main

This commit is contained in:
Theophyl Hoguet committed 2021-11-14 21:02:24 +01:00
commit 0500546263
3 files changed
+308 -51

No files matched your search

+1
View File
@@ -29,6 +29,7 @@ add_test(test_thoguet_default ./game_test_thoguet game_default)
add_test(test_thoguet_game_default_solution ./game_test_thoguet game_default_solution)
add_test(test_game_restart ./game_test_thoguet game_restart)
add_test(test_game_is_over ./game_test_thoguet game_is_over)
add_test(test_game_update_flags ./game_test_thoguet game_update_flags)
add_test(test_thperrier_dummy ./game_test_thperrier dummy)
add_test(test_thperrier_game_new_empty ./game_test_thperrier game_new_empty)
+26 -26
View File
@@ -5,29 +5,29 @@ Projet Techno
:warning: <b>LE PREMIER QUI COMMIT DU CODE QUI COMPILE PAS JE L'ATTRAPE </b> :warning:
## Tests
| Test | REF | Assigned to | Notes | Finished |
| --------------------------------------------------- | ---------------------------------------------------------------------------- | ----------- | -------------------------------------------------------- | :--------------------: |
| [game_new](./game_test_thoguet.c#L49) | [game game_new(square *squares)](./game.h#L73-81) | @thoguet | | <ul><li>[x] </li></ul> |
| [game_new_empty](./game_test_thperrier.c#L16) | [game game_new_empty(void)](./game.h#L83-88) | @thperrier | | <ul><li>[X] </li></ul> |
| [game_copy](./game_test_thperrier.c#L32) | [game game_copy(cgame g)](./game.h#L90-96) | @thperrier | | <ul><li>[X] </li></ul> |
| [game_equal](./game_test_thperrier.c#L?) | [bool game_equal(cgame g1, cgame g2)](./game.h#L98-106) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_delete](./game_test_thperrier.c#L?) | [void game_delete(game g);](./game.h#L108-113) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_set_square](./game_test_nnunes.c#L?) | [void game_set_square(game g, uint i, uint j, square s)](./game.h#L115-128) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_get_square](./game_test_thoguet.c#L89) | [square game_get_square(cgame g, uint i, uint j)](./game.h#L130-140) | @thoguet | | <ul><li>[x] </li></ul> |
| [game_get_state](./game_test_nnunes.c#L?) | [square game_get_state(cgame g, uint i, uint j)](./game.h#L142-153) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_get_flags](./game_test_nnunes.c#L?) | [square game_get_flags(cgame g, uint i, uint j)](./game.h#L155-166) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_is_blank](./game_test_thperrier.c#L?) | [bool game_is_blank(cgame g, uint i, uint j)](./game.h#L168-178) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_is_lightbulb](./game_test_thperrier.c#L?) | [bool game_is_lightbulb(cgame g, uint i, uint j)](./game.h#L180-190) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_is_black](./game_test_thperrier.c#L?) | [bool game_is_black(cgame g, uint i, uint j)](./game.h#L192-202) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_get_black_number](./game_test_thperrier.c#L?) | [int game_get_black_number(cgame g, uint i, uint j)](./game.h#L204-214) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_is_marked](./game_test_nnunes.c#L?) | [bool game_is_marked(cgame g, uint i, uint j)](./game.h#L216-226) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_is_lighted](./game_test_nnunes.c#L?) | [bool game_is_lighted(cgame g, uint i, uint j)](./game.h#L228-238) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_has_error](./game_test_nnunes.c#L?) | [bool game_has_error(cgame g, uint i, uint j)](./game.h#L240-252) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_check_move](./game_test_nnunes.c#L?) | [bool game_check_move(cgame g, uint i, uint j, square s)](./game.h#L242-269) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_play_move](./game_test_nnunes.c#L?) | [void game_play_move(game g, uint i, uint j, square s)](./game.h#L271-284) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_update_flags](./game_test_thoguet.c#L?) | [void game_update_flags(game g)](./game.h#L286-293) | @thoguet | finir checks walls / faire les autres directions / debug | <ul><li>[ ] </li></ul> |
| [game_is_over](./game_test_thoguet.c#L17) | [bool game_is_over(cgame g)](./game.h#L295-304) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_restart](./game_test_thoguet.c#L37) | [void game_restart(game g)](./game.h#L306-313) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_print](./game_test_thoguet.c#L70) | [void game_print(cgame g)](./game_aux.h#L13-20) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_default](./game_test_thoguet.c#L107) | [game game_default(void)](./game_aux.h#L22-28) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_default_solution](./game_test_thoguet.c#L125) | [game game_default_solution(void)](./game_aux.h#L30-36) | @thoguet | | <ul><li>[X] </li></ul> |
| Test | REF | Assigned to | Notes | Finished |
| --------------------------------------------------- | ---------------------------------------------------------------------------- | ----------- | ------------------------ | :--------------------: |
| [game_new](./game_test_thoguet.c#L49) | [game game_new(square *squares)](./game.h#L73-81) | @thoguet | | <ul><li>[x] </li></ul> |
| [game_new_empty](./game_test_thperrier.c#L16) | [game game_new_empty(void)](./game.h#L83-88) | @thperrier | | <ul><li>[X] </li></ul> |
| [game_copy](./game_test_thperrier.c#L32) | [game game_copy(cgame g)](./game.h#L90-96) | @thperrier | | <ul><li>[X] </li></ul> |
| [game_equal](./game_test_thperrier.c#L?) | [bool game_equal(cgame g1, cgame g2)](./game.h#L98-106) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_delete](./game_test_thperrier.c#L?) | [void game_delete(game g);](./game.h#L108-113) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_set_square](./game_test_nnunes.c#L?) | [void game_set_square(game g, uint i, uint j, square s)](./game.h#L115-128) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_get_square](./game_test_thoguet.c#L89) | [square game_get_square(cgame g, uint i, uint j)](./game.h#L130-140) | @thoguet | | <ul><li>[x] </li></ul> |
| [game_get_state](./game_test_nnunes.c#L?) | [square game_get_state(cgame g, uint i, uint j)](./game.h#L142-153) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_get_flags](./game_test_nnunes.c#L?) | [square game_get_flags(cgame g, uint i, uint j)](./game.h#L155-166) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_is_blank](./game_test_thperrier.c#L?) | [bool game_is_blank(cgame g, uint i, uint j)](./game.h#L168-178) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_is_lightbulb](./game_test_thperrier.c#L?) | [bool game_is_lightbulb(cgame g, uint i, uint j)](./game.h#L180-190) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_is_black](./game_test_thperrier.c#L?) | [bool game_is_black(cgame g, uint i, uint j)](./game.h#L192-202) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_get_black_number](./game_test_thperrier.c#L?) | [int game_get_black_number(cgame g, uint i, uint j)](./game.h#L204-214) | @thperrier | | <ul><li>[ ] </li></ul> |
| [game_is_marked](./game_test_nnunes.c#L?) | [bool game_is_marked(cgame g, uint i, uint j)](./game.h#L216-226) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_is_lighted](./game_test_nnunes.c#L?) | [bool game_is_lighted(cgame g, uint i, uint j)](./game.h#L228-238) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_has_error](./game_test_nnunes.c#L?) | [bool game_has_error(cgame g, uint i, uint j)](./game.h#L240-252) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_check_move](./game_test_nnunes.c#L?) | [bool game_check_move(cgame g, uint i, uint j, square s)](./game.h#L242-269) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_play_move](./game_test_nnunes.c#L?) | [void game_play_move(game g, uint i, uint j, square s)](./game.h#L271-284) | @nnunes | | <ul><li>[ ] </li></ul> |
| [game_update_flags](./game_test_thoguet.c#L?) | [void game_update_flags(game g)](./game.h#L145-406) | @thoguet | not sure if it efficient | <ul><li>[X] </li></ul> |
| [game_is_over](./game_test_thoguet.c#L17) | [bool game_is_over(cgame g)](./game.h#L295-304) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_restart](./game_test_thoguet.c#L37) | [void game_restart(game g)](./game.h#L306-313) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_print](./game_test_thoguet.c#L70) | [void game_print(cgame g)](./game_aux.h#L13-20) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_default](./game_test_thoguet.c#L107) | [game game_default(void)](./game_aux.h#L22-28) | @thoguet | | <ul><li>[X] </li></ul> |
| [game_default_solution](./game_test_thoguet.c#L125) | [game game_default_solution(void)](./game_aux.h#L30-36) | @thoguet | | <ul><li>[X] </li></ul> |
+281 -25
View File
@@ -5,9 +5,9 @@
#include <assert.h>
#include "game.h"
#include "game_aux.h"
typedef unsigned int uint;
/* ********** DUMMY ********** */
bool test_dummy(void){
return EXIT_SUCCESS;
}
@@ -15,19 +15,12 @@ bool test_dummy(void){
/* ******* game_is_over ******* */
bool test_game_is_over(void){
square tab[49] =
{
S_LIGHTBULB,S_BLANK ,S_BLACK1 ,S_LIGHTBULB,S_BLANK ,S_BLANK ,S_BLANK ,
S_BLANK ,S_LIGHTBULB,S_BLACK2 ,S_BLANK ,S_BLANK ,S_BLANK ,S_LIGHTBULB,
S_BLANK ,S_BLANK ,S_LIGHTBULB,S_BLANK ,S_BLANK ,S_BLACKU ,S_BLACK2 ,
S_BLANK ,S_BLANK ,S_BLANK ,S_BLANK ,S_BLANK ,S_BLANK ,S_LIGHTBULB,
S_BLACK1 ,S_BLACKU ,S_BLANK ,S_BLANK ,S_LIGHTBULB,S_BLANK ,S_BLANK ,
S_LIGHTBULB,S_BLANK ,S_BLANK ,S_BLANK ,S_BLACK2 ,S_LIGHTBULB,S_BLANK ,
S_BLANK ,S_LIGHTBULB,S_BLANK ,S_BLANK ,S_BLACKU ,S_BLANK ,S_BLANK
};
game g = game_new(tab);
game g = game_default_solution();
game_update_flags(g);
assert(game_is_over(g));
g = game_default();
game_update_flags(g);
assert(!game_is_over(g));
game_delete(g);
return EXIT_SUCCESS;
}
@@ -49,11 +42,11 @@ bool test_game_restart(void){
bool test_game_new(void){
square tab[DEFAULT_SIZE*DEFAULT_SIZE];
game g;
int tab2[] = {0,1,2,8,9,10,11,12,13,16,32};
for (int i = 0; i < 11; i ++){
square list[] = {S_BLANK,S_LIGHTBULB,S_MARK,S_BLACK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,F_ERROR};
for (uint i = 0; i < sizeof(list)/sizeof(list[0]) ; i ++){
g = NULL;
for (int j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){
tab[j] = tab2[i];
for (uint j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){
tab[j] = list[i];
}
printf("%d\n",i);
g = game_new(tab);
@@ -70,11 +63,11 @@ bool test_game_new(void){
bool test_game_print(void){
game_print(NULL);
square tab[DEFAULT_SIZE*DEFAULT_SIZE];
int tab2[] = {0,1,2,8,9,10,11,12,13,16,32};
square list[] = {S_BLANK,S_LIGHTBULB,S_MARK,S_BLACK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,F_ERROR};
game g;
for (int i = 0; i < 11; i ++){
for (int j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){
tab[j] = tab2[i];
for (uint i = 0; i < sizeof(list)/sizeof(list[0]) ; i ++){
for (uint j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){
tab[j] = list[i];
}
printf("%d\n",i);
g = game_new(tab);
@@ -89,10 +82,10 @@ bool test_game_print(void){
bool test_game_get_square(void){
game g;
square tab[DEFAULT_SIZE*DEFAULT_SIZE];
int tab2[] = {0,1,2,8,9,10,11,12,13,16,32};
for (int i = 0; i < 11; i ++){
for (int j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){
tab[j] = tab2[i];
square list[] = {S_BLANK,S_LIGHTBULB,S_MARK,S_BLACK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,F_ERROR};
for (uint i = 0; i < sizeof(list)/sizeof(list[0]) ; i ++){
for (uint j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){
tab[j] = list[i];
g = game_new(tab);
printf("[%d, %d] %d %d\n",j/(DEFAULT_SIZE),j-(j/(DEFAULT_SIZE))-(j/(DEFAULT_SIZE)*(DEFAULT_SIZE-1)),game_get_square(g,j/(DEFAULT_SIZE),j-(j/(DEFAULT_SIZE))-(j/(DEFAULT_SIZE)*(DEFAULT_SIZE-1))),tab[j]);
assert(game_get_square(g,j/(DEFAULT_SIZE),j-(j/(DEFAULT_SIZE))-(j/(DEFAULT_SIZE)*(DEFAULT_SIZE-1))) == tab[j]);
@@ -124,7 +117,6 @@ bool test_game_default(void){
bool test_game_default_solution(void){
game g = game_default_solution();
//a verif aps sur
assert(g);
//default
assert(game_get_square(g,0,2) == S_BLACK1);
@@ -150,6 +142,268 @@ bool test_game_default_solution(void){
return EXIT_SUCCESS;
}
/* **** game_update_flags **** */
int checklightbulb(game g, uint i, uint j, bool wall){
square flags = game_get_flags(g,i,j);
square state = game_get_state(g,i,j);
//test if the current case is a lightbulb and if it has been well updated if true we can break the loop because every others cases wont be lighted by the bulb we are testing
if (game_is_lightbulb(g,i,j)){
if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR){
//printf("156\n");
return 0;
}
return -1;
}
//if no wall(s) before test if the light of the lightbulb has been well updated
if (!wall){
//test if the current case is not lighted
if (flags != F_LIGHTED){
//printf("170\n");
return 0;
}
}
else{
if (/*test if the current case is lighted*/ flags == F_LIGHTED){
//printf("i:%d j:%d\n",i,j);
for (int i2 = i-1 ; i2 >= 0; i2--){
flags = game_get_flags(g,i2,j);
state = game_get_state(g,i2,j);
//printf("i2:%d i2state:%d i2flags:%d\n",i2,state,flags);
if (state == S_LIGHTBULB){
return -1;
}
if (flags != F_LIGHTED){
break;
}
}
for (int i2 = i+1 ; i2 < DEFAULT_SIZE; i2++){
flags = game_get_flags(g,i2,j);
state = game_get_state(g,i2,j);
if (state == S_LIGHTBULB){
return -1;
}
if (flags != F_LIGHTED){
break;
}
}
for (int j2 = j-1 ; j2 >= 0; j2--){
flags = game_get_flags(g,i,j2);
state = game_get_state(g,i,j2);
if (state == S_LIGHTBULB){
return -1;
}
if (flags != F_LIGHTED){
break;
}
}
for (int j2 = j+1 ; j2 < DEFAULT_SIZE; j2++){
flags = game_get_flags(g,i,j2);
state = game_get_state(g,i,j2);
if (state == S_LIGHTBULB){
return -1;
}
if (flags != F_LIGHTED){
break;
}
}
//printf("220\n");
return 0;
}
}
return 1;
}
bool check_update(game g){
uint size;
square choice[4];
uint lb;
uint notempty;
bool res;
bool wall;
for (uint i = 0; i < DEFAULT_SIZE; i++){
for (uint j = 0; j< DEFAULT_SIZE; j++){
if (game_is_lightbulb(g,i,j)){
square flags = game_get_flags(g,i,j);
if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR){
//printf("196\n");
return false;
}
//loop checking every case before i
wall = false;
for (int k = i-1; k > 0; k--){
if (/*test if current case is a wall*/ game_is_black(g,k,j)){
wall = true;
}
else {
int tmp = checklightbulb(g,k,j,wall);
if (tmp == -1){
break;
}
if (!tmp){
return tmp;
}
}
}
wall = false;
for (int k = i+1; k < DEFAULT_SIZE; k++){
if (/*test if current case is a wall*/ game_is_black(g,k,j)){
wall = true;
}
else {
int tmp = checklightbulb(g,k,j,wall);
if (tmp == -1){
break;
}
if (!tmp){
return tmp;
}
}
}
wall = false;
for (int k = j-1; k > 0; k--){
if (/*test if current case is a wall*/ game_is_black(g,i,k)){
wall = true;
}
else {
int tmp = checklightbulb(g,i,k,wall);
if (tmp == -1){
break;
}
if (!tmp){
return tmp;
}
}
}
wall = false;
for (int k = j+1; k < DEFAULT_SIZE; k++){
if (/*test if current case is a wall*/ game_is_black(g,i,k)){
wall = true;
}
else {
int tmp = checklightbulb(g,i,k,wall);
if (tmp == -1){
break;
}
if (!tmp){
return tmp;
}
}
}
}
else if (/*test if current case is a wall*/game_is_black(g,i,j)){
if (game_get_black_number(g,i,j) != -1){
//lookup for lightbulb and emptycells
size = 0;
lb = 0;
notempty = 0;
res = true;
if (i > 0){
choice[size] = game_get_square(g,i-1,j);
size++;
}
else{
notempty++;
}
if (i < DEFAULT_SIZE-1){
choice[size] = game_get_square(g,i+1,j);
size++;
}
else{
notempty++;
}
if (j > 0){
choice[size] = game_get_square(g,i,j-1);
size++;
}
else{
notempty++;
}
if (j < DEFAULT_SIZE-1){
choice[size] = game_get_square(g,i,j+1);
size++;
}
else{
notempty++;
}
for (int l = 0 ; l < size ; l++){
if (choice[l] == (S_LIGHTBULB|F_LIGHTED) || choice[l] == (S_LIGHTBULB|F_LIGHTED|F_ERROR)){
lb++;
}
else if (choice[l] != S_BLANK){
notempty++;
}
}
if (lb > game_get_black_number(g,i,j)){
res = false;
}
//look if there is enough empty cell around the wall
else if (notempty > abs(game_get_black_number(g,i,j)-4)){
res = false;
}
if (game_get_flags(g,i,j) == F_ERROR){
res = !res;
}
if (!res){
//printf("386\n");
//printf("lb:%d notempty:%d i:%d j:%d state : %d, flags: %d\n",lb,notempty,i,j,game_get_state(g,i,j),game_get_flags(g,i,j));
return res;
}
}
}
}
}
return true;
}
// a > 2 = temps d'exec > 3 min et expo
bool brutforce(game g,int a){
square list[] = {S_LIGHTBULB,S_MARK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,S_BLANK};
uint * deldup = (uint *) calloc(DEFAULT_SIZE*DEFAULT_SIZE,sizeof(uint));
for (uint i = 0; i < DEFAULT_SIZE; i++){
for (uint j = 0 ;j < DEFAULT_SIZE; j++){
if (!deldup[i+j]){
if (game_get_state(g,i,j) == S_BLANK){
for (uint k = 0 ; k < sizeof(list)/sizeof(list[0]); k++){
game_set_square(g,i,j,list[k]);
deldup[i+j] = 1;
game_update_flags(g);
// game_print(g);
if (!check_update(g)){
game_print(g);
//printf("i:%d j:%d state : %d, flags: %d\n",i,j,game_get_state(g,i,j),game_get_flags(g,i,j));
free(deldup);
return false;
}
if (a > 0){
if (!brutforce(g,a-1)){
//printf("341\n");
free(deldup);
return false;
}
}
}
}
}
}
}
free(deldup);
return true;
}
bool test_game_update_flags(void){
game g = game_new_empty();
if (!brutforce(g,2)){
game_delete(g);
return EXIT_FAILURE;
}
game_delete(g);
return EXIT_SUCCESS;
}
/* ********** USAGE ********** */
int usage(int argc, char *argv[])
@@ -182,6 +436,8 @@ int main(int argc, char * argv[]){
ok = test_game_get_square();
else if (strcmp("game_is_over", argv[1]) == 0)
ok = test_game_is_over();
else if (strcmp("game_update_flags", argv[1]) == 0)
ok = test_game_update_flags();
else {
fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]);
exit(EXIT_FAILURE);